org.apache.commons.configuration
Class AbstractConfiguration

java.lang.Object
  extended byorg.apache.commons.configuration.AbstractConfiguration
All Implemented Interfaces:
Configuration
Direct Known Subclasses:
BaseConfiguration, HierarchicalConfiguration

public abstract class AbstractConfiguration
extends java.lang.Object
implements Configuration

Abstract configuration class. Provide basic functionality but does not store any data. If you want to write your own Configuration class then you should implement only abstract methods from this class.

Version:
$Id: AbstractConfiguration.java,v 1.2 2003/12/24 14:28:22 epugh Exp $
Author:
Konstantin Shaposhnikov, Oliver Heger

Nested Class Summary
(package private) static class AbstractConfiguration.Container
          Private Wrapper class for List, so we can distinguish between List objects and our container
(package private)  class AbstractConfiguration.PropertiesTokenizer
          This class divides into tokens a property value.
 
Field Summary
protected  Configuration defaults
          stores the configuration key-value pairs
protected static java.lang.String END_TOKEN
          end token
private static int INITIAL_LIST_SIZE
          how big the initial arraylist for splitting up name value pairs
protected static java.lang.String START_TOKEN
          start token
 
Constructor Summary
AbstractConfiguration()
          Empty constructor.
AbstractConfiguration(Configuration defaults)
          Creates an empty AbstractConfiguration object with a Super-Object which is queries for every key.
 
Method Summary
 void addProperty(java.lang.String key, java.lang.Object token)
          Add a property to the configuration.
protected abstract  void addPropertyDirect(java.lang.String key, java.lang.Object obj)
          Adds a key/value pair to the Configuration.
abstract  void clearProperty(java.lang.String key)
          Clear a property in the configuration.
abstract  boolean containsKey(java.lang.String key)
          check if the configuration contains the key
 boolean getBoolean(java.lang.String key)
          Get a boolean associated with the given configuration key.
 boolean getBoolean(java.lang.String key, boolean defaultValue)
          Get a boolean associated with the given configuration key.
 java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean defaultValue)
          Get a boolean associated with the given configuration key.
 byte getByte(java.lang.String key)
          Get a byte associated with the given configuration key.
 byte getByte(java.lang.String key, byte defaultValue)
          Get a byte associated with the given configuration key.
 java.lang.Byte getByte(java.lang.String key, java.lang.Byte defaultValue)
          Get a byte associated with the given configuration key.
 double getDouble(java.lang.String key)
          Get a double associated with the given configuration key.
 double getDouble(java.lang.String key, double defaultValue)
          Get a double associated with the given configuration key.
 java.lang.Double getDouble(java.lang.String key, java.lang.Double defaultValue)
          Get a double associated with the given configuration key.
 float getFloat(java.lang.String key)
          Get a float associated with the given configuration key.
 float getFloat(java.lang.String key, float defaultValue)
          Get a float associated with the given configuration key.
 java.lang.Float getFloat(java.lang.String key, java.lang.Float defaultValue)
          Get a float associated with the given configuration key.
 int getInt(java.lang.String key)
          Get a int associated with the given configuration key.
 int getInt(java.lang.String key, int defaultValue)
          Get a int associated with the given configuration key.
 java.lang.Integer getInteger(java.lang.String key, java.lang.Integer defaultValue)
          Get a int associated with the given configuration key.
abstract  java.util.Iterator getKeys()
          Get the list of the keys contained in the configuration repository.
 java.util.Iterator getKeys(java.lang.String prefix)
          Get the list of the keys contained in the configuration repository that match the specified prefix.
 java.util.List getList(java.lang.String key)
          Get a List of strings associated with the given configuration key.
 java.util.List getList(java.lang.String key, java.util.List defaultValue)
          Get a List of strings associated with the given configuration key.
 long getLong(java.lang.String key)
          Get a long associated with the given configuration key.
 long getLong(java.lang.String key, long defaultValue)
          Get a long associated with the given configuration key.
 java.lang.Long getLong(java.lang.String key, java.lang.Long defaultValue)
          Get a long associated with the given configuration key.
 java.util.Properties getProperties(java.lang.String key)
          Get a list of properties associated with the given configuration key.
 java.util.Properties getProperties(java.lang.String key, java.util.Properties defaults)
          Get a list of properties associated with the given configuration key.
 java.lang.Object getProperty(java.lang.String key)
          Gets a property from the configuration.
