dfki.util.config
Class Config

java.lang.Object
  |
  +--dfki.util.config.Config
All Implemented Interfaces:
java.lang.Cloneable

public class Config
extends java.lang.Object
implements java.lang.Cloneable

Class to support application and software module configuration. An instance of the Config class stores a number of property settings. A property can be either single valued (like the java Properties) or it can have multiple values (an array of values). Values are always strings. Settings and default settings are distinguished. They can be set by using the setProperyValue(...) methods or by applying ConfigReaders. ConfigReaders can read settings from various sources (probably stored in different representations). See direct subclasses of ConfigReader for currently availabe readers.
Additional self made readers can be defined by extending the abstract ConfigReader class.

When property values change: It is possible to subscribe to config changes by adding a Config.ChangeListener. Whenever the settings of the config intstance to which the listener is subscribed to change, its Config.ChangeListener.hasChanged() method is called.


Inner Class Summary
static interface Config.ChangeListener
          Listener interface for publishing change events.
static class Config.Exception
           
static class Config.MultiValueException
          Exception denoting illegal value/multivalue assignments.
static class Config.Property
          Defines a property in a configuration.
 
Field Summary
static java.lang.String ENABLED
           
 
Method Summary
 void addChangeListener(Config.ChangeListener l)
           
 void addDefaultSettings(ConfigReader r)
          Adds some default settings to the current configuration.
 void addSettings(ConfigReader r)
          Adds some new settings to the current configuration.
static Config cfg()
           
static Config cfg(ConfigReader r)
           
 java.lang.Object clone()
          Creates a copy of the configuration.
 void fillInDefaultPropertyValue(Config.Property p, java.lang.String value)
          Set the default value for a property only if no value has been set already.
 void fillInDefaultPropertyValue(Config.Property p, java.lang.String[] values)
          Set the default values for a property only if no values have been set already.
 java.lang.String[] getDefaultPropertyValues(Config.Property p)
           
 java.lang.String getPropertyValue(Config.Property p)
          Reads the value for a given property.
 java.lang.String getPropertyValue(java.lang.String moduleName, Config.Property p)
          Reads the value of a given property local to a particular module.
 java.lang.String getPropertyValueInPath(Config.Property p)
          Searches for the value of the given property p.
 java.lang.String getPropertyValueInPath(java.lang.String moduleName, Config.Property p)
          Uses the same search strategy as getPropertyValueInPath(Config.Property).
 java.lang.String[] getPropertyValues(Config.Property p)
          Reads the multivalue of a given property.
 java.lang.String[] getPropertyValues(java.lang.String moduleName, Config.Property p)
           
 java.lang.String[] getPropertyValuesInPath(Config.Property p)
           
 boolean hasDefaultValue(Config.Property p)
           
 boolean hasProperty(Config.Property p)
          Tests if any value has been set for a property.
 boolean isEnabled(Config.Property p)
          Checks whether a property is enabled or not.
 java.lang.String printProperty(Config.Property p)
           
 void printSettings(Config.Property[] props, java.io.PrintWriter pw)
          Prints the settigs for the given properties to PrintWriter w.
 void printSettings(Config.Property[] props, java.io.Writer w)
          Prints the settigs for the given properties to Writer w.
 void printTo(java.io.OutputStream os)
           
 void printTo(java.io.PrintWriter pw)
           
 void printTo(java.io.Writer w)
           
 void removeChangeListener(Config.ChangeListener l)
           
 void setDefaultPropertyValue(Config.Property p, java.lang.String value)
          Sets the default value for a property.
 void setDefaultPropertyValues(Config.Property p, java.lang.String[] values)
          Sets the default values for a property.
 void setPropertyValue(Config.Property p, java.lang.String value)
          Sets the value for a property, overriding the default value (if any).
 void setPropertyValues(Config.Property p, java.lang.String[] values)
          Sets the values for a property, overriding the default value (if any).
 java.lang.String toString()
           
 java.lang.String toString(Config.Property[] props)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ENABLED

public static final java.lang.String ENABLED
Method Detail

cfg

public static Config cfg()

cfg

public static Config cfg(ConfigReader r)
                  throws ConfigReader.Exception

clone

public java.lang.Object clone()
Creates a copy of the configuration.

addChangeListener

public void addChangeListener(Config.ChangeListener l)

removeChangeListener

