org.apache.commons.configuration
Class AbstractConfiguration

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

public abstract class AbstractConfiguration
extends 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.25 2004/10/05 21:17:25 ebourg Exp $
Author:
Konstantin Shaposhnikov, Oliver Heger, Henning P. Schmiedehausen

Nested Class Summary
(package private) static class AbstractConfiguration.PropertiesTokenizer
          This class divides into tokens a property value.
 
Field Summary
protected static String END_TOKEN
          end token
protected static String START_TOKEN
          start token
 
Constructor Summary
AbstractConfiguration()
           
 
Method Summary
 void addProperty(String key, 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 the property:
resource.loader = file
is already present in the configuration and you call
addProperty("resource.loader", "classpath")
Then you will end up with a List like the following:
["file", "classpath"]
protected abstract  void addPropertyDirect(String key, Object obj)
          Adds a key/value pair to the Configuration.
abstract  void clearProperty(String key)
          Remove a property from the configuration.
abstract  boolean containsKey(String key)
          Check if the configuration contains the specified key.
 BigDecimal getBigDecimal(String key)
          Get a BigDecimal associated with the given configuration key.
 BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
          Get a BigDecimal associated with the given configuration key.
 BigInteger getBigInteger(String key)
          Get a BigInteger associated with the given configuration key.
 BigInteger getBigInteger(String key, BigInteger defaultValue)
          Get a BigInteger associated with the given configuration key.
 boolean getBoolean(String key)
          Get a boolean associated with the given configuration key.
 boolean getBoolean(String key, boolean defaultValue)
          Get a boolean associated with the given configuration key.
 Boolean getBoolean(String key, Boolean defaultValue)
          Get a Boolean associated with the given configuration key.
 byte getByte(String key)
          Get a byte associated with the given configuration key.
 byte getByte(String key, byte defaultValue)
          Get a byte associated with the given configuration key.
 Byte getByte(String key, Byte defaultValue)
          Get a Byte associated with the given configuration key.
static char getDelimiter()
          Retrieve the current delimiter.
 double getDouble(String key)
          Get a double associated with the given configuration key.
 double getDouble(String key, double defaultValue)
          Get a double associated with the given configuration key.
 Double getDouble(String key, Double defaultValue)
          Get a Double associated with the given configuration key.
 float getFloat(String key)
          Get a float associated with the given configuration key.
 float getFloat(String key, float defaultValue)
          Get a float associated with the given configuration key.
 Float getFloat(String key, Float defaultValue)
          Get a Float associated with the given configuration key.
 int getInt(String key)
          Get a int associated with the given configuration key.
 int getInt(String key, int defaultValue)
          Get a int associated with the given configuration key.
 Integer getInteger(String key, Integer defaultValue)
          Get an Integer associated with the given configuration key.
abstract  Iterator getKeys()
          Get the list of the keys contained in the configuration.
 Iterator getKeys(String prefix)
          Get the list of the keys contained in the configuration that match the specified prefix.
 List getList(String key)
          Get a List of strings associated with the given configuration key. If the key doesn't map to an existing object an empty List is returned.
 List getList(String key, List defaultValue)
          Get a List of strings associated with the given configuration key.
 long getLong(String key)
          Get a long associated with the given configuration key.
 long getLong(String key, long defaultValue)
          Get a long associated with the given configuration key.
 Long getLong(String key, Long defaultValue)
          Get a Long associated with the given configuration key.
 Properties getProperties(String key)
          Get a list of properties associated with the given configuration key.
 Properties getProperties(String key, Properties defaults)
          Get a list of properties associated with the given configuration key.
 Object getProperty(String key)
          Gets a property from the configuration.
protected abstract  Object getPropertyDirect(String key)
          Read property.
 short getShort(String key)
          Get a short associated with the given configuration key.
 short getShort(String key, short defaultValue)
          Get a short associated with the given configuration key.
 Short getShort(String key, Short defaultValue)
          Get a Short associated with the given configuration key.
 String getString(String key)
          Get a string associated with the given configuration key.
 String getString(String key, String defaultValue)
          Get a string associated with the given configuration key.
 String[] getStringArray(String key)
          Get an array of strings associated with the given configuration key. If the key doesn't map to an existing object an empty array is returned
 Vector getVector(String key)
          Get a Vector of strings associated with the given configuration key. If the key doesn't map to an existing object an empty Vector is returned.
 Vector getVector(String key, Vector defaultValue)
          Get a Vector of strings associated with the given configuration key.
protected  String interpolate(String base)
          interpolate key names to handle ${key} stuff
protected  String interpolateHelper(String base, List priorVariables)
          Recursive handler for multple levels of interpolation.
abstract  boolean isEmpty()
          Check if the configuration is empty.
 boolean isThrowExceptionOnMissing()
          Returns true if missing values throw Exceptions.
static void setDelimiter(char delimiter)
          For configurations extending AbstractConfiguration, allow them to change the delimiter from the default comma (",").
 void setProperty(String key, Object value)
          Set a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key), addProperty(key, value).
 void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
          If set to false, missing elements return null if possible (for objects).