protected abstract  java.lang.Object getPropertyDirect(java.lang.String key)
          Read property.
 short getShort(java.lang.String key)
          Get a short associated with the given configuration key.
 short getShort(java.lang.String key, short defaultValue)
          Get a short associated with the given configuration key.
 java.lang.Short getShort(java.lang.String key, java.lang.Short defaultValue)
          Get a short associated with the given configuration key.
 java.lang.String getString(java.lang.String key)
          Get a string associated with the given configuration key.
 java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
          Get a string associated with the given configuration key.
 java.lang.String[] getStringArray(java.lang.String key)
          Get an array of strings associated with the given configuration key.
protected  java.lang.String interpolate(java.lang.String base)
          interpolate key names to handle ${key} stuff
protected  java.lang.String interpolateHelper(java.lang.String base, java.util.List priorVariables)
          Recursive handler for multple levels of interpolation.
abstract  boolean isEmpty()
          Check if the configuration is empty
protected  java.util.List processString(java.lang.String token)
          Returns a List of Strings built from the supplied String.
private  java.lang.Object resolveContainerStore(java.lang.String key)
          Returns an object from the store described by the key.
 void setProperty(java.lang.String key, java.lang.Object value)
          Set a property, this will replace any previously set values.
 Configuration subset(java.lang.String prefix)
          Create an BaseConfiguration object that is a subset of this one.
protected  java.lang.Boolean testBoolean(java.lang.String value)
          Test whether the string represent by value maps to a boolean value or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INITIAL_LIST_SIZE

private static final int INITIAL_LIST_SIZE
how big the initial arraylist for splitting up name value pairs

See Also:
Constant Field Values

defaults

protected Configuration defaults
stores the configuration key-value pairs


START_TOKEN

protected static final java.lang.String START_TOKEN
start token

See Also:
Constant Field Values

END_TOKEN

protected static final java.lang.String END_TOKEN
end token

See Also:
Constant Field Values
Constructor Detail

AbstractConfiguration

public AbstractConfiguration()
Empty constructor.


AbstractConfiguration

public AbstractConfiguration(Configuration defaults)
Creates an empty AbstractConfiguration object with a Super-Object which is queries for every key.

Parameters:
defaults - Configuration defaults to use if key not in file
Method Detail

addProperty

public void addProperty(java.lang.String key,
                        java.lang.Object token)
Add a property to the configuration. If it already exists then the value stated here will be added to the configuration entry. For example, if resource.loader = file is already present in the configuration and you addProperty("resource.loader", "classpath") Then you will end up with a List like the following: ["file", "classpath"]

Specified by:
addProperty in interface Configuration
Parameters:
key - The Key to add the property to.
token - The Value to add.

getPropertyDirect

protected abstract java.lang.Object getPropertyDirect(java.lang.String key)
Read property. Should return null if the key doesn't map to an existing object.

Parameters:
key - key to use for mapping
Returns:
object associated with the given configuration key.

addPropertyDirect

protected abstract void addPropertyDirect(java.lang.String key,
                                          java.lang.Object obj)
Adds a key/value pair to the Configuration. Override this method to provide write acces to underlying Configuration store.

Parameters:
key - key to use for mapping
obj - object to store

interpolate

protected java.lang.String interpolate(java.lang.String base)
interpolate key names to handle ${key} stuff

Parameters:
base - string to interpolate
Returns:
returns the key name with the ${key} substituted

interpolateHelper

protected java.lang.String interpolateHelper(java.lang.String base,
                                             java.util.List priorVariables)
Recursive handler for multple levels of interpolation. When called the first time, priorVariables should be null.