public void removeChangeListener(Config.ChangeListener l)

addDefaultSettings

public void addDefaultSettings(ConfigReader r)
                        throws ConfigReader.Exception
Adds some default settings to the current configuration.

addSettings

public void addSettings(ConfigReader r)
                 throws ConfigReader.Exception
Adds some new settings to the current configuration. Values for a particular property override given default values.
Throws:
ConfigReader.Exception -  

getPropertyValue

public java.lang.String getPropertyValue(Config.Property p)
                                  throws Config.MultiValueException
Reads the value for a given property.
Parameters:
p - The given property.
Returns:
The value for the property.

getPropertyValue

public java.lang.String getPropertyValue(java.lang.String moduleName,
                                         Config.Property p)
                                  throws Config.MultiValueException
Reads the value of a given property local to a particular module. The property id has the modul name as a prefix: Modulename.Property = ...

getPropertyValueInPath

public java.lang.String getPropertyValueInPath(Config.Property p)
                                        throws Config.MultiValueException
Searches for the value of the given property p. If the property value is not defined this methods searches the value in the enclosing module(s) (from innermost to the outermost).
Example: For the Propery Module.SubModule.Debug this method will search the properties Module.SubModule.Debug, Module.Debug and Module.SubModule.Debug (in this order).

getPropertyValueInPath

public java.lang.String getPropertyValueInPath(java.lang.String moduleName,
                                               Config.Property p)
                                        throws Config.MultiValueException
Uses the same search strategy as getPropertyValueInPath(Config.Property). The string moduleName is used as an prefix.
See Also:
getPropertyValueInPath(Config.Property), getPropertyValue(String,Config.Property)

getPropertyValues

public java.lang.String[] getPropertyValues(Config.Property p)
Reads the multivalue of a given property.

getPropertyValues

public java.lang.String[] getPropertyValues(java.lang.String moduleName,
                                            Config.Property p)

getDefaultPropertyValues

public java.lang.String[] getDefaultPropertyValues(Config.Property p)

getPropertyValuesInPath

public java.lang.String[] getPropertyValuesInPath(Config.Property p)
                                           throws Config.MultiValueException

hasProperty

public boolean hasProperty(Config.Property p)
Tests if any value has been set for a property.
Returns:
true, if a value for the given property is available (or a default value).

hasDefaultValue

public boolean hasDefaultValue(Config.Property p)

isEnabled

public boolean isEnabled(Config.Property p)
Checks whether a property is enabled or not. This is helpful for testing checkboxes in HTML-forms when handled by a servlet.
Returns:
true, if the given property is enabled.
See Also:
ServletRequestReader

setDefaultPropertyValue

public void setDefaultPropertyValue(Config.Property p,
                                    java.lang.String value)
Sets the default value for a property.

setDefaultPropertyValues

public void setDefaultPropertyValues(Config.Property p,
                                     java.lang.String[] values)
                              throws Config.Exception
Sets the default values for a property.

fillInDefaultPropertyValue

public void fillInDefaultPropertyValue(Config.Property p,
                                       java.lang.String value)
Set the default value for a property only if no value has been set already.

fillInDefaultPropertyValue

public void fillInDefaultPropertyValue(Config.Property p,
                                       java.lang.String[] values)
                                throws Config.Exception
Set the default values for a property only if no values have been set already.

setPropertyValue

public void setPropertyValue(Config.Property p,
                             java.lang.String value)
Sets the value for a property, overriding the default value (if any).

setPropertyValues

public void setPropertyValues(Config.Property p,
                              java.lang.String[] values)
                       throws Config.Exception
Sets the values for a property, overriding the default value (if any).

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

printTo

public void printTo(java.io.OutputStream os)
             throws java.io.IOException

printTo

public void printTo(java.io.Writer w)
             throws java.io.IOException

printTo

public void printTo(java.io.PrintWriter pw)
             throws java.io.IOException

toString

public java.lang.String toString(Config.Property[] props)

printSettings

public void printSettings(Config.Property[] props,
                          java.io.Writer w)
                   throws java.io.IOException
Prints the settigs for the given properties to Writer w.

printSettings

public void printSettings(Config.Property[] props,
                          java.io.PrintWriter pw)
                   throws java.io.IOException
Prints the settigs for the given properties to PrintWriter w.

printProperty

public java.lang.String printProperty(Config.Property p)