org.jconfig
Class DefaultConfiguration

java.lang.Object
  extended byorg.jconfig.DefaultConfiguration
All Implemented Interfaces:
Configuration, java.io.Serializable
Direct Known Subclasses:
ExtensibleConfiguration, NestedConfiguration

public class DefaultConfiguration
extends java.lang.Object
implements java.io.Serializable, Configuration

This class is the configuration itself. The Configuration is useful if one wants to manage multiple configurations. A single instance of the Configuration may contain, for example, information for one application or user.

Author:
Andreas Mecky andreas.mecky@xcom.de, Terry Dye terry.dye@xcom.de
See Also:
Serialized Form

Nested Class Summary
protected  class DefaultConfiguration.MyCategoryListener
           
 
Field Summary
protected  java.lang.String baseConfigName
           
protected  java.util.HashMap categories
           
protected  java.lang.String configName
           
protected  java.lang.String mainCategory
           
protected static VariableManager vm
           
 
Constructor Summary
protected DefaultConfiguration()
           
  DefaultConfiguration(java.lang.String configName)
          The constructor that creates a new configuration with one empty category called "general".
 
Method Summary
 void addCategoryListener(CategoryListener listener)
           
 void addCategoryListener(CategoryListener listener, java.lang.String categoryName)
           
 void addConfigurationListener(ConfigurationListener listener)
          Adds the specified configuration listener to receive configuration changed events from this configuration.
 void addInclude(int type, java.lang.String name)
           
protected  void addIncludeBlock(java.lang.StringBuffer buffer)
           
 void addPropertyListener(PropertyListener listener)
          Adds the PropertyListener to the main category.
 void addPropertyListener(PropertyListener listener, java.lang.String categoryName)
          Adds the PropertyListener to the given category.
protected  void addVariableBlock(java.lang.StringBuffer buffer)
           
 boolean containsCategory(java.lang.String categoryName)
          Use this method to determine if a category exists.
protected  java.lang.String escapeForXML(java.lang.String text)
           
 void fireConfigurationChangedEvent(ConfigurationChangedEvent event)
          Deliver configuration changed event to all listeners that are registered with our listener list.
 java.lang.String[] getArray(java.lang.String key)
           
 java.lang.String[] getArray(java.lang.String key, java.lang.String[] defaultValue)
           
 java.lang.String[] getArray(java.lang.String key, java.lang.String[] defaultValue, java.lang.String category)
           
 java.lang.String[] getArray(java.lang.String key, java.lang.String[] defaultValue, java.lang.String category, java.lang.String separator)
           
 java.lang.String getBaseConfiguration()
           
 boolean getBooleanProperty(java.lang.String name, boolean defaultValue)
          Wrapper method to keep API simple
 boolean getBooleanProperty(java.lang.String name, boolean defaultValue, java.lang.String categoryName)
          Wrapper method to Category method
 Category getCategory()
          Returns the main category for this configuration
 Category getCategory(java.lang.String categoryName)
          Returns a category based on the name provided.
 java.lang.String[] getCategoryNames()
          This method returns a string array with all category names.
protected  java.lang.String[] getCategoryNames(boolean includeParent)
          A convenience method that returns the key set in the form or a String Array.
 char getCharProperty(java.lang.String name, char defaultValue)
          Wrapper method to Category method
 char getCharProperty(java.lang.String name, char defaultValue, java.lang.String category)
          Wrapper method to Category method
 java.lang.String getConfigName()
          Return the name of the current configuration.
 double getDoubleProperty(java.lang.String name, double defaultValue)
          Wrapper method to Category method
 double getDoubleProperty(java.lang.String name, double defaultValue, java.lang.String category)
          Wrapper method to Category method
 java.lang.String getEncoding()
           
 java.util.Vector getIncludes()
           
 int getIntProperty(java.lang.String name, int defaultValue)
           
 int getIntProperty(java.lang.String name, int defaultValue, java.lang.String category)
           
 long getLongProperty(java.lang.String name, long defaultValue)
          Wrapper method to Category method
 long getLongProperty(java.lang.String name, long defaultValue, java.lang.String categoryName)
          Wrapper method to Category method
 java.lang.String getMainCategoryName()
          This method returns the name of the default category
 int getNumberOfCategories()
          The method returns the number of categories inside this configuration.
 java.util.Properties getProperties()
          This method returns all properties for the default category
 java.util.Properties getProperties(java.lang.String category)
          This method returns all properties for the category with the given name