Parameters:
base - string with the ${key} variables
priorVariables - serves two purposes: to allow checking for loops, and creating a meaningful exception message should a loop occur. It's 0'th element will be set to the value of base from the first call. All subsequent interpolated variables are added afterward.
Returns:
the string with the interpolation taken care of

processString

protected java.util.List processString(java.lang.String token)
Returns a List of Strings built from the supplied String. Splits up CSV lists. If no commas are in the String, simply returns a List with the String as its first element

Parameters:
token - The String to tokenize
Returns:
A List of Strings

testBoolean

protected final java.lang.Boolean testBoolean(java.lang.String value)
Test whether the string represent by value maps to a boolean value or not. We will allow true, on, and yes for a true boolean value, and false, off, and no for false boolean values. Case of value to test for boolean status is ignored.

Parameters:
value - The value to test for boolean state.
Returns:
true or false if the supplied text maps to a boolean value, or null otherwise.

subset

public Configuration subset(java.lang.String prefix)
Create an BaseConfiguration object that is a subset of this one.

Specified by:
subset in interface Configuration
Parameters:
prefix - prefix string for keys
Returns:
subset of configuration if there is keys, that match given prefix, or null if there is no such keys.

isEmpty

public abstract boolean isEmpty()
Check if the configuration is empty

Specified by:
isEmpty in interface Configuration
Returns:
true if Configuration is empty, false otherwise.

containsKey

public abstract boolean containsKey(java.lang.String key)
check if the configuration contains the key

Specified by:
containsKey in interface Configuration
Parameters:
key - the configuration key
Returns:
true if Configuration contain given key, false otherwise.

setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object value)
Set a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key), addProperty(key,value).

Specified by:
setProperty in interface Configuration
Parameters:
key - the configuration key
value - the property value

clearProperty

public abstract void clearProperty(java.lang.String key)
Clear a property in the configuration.

Specified by:
clearProperty in interface Configuration
Parameters:
key - the key to remove along with corresponding value.

getKeys

public abstract java.util.Iterator getKeys()
Get the list of the keys contained in the configuration repository.

Specified by:
getKeys in interface Configuration
Returns:
An Iterator.

getKeys

public java.util.Iterator getKeys(java.lang.String prefix)
Get the list of the keys contained in the configuration repository that match the specified prefix.

Specified by:
getKeys in interface Configuration
Parameters:
prefix - The prefix to test against.
Returns:
An Iterator of keys that match the prefix.

getProperties

public java.util.Properties getProperties(java.lang.String key)
Get a list of properties associated with the given configuration key.

Specified by:
getProperties in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated properties if key is found.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String/List.
java.lang.IllegalArgumentException - if one of the tokens is malformed (does not contain an equals sign).
See Also:
getProperties(String, Properties)

getProperties

public java.util.Properties getProperties(java.lang.String key,
                                          java.util.Properties defaults)
Get a list of properties associated with the given configuration key.

Parameters:
key - The configuration key.
defaults - Any default values for the returned Properties object. Ignored if null.
Returns:
The associated properties if key is found.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String/List of Strings.
java.lang.IllegalArgumentException - if one of the tokens is malformed (does not contain an equals sign).

getProperty

public java.lang.Object getProperty(java.lang.String key)
Gets a property from the configuration.

Specified by:
getProperty in interface Configuration
Parameters:
key - property to retrieve
Returns:
value as object. Will return user value if exists, if not then default value if exists, otherwise null

getBoolean

public boolean getBoolean(java.lang.String key)
Get a boolean associated with the given configuration key.

Specified by:
getBoolean in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated boolean.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Boolean.

getBoolean

public boolean getBoolean(java.lang.String key,
                          boolean defaultValue)
Get a boolean associated with the given configuration key.

Specified by:
getBoolean in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated boolean.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Boolean.

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key,
                                    java.lang.Boolean defaultValue)
Get a boolean associated with the given configuration key.

Specified by:
getBoolean in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated boolean if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Boolean.

getByte

public byte getByte(java.lang.String key)
Get a byte associated with the given configuration key.

Specified by:
getByte in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated byte.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Byte.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getByte

