org.apache.commons.configuration
Class AbstractConfiguration

java.lang.Object
  |
  +--org.apache.commons.configuration.AbstractConfiguration
All Implemented Interfaces:
Configuration
Direct Known Subclasses:
BaseConfiguration, org.apache.commons.configuration.web.BaseWebConfiguration, CompositeConfiguration, DatabaseConfiguration, DataConfiguration, HierarchicalConfiguration, JNDIConfiguration, MapConfiguration, 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.29 2004/12/02 22:05:52 ebourg Exp $
Author:
Konstantin Shaposhnikov , Oliver Heger , Henning P. Schmiedehausen

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 value)
          {@inheritDoc}
protected abstract  void addPropertyDirect(String key, Object value)
          Adds a key/value pair to the Configuration.
 void clear()
          {@inheritDoc}
abstract  void clearProperty(String key)
          {@inheritDoc}
abstract  boolean containsKey(String key)
          {@inheritDoc}
 BigDecimal getBigDecimal(String key)
          {@inheritDoc}
 BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
          {@inheritDoc}
 BigInteger getBigInteger(String key)
          {@inheritDoc}
 BigInteger getBigInteger(String key, BigInteger defaultValue)
          {@inheritDoc}
 boolean getBoolean(String key)
          {@inheritDoc}
 boolean getBoolean(String key, boolean defaultValue)
          {@inheritDoc}
 Boolean getBoolean(String key, Boolean defaultValue)
          {@inheritDoc}
 byte getByte(String key)
          {@inheritDoc}
 byte getByte(String key, byte defaultValue)
          {@inheritDoc}
 Byte getByte(String key, Byte defaultValue)
          {@inheritDoc}
static char getDelimiter()
          Retrieve the current delimiter.
 double getDouble(String key)
          {@inheritDoc}
 double getDouble(String key, double defaultValue)
          {@inheritDoc}
 Double getDouble(String key, Double defaultValue)
          {@inheritDoc}
 float getFloat(String key)
          {@inheritDoc}
 float getFloat(String key, float defaultValue)
          {@inheritDoc}
 Float getFloat(String key, Float defaultValue)
          {@inheritDoc}
 int getInt(String key)
          {@inheritDoc}
 int getInt(String key, int defaultValue)
          {@inheritDoc}
 Integer getInteger(String key, Integer defaultValue)
          {@inheritDoc}
abstract  Iterator getKeys()
          {@inheritDoc}
 Iterator getKeys(String prefix)
          {@inheritDoc}
 List getList(String key)
          {@inheritDoc}
 List getList(String key, List defaultValue)
          {@inheritDoc}
 long getLong(String key)
          {@inheritDoc}
 long getLong(String key, long defaultValue)
          {@inheritDoc}
 Long getLong(String key, Long defaultValue)
          {@inheritDoc}
 Properties getProperties(String key)
          {@inheritDoc}
 Properties getProperties(String key, Properties defaults)
          Get a list of properties associated with the given configuration key.
 short getShort(String key)
          {@inheritDoc}
 short getShort(String key, short defaultValue)
          {@inheritDoc}
 Short getShort(String key, Short defaultValue)
          {@inheritDoc}
 String getString(String key)
          {@inheritDoc}
 String getString(String key, String defaultValue)
          {@inheritDoc}
 String[] getStringArray(String key)
          {@inheritDoc}
protected  Object interpolate(Object value)
          Returns the interpolated value.
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()
          {@inheritDoc}
 boolean isThrowExceptionOnMissing()
          Returns true if missing values throw Exceptions.
protected  Object resolveContainerStore(String key)
          Returns an object from the store described by the key.
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)
          {@inheritDoc}
 void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
          Allows to set the throwExceptionOnMissing flag.
 Configuration subset(String prefix)
          {@inheritDoc}
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.commons.configuration.Configuration
getProperty
 

Field Detail

START_TOKEN

protected static final String START_TOKEN
start token

END_TOKEN

protected static final String END_TOKEN
end token
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)
Allows to set the throwExceptionOnMissing flag. This flag controls the behavior of property getter methods that return objects if the requested property is missing. If the flag is set to false (which is the default value), these methods will return null. If set to true, they will throw a NoSuchElementException exception. Note that getter methods for primitive data types are not affected by this flag.
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 value)
{@inheritDoc}
Specified by:
addProperty in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The key to add the property to.
value - The value to add.

addPropertyDirect

protected abstract void addPropertyDirect(String key,
                                          Object value)
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
value - 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

interpolate

protected Object interpolate(Object value)
Returns the interpolated value. Non String values are returned without change.
Parameters:
value - the value to interpolate
Returns:
returns the value with variables 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

subset

public Configuration subset(String prefix)
{@inheritDoc}
Specified by:
subset in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
prefix - The prefix used to select the properties.
Returns:
a subset configuration
See Also:
SubsetConfiguration

isEmpty

public abstract boolean isEmpty()
{@inheritDoc}
Specified by:
isEmpty in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Returns:
true if the configuration contains no property, false otherwise.

containsKey

public abstract boolean containsKey(String key)
{@inheritDoc}
Specified by:
containsKey in interface Configuration
Following copied from interface: org.apache.commons.configuration.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)
{@inheritDoc}
Specified by:
setProperty in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The key of the property to change
value - The new value

clearProperty

public abstract void clearProperty(String key)
{@inheritDoc}
Specified by:
clearProperty in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - the key to remove along with corresponding value.

clear

public void clear()
{@inheritDoc}
Specified by:
clear in interface Configuration