protected  java.util.Properties getProperties(java.lang.String category, boolean includeParent)
           
 java.lang.String getProperty(java.lang.String key)
          This method returns the String value based on the given key.
 java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
          This method is the same as getProperty(key) but it returns the defaultValue if the property cannot be found.
 java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue, java.lang.String categoryName)
          This is the real implementation.
 java.lang.String[] getPropertyNames(java.lang.String category)
          This method returns all the names of the properties for the specific category
protected  java.util.SortedMap getSortedProperties(java.lang.String categoryName, boolean includeParent)
           
 java.lang.String getVariable(java.lang.String name)
           
 java.util.HashMap getVariables()
          It returns a HashMap with all variables with the variable name as key
 java.lang.String getXMLAsString()
          This method converts the Configuration into a String which looks like XML.
 boolean hasChanged()
          This methods returns a flag which indicates if the configuration has changed since it was loaded.
 boolean isNew()
          This method determines if the configuration was loaded successfully (returns false) or if the configuration was created by the ConfigurationManager.
protected  void markDirty()
           
 void removeCategory(java.lang.String category)
          This method deletes a category with all its properties
 void removeConfigurationListener(ConfigurationListener listener)
          Removes the specified configuration listener from the configuration change events from this configuration.
 void removeProperty(java.lang.String name)
          This method deletes a property from the default category.
 void removeProperty(java.lang.String name, java.lang.String category)
          This method deletes a property with the given name from the specific category.
 void renameCategory(java.lang.String newName, java.lang.String oldName)
           
 void resetCreated()
          This method is used by the handlers to reset the isNew flag to false.
 void setBaseConfiguration(java.lang.String name)
          This method is used to define the base configuration of a configuration.
 void setBooleanProperty(java.lang.String key, boolean value)
          Wrapper method to Category method
 void setBooleanProperty(java.lang.String key, boolean value, java.lang.String category)
          Wrapper method to Category method
 void setCategory(Category category)
           
 void setCategory(java.lang.String name)
          This method sets a category but it does not set this category as default.
 void setCategory(java.lang.String categoryName, boolean main)
          Besides setting the category, it will also set this category as default category if main is true.
 void setCharProperty(java.lang.String key, char value)
          Wrapper method to Category method
 void setCharProperty(java.lang.String key, char value, java.lang.String category)
          Wrapper method to Category method
 void setConfigName(java.lang.String configName)
          Sets the name of the configuration.
 void setDoubleProperty(java.lang.String key, double value)
          Wrapper method to Category method
 void setDoubleProperty(java.lang.String key, double value, java.lang.String category)
          Wrapper method to Category method
 void setEncoding(java.lang.String encoding)
           
 void setIntProperty(java.lang.String key, int value)
          Wrapper method to Category method
 void setIntProperty(java.lang.String key, int value, java.lang.String category)
          Wrapper method to Category method
 void setLongProperty(java.lang.String key, long value)
          Wrapper method to Category method
 void setLongProperty(java.lang.String key, long value, java.lang.String category)
          Wrapper method to Category method
 void setProperty(java.lang.String name, java.lang.String value)
          This method sets a property with the name and the value in the default category.
 void setProperty(java.lang.String name, java.lang.String value, java.lang.String categoryName)
          This method sets the value for a property for the given category.
 void setVariable(java.lang.String name, java.lang.String value)
          This method sets a variable inside the configuration.
 java.lang.String toString()
          This method creates a string representation of the configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

vm

protected static final VariableManager vm

configName

protected java.lang.String configName

categories

protected java.util.HashMap categories

mainCategory

protected java.lang.String mainCategory

baseConfigName

protected java.lang.String baseConfigName
Constructor Detail

DefaultConfiguration

protected DefaultConfiguration()

DefaultConfiguration