protected  List split(String token)
          Returns a List of Strings built from the supplied String.
 Configuration subset(String prefix)
          Return a decorator Configuration containing every key from the current Configuration that starts with the specified prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following properties:
    prefix.number = 1
    prefix.string = Apache
    prefixed.foo = bar
    prefix = Jakarta
the Configuration returned by subset("prefix") will contain the properties:
    number = 1
    string = Apache
    = Jakarta
(The key for the value "Jakarta" is an empty string)

Since the subset is a decorator and not a modified copy of the initial Configuration, any change made to the subset is available to the Configuration, and reciprocally.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

START_TOKEN

protected static final String START_TOKEN
start token

See Also:
Constant Field Values

END_TOKEN

protected static final String END_TOKEN
end token

See Also:
Constant Field Values
Constructor Detail

AbstractConfiguration

public AbstractConfiguration()
Method Detail

setDelimiter

public static void setDelimiter(char delimiter)
For configurations extending AbstractConfiguration, allow them to change the delimiter from the default comma (",").

Parameters:
delimiter - The new delimiter

getDelimiter

public static char getDelimiter()
Retrieve the current delimiter. By default this is a comma (",").

Returns:
The delimiter in use

setThrowExceptionOnMissing

public void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
If set to false, missing elements return null if possible (for objects).

Parameters:
throwExceptionOnMissing - The new value for the property

isThrowExceptionOnMissing

public boolean isThrowExceptionOnMissing()
Returns true if missing values throw Exceptions.

Returns:
true if missing values throw Exceptions

addProperty

