public class ConfigurationManager extends Object implements FileListener
//
// first we get an existing property from the category "general"
//
String debug_file = cfgmgr.getProperty("debug_file");
System.out.println("The debug_file is:" + debug_file);
//
// now we try to get a non-existing property from the category
"general"
//
String neProp = cfgmgr.getProperty("hello","my
default");
System.out.println("The value is:" + neProp );
//
// last step is we get a property from the category "JDBC"
//
String driver = cfgmgr.getProperty("DRIVER","","JDBC");
System.out.println("The driver is:" + driver);
Modifier and Type | Field and Description |
---|---|
private static char |
C_PFIX |
private static char |
C_SEP |
private Vector |
categories |
private File |
configFile |
private static HashMap |
configurationManagers |
private String |
name |
private HashMap |
parameter |
private boolean |
validate |
private FileWatcher |
watcher |
Modifier | Constructor and Description |
---|---|
private |
ConfigurationManager(String name)
Constructor for the ConfigurationManager object
|
Modifier and Type | Method and Description |
---|---|
void |
addCategory(String category)
Adds a feature to the Category attribute of the ConfigurationManager
object
|
void |
addFileListener(FileListener fileListener)
Adds a FileListener to the current configuration
|
void |
fileChanged(FileListenerEvent evt)
This method, once implemented, will be called when the File object
itself changes.
|
String[] |
getAllCategoryNames()
This method returns an
String array of the names of all
categories. |
String[] |
getAllPropertyNamesFromCategory(String category)
Gets all property names for a given category.
|
Enumeration |
getCategoryNames()
Deprecated.
use getAllCategoryNames instead
|
File |
getConfigFile()
Getter for the File Object of the configuration file used.
|
String |
getConfigurationName()
Getter for property Configuration Name.
|
static ConfigurationManager |
getInstance()
This method returns the default instance of the ConfigurationManager.
|
static ConfigurationManager |
getInstance(String instanceName)
This method returns an instance of the ConfigurationManager with the
given name
If no instance is found it will create a new instance and will try to load the config.xml. |
static ConfigurationManager |
getInstance(String instanceName,
boolean autoLoad)
This method returns an instance of the ConfigurationManager with this
name.
|
Hashtable |
getProperties()
This method will return a Hashtable that contains all properties for the
category "general".
|
Hashtable |
getProperties(String category)
This method will return a Hashtable that contains all properties for the
selected category.
|
String |
getProperty(String key)
This method returns the value for a particular key.
|
String |
getProperty(String key,
String defaultValue)
This method returns the value for a particular key.
|
String |
getProperty(String key,
String defaultValue,
String category)
This method returns the value for a particular key in the certain
category.
|
FileWatcher |
getWatcher()
Getter for property FileWatcher
|
void |
load(File file)
This method reads a XML file.
|
private void |
load(HashMap params)
Description of the Method
|
void |
load(InputStream is)
This method will read an
InputStream . |
void |
load(String theURL)
This method reads the configuration from an URL.
|
void |
loadProperties(File file)
This method will store all categories and properties to a
Java-properties file.
|
private void |
reloadWatcher()
Description of the Method
|
void |
removeCategory(String category)
This method removes the specific category.
|
void |
removeProperty(String name)
This method removes the property with the given name from the category
general.
|
void |
removeProperty(String name,
String category)
This method will remove a property with the given name from the specific
category.
|
void |
setAutoReload(boolean reload)
Sets the reload flag.
|
void |
setProperty(String name,
String value)
This method will set the value for a specific property in the category
"general".
|
void |
setProperty(String name,
String value,
String category)
This method will set the value for a specific property in the selected
category.
|
void |
setReloadIntervall(int seconds)
Sets the reloadIntervall attribute of the ConfigurationManager object
|
void |
setValidation(boolean validate)
Sets the validation attribute of the ConfigurationManager object
|
void |
setWatcher(FileWatcher watcher)
Setter for FileWatcher .
|
void |
store(File file)
This method will save the current categories and their properties to a
file
|
void |
storeProperties(File file)
This method will generate a Java-properties file.
|
void |
storeProperties(File file,
String category)
This method will write a Java-properties file and fill in the properties
for the particular category.
|
String |
toString()
This method creates a string representation of this configuration.
|
private static final char C_SEP
private static final char C_PFIX
private static HashMap configurationManagers
private Vector categories
private boolean validate
private FileWatcher watcher
private File configFile
private ConfigurationManager(String name)
name
- Description of the Parameterpublic String getProperty(String key)
key
- the name of the propertypublic String getProperty(String key, String defaultValue)
key
- the name of the propertydefaultValue
- the value that will be returned if the property
cannot be foundpublic String getProperty(String key, String defaultValue, String category)
key
- the name of the propertydefaultValue
- the value that will be returned if the property
cannot be foundcategory
- the name of the categorypublic static ConfigurationManager getInstance()
public static ConfigurationManager getInstance(String instanceName)
instanceName
- public static ConfigurationManager getInstance(String instanceName, boolean autoLoad)
instanceName
- autoLoad
- if true then the method wil try to load the
config.xml if it can find it in the classpathpublic Enumeration getCategoryNames()
public String[] getAllCategoryNames()
String
array of the names of all
categories.String
containing all names of all categoriespublic Hashtable getProperties()
public Hashtable getProperties(String category)
category
- the name of the categorypublic void setProperty(String name, String value)
name
- the name of the propertyvalue
- the value for this propertypublic void setProperty(String name, String value, String category)
name
- the name of the propertyvalue
- the value for this propertycategory
- the name of the categorypublic void removeProperty(String name)
removeProperty(name,"general")
.name
- the name of the property that will be removedpublic void removeProperty(String name, String category)
name
- the name of the propertycategory
- the name of the categorypublic void addCategory(String category)
category
- The feature to be added to the Category attributepublic void removeCategory(String category)
category
- the name of the category that will be removedpublic String[] getAllPropertyNamesFromCategory(String category)
null
then the category is "general".category
- the name of the categorypublic void store(File file) throws ConfigurationManagerException
file
- the file that will be generatedConfigurationManagerException
- Description of the Exceptionpublic void loadProperties(File file) throws ConfigurationManagerException
file
- the fileConfigurationManagerException
- if the file cannot be processedpublic void storeProperties(File file) throws ConfigurationManagerException
file
- the fileConfigurationManagerException
- if the file cannot be processedpublic void storeProperties(File file, String category) throws ConfigurationManagerException
file
- the filecategory
- Description of the ParameterConfigurationManagerException
- if the file cannot be processedpublic void load(String theURL) throws ConfigurationManagerException
theURL
- the URLConfigurationManagerException
- if an error occurspublic void load(InputStream is) throws ConfigurationManagerException
InputStream
.is
- the InputStream
ConfigurationManagerException
- if an error occurspublic void load(File file) throws ConfigurationManagerException
file
- the file that will be readConfigurationManagerException
- if the file cannot be readprivate void load(HashMap params) throws ConfigurationManagerException
params
- Description of the ParameterConfigurationManagerException
- Description of the Exceptionprivate void reloadWatcher()
public String toString()
public void fileChanged(FileListenerEvent evt)
fileChanged
in interface FileListener
evt
- Description of the Parameterpublic void setValidation(boolean validate)
validate
- The new validation valuepublic void setReloadIntervall(int seconds)
seconds
- The new reloadIntervall valuepublic void setAutoReload(boolean reload)
reload
- The new autoReload valuepublic FileWatcher getWatcher()
public void setWatcher(FileWatcher watcher)
watcher
- New value of property watcher.public String getConfigurationName()
public void addFileListener(FileListener fileListener)
fileListener
- The FileListenerpublic File getConfigFile()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses