public abstract class AbstractConfiguration extends EventSource implements Configuration
Abstract configuration class. Provides 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. A lot of functionality
needed by typical implementations of the Configuration
interface is already provided by this base class. Following is a list of
features implemented here:
Configuration interface are already handled by this base class.
A concrete sub class only needs to provide a generic getProperty()
method.${var}) will be replaced by their
corresponding values.setListDelimiter() method the delimiter character can be
specified; per default a comma is used. The
setDelimiterParsingDisabled() method can be used to disable
list splitting completely.)setThrowExceptionOnMissing() method this behavior can be
changed to throw an exception when a requested property cannot be found.EVENT_XXX
constants to get an impression about which event types are supported.| Modifier and Type | Field and Description |
|---|---|
protected static String |
END_TOKEN
end token
|
static int |
EVENT_ADD_PROPERTY
Constant for the add property event type.
|
static int |
EVENT_CLEAR
Constant for the clear configuration event type.
|
static int |
EVENT_CLEAR_PROPERTY
Constant for the clear property event type.
|
static int |
EVENT_READ_PROPERTY
Constant for the get property event type.
|
static int |
EVENT_SET_PROPERTY
Constant for the set property event type.
|
protected static String |
START_TOKEN
start token
|
| Constructor and Description |
|---|
AbstractConfiguration()
Creates a new instance of
AbstractConfiguration. |
| Modifier and Type | Method and Description |
|---|---|
void |
addErrorLogListener()
Adds a special
ConfigurationErrorListener
object to this configuration that will log all internal errors. |
void |
addProperty(String key,
Object value)
Add a property to the configuration.
|
protected abstract void |
addPropertyDirect(String key,
Object value)
Adds a key/value pair to the Configuration.
|
void |
append(Configuration c)
Appends the content of the specified configuration to this configuration.
|
void |
clear()
Remove all properties from the configuration.
|
void |
clearProperty(String key)
Removes the specified property from this configuration.
|
protected void |
clearPropertyDirect(String key)
Removes the specified property from this configuration.
|
void |
copy(Configuration c)
Copies the content of the specified configuration into this
configuration.
|
protected ConfigurationInterpolator |
createInterpolator()
Creates the interpolator object that is responsible for variable
interpolation.
|
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)
Obtains the value of the specified key and tries to convert it into a
Boolean object. |
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 |
getDefaultListDelimiter()
Retrieve the current delimiter.
|
static char |
getDelimiter()
Deprecated.
Use AbstractConfiguration.getDefaultListDelimiter() instead
|
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. |
ConfigurationInterpolator |
getInterpolator()
Returns the
ConfigurationInterpolator object that manages
the lookup objects for resolving variables. |
Iterator<String> |
getKeys(String prefix)
Get the list of the keys contained in the configuration that match the
specified prefix.
|
List<Object> |
getList(String key)
Get a List of strings associated with the given configuration key.
|
List<Object> |
getList(String key,
List<?> defaultValue)
Get a List of strings associated with the given configuration key.
|
char |
getListDelimiter()
Retrieve the delimiter for this configuration.
|
org.apache.commons.logging.Log |
getLogger()
Returns the logger used by this configuration object.
|
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.
|
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.
|
org.apache.commons.lang.text.StrSubstitutor |
getSubstitutor()
Returns the object that is responsible for variable interpolation.
|
protected Object |
interpolate(Object value)
Returns the interpolated value.
|
protected String |
interpolate(String base)
interpolate key names to handle ${key} stuff
|
Configuration |
interpolatedConfiguration()
Returns a configuration with the same content as this configuration, but
with all variables replaced by their actual values.
|
protected String |
interpolateHelper(String base,
List<?> priorVariables)
Deprecated.
Interpolation is now handled by
PropertyConverter; this method will no longer be
called |
boolean |
isDelimiterParsingDisabled()
Determine if this configuration is using delimiters when parsing
property values to convert them to lists of values.
|
protected boolean |
isScalarValue(Object value)
Checks whether the specified object is a scalar value.
|
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 |
setDefaultListDelimiter(char delimiter)
For configurations extending AbstractConfiguration, allow them to change
the listDelimiter from the default comma (",").
|
static void |
setDelimiter(char delimiter)
Deprecated.
Use AbstractConfiguration.setDefaultListDelimiter(char)
instead
|
void |
setDelimiterParsingDisabled(boolean delimiterParsingDisabled)
Set whether this configuration should use delimiters when parsing
property values to convert them to lists of values.
|
void |
setListDelimiter(char listDelimiter)
Change the list delimiter for this configuration.
|
void |
setLogger(org.apache.commons.logging.Log log)
Allows to set the logger to be used by this configuration object.
|
void |
setProperty(String key,
Object value)
Set a property, this will replace any previously set values.
|
void |
setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
Allows to set the
throwExceptionOnMissing flag. |
Configuration |
subset(String prefix)
Return a decorator Configuration containing every key from the current
Configuration that starts with the specified prefix.
|
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, clone, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEventsequals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcontainsKey, getKeys, getProperty, isEmptypublic static final int EVENT_ADD_PROPERTY
public static final int EVENT_CLEAR_PROPERTY
public static final int EVENT_SET_PROPERTY
public static final int EVENT_CLEAR
public static final int EVENT_READ_PROPERTY
protected static final String START_TOKEN
protected static final String END_TOKEN
public AbstractConfiguration()
AbstractConfiguration.public static void setDefaultListDelimiter(char delimiter)
delimiter - The new listDelimiter@Deprecated public static void setDelimiter(char delimiter)
delimiter - the delimiter characterpublic static char getDefaultListDelimiter()
@Deprecated public static char getDelimiter()
public void setListDelimiter(char listDelimiter)
listDelimiter - The new listDelimiterpublic char getListDelimiter()
public boolean isDelimiterParsingDisabled()
public void setDelimiterParsingDisabled(boolean delimiterParsingDisabled)
delimiterParsingDisabled - a flag whether delimiter parsing should
be disabledpublic void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
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.throwExceptionOnMissing - The new value for the propertypublic boolean isThrowExceptionOnMissing()
public org.apache.commons.lang.text.StrSubstitutor getSubstitutor()
public ConfigurationInterpolator getInterpolator()
ConfigurationInterpolator object that manages
the lookup objects for resolving variables. Note: If this
object is manipulated (e.g. new lookup objects added), synchronization
has to be manually ensured. Because
ConfigurationInterpolator is not thread-safe concurrent
access to properties of this configuration instance (which causes the
interpolator to be invoked) may cause race conditions.ConfigurationInterpolator associated with this
configurationprotected ConfigurationInterpolator createInterpolator()
ConfigurationInterpolator and sets the default lookup
object to an implementation that queries this configuration.public org.apache.commons.logging.Log getLogger()
public void setLogger(org.apache.commons.logging.Log log)
log - the new loggerpublic void addErrorLogListener()
ConfigurationErrorListener
object to this configuration that will log all internal errors. This
method is intended to be used by certain derived classes, for which it is
known that they can fail on property access (e.g.
DatabaseConfiguration).public void addProperty(String key, Object value)
Configurationresource.loader = fileis 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"]
addProperty in interface Configurationkey - The key to add the property to.value - The value to add.protected abstract void addPropertyDirect(String key, Object value)
key - key to use for mappingvalue - object to storeprotected String interpolate(String base)
base - string to interpolateprotected Object interpolate(Object value)
value - the value to interpolate@Deprecated protected String interpolateHelper(String base, List<?> priorVariables)
PropertyConverter; this method will no longer be
calledbase - string with the ${key} variablespriorVariables - 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.public Configuration subset(String prefix)
Configuration
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.
subset in interface Configurationprefix - The prefix used to select the properties.SubsetConfigurationpublic void setProperty(String key, Object value)
ConfigurationsetProperty in interface Configurationkey - The key of the property to changevalue - The new valuepublic void clearProperty(String key)
clearPropertyDirect(), which will do the real work.clearProperty in interface Configurationkey - the key to be removedprotected void clearPropertyDirect(String key)
clearProperty() after it has done some
preparations. It should be overridden in sub classes. This base
implementation is just left empty.key - the key to be removedpublic void clear()
Configurationclear in interface Configurationpublic Iterator<String> getKeys(String prefix)
db.user, db.pwd, db.url, window.xpos, window.ypos,getKeys("db");db.user, db.pwd, db.url.getKeys("db"); will find the keys db,
db.user, or db.password, but not the key
dbdriver.getKeys in interface Configurationprefix - The prefix to test against.Configuration.getKeys()public Properties getProperties(String key)
ConfigurationProperties object, the value parts
become values.getProperties in interface Configurationkey - The configuration key.public Properties getProperties(String key, Properties defaults)
key - The configuration key.defaults - Any default values for the returned
Properties object. Ignored if null.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).public boolean getBoolean(String key)
getBoolean in interface Configurationkey - The configuration key.PropertyConverter.toBoolean(Object)public boolean getBoolean(String key, boolean defaultValue)
getBoolean in interface Configurationkey - The configuration key.defaultValue - The default value.PropertyConverter.toBoolean(Object)public Boolean getBoolean(String key, Boolean defaultValue)
Boolean object. If the property has no value, the passed
in default value will be used.getBoolean in interface Configurationkey - the key of the propertydefaultValue - the default valueBooleanConversionException - if the value cannot be converted to a
BooleanPropertyConverter.toBoolean(Object)public byte getByte(String key)
ConfigurationgetByte in interface Configurationkey - The configuration key.public byte getByte(String key, byte defaultValue)
ConfigurationgetByte in interface Configurationkey - The configuration key.defaultValue - The default value.public Byte getByte(String key, Byte defaultValue)
ConfigurationByte associated with the given configuration key.getByte in interface Configurationkey - The configuration key.defaultValue - The default value.public double getDouble(String key)
ConfigurationgetDouble in interface Configurationkey - The configuration key.public double getDouble(String key, double defaultValue)
ConfigurationgetDouble in interface Configurationkey - The configuration key.defaultValue - The default value.public Double getDouble(String key, Double defaultValue)
ConfigurationDouble associated with the given configuration key.getDouble in interface Configurationkey - The configuration key.defaultValue - The default value.public float getFloat(String key)
ConfigurationgetFloat in interface Configurationkey - The configuration key.public float getFloat(String key, float defaultValue)
ConfigurationgetFloat in interface Configurationkey - The configuration key.defaultValue - The default value.public Float getFloat(String key, Float defaultValue)
ConfigurationFloat associated with the given configuration key.
If the key doesn't map to an existing object, the default value
is returned.getFloat in interface Configurationkey - The configuration key.defaultValue - The default value.public int getInt(String key)
ConfigurationgetInt in interface Configurationkey - The configuration key.public int getInt(String key, int defaultValue)
ConfigurationgetInt in interface Configurationkey - The configuration key.defaultValue - The default value.public Integer getInteger(String key, Integer defaultValue)
ConfigurationInteger associated with the given configuration key.
If the key doesn't map to an existing object, the default value
is returned.getInteger in interface Configurationkey - The configuration key.defaultValue - The default value.public long getLong(String key)
ConfigurationgetLong in interface Configurationkey - The configuration key.public long getLong(String key, long defaultValue)
ConfigurationgetLong in interface Configurationkey - The configuration key.defaultValue - The default value.public Long getLong(String key, Long defaultValue)
ConfigurationLong associated with the given configuration key.
If the key doesn't map to an existing object, the default value
is returned.getLong in interface Configurationkey - The configuration key.defaultValue - The default value.public short getShort(String key)
ConfigurationgetShort in interface Configurationkey - The configuration key.public short getShort(String key, short defaultValue)
ConfigurationgetShort in interface Configurationkey - The configuration key.defaultValue - The default value.public Short getShort(String key, Short defaultValue)
ConfigurationShort associated with the given configuration key.
If the key doesn't map to an existing object, the default value
is returned.getShort in interface Configurationkey - The configuration key.defaultValue - The default value.public BigDecimal getBigDecimal(String key)
BigDecimal associated with the given configuration key.getBigDecimal in interface Configurationkey - The configuration key.setThrowExceptionOnMissing(boolean)public BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
ConfigurationBigDecimal associated with the given configuration key.
If the key doesn't map to an existing object, the default value
is returned.getBigDecimal in interface Configurationkey - The configuration key.defaultValue - The default value.public BigInteger getBigInteger(String key)
BigInteger associated with the given configuration key.getBigInteger in interface Configurationkey - The configuration key.setThrowExceptionOnMissing(boolean)public BigInteger getBigInteger(String key, BigInteger defaultValue)
ConfigurationBigInteger associated with the given configuration key.
If the key doesn't map to an existing object, the default value
is returned.getBigInteger in interface Configurationkey - The configuration key.defaultValue - The default value.public String getString(String key)
getString in interface Configurationkey - The configuration key.setThrowExceptionOnMissing(boolean)public String getString(String key, String defaultValue)
ConfigurationgetString in interface Configurationkey - The configuration key.defaultValue - The default value.public String[] getStringArray(String key)
setListDelimiter() method. If this is the case, the string
is split at these positions resulting in a property with multiple
values.getStringArray in interface Configurationkey - The configuration key.ConversionException - is thrown if the key maps to an
object that is not a String/List of Strings.setListDelimiter(char),
setDelimiterParsingDisabled(boolean)public List<Object> getList(String key)
getList in interface Configurationkey - The configuration key.getStringArray(String)public List<Object> getList(String key, List<?> defaultValue)
ConfigurationgetList in interface Configurationkey - The configuration key.defaultValue - The default value.protected Object resolveContainerStore(String key)
key - The property key.protected boolean isScalarValue(Object value)
getList() and getStringArray() if the
property requested is not a string, a list, or an array. If it returns
true, the calling method transforms the value to a string and
returns a list or an array with this single element. This implementation
returns true if the value is of a wrapper type for a primitive
type.value - the value to be checkedpublic void copy(Configuration c)
clone() methods
should be used.c - the configuration to copy (can be null, then this
operation will have no effect)public void append(Configuration c)
clone() methods
should be used.c - the configuration to be appended (can be null, then this
operation will have no effect)public Configuration interpolatedConfiguration()
${var} will be resolved as
far as possible (if a variable cannot be resolved, it remains unchanged).
This operation is useful if the content of a configuration is to be
exported or processed by an external component that does not support
variable interpolation.ConfigurationRuntimeException - if this configuration cannot be
clonedCopyright © 2001–2013 The Apache Software Foundation. All rights reserved.