public void addProperty(String key,
                        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 the property:
resource.loader = file
is already present in the configuration and you call
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 Object getPropertyDirect(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(String key,
                                          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 String interpolate(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 String interpolateHelper(String base,
                                   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

split

protected List split(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

subset

public Configuration subset(String prefix)
Return a decorator Configuration containing every key from the current Configuration that starts with the specified prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following properties:
    prefix.number = 1
    prefix.string = Apache
    prefixed.foo = bar
    prefix = Jakarta
the Configuration returned by subset("prefix") will contain the properties:
    number = 1
    string = Apache
    = Jakarta
(The key for the value "Jakarta" is an empty string)

Since the subset is a decorator and not a modified copy of the initial Configuration, any change made to the subset is available to the Configuration, and reciprocally.

Specified by:
subset in interface Configuration
Parameters:
prefix - The prefix used to select the properties.
See Also:
SubsetConfiguration

isEmpty

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

Specified by:
isEmpty in interface Configuration
Returns:
true if the configuration contains no property, false otherwise.

containsKey

public abstract boolean containsKey(String key)
Check if the configuration contains the specified key.

Specified by:
containsKey in interface Configuration
Parameters:
key - the key whose presence in this configuration is to be tested
Returns:
true if the configuration contains a value for this key, false otherwise

setProperty

public void setProperty(String key,
                        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 key of the property to change
value - The new value

clearProperty

public abstract void clearProperty(String key)
Remove a property from the configuration.

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

getKeys

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

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

getKeys

public Iterator getKeys(String prefix)
Get the list of the keys contained in the configuration 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 Properties getProperties(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.

getProperties

public Properties getProperties(String key,
                                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:
ConversionException - is thrown if the key maps to an object that is not a String/List of Strings.
IllegalArgumentException - if one of the tokens is malformed (does not contain an equals sign).

getProperty

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

Specified by:
getProperty in interface Configuration
Parameters:
key - property to retrieve
Returns:
the value to which this configuration maps the specified key, or null if the configuration contains no mapping for this key.

getBoolean

public boolean getBoolean(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.

getBoolean

public boolean getBoolean(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.

getBoolean

public Boolean getBoolean(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 if key is found and has valid format, default value otherwise.

getByte

public byte getByte(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.

getByte

public byte getByte(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.

getByte

public Byte getByte(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 if key is found and has valid format, default value otherwise.

getDouble

public double getDouble(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.

getDouble

public double getDouble(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.

getDouble

public Double getDouble(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 if key is found and has valid format, default value otherwise.

getFloat

public float getFloat(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.

getFloat

public float getFloat(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.

getFloat

public Float getFloat(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 if key is found and has valid format, default value otherwise.

getInt

public int getInt(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.

getInt

public int getInt(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.

getInteger

public Integer getInteger(String key,
                          Integer defaultValue)
Get an Integer 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.

getLong

public long getLong(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.

getLong

public long getLong(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.

getLong

public Long getLong(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 if key is found and has valid format, default value otherwise.

getShort

public short getShort(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.

getShort

public short getShort(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.

getShort

public Short getShort(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 if key is found and has valid format, default value otherwise.

getBigDecimal

public BigDecimal getBigDecimal(String key)
Get a BigDecimal associated with the given configuration key.

Specified by:
getBigDecimal in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated BigDecimal if key is found and has valid format

getBigDecimal

public BigDecimal getBigDecimal(String key,
                                BigDecimal defaultValue)
Get a BigDecimal associated with the given configuration key.

Specified by:
getBigDecimal in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated BigDecimal if key is found and has valid format, default value otherwise.

getBigInteger

public BigInteger getBigInteger(String key)
Get a BigInteger associated with the given configuration key.

Specified by:
getBigInteger in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated BigInteger if key is found and has valid format

getBigInteger

public BigInteger getBigInteger(String key,
                                BigInteger defaultValue)
Get a BigInteger associated with the given configuration key.

Specified by:
getBigInteger in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated BigInteger if key is found and has valid format, default value otherwise.

getString

public String getString(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.

getString

public String getString(String key,
                        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 and has valid format, default value otherwise.

getStringArray

public String[] getStringArray(String key)
Get an array of strings associated with the given configuration key. If the key doesn't map to an existing object an empty array is returned

Specified by:
getStringArray in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated string array if key is found.

getList

public List getList(String key)
Get a List of strings associated with the given configuration key. If the key doesn't map to an existing object an empty List is returned.

Specified by:
getList in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated List.

getList

public List getList(String key,
                    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.

getVector

public Vector getVector(String key)
Get a Vector of strings associated with the given configuration key. If the key doesn't map to an existing object an empty Vector is returned.

Specified by:
getVector in interface Configuration
Parameters:
key - The configuration key.
Returns:
The associated Vector.

getVector

public Vector getVector(String key,
                        Vector defaultValue)
Get a Vector of strings associated with the given configuration key.

Specified by:
getVector in interface Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated Vector.


Copyright © 2001-2004 The Apache Software Foundation. All Rights Reserved.