Class AbstractHierarchicalConfiguration<T>
- Type Parameters:
T- the type of the nodes managed by this hierarchical configuration
- All Implemented Interfaces:
Cloneable,Configuration,EventSource,HierarchicalConfiguration<T>,ImmutableConfiguration,ImmutableHierarchicalConfiguration,SynchronizerSupport,NodeKeyResolver<T>,NodeModelSupport<T>
- Direct Known Subclasses:
BaseHierarchicalConfiguration
A specialized configuration class that extends its base class by the ability of keeping more structure in the stored properties.
There are some sources of configuration data that cannot be stored very well in a BaseConfiguration object
because then their structure is lost. This is for instance true for XML documents. This class can deal with such
structured configuration sources by storing the properties in a tree-like organization. The exact storage structure
of the underlying data does not matter for the configuration instance; it uses a NodeModel object for
accessing it.
The hierarchical organization allows for a more sophisticated access to single properties. As an example consider the following XML document:
<database>
<tables>
<table>
<name>users</name>
<fields>
<field>
<name>lid</name>
<type>long</name>
</field>
<field>
<name>usrName</name>
<type>java.lang.String</type>
</field>
...
</fields>
</table>
<table>
<name>documents</name>
<fields>
<field>
<name>docid</name>
<type>long</type>
</field>
...
</fields>
</table>
...
</tables>
</database>
If this document is parsed and stored in a hierarchical configuration object (which can be done by one of the sub classes), there are enhanced possibilities of accessing properties. Per default, the keys for querying information can contain indices that select a specific element if there are multiple hits.
For instance the key tables.table(0).name can be used to find out the name of the first table. In opposite
tables.table.name would return a collection with the names of all available tables. Similarly the key
tables.table(1).fields.field.name returns a collection with the names of all fields of the second table. If
another index is added after the field element, a single field can be accessed:
tables.table(1).fields.field(0).name.
There is a getMaxIndex() method that returns the maximum allowed index that can be added to a given property
key. This method can be used to iterate over all values defined for a certain property.
Since the 1.3 release of Commons Configuration hierarchical configurations support an expression
engine. This expression engine is responsible for evaluating the passed in configuration keys and map them to
the stored properties. The examples above are valid for the default expression engine, which is used when a new
AbstractHierarchicalConfiguration instance is created. With the setExpressionEngine() method a
different expression engine can be set. For instance with
XPathExpressionEngine there is an expression engine available
that supports configuration keys in XPATH syntax.
In addition to the events common for all configuration classes, hierarchical configurations support some more events
that correspond to some specific methods and features. For those events specific event type constants in
ConfigurationEvent exist:
- ADD_NODES
- The
addNodes()method was called; the event object contains the key, to which the nodes were added, and a collection with the new nodes as value. - CLEAR_TREE
- The
clearTree()method was called; the event object stores the key of the removed sub tree. - SUBNODE_CHANGED
- A
SubnodeConfigurationthat was created from this configuration has been changed. The value property of the event object contains the original event object as it was sent by the subnode configuration.
Whether an AbstractHierarchicalConfiguration object is thread-safe or not depends on the underlying
NodeModel and the Synchronizer it is associated
with. Some NodeModel implementations are inherently thread-safe; they do not require a special
Synchronizer. (Per default, a dummy Synchronizer is used which is not thread-safe!) The methods for
querying or updating configuration data invoke this Synchronizer accordingly. When accessing the
configuration's root node directly, the client application is responsible for proper synchronization. This is
achieved by calling the methods lock(), and
unlock() with a proper
LockMode argument. In any case, it is recommended to not
access the root node directly, but to use corresponding methods for querying or updating configuration data instead.
Direct manipulations of a configuration's node structure circumvent many internal mechanisms and thus can cause
undesired effects. For concrete subclasses dealing with specific node structures, this situation may be different.
- Since:
- 2.0
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractHierarchicalConfiguration(NodeModel<T> nodeModel) Creates a new instance ofAbstractHierarchicalConfigurationand sets theNodeModelto be used. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddNodes(String key, Collection<? extends T> nodes) Adds a collection of nodes at the specified position of the configuration tree.protected voidaddNodesInternal(String key, Collection<? extends T> nodes) Actually adds a collection of new nodes to this configuration.protected voidaddPropertyDirect(String key, Object value) Adds a key/value pair to the Configuration.protected voidaddPropertyInternal(String key, Object obj) Adds the property with the specified key.protected voidClears this configuration.protected voidRemoves the property with the given key.final voidRemoves all values of the property with the given name and of keys that start with this name.protected ObjectclearTreeInternal(String key) Actually clears the tree of elements referenced by the given key.clone()Creates a copy of this object.Creates a clone of the node model.protected booleanChecks if the specified key is contained in this configuration.protected booleancontainsValueInternal(Object value) Tests whether this configuration contains one or more matches to this value.protected List<QueryResult<T>> fetchNodeList(String key) Helper method for resolving the specified key.Gets the expression engine used by this configuration.Gets an iterator with all keys defined in this configuration.getKeysInternal(String prefix) Gets an iterator with all keys defined in this configuration that start with the given prefix.getKeysInternal(String prefix, String delimiter) Gets an iterator with all keys defined in this configuration that start with the given prefix.final intgetMaxIndex(String key) Gets the maximum defined index for the given key.protected intActually retrieves the maximum defined index for the given key.getModel()Gets theNodeModelused by this configuration.Gets theNodeModelsupported by this object.protected ObjectFetches the specified property.final StringGets the name of the root element of this configuration.protected StringActually obtains the name of the root element.protected booleanChecks if this configuration is empty.protected booleannodeDefined(T node) Checks if the specified node is defined.Generates a unique key for the specified node.resolveAddKey(T root, String key, NodeHandler<T> handler) Resolves a key of an add operation.List<QueryResult<T>> resolveKey(T root, String key, NodeHandler<T> handler) Performs a query for the specified key on the given root node.resolveNodeKey(T root, String key, NodeHandler<T> handler) Performs a query for the specified key on the given root node returning only node results.resolveUpdateKey(T root, String key, Object newValue, NodeHandler<T> handler) Resolves a key for an update operation.voidsetExpressionEngine(ExpressionEngine expressionEngine) Sets the expression engine to be used by this configuration.protected voidsetPropertyInternal(String key, Object value) Sets the value of the specified property.protected intActually calculates the size of this configuration.toString()Methods inherited from class org.apache.commons.configuration2.AbstractConfiguration
addErrorLogListener, addProperty, append, beginRead, beginWrite, clear, clearProperty, cloneInterpolator, contains, containsKey, containsValue, 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, getKeys, getList, getList, getList, getList, getListDelimiterHandler, getLogger, getLong, getLong, getLong, getProperties, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, getSynchronizer, immutableSubset, initLogger, installInterpolator, interpolate, interpolate, interpolatedConfiguration, isEmpty, isScalarValue, isThrowExceptionOnMissing, lock, setConfigurationDecoder, setConversionHandler, setDefaultLookups, setInterpolator, setListDelimiterHandler, setLogger, setParentInterpolator, setPrefixLookups, setProperty, setSynchronizer, setThrowExceptionOnMissing, size, subset, unlockMethods inherited from class org.apache.commons.configuration2.event.BaseEventSource
addEventListener, clearErrorListeners, clearEventListeners, copyEventListeners, createErrorEvent, createEvent, fireError, fireEvent, getEventListenerRegistrations, getEventListeners, isDetailEvents, removeEventListener, setDetailEventsMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.commons.configuration2.Configuration
addProperty, clear, clearProperty, getInterpolator, installInterpolator, setInterpolator, setProperty, subsetMethods inherited from interface org.apache.commons.configuration2.HierarchicalConfiguration
childConfigurationsAt, childConfigurationsAt, configurationAt, configurationAt, configurationsAt, configurationsAtMethods inherited from interface org.apache.commons.configuration2.ImmutableConfiguration
containsKey, containsValue, get, get, getArray, getArray, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getCollection, getCollection, getDouble, getDouble, getDouble, getDuration, getDuration, getEncodedString, getEncodedString, getEnum, getEnum, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getKeys, getKeys, getKeys, getList, getList, getList, getList, getLong, getLong, getLong, getProperties, getProperty, getShort, getShort, getShort, getString, getString, getStringArray, immutableSubset, isEmpty, sizeMethods inherited from interface org.apache.commons.configuration2.ImmutableHierarchicalConfiguration
immutableChildConfigurationsAt, immutableConfigurationAt, immutableConfigurationAt, immutableConfigurationsAtMethods inherited from interface org.apache.commons.configuration2.sync.SynchronizerSupport
getSynchronizer, lock, setSynchronizer, unlock
-
Constructor Details
-
AbstractHierarchicalConfiguration
Creates a new instance ofAbstractHierarchicalConfigurationand sets theNodeModelto be used.- Parameters:
nodeModel- theNodeModel
-
-
Method Details
-
addNodes
Adds a collection of nodes at the specified position of the configuration tree. This method works similar toaddProperty(), but instead of a single property a whole collection of nodes can be added - and thus complete configuration sub trees. E.g. with this method it is possible to add parts of anotherBaseHierarchicalConfigurationobject to this object. If the passed in key refers to an existing and unique node, the new nodes are added to this node. Otherwise a new node will be created at the specified position in the hierarchy. Implementation node: This method performs some book-keeping and then delegates toaddNodesInternal().- Specified by:
addNodesin interfaceHierarchicalConfiguration<T>- Parameters:
key- the key where the nodes are to be added; can be null, then they are added to the root nodenodes- a collection with theNodeobjects to be added
-
addNodesInternal
Actually adds a collection of new nodes to this configuration. This method is called byaddNodes(). It can be overridden by subclasses that need to adapt this operation.- Parameters:
key- the key where the nodes are to be added; can be null, then they are added to the root nodenodes- a collection with theNodeobjects to be added- Since:
- 2.0
-
addPropertyDirect
Adds a key/value pair to the Configuration. Override this method to provide write access to underlying Configuration store. This method is not called in the normal way (viaaddProperty()for hierarchical configurations because all values to be added for the property have to be passed to the model in a single step. However, to allow derived classes to add an arbitrary value as an object, a special implementation is provided here. The passed in object is not parsed as a list, but passed directly as only value to the model.- Specified by:
addPropertyDirectin classAbstractConfiguration- Parameters:
key- key to use for mappingvalue- object to store
-
addPropertyInternal
Adds the property with the specified key. This task will be delegated to the associatedExpressionEngine, so the passed in key must match the requirements of this implementation.- Overrides:
addPropertyInternalin classAbstractConfiguration- Parameters:
key- the key of the new propertyobj- the value of the new property
-
clearInternal
Clears this configuration. This is a more efficient implementation than the one inherited from the base class. It delegates to the node model.- Overrides:
clearInternalin classAbstractConfiguration
-
clearPropertyDirect
Removes the property with the given key. Properties with names that start with the given key (i.e. properties below the specified key in the hierarchy) won't be affected. This implementation delegates to the node+ model.- Specified by:
clearPropertyDirectin classAbstractConfiguration- Parameters:
key- the key of the property to be removed
-
clearTree
Removes all values of the property with the given name and of keys that start with this name. So if there is a property with the key "foo" and a property with the key "foo.bar", a call ofclearTree("foo")would remove both properties.- Specified by:
clearTreein interfaceHierarchicalConfiguration<T>- Parameters:
key- the key of the property to be removed
-
clearTreeInternal
Actually clears the tree of elements referenced by the given key. This method is called byclearTree(). Subclasses that need to adapt this operation can override this method. This base implementation delegates to the node model.- Parameters:
key- the key of the property to be removed- Returns:
- an object with information about the nodes that have been removed (this is needed for firing a meaningful event of type CLEAR_TREE)
- Since:
- 2.0
-
clone
Creates a copy of this object. This new configuration object will contain copies of all nodes in the same structure. Registered event listeners won't be cloned; so they are not registered at the returned copy.- Overrides:
clonein classBaseEventSource- Returns:
- the copy
- Since:
- 1.2
-
cloneNodeModel
Creates a clone of the node model. This method is called byclone().- Returns:
- the clone of the
NodeModel - Since:
- 2.0
-
containsKeyInternal
Checks if the specified key is contained in this configuration. Note that for this configuration the term "contained" means that the key has an associated value. If there is a node for this key that has no value but children (either defined or undefined), this method will still return false .- Specified by:
containsKeyInternalin classAbstractConfiguration- Parameters:
key- the key to be checked- Returns:
- a flag if this key is contained in this configuration
-
containsValueInternal
Tests whether this configuration contains one or more matches to this value. This operation stops at first match but may be more expensive than the containsKey method.- Specified by:
containsValueInternalin classAbstractConfiguration- Parameters:
value- the value in question- Returns:
trueif and only if some key maps to thevalueargument in this configuration as determined by theequalsmethod;falseotherwise.- Since:
- 2.11.0
-
fetchNodeList
Helper method for resolving the specified key.- Parameters:
key- the key- Returns:
- a list with all results selected by this key
-
getExpressionEngine
Gets the expression engine used by this configuration. This method will never return null; if no specific expression engine was set, the default expression engine will be returned.- Specified by:
getExpressionEnginein interfaceImmutableHierarchicalConfiguration- Returns:
- the current expression engine
- Since:
- 1.3
-
getKeysInternal
Gets an iterator with all keys defined in this configuration. Note that the keys returned by this method will not contain any indices. This means that some structure will be lost.- Specified by:
getKeysInternalin classAbstractConfiguration- Returns:
- an iterator with the defined keys in this configuration
-
getKeysInternal
Gets an iterator with all keys defined in this configuration that start with the given prefix. The returned keys will not contain any indices. This implementation tries to locate a node whose key is the same as the passed in prefix. Then the subtree of this node is traversed, and the keys of all nodes encountered (including attributes) are added to the result set.- Overrides:
getKeysInternalin classAbstractConfiguration- Parameters:
prefix- the prefix of the keys to start with- Returns:
- an iterator with the found keys
-
getKeysInternal
Gets an iterator with all keys defined in this configuration that start with the given prefix. The returned keys will not contain any indices. This implementation tries to locate a node whose key is the same as the passed in prefix. Then the subtree of this node is traversed, and the keys of all nodes encountered (including attributes) are added to the result set.- Overrides:
getKeysInternalin classAbstractConfiguration- Parameters:
prefix- the prefix of the keys to start withdelimiter- TODO- Returns:
- an iterator with the found keys
- Since:
- 2.12.0
-
getMaxIndex
Gets the maximum defined index for the given key. This is useful if there are multiple values for this key. They can then be addressed separately by specifying indices from 0 to the return value of this method. If the passed in key is not contained in this configuration, result is -1.- Specified by:
getMaxIndexin interfaceImmutableHierarchicalConfiguration- Parameters:
key- the key to be checked- Returns:
- the maximum defined index for this key
-
getMaxIndexInternal
Actually retrieves the maximum defined index for the given key. This method is called bygetMaxIndex(). Subclasses that need to adapt this operation have to override this method.- Parameters:
key- the key to be checked- Returns:
- the maximum defined index for this key
- Since:
- 2.0
-
getModel
Gets theNodeModelused by this configuration. This method is intended for internal use only. Access to the model is granted without any synchronization. This is in contrast to the "official"getNodeModel()method which is guarded by the configuration'sSynchronizer.- Returns:
- the node model
-
getNodeModel
Gets theNodeModelsupported by this object. This implementation returns the configuration'sNodeModel. It is guarded by the currentSynchronizer.- Specified by:
getNodeModelin interfaceNodeModelSupport<T>- Returns:
- the
NodeModel
-
getPropertyInternal
Fetches the specified property. This task is delegated to the associated expression engine.- Specified by:
getPropertyInternalin classAbstractConfiguration- Parameters:
key- the key to be looked up- Returns:
- the found value
-
getRootElementName
Gets the name of the root element of this configuration. This information may be of use in some cases, for example for sub configurations created using theimmutableConfigurationsAt()method. The exact meaning of the string returned by this method is specific to a concrete implementation. For instance, an XML configuration might return the name of the document element. This implementation handles synchronization and delegates togetRootElementNameInternal().- Specified by:
getRootElementNamein interfaceImmutableHierarchicalConfiguration- Returns:
- the name of the root element of this configuration
-
getRootElementNameInternal
Actually obtains the name of the root element. This method is called bygetRootElementName(). It just returns the name of the root node. Subclasses that treat the root element name differently can override this method.- Returns:
- the name of this configuration's root element
-
isEmptyInternal
Checks if this configuration is empty. Empty means that there are no keys with any values, though there can be some (empty) nodes.- Specified by:
isEmptyInternalin classAbstractConfiguration- Returns:
- a flag if this configuration is empty
-
nodeDefined
Checks if the specified node is defined.- Parameters:
node- the node to be checked- Returns:
- a flag if this node is defined
-
nodeKey
Generates a unique key for the specified node. This method is used if keys have to be generated for nodes received as query results. An implementation must generate a canonical key which is compatible with the current expression engine. The passed in map can be used by an implementation as cache. It is created initially by the caller and then passed in subsequent calls. An implementation may use this to avoid that keys for nodes already encountered have to be generated again. This implementation uses the expression engine to generate a canonical key for the passed in node. For this purpose, the path to the root node has to be traversed. The cache is used to store and access keys for nodes encountered on the path.- Specified by:
nodeKeyin interfaceNodeKeyResolver<T>- Parameters:
node- the node in questioncache- a map serving as cachehandler- theNodeHandler- Returns:
- a key for the specified node
-
resolveAddKey
Resolves a key of an add operation. Result is aNodeAddDataobject containing all information for actually performing the add operation at the specified key. This implementation delegates to the expression engine.- Specified by:
resolveAddKeyin interfaceNodeKeyResolver<T>- Parameters:
root- the root nodekey- the key to be resolvedhandler- theNodeHandler- Returns:
- a
NodeAddDataobject to be used for the add operation
-
resolveKey
Performs a query for the specified key on the given root node. This is a thin wrapper over thequery()method of anExpressionEngine. This implementation delegates to the expression engine.- Specified by:
resolveKeyin interfaceNodeKeyResolver<T>- Parameters:
root- the root nodekey- the key to be resolvedhandler- theNodeHandler- Returns:
- a list with query results
-
resolveNodeKey
Performs a query for the specified key on the given root node returning only node results. Some operations require results of type node and do not support attributes (for example for tracking nodes). This operation can be used in such cases. It works likeresolveKey(), but filters only for results of type node. This implementation delegates toresolveKey()and then filters out attribute results.- Specified by:
resolveNodeKeyin interfaceNodeKeyResolver<T>- Parameters:
root- the root nodekey- the key to be resolvedhandler- theNodeHandler- Returns:
- a list with the resolved nodes
-
resolveUpdateKey
public NodeUpdateData<T> resolveUpdateKey(T root, String key, Object newValue, NodeHandler<T> handler) Resolves a key for an update operation. Result is aNodeUpdateDataobject containing all information for actually performing the update operation at the specified key using the provided new value object. This implementation executes a query for the given key and constructs aNodeUpdateDataobject based on the results. It determines which nodes need to be changed and whether new ones need to be added or existing ones need to be removed.- Specified by:
resolveUpdateKeyin interfaceNodeKeyResolver<T>- Parameters:
root- the root nodekey- the key to be resolvednewValue- the new value for the key to be updated; this can be a single value or a container for multiple valueshandler- theNodeHandler- Returns:
- a
NodeUpdateDataobject to be used for this update operation
-
setExpressionEngine
Sets the expression engine to be used by this configuration. All property keys this configuration has to deal with will be interpreted by this engine.- Specified by:
setExpressionEnginein interfaceHierarchicalConfiguration<T>- Parameters:
expressionEngine- the new expression engine; can be null, then the default expression engine will be used- Since:
- 1.3
-
setPropertyInternal
Sets the value of the specified property.- Overrides:
setPropertyInternalin classAbstractConfiguration- Parameters:
key- the key of the property to setvalue- the new value of this property
-
sizeInternal
Actually calculates the size of this configuration. This method is called bysize()with a read lock held. The base implementation provided here calculates the size based on the iterator returned bygetKeys(). Sub classes which can determine the size in a more efficient way should override this method. This implementation is slightly more efficient than the default implementation. It does not iterate over the key set, but directly queries its size after it has been constructed. Note that constructing the key set is still an O(n) operation.- Overrides:
sizeInternalin classAbstractConfiguration- Returns:
- the size of this configuration (i.e. the number of keys)
-
toString
-