public class CompositeConfiguration extends AbstractConfiguration implements Cloneable
CompositeConfiguration
allows you to add multiple Configuration
objects to an aggregated
configuration. If you add Configuration1, and then Configuration2, any properties shared will mean that the value
defined by Configuration1 will be returned. If Configuration1 doesn't have the property, then Configuration2 will be
checked. You can add multiple different types or the same type of properties file.
When querying properties the order in which child configurations have been added is relevant. To deal with property updates, a so-called in-memory configuration is used. Per default, such a configuration is created automatically. All property writes target this special configuration. There are constructors which allow you to provide a specific in-memory configuration. If used that way, the in-memory configuration is always the last one in the list of child configurations. This means that for query operations all other configurations take precedence.
Alternatively it is possible to mark a child configuration as in-memory configuration when it is added. In this case the treatment of the in-memory configuration is slightly different: it remains in the list of child configurations at the position it was added, i.e. its priority for property queries can be defined by adding the child configurations in the correct order.
This configuration class uses a Synchronizer
to control concurrent access. While all methods for reading and
writing configuration properties make use of this Synchronizer
per default, the methods for managing the list
of child configurations and the in-memory configuration
(addConfiguration(), getNumberOfConfigurations(), removeConfiguration(),
getConfiguration(), getInMemoryConfiguration()
) are guarded, too. Because most methods for accessing configuration
data delegate to the list of child configurations, the thread-safety of a CompositeConfiguration
object also
depends on the Synchronizer
objects used by these children.
Constructor and Description |
---|
CompositeConfiguration()
Creates an empty CompositeConfiguration object which can then be added some other Configuration files
|
CompositeConfiguration(Collection<? extends Configuration> configurations)
Create a CompositeConfiguration with an empty in memory configuration and adds the collection of configurations
specified.
|
CompositeConfiguration(Configuration inMemoryConfiguration)
Creates a CompositeConfiguration object with a specified in-memory configuration.
|
CompositeConfiguration(Configuration inMemoryConfiguration,
Collection<? extends Configuration> configurations)
Creates a CompositeConfiguration with a specified in-memory configuration, and then adds the given
collection of configurations.
|
Modifier and Type | Method and Description |
---|---|
void |
addConfiguration(Configuration config)
Add a configuration.
|
void |
addConfiguration(Configuration config,
boolean asInMemory)
Adds a child configuration and optionally makes it the in-memory configuration.
|
void |
addConfigurationFirst(Configuration config)
Add a configuration to the start of the list of child configurations.
|
void |
addConfigurationFirst(Configuration config,
boolean asInMemory)
Adds a child configuration to the start of the collection and optionally makes it the in-memory
configuration.
|
protected void |
addPropertyDirect(String key,
Object token)
Add this property to the in-memory Configuration.
|
protected void |
clearInternal()
Removes all child configurations and reinitializes the in-memory configuration.
|
protected void |
clearPropertyDirect(String key)
Removes the specified property from this configuration.
|
Object |
clone()
Returns a copy of this object.
|
protected boolean |
containsKeyInternal(String key)
Actually checks whether the specified key is contained in this configuration.
|
Configuration |
getConfiguration(int index)
Return the configuration at the specified index.
|
Configuration |
getInMemoryConfiguration()
Returns the "in memory configuration".
|
protected Iterator<String> |
getKeysInternal()
Actually creates an iterator for iterating over the keys in this configuration.
|
protected Iterator<String> |
getKeysInternal(String key)
Returns an
Iterator with all property keys starting with the specified prefix. |
List<Object> |
getList(String key,
List<?> defaultValue)
Gets a List of strings associated with the given configuration key.
|
int |
getNumberOfConfigurations()
Return the number of configurations.
|
protected Object |
getPropertyInternal(String key)
Read property from underlying composite
|
Configuration |
getSource(String key)
Returns the configuration source, in which the specified key is defined.
|
String[] |
getStringArray(String key)
Get an array of strings associated with the given configuration key.
|
protected boolean |
isEmptyInternal()
Actually checks whether this configuration contains data.
|
void |
removeConfiguration(Configuration config)
Remove a configuration.
|
void |
setListDelimiterHandler(ListDelimiterHandler listDelimiterHandler)
Sets the
ListDelimiterHandler to be used by this instance. |
addErrorLogListener, addProperty, addPropertyInternal, append, beginRead, beginWrite, clear, clearProperty, cloneInterpolator, containsKey, copy, endRead, endWrite, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getConfigurationDecoder, getConversionHandler, getDouble, getDouble, getDouble, getDuration, getDuration, getEncodedString, getEncodedString, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getKeys, getList, getList, getList, getListDelimiterHandler, getLogger, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getSynchronizer, immutableSubset, initLogger, installInterpolator, interpolate, interpolate, interpolatedConfiguration, isEmpty, isScalarValue, isThrowExceptionOnMissing, lock, setConfigurationDecoder, setConversionHandler, setDefaultLookups, setInterpolator, setLogger, setParentInterpolator, setPrefixLookups, setProperty, setPropertyInternal, setSynchronizer, setThrowExceptionOnMissing, size, sizeInternal, subset, unlock
addEventListener, clearErrorListeners, clearEventListeners, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEvents
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getEnum, getEnum
public CompositeConfiguration()
public CompositeConfiguration(Configuration inMemoryConfiguration)
CompositeConfiguration
. Note: Use this constructor if you want to set a special
type of in-memory configuration. If you have a configuration which should act as both a child configuration and as
in-memory configuration, use addConfiguration(Configuration, boolean)
with a value of true instead.inMemoryConfiguration
- the in memory configuration to usepublic CompositeConfiguration(Collection<? extends Configuration> configurations)
configurations
- the collection of configurations to addpublic CompositeConfiguration(Configuration inMemoryConfiguration, Collection<? extends Configuration> configurations)
inMemoryConfiguration
- the in memory configuration to useconfigurations
- the collection of configurations to addCompositeConfiguration(Configuration)
public void addConfiguration(Configuration config)
config
- the configuration to addpublic void addConfiguration(Configuration config, boolean asInMemory)
config
- the configuration to be addedasInMemory
- true if this configuration becomes the new in-memory configuration, false
otherwisepublic void addConfigurationFirst(Configuration config)
config
- the configuration to addpublic void addConfigurationFirst(Configuration config, boolean asInMemory)
config
- the configuration to be addedasInMemory
- true if this configuration becomes the new in-memory configuration, false
otherwisepublic void removeConfiguration(Configuration config)
config
- The configuration to removepublic int getNumberOfConfigurations()
protected void clearInternal()
clearInternal
in class AbstractConfiguration
protected void addPropertyDirect(String key, Object token)
addPropertyDirect
in class AbstractConfiguration
key
- The Key to add the property to.token
- The Value to add.protected Object getPropertyInternal(String key)
getPropertyInternal
in class AbstractConfiguration
key
- key to use for mappingprotected Iterator<String> getKeysInternal()
AbstractConfiguration
getKeys()
, it has to be defined by concrete subclasses.getKeysInternal
in class AbstractConfiguration
Iterator
with all property keys in this configurationprotected Iterator<String> getKeysInternal(String key)
AbstractConfiguration
Iterator
with all property keys starting with the specified prefix. This method is called by
AbstractConfiguration.getKeys(String)
. It is fully implemented by delegating to getKeysInternal()
and returning a special
iterator which filters for the passed in prefix. Subclasses can override it if they can provide a more efficient way
to iterate over specific keys only.getKeysInternal
in class AbstractConfiguration
key
- the prefix for the keys to be taken into accountIterator
returning the filtered keysprotected boolean isEmptyInternal()
AbstractConfiguration
isEmpty()
. It has to be
defined by concrete subclasses.isEmptyInternal
in class AbstractConfiguration
protected void clearPropertyDirect(String key)
AbstractConfiguration
clearProperty()
after it has
done some preparations. It must be overridden in sub classes.clearPropertyDirect
in class AbstractConfiguration
key
- the key to be removedprotected boolean containsKeyInternal(String key)
AbstractConfiguration
containsKey()
. It has to be defined by concrete subclasses.containsKeyInternal
in class AbstractConfiguration
key
- the key in questionpublic List<Object> getList(String key, List<?> defaultValue)
ImmutableConfiguration
getList
in interface ImmutableConfiguration
getList
in class AbstractConfiguration
key
- The configuration key.defaultValue
- The default value.ImmutableConfiguration.getList(Class, String, List)
public String[] getStringArray(String key)
AbstractConfiguration
ListDelimiterHandler
is consulted to find out whether the string can be split into multiple values.getStringArray
in interface ImmutableConfiguration
getStringArray
in class AbstractConfiguration
key
- The configuration key.AbstractConfiguration.setListDelimiterHandler(ListDelimiterHandler)
public Configuration getConfiguration(int index)
index
- The index of the configuration to retrievepublic Configuration getInMemoryConfiguration()
public Object clone()
clone
in class BaseEventSource
public void setListDelimiterHandler(ListDelimiterHandler listDelimiterHandler)
Sets the ListDelimiterHandler
to be used by this instance. This object is invoked every time when dealing
with string properties that may contain a list delimiter and thus have to be split to multiple values. Per default, a
ListDelimiterHandler
implementation is set which does not support list splitting. This can be changed for
instance by setting a DefaultListDelimiterHandler
object.
Warning: Be careful when changing the list delimiter handler when the configuration has already been loaded/populated. List handling is typically applied already when properties are added to the configuration. If later another handler is set which processes lists differently, results may be unexpected; some operations may even cause exceptions.
This implementation ensures that the in memory configuration is correctly initialized.setListDelimiterHandler
in class AbstractConfiguration
listDelimiterHandler
- the ListDelimiterHandler
to be used (must not be null)public Configuration getSource(String key)
IllegalArgumentException
is thrown. In this case the source configuration cannot be determined.key
- the key to be checkedIllegalArgumentException
- if the source configuration cannot be determinedCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.