public DefaultConfiguration(java.lang.String configName)
The constructor that creates a new configuration with one empty category called "general". This category is also the default category.

Parameters:
configName - the name of the configuration
Method Detail

setCategory

public void setCategory(java.lang.String name)
This method sets a category but it does not set this category as default.

Specified by:
setCategory in interface Configuration
Parameters:
name - the name of the category

renameCategory

public void renameCategory(java.lang.String newName,
                           java.lang.String oldName)
Specified by:
renameCategory in interface Configuration

setCategory

public void setCategory(java.lang.String categoryName,
                        boolean main)
Besides setting the category, it will also set this category as default category if main is true. It will only set (ie create) the category if it does not exist. If you want delete a category then use @see #removeCategory(String)

Specified by:
setCategory in interface Configuration
Parameters:
categoryName - the name of the category
main - if true then this category is the default category

setCategory

public void setCategory(Category category)
Specified by:
setCategory in interface Configuration

getMainCategoryName

public java.lang.String getMainCategoryName()
This method returns the name of the default category

Specified by:
getMainCategoryName in interface Configuration
Returns:
name of the default category

getCategoryNames

public java.lang.String[] getCategoryNames()
This method returns a string array with all category names.

Specified by:
getCategoryNames in interface Configuration
Returns:
a string array with all category names

getCategoryNames

protected java.lang.String[] getCategoryNames(boolean includeParent)
A convenience method that returns the key set in the form or a String Array.

Parameters:
includeParent - true will check for a base configuration and include those category names as well.
Returns:
the String Array as described

getProperty

public java.lang.String getProperty(java.lang.String key)
This method returns the String value based on the given key. Implementation details: It calls getProperty(name,null,null). It searches inside the default category for the property.

Specified by:
getProperty in interface Configuration
Parameters:
key - the name of the property
Returns:
the value as String if it is found or null

getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultValue)
This method is the same as getProperty(key) but it returns the defaultValue if the property cannot be found. Implementation details: It calls getProperty(key,defaultValue,null).

Specified by:
getProperty in interface Configuration
Parameters:
key - the name of the property
defaultValue - the defaultValue that will be returned if the property cannot be found
Returns:
the value as String

getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultValue,
                                    java.lang.String categoryName)
This is the real implementation. It will return the value of the property with the specific name. First of all, it checks if the name of the category exists. If not, then it will use the name of the default category. The next step is that it will look for the property. If it is not found in the category, it will look inside the default category (inheritance). If it still cannot find the property, it will return the defaultValue

Specified by:
getProperty in interface Configuration
Parameters:
key - the name of the property
defaultValue - the default value
categoryName - the name of the category
Returns:
the value as String

setProperty

public void setProperty(java.lang.String name,
                        java.lang.String value)
This method sets a property with the name and the value in the default category. It calls setProperty(name.value,null).

Specified by:
setProperty in interface Configuration
Parameters:
name - the name of the property
value - the value as String

setProperty

public void setProperty(java.lang.String name,
                        java.lang.String value,
                        java.lang.String categoryName)
This method sets the value for a property for the given category. It also raises a PropertyEvent. If the category is null then it uses the default category.

Specified by:
setProperty in interface Configuration
Parameters:
name - the name of the property
value - the value as String
categoryName - the name of the category

removeProperty

public void removeProperty(java.lang.String name)
This method deletes a property from the default category. It calls removeProperty(name,null).

Specified by:
removeProperty in interface Configuration
Parameters:
name - the name of the property

removeProperty

public void removeProperty(java.lang.String name,
                           java.lang.String category)
This method deletes a property with the given name from the specific category. If the category is null then it will delete the property from the default category.

Specified by:
removeProperty in interface Configuration
Parameters:
name - the name of the property
category - the name of the category

getNumberOfCategories

public int getNumberOfCategories()
The method returns the number of categories inside this configuration.

Specified by:
getNumberOfCategories in interface Configuration
Returns:
the number of categories

removeCategory

public void removeCategory(java.lang.String category)
This method deletes a category with all its properties

Specified by:
removeCategory in interface Configuration
Parameters:
category - the name of the category

getProperties

