org.apache.commons.configuration
Class CompositeConfiguration

java.lang.Object
  extended byorg.apache.commons.configuration.CompositeConfiguration
All Implemented Interfaces:
Configuration

public class CompositeConfiguration
extends java.lang.Object
implements Configuration

This Configuration class allows you to add multiple different types of Configuration to this CompositeConfiguration. If you add Configuration1, and then Configuration2, any properties shared will mean that Configuration1 will be returned. You can add multiple different types or the same type of properties file. If Configuration1 doesn't have the property, then Configuration2 will be checked.

Version:
$Id: CompositeConfiguration.java,v 1.2 2003/12/24 14:28:22 epugh Exp $
Author:
Eric Pugh, Henning P. Schmiedehausen

Field Summary
private  java.util.LinkedList configList
          Array holding all the configuration
private  BaseConfiguration inMemoryConfiguration
          Configuration that holds in memory stuff.
 
Constructor Summary
CompositeConfiguration()
          Creates an empty CompositeConfiguration object which can then be added some other Configuration files
 
Method Summary
 void addConfiguration(Configuration config)
           
 void addProperty(java.lang.String key, java.lang.Object token)
          CompositeConfigurations can not be added to
 void clear()
           
 void clearProperty(java.lang.String key)
          Clear a property in the configuration.
 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.
 Configuration getConfiguration(int index)
           
 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.
private  Configuration getFirstMatchingConfig(java.lang.String 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.
 java.util.Iterator getKeys()
          Get the list of the keys contained in the configuration repository.
 java.util.Iterator getKeys(java.lang.String key)
          Get the list of the keys contained in the configuration repository.
 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.
 int getNumberOfConfigurations()
           
 java.util.Properties getProperties(java.lang.String key)
          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.
 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.
 boolean isEmpty()
          Check if the configuration is empty.
 void removeConfiguration(Configuration config)
           
 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 a CompositeConfiguration object that is a subset of this one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

configList

private java.util.LinkedList configList
Array holding all the configuration


inMemoryConfiguration

private BaseConfiguration inMemoryConfiguration
Configuration that holds in memory stuff. Inserted as first so any setProperty() override anything else added.

Constructor Detail

CompositeConfiguration

public CompositeConfiguration()
Creates an empty CompositeConfiguration object which can then be added some other Configuration files

Method Detail

addConfiguration

public void addConfiguration(Configuration config)

removeConfiguration

public void removeConfiguration(Configuration config)

getNumberOfConfigurations

public int getNumberOfConfigurations()

clear

public void clear()

addProperty

public void addProperty(java.lang.String key,
                        java.lang.Object token)
CompositeConfigurations can not be added to

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

getKeys

public 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 key)
Get the list of the keys contained in the configuration repository.

Specified by:
getKeys in interface Configuration
Parameters:
key - The prefix to test against.
Returns:
An Iterator.

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)

isEmpty

public boolean isEmpty()
Description copied from interface: Configuration
Check if the configuration is empty.

Specified by:
isEmpty in interface Configuration
Returns:
true is the configuration contains no key/value pair, false otherwise

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

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 -
value -

clearProperty

public 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.

containsKey

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

Specified by:
containsKey in interface Configuration
Returns:
true is the configuration contains a value for this key, false otherwise

subset

public Configuration subset(java.lang.String prefix)
Create a CompositeConfiguration object that is a subset of this one. Cycles over all the config objects, and calls their subset method and then just adds that.

Specified by:
subset in interface Configuration
Parameters:
prefix -

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.

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,
                      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.

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.

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.

getFirstMatchingConfig

private Configuration getFirstMatchingConfig(java.lang.String key)

getConfiguration

public Configuration getConfiguration(int index)