public byte getByte(java.lang.String key,
                    byte defaultValue)
Get a byte associated with the given configuration key.

Specified by:
getByte in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated byte.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Byte.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getByte

public java.lang.Byte getByte(java.lang.String key,
                              java.lang.Byte defaultValue)
Get a byte associated with the given configuration key.

Specified by:
getByte in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated byte if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Byte.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getDouble

public double getDouble(java.lang.String key)
Get a double associated with the given configuration key.

Specified by:
getDouble in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated double.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Double.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getDouble

public double getDouble(java.lang.String key,
                        double defaultValue)
Get a double associated with the given configuration key.

Specified by:
getDouble in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated double.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Double.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getDouble

public java.lang.Double getDouble(java.lang.String key,
                                  java.lang.Double defaultValue)
Get a double associated with the given configuration key.

Specified by:
getDouble in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated double if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Double.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getFloat

public float getFloat(java.lang.String key)
Get a float associated with the given configuration key.

Specified by:
getFloat in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated float.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Float.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getFloat

public float getFloat(java.lang.String key,
                      float defaultValue)
Get a float associated with the given configuration key.

Specified by:
getFloat in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated float.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Float.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getFloat

public java.lang.Float getFloat(java.lang.String key,
                                java.lang.Float defaultValue)
Get a float associated with the given configuration key.

Specified by:
getFloat in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated float if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Float.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getInt

public int getInt(java.lang.String key)
Get a int associated with the given configuration key.

Specified by:
getInt in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated int.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Integer.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getInt

public int getInt(java.lang.String key,
                  int defaultValue)
Get a int associated with the given configuration key.

Specified by:
getInt in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated int.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Integer.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getInteger

public java.lang.Integer getInteger(java.lang.String key,
                                    java.lang.Integer defaultValue)
Get a int associated with the given configuration key.

Specified by:
getInteger in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated int if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Integer.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getLong

public long getLong(java.lang.String key)
Get a long associated with the given configuration key.

Specified by:
getLong in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated long.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Long.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getLong

public long getLong(java.lang.String key,
                    long defaultValue)
Get a long associated with the given configuration key.

Specified by:
getLong in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated long.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Long.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getLong

public java.lang.Long getLong(java.lang.String key,
                              java.lang.Long defaultValue)
Get a long associated with the given configuration key.

Specified by:
getLong in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated long if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Long.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getShort

public short getShort(java.lang.String key)
Get a short associated with the given configuration key.

Specified by:
getShort in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated short.
Throws:
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Short.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getShort

public short getShort(java.lang.String key,
                      short defaultValue)
Get a short associated with the given configuration key.

Specified by:
getShort in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated short.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Short.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getShort

public java.lang.Short getShort(java.lang.String key,
                                java.lang.Short defaultValue)
Get a short associated with the given configuration key.

Specified by:
getShort in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated short if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Short.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getString

public java.lang.String getString(java.lang.String key)
Get a string associated with the given configuration key.

Specified by:
getString in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated string.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String.
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String defaultValue)
Get a string associated with the given configuration key.

Specified by:
getString in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated string if key is found, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String.

getStringArray

public java.lang.String[] getStringArray(java.lang.String key)
Get an array of strings associated with the given configuration key.

Specified by:
getStringArray in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated string array if key is found.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String/List of Strings.

getList

public java.util.List getList(java.lang.String key)
Get a List of strings associated with the given configuration key.

Specified by:
getList in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated List.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a List.
java.util.NoSuchElementException - is thrown if the key doesn't map to an existing object.

getList

public java.util.List getList(java.lang.String key,
                              java.util.List defaultValue)
Get a List of strings associated with the given configuration key.

Specified by:
getList in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated List.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a List.

resolveContainerStore

private java.lang.Object resolveContainerStore(java.lang.String key)
Returns an object from the store described by the key. If the value is a Container object, replace it with the first object in the container

Parameters:
key - The property key.
Returns:
value Value, transparently resolving a possible Container dependency.