public java.util.Properties getProperties(java.lang.String category)
Description copied from interface: Configuration
This method returns all properties for the category with the given name

Specified by:
getProperties in interface Configuration
Parameters:
category - the name of the category
Returns:
all properties for the default category

getProperties

public java.util.Properties getProperties()
Description copied from interface: Configuration
This method returns all properties for the default category

Specified by:
getProperties in interface Configuration
Returns:
all properties for the default category

getProperties

protected java.util.Properties getProperties(java.lang.String category,
                                             boolean includeParent)
Parameters:
category -
includeParent -
Returns:

getPropertyNames

public java.lang.String[] getPropertyNames(java.lang.String category)
This method returns all the names of the properties for the specific category

Specified by:
getPropertyNames in interface Configuration
Parameters:
category - the name of the category
Returns:
the names as string array

setVariable

public void setVariable(java.lang.String name,
                        java.lang.String value)
This method sets a variable inside the configuration.

Specified by:
setVariable in interface Configuration
Parameters:
name - the name of the variable
value - the value of the variable

getVariables

public java.util.HashMap getVariables()
It returns a HashMap with all variables with the variable name as key

Specified by:
getVariables in interface Configuration
Returns:
a HashMap with all variables

getVariable

public java.lang.String getVariable(java.lang.String name)
Specified by:
getVariable in interface Configuration

getIntProperty

public int getIntProperty(java.lang.String name,
                          int defaultValue)
Specified by:
getIntProperty in interface Configuration
Parameters:
name -
defaultValue -
Returns:

getIntProperty

public int getIntProperty(java.lang.String name,
                          int defaultValue,
                          java.lang.String category)
Specified by:
getIntProperty in interface Configuration
Parameters:
name -
defaultValue -
category -
Returns:

getBooleanProperty

public boolean getBooleanProperty(java.lang.String name,
                                  boolean defaultValue)
Wrapper method to keep API simple

Specified by:
getBooleanProperty in interface Configuration
Parameters:
name -
defaultValue -
Returns:
See Also:
Category.getBooleanProperty(String, boolean)

getBooleanProperty

public boolean getBooleanProperty(java.lang.String name,
                                  boolean defaultValue,
                                  java.lang.String categoryName)
Wrapper method to Category method

Specified by:
getBooleanProperty in interface Configuration
Parameters:
defaultValue -
categoryName -
Returns:
See Also:
Category#getBooleanProperty(String, boolean, String)

setBooleanProperty

public void setBooleanProperty(java.lang.String key,
                               boolean value)
Wrapper method to Category method

Specified by:
setBooleanProperty in interface Configuration
Parameters:
key -
value -

setBooleanProperty

public void setBooleanProperty(java.lang.String key,
                               boolean value,
                               java.lang.String category)
Wrapper method to Category method

Specified by:
setBooleanProperty in interface Configuration
Parameters:
key -
value -
category -

getLongProperty

public long getLongProperty(java.lang.String name,
                            long defaultValue)
Wrapper method to Category method

Specified by:
getLongProperty in interface Configuration
Parameters:
name -
defaultValue -
Returns:

getLongProperty

public long getLongProperty(java.lang.String name,
                            long defaultValue,
                            java.lang.String categoryName)
Wrapper method to Category method

Specified by:
getLongProperty in interface Configuration
Parameters:
name -
defaultValue -
categoryName -
Returns:

getDoubleProperty

public double getDoubleProperty(java.lang.String name,
                                double defaultValue)
Wrapper method to Category method

Specified by:
getDoubleProperty in interface Configuration
Parameters:
name -
defaultValue -
Returns:

getDoubleProperty

public double getDoubleProperty(java.lang.String name,
                                double defaultValue,
                                java.lang.String category)
Wrapper method to Category method

Specified by:
getDoubleProperty in interface Configuration
Parameters:
name -
defaultValue -
category -
Returns:

getCharProperty

public char getCharProperty(java.lang.String name,
                            char defaultValue)
Wrapper method to Category method

Specified by:
getCharProperty in interface Configuration
Parameters:
name -
defaultValue -
Returns:

getCharProperty

public char getCharProperty(java.lang.String name,
                            char defaultValue,
                            java.lang.String category)