getKeys

public abstract Iterator getKeys()
{@inheritDoc}
Specified by:
getKeys in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Returns:
An Iterator.

getKeys

public Iterator getKeys(String prefix)
{@inheritDoc}
Specified by:
getKeys in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
prefix - The prefix to test against.
Returns:
An Iterator of keys that match the prefix.
See Also:
Configuration.getKeys()

getProperties

public Properties getProperties(String key)
{@inheritDoc}
Specified by:
getProperties in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
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.
IllegalArgumentException - if one of the tokens is malformed (does not contain an equals sign).

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

getBoolean

public boolean getBoolean(String key)
{@inheritDoc}
Specified by:
getBoolean in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated boolean.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Boolean.

getBoolean

public boolean getBoolean(String key,
                          boolean defaultValue)
{@inheritDoc}
Specified by:
getBoolean in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated boolean.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Boolean.

getBoolean

public Boolean getBoolean(String key,
                          Boolean defaultValue)
{@inheritDoc}
Specified by:
getBoolean in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Boolean.

getByte

public byte getByte(String key)
{@inheritDoc}
Specified by:
getByte in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated byte.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Byte.

getByte

public byte getByte(String key,
                    byte defaultValue)
{@inheritDoc}
Specified by:
getByte in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated byte.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Byte.

getByte

public Byte getByte(String key,
                    Byte defaultValue)
{@inheritDoc}
Specified by:
getByte in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Byte.

getDouble

public double getDouble(String key)
{@inheritDoc}
Specified by:
getDouble in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated double.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Double.

getDouble

public double getDouble(String key,
                        double defaultValue)
{@inheritDoc}
Specified by:
getDouble in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated double.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Double.

getDouble

public Double getDouble(String key,
                        Double defaultValue)
{@inheritDoc}
Specified by:
getDouble in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Double.

getFloat

public float getFloat(String key)
{@inheritDoc}
Specified by:
getFloat in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated float.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Float.

getFloat

public float getFloat(String key,
                      float defaultValue)
{@inheritDoc}
Specified by:
getFloat in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated float.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Float.

getFloat

public Float getFloat(String key,
                      Float defaultValue)
{@inheritDoc}
Specified by:
getFloat in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Float.

getInt

public int getInt(String key)
{@inheritDoc}
Specified by:
getInt in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated int.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Integer.

getInt

public int getInt(String key,
                  int defaultValue)
{@inheritDoc}
Specified by:
getInt in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated int.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Integer.

getInteger

public Integer getInteger(String key,
                          Integer defaultValue)
{@inheritDoc}
Specified by:
getInteger in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Integer.

getLong

public long getLong(String key)
{@inheritDoc}
Specified by:
getLong in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated long.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Long.

getLong

public long getLong(String key,
                    long defaultValue)
{@inheritDoc}
Specified by:
getLong in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated long.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Long.

getLong

public Long getLong(String key,
                    Long defaultValue)
{@inheritDoc}
Specified by:
getLong in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Long.

getShort

public short getShort(String key)
{@inheritDoc}
Specified by:
getShort in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated short.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Short.

getShort

public short getShort(String key,
                      short defaultValue)
{@inheritDoc}
Specified by:
getShort in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated short.
Throws:
ConversionException - is thrown if the key maps to an object that is not a Short.

getShort

public Short getShort(String key,
                      Short defaultValue)
{@inheritDoc}
Specified by:
getShort in interface Configuration
Following copied from interface: org.apache.commons.configuration.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:
ConversionException - is thrown if the key maps to an object that is not a Short.

getBigDecimal

public BigDecimal getBigDecimal(String key)
{@inheritDoc}
Specified by:
getBigDecimal in interface Configuration
Following copied from interface: org.apache.commons.configuration.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)
{@inheritDoc}
Specified by:
getBigDecimal in interface Configuration
Following copied from interface: org.apache.commons.configuration.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)
{@inheritDoc}
Specified by:
getBigInteger in interface Configuration
Following copied from interface: org.apache.commons.configuration.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)
{@inheritDoc}
Specified by:
getBigInteger in interface Configuration
Following copied from interface: org.apache.commons.configuration.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)
{@inheritDoc}
Specified by:
getString in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated string.
Throws:
ConversionException - is thrown if the key maps to an object that is not a String.

getString

public String getString(String key,
                        String defaultValue)
{@inheritDoc}
Specified by:
getString in interface Configuration
Following copied from interface: org.apache.commons.configuration.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.
Throws:
ConversionException - is thrown if the key maps to an object that is not a String.

getStringArray

public String[] getStringArray(String key)
{@inheritDoc}
Specified by:
getStringArray in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated string array if key is found.
Throws:
ConversionException - is thrown if the key maps to an object that is not a String/List of Strings.

getList

public List getList(String key)
{@inheritDoc}
Specified by:
getList in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
Returns:
The associated List.
Throws:
ConversionException - is thrown if the key maps to an object that is not a List.

getList

public List getList(String key,
                    List defaultValue)
{@inheritDoc}
Specified by:
getList in interface Configuration
Following copied from interface: org.apache.commons.configuration.Configuration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated List of strings.
Throws:
ConversionException - is thrown if the key maps to an object that is not a List.

resolveContainerStore

protected Object resolveContainerStore(String key)
Returns an object from the store described by the key. If the value is a List object, replace it with the first object in the list.
Parameters:
key - The property key.
Returns:
value Value, transparently resolving a possible List dependency.


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