|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.io.ConfigSource
public class ConfigSource
Base class for the purpose of opening configuration files.
For an general overview of configuration issues (and how this class fits in to the scheme), refer to the Configuration Files document.
This class provides the foundation for consistently opening and closing configuration file(s). In provides the mechanisms to open up various input and output forms for the purpose of loading and saving configuration infromation.
The package and class information is used in forming the location of the configuration file in a manner that it is very unlikely that one will have naming conflicts. For example, if you create the classes: com.ccg.run.Main and com.ccg.wp.Main then this class can be used to keep their respective configuration files isolated from one another. Their associated configuration files will be found via:
$HOME/.etc/com/ccg/run/*.Main $HOME/.etc/com/ccg/wp/*.Main
The package name and class name are used in combination with the user's home directory (as reported by the JVM) as to where the final location of the configuration files will be located at. So, if you use packages (as you should), and you don't have any name conflicts, then you won't have any configuration conflicts either.
getFile()| Constructor Summary | |
|---|---|
ConfigSource(Class cl)
Constructor which associates a ConfigSource with a class. |
|
| Method Summary | |
|---|---|
ObjectOutputStream |
createObjectOutputStream()
Attempts to open a ObjectOutputStream for the purpose of saving. |
OutputStream |
createOutputStream()
Attempts to open a OutputStream for the purpose of saving. |
PrintWriter |
createPrintWriter()
Attempts to open a PrintWriter for the purpose of saving. |
String[] |
getAvailableConfigs()
Get list of available configurations. |
String |
getClassName()
Get the full package.class name to identify these configuration objects. |
File |
getFile()
Get's file representation of where the configuration is located at. |
String |
getSourceName()
Get the source name of the configuration file. |
Object |
loadObject()
Load a single Object as entire configuration. |
LookupProperties |
loadProperties()
Loads the set of properties which had been previously saved. |
InputStream |
openInputStream()
Attempts to open a InputStream for the purpose of reading. |
LineNumberReader |
openLineNumberReader()
Attempts to open a LineNumberReader for the purpose of reading. |
ObjectInputStream |
openObjectInputStream()
Attempts to open a ObjectInputStream for the purpose of reading. |
void |
removeConfig()
Remove the configuration from existance. |
void |
saveObject(Object object)
Save single Object as entire configuration. |
void |
saveProperties(Properties props)
Saves the set of properties specified in a manner that they can be loaded in the future. |
void |
setClassName(String val)
Set the full package.class name to identify these configuration objects. |
void |
setSourceName(String val)
Set the source name of the configuration file. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ConfigSource(Class cl)
This is the way in which you construct this object. You associate a unique class at the time of construction. The full class name (including package) information is used to differentiate the configuration files created/read from configuration files created for other classes.
cl - The class to associate with the
configuration files (must not be null).getClassName()| Method Detail |
|---|
public void setClassName(String val)
If you derive your own ConfigSource object from this
class, then you won't need to specify a class name here.
If you are being lazy (or want to reduce the number of classes required for a applet), you can skip the formal step of extending this class. Instead, you can associate a class name with it instead.
The value of the class name affects the form of the configuration files (both directory and extension). For example, if you were to set the class name to "com.ccg.net.URLCat", this would result in the default configuration file being found at "$HOME/.etc/com/ccg/net/default.URLCat".
NOTE: This method may disappear in the future, I'm thinking that I should not allow one to change this on the fly.
val - New String value to assign (if you pass null - we will ignore it).getClassName()public String getClassName()
setClassName(java.lang.String)public void setSourceName(String val)
Set's the "name" of the configuration file. Note, this is the
simple name of the configuration file - it typically does not
contain any path or extension information. By omitting path and
extension information, the defaults will be applied. Refer to
getFile() to see how this string is combined to form the
actual file name.
NOTE: If you really need to force the path and extension information for the file, you can specify the full location of where you would like the file to be. For example, if you specify "/etc/com/ccg/io/default.MyConfig", then that is the file that will be used. However, in doing so, you must specify ALL information.
val - New String value to assign.getSourceName()public String getSourceName()
If a name hasn't been explicitly set, the default ("default")
will be returned. It should be noted, that this is typically the
"simple" name of the configuration file (it doesn't contain any
path or extension information). For the full file name, use the
getFile() method.
setSourceName(java.lang.String)public String[] getAvailableConfigs()
This method returns the "simple" names of ALL of the currently
available configuration files that have been saved to disk (which
can then be passed to setSourceName(java.lang.String)).
public void removeConfig()
throws IOException
This method attempts to delete the configuration (file) which
the ConfigSource currently is pointing
at.
cname - Name of the configuration to be removed - must not be null.
IOException - If there is a problem removing the configuration (didn't
exist, insufficient permissions, etc).getFile(),
setSourceName(java.lang.String)public File getFile()
This method builds the full representation of where the configuration file should be located at. Currently, this can result in a rather LONG path. For example, if you had derived an object from this class called: "com.ccg.run.RunConfig", then the config files for this class would be created in the form:
$HOME/.etc/com/ccg/run/NAME.RunConfig
The "$HOME" represents the user's home directory (as indicated
by the JVM), and the "NAME" represents the name of the
configuration to open (it will be "default" until the setSourceName(java.lang.String) method is used to change it.
It should be noted that if the object does not belong to a package, the name will be of the form:
$HOME/.etc/jconf/NAME.CLASS
If you use the setSourceName(NAME) method, you have the option of using a full
path/extension filename to force the configuration to be at a
certain location (however, this is typically frowned upon - try
to stick with simple names and use the default path/extensions
generated).
File object that can be used to open the config file.createOutputStream(),
openInputStream()
public OutputStream createOutputStream()
throws ConfigException
OutputStream for the purpose of saving.
When you are ready to save your configuration, you can use
this method to open a OutputStream to write to. This
method will create the necessary directories and then open the
file as a stream.
OutputStream to write to (never returns null).
ConfigException - If we are unable to open/create the output stream.getFile(),
createObjectOutputStream(),
createPrintWriter()
public PrintWriter createPrintWriter()
throws ConfigException
PrintWriter for the purpose of saving.
If you are saving your configuration as a ASCII representation,
you can use this method to open a PrintWriter to
write to. This method will create the necessary directories and
then open the file such that you can 'print' information to it.
PrintWriter to write to (never returns null).
ConfigException - If we are unable to open/create the output.getFile(),
createOutputStream()
public InputStream openInputStream()
throws ConfigException
InputStream for the purpose of reading.
When you are ready to load your configuration, you can use
this method to open a InputStream to read from. If the
config file isn't found, a exception will be thrown. This is the
counterpart to createOutputStream().
InputStream to read from (never returns null).
ConfigException - If we are unable to open the input stream.getFile(),
openObjectInputStream(),
openLineNumberReader()
public LineNumberReader openLineNumberReader()
throws ConfigException
LineNumberReader for the purpose of reading.
If you had previously saved your configuration as ASCII text
using a PrintWriter, you can use this method to open the
configuration file in a manner that you can read it back in. This
is the counterpart to createPrintWriter().
LineNumberReader to read ASCII text from (never
returns null).
ConfigException - If we are unable to open the file.getFile(),
openInputStream()
public ObjectOutputStream createObjectOutputStream()
throws ConfigException
ObjectOutputStream for the purpose of saving.
If you are saving your configuration as serialized objects,
you can use this method to open a ObjectOutputStream to
write to. This method will create the necessary directories and
then open the file such that you can serialize data to it.
ObjectOutputStream to write to (never returns null).
ConfigException - If we are unable to open/create the output.getFile(),
createOutputStream()
public ObjectInputStream openObjectInputStream()
throws ConfigException
ObjectInputStream for the purpose of reading.
If you had previously saved your configuration as serialized
Java objects, you can use this method to open the configuration
file in a manner that youcan read them back in. This is the
counterpart to createObjectOutputStream().
ObjectInputStream to read objects from (never
returns null).
ConfigException - If we are unable to open the stream.getFile(),
openInputStream()
public void saveObject(Object object)
throws ConfigException
Object as entire configuration.
If your configuration can be represented as a single Serializable object, you can use this method to save it to
disk. The ConfigAttributes object is highly recommended
for this purpose. This method does the following:
output file
(creating any directories as needed).
Object to the file.
To later retrieve the configuration, you would use the loadObject() method.
obj - The Serializable object to write out.
ConfigException - If there is a problem creating/writing to the fileloadObject()
public Object loadObject()
throws ConfigException
Object as entire configuration.
If your configuration can be represented as a single Serializable object, you can use this method to load it back
from disk. The ConfigAttributes object is highly
recommended for this purpose. This method does the following:
output file
(creating any directories as needed).
Object from the file.
Typically a configuration object that is loaded this way was
previously stored using the saveObject(java.lang.Object) method.
Serializable object we read back in.
ConfigException - If there is a problem opening/reading to the file (triggered
by a IOException). Or, if we don't have knowledge of the
class we are trying to de-serialize (class isn't found in the
class path - triggered by a ClassNotFoundException).saveObject(java.lang.Object)
public void saveProperties(Properties props)
throws ConfigException,
IOException
loaded in the future.
This method attempts to save the set of Properties you pass to the current configuration file in a
manner that it can be loaded in the future.
props - The properties to be saved - must not be null.
ConfigException - If there is a problem with the configuration
IOException - If there is a problem opening, reading or closing the associated file.
public LookupProperties loadProperties()
throws ConfigException,
IOException
saved.
This method attempts to load the set of properties which had been saved at some time in the past.
props - The properties which were loaded (extended version of Properties) - does not return null.
ConfigException - If there is a problem with the configuration
IOException - If there is a problem opening, reading or closing the associated file.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||