Wrapper method to Category method

Specified by:
getCharProperty in interface Configuration
Parameters:
name -
defaultValue -
category -
Returns:

toString

public java.lang.String toString()
This method creates a string representation of the configuration.

Specified by:
toString in interface Configuration
Returns:
a string with the configuration

getXMLAsString

public java.lang.String getXMLAsString()
This method converts the Configuration into a String which looks like XML.

Specified by:
getXMLAsString in interface Configuration
Returns:
the Configuration as String in XML format

getSortedProperties

protected java.util.SortedMap getSortedProperties(java.lang.String categoryName,
                                                  boolean includeParent)

addIncludeBlock

protected void addIncludeBlock(java.lang.StringBuffer buffer)

addVariableBlock

protected void addVariableBlock(java.lang.StringBuffer buffer)

escapeForXML

protected java.lang.String escapeForXML(java.lang.String text)

addPropertyListener

public void addPropertyListener(PropertyListener listener)
Adds the PropertyListener to the main category. If the main category changes after the listener has already been added, the listener is still registered, but not to the main category any longer. One way to handle this behavior is to remove the listener from the old main category and add it to the new main category.

Specified by:
addPropertyListener in interface Configuration
Parameters:
listener -

addPropertyListener

public void addPropertyListener(PropertyListener listener,
                                java.lang.String categoryName)
Adds the PropertyListener to the given category.

Specified by:
addPropertyListener in interface Configuration

addCategoryListener

public void addCategoryListener(CategoryListener listener)
Specified by:
addCategoryListener in interface Configuration

addCategoryListener

public void addCategoryListener(CategoryListener listener,
                                java.lang.String categoryName)
Specified by:
addCategoryListener in interface Configuration

getConfigName

public java.lang.String getConfigName()
Return the name of the current configuration.

Specified by:
getConfigName in interface Configuration
Returns:

setConfigName

public void setConfigName(java.lang.String configName)
Description copied from interface: Configuration
Sets the name of the configuration.

Specified by:
setConfigName in interface Configuration
Parameters:
configName -

addConfigurationListener

public void addConfigurationListener(ConfigurationListener listener)
Adds the specified configuration listener to receive configuration changed events from this configuration.

Specified by:
addConfigurationListener in interface Configuration
Parameters:
listener - The ConfigurationListener

removeConfigurationListener

public void removeConfigurationListener(ConfigurationListener listener)
Removes the specified configuration listener from the configuration change events from this configuration.

Specified by:
removeConfigurationListener in interface Configuration
Parameters:
listener - The ConfigurationListener

fireConfigurationChangedEvent

public void fireConfigurationChangedEvent(ConfigurationChangedEvent event)
Deliver configuration changed event to all listeners that are registered with our listener list.

Specified by:
fireConfigurationChangedEvent in interface Configuration
Parameters:
event - The ConfigurationChangedEvent

getCategory

public Category getCategory()
Returns the main category for this configuration

Specified by:
getCategory in interface Configuration
Returns:
The main category

getCategory

public Category getCategory(java.lang.String categoryName)
Returns a category based on the name provided. This implementation has an interesting twist now inside. We need to check if the parent's configuration has a category of the same name. The normal case is that we override a value in a parent configuration, but we still want the values from the parent configuration if they aren't overridden. Example. parentConfiguration has category A and has property key AKey childConfiguration extends parentConfiguration has category and has property key BKey childConfiguration.getCategory("A").getProperty("AKey"); // didn't work, works with this implementation childConfiguration.getCategory("A").getProperty("BKey"); // always worked The reason for this change is that the category objects don't know anything about brother, sister, parent or any other relations. This method handles the logic to gather the information about brother, sister, parent, cousin or whatever.

Specified by:
getCategory in interface Configuration
Parameters:
categoryName - The name of the category (if null, main category will be used)
Returns:
The category object (new instance if necessary)

setLongProperty

public void setLongProperty(java.lang.String key,
                            long value)
Wrapper method to Category method

Specified by:
setLongProperty in interface Configuration
Parameters:
key -
value -

setIntProperty

public void setIntProperty(java.lang.String key,
                           int value)
Wrapper method to Category method

Specified by:
setIntProperty in interface Configuration
Parameters:
key -
value -

setCharProperty

public void setCharProperty(java.lang.String key,
                            char value)
Wrapper method to Category method

Specified by:
setCharProperty in interface Configuration
Parameters:
key -
value -

setCharProperty

public void setCharProperty(java.lang.String key,
                            char value,
                            java.lang.String category)
Wrapper method to Category method

Specified by:
setCharProperty in interface Configuration
Parameters:
key -
value -
category -

setDoubleProperty

public void setDoubleProperty(java.lang.String key,
                              double value)
Wrapper method to Category method

Specified by:
setDoubleProperty in interface Configuration
Parameters:
key -
value -

setLongProperty

public void setLongProperty(java.lang.String key,
                            long value,
                            java.lang.String category)
Wrapper method to Category method

Specified by:
setLongProperty in interface Configuration
Parameters:
key -
value -
category -

setIntProperty

public void setIntProperty(java.lang.String key,
                           int value,
                           java.lang.String category)
Wrapper method to Category method

Specified by:
setIntProperty in interface Configuration
Parameters:
key -
value -
category -

setDoubleProperty

public void setDoubleProperty(java.lang.String key,
                              double value,
                              java.lang.String category)
Wrapper method to Category method

Specified by:
setDoubleProperty in interface Configuration
Parameters:
key -
value -
category -

hasChanged

public boolean hasChanged()
Description copied from interface: Configuration
This methods returns a flag which indicates if the configuration has changed since it was loaded.

Specified by:
hasChanged in interface Configuration
Returns:
true if the configuration has changed

getArray

public java.lang.String[] getArray(java.lang.String key)
Specified by:
getArray in interface Configuration
Parameters:
key -
Returns:

getArray

public java.lang.String[] getArray(java.lang.String key,
                                   java.lang.String[] defaultValue)
Specified by:
getArray in interface Configuration
Parameters:
key -
defaultValue -
Returns:

getArray

public java.lang.String[] getArray(java.lang.String key,
                                   java.lang.String[] defaultValue,
                                   java.lang.String category)
Specified by:
getArray in interface Configuration
Parameters:
key -
defaultValue -
category -
Returns:

getArray

public java.lang.String[] getArray(java.lang.String key,
                                   java.lang.String[] defaultValue,
                                   java.lang.String category,
                                   java.lang.String separator)

isNew

public boolean isNew()
Description copied from interface: Configuration
This method determines if the configuration was loaded successfully (returns false) or if the configuration was created by the ConfigurationManager.

Specified by:
isNew in interface Configuration
Returns:
true if created by the ConfigurationManager

resetCreated

public void resetCreated()
Description copied from interface: Configuration
This method is used by the handlers to reset the isNew flag to false. This indicates that the configuration was loaded and not created.

Specified by:
resetCreated in interface Configuration

getEncoding

public java.lang.String getEncoding()
Specified by:
getEncoding in interface Configuration

markDirty

protected void markDirty()

setEncoding

public void setEncoding(java.lang.String encoding)
Specified by:
setEncoding in interface Configuration

containsCategory

public boolean containsCategory(java.lang.String categoryName)
Description copied from interface: Configuration
Use this method to determine if a category exists. The getCategory method will return the default category if the requested category was not found. So if you need to know if a category already exists then call this method.

Specified by:
containsCategory in interface Configuration

addInclude

public void addInclude(int type,
                       java.lang.String name)
Specified by:
addInclude in interface Configuration

getIncludes

public java.util.Vector getIncludes()
Specified by:
getIncludes in interface Configuration

setBaseConfiguration

public void setBaseConfiguration(java.lang.String name)
Description copied from interface: Configuration
This method is used to define the base configuration of a configuration. This means if config A extends config B then this method will be called in config A with the name of config B.

Specified by:
setBaseConfiguration in interface Configuration

getBaseConfiguration

public java.lang.String getBaseConfiguration()
Specified by:
getBaseConfiguration in interface Configuration


Copyright © 2001-2005 Andreas Mecky, Terry Dye. All Rights Reserved.