Class TrackedNodeModel
- java.lang.Object
-
- org.apache.commons.configuration2.tree.TrackedNodeModel
-
- All Implemented Interfaces:
NodeModel<ImmutableNode>
- Direct Known Subclasses:
INIConfiguration.GlobalSectionNodeModel
public class TrackedNodeModel extends java.lang.Object implements NodeModel<ImmutableNode>
A specialized
NodeModel
implementation that uses a tracked node managed by anInMemoryNodeModel
object as root node.Models of this type are useful when working on specific sub trees of a nodes structure. This is the case for instance for a
SubnodeConfiguration
.An instance of this class is constructed with an
InMemoryNodeModelSupport
object providing a reference to the underlyingInMemoryNodeModel
and theNodeSelector
pointing to the tracked node acting as this model's root node. TheNodeModel
operations are implemented by delegating to the wrappedInMemoryNodeModel
object specifying the selector to the tracked node as target root node for the update transaction. Note that the tracked node can become detached at any time. This situation is handled transparently by the implementation ofInMemoryNodeModel
. The reason for using anInMemoryNodeModelSupport
object rather than anInMemoryNodeModel
directly is that this additional layer of indirection can be used for performing special initializations on the model before it is returned to theTrackedNodeModel
object. This is needed by some dynamic configuration implementations, e.g. byCombinedConfiguration
.If the tracked node acting as root node is exclusively used by this model, it should be released when this model is no longer needed. This can be done manually by calling the
close()
method. It is also possible to pass a value of true to theuntrackOnFinalize
argument of the constructor. This causesclose()
to be called automatically if this object gets claimed by the garbage collector.As
InMemoryNodeModel
, this class is thread-safe.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBoolean
closed
A flag whether this model has already been closed.private InMemoryNodeModelSupport
parentModelSupport
Stores the underlying parent model.private boolean
releaseTrackedNodeOnFinalize
A flag whether the tracked not should be released when this object is finalized.private NodeSelector
selector
The selector for the managed tracked node.
-
Constructor Summary
Constructors Constructor Description TrackedNodeModel(InMemoryNodeModelSupport modelSupport, NodeSelector sel, boolean untrackOnFinalize)
Creates a new instance ofTrackedNodeModel
and initializes it with the given underlying model and the selector to the root node.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addNodes(java.lang.String key, java.util.Collection<? extends ImmutableNode> nodes, NodeKeyResolver<ImmutableNode> resolver)
Adds a collection of new nodes to this model.void
addProperty(java.lang.String key, java.lang.Iterable<?> values, NodeKeyResolver<ImmutableNode> resolver)
Adds a new property to this node model consisting of an arbitrary number of values.void
clear(NodeKeyResolver<ImmutableNode> resolver)
Removes all data from this model.void
clearProperty(java.lang.String key, NodeKeyResolver<ImmutableNode> resolver)
Clears the value of a property.java.util.List<QueryResult<ImmutableNode>>
clearTree(java.lang.String key, NodeKeyResolver<ImmutableNode> resolver)
Removes the sub trees defined by the given key from this model.void
close()
Closes this model.protected void
finalize()
This implementation callsclose()
if theuntrackOnFinalize
flag was set when this instance was constructed.ImmutableNode
getInMemoryRepresentation()
Gets a representation of the data stored in this model in form of a nodes hierarchy ofImmutableNode
objects.NodeHandler<ImmutableNode>
getNodeHandler()
Gets aNodeHandler
for dealing with the nodes managed by this model.InMemoryNodeModel
getParentModel()
Gets the parent model.InMemoryNodeModelSupport
getParentModelSupport()
Gets theInMemoryNodeModelSupport
object which is used to gain access to the underlying node model.NodeSelector
getSelector()
Gets theNodeSelector
pointing to the tracked node managed by this model.boolean
isReleaseTrackedNodeOnFinalize()
Returns the flag whether the managed tracked node is to be released when this object gets finalized.void
setProperty(java.lang.String key, java.lang.Object value, NodeKeyResolver<ImmutableNode> resolver)
Changes the value of a property.void
setRootNode(ImmutableNode newRoot)
Sets a new root node for this model.
-
-
-
Field Detail
-
parentModelSupport
private final InMemoryNodeModelSupport parentModelSupport
Stores the underlying parent model.
-
selector
private final NodeSelector selector
The selector for the managed tracked node.
-
releaseTrackedNodeOnFinalize
private final boolean releaseTrackedNodeOnFinalize
A flag whether the tracked not should be released when this object is finalized.
-
closed
private final java.util.concurrent.atomic.AtomicBoolean closed
A flag whether this model has already been closed.
-
-
Constructor Detail
-
TrackedNodeModel
public TrackedNodeModel(InMemoryNodeModelSupport modelSupport, NodeSelector sel, boolean untrackOnFinalize)
Creates a new instance ofTrackedNodeModel
and initializes it with the given underlying model and the selector to the root node. The boolean argument controls whether the associated tracked node should be released when this object gets finalized. This allows the underlying model to free some resources. If used as model within aSubnodeConfiguration
, there is typically no way to discard the model explicitly. Therefore, it makes sense to do this automatically on finalization.- Parameters:
modelSupport
- the underlyingInMemoryNodeModelSupport
(must not be null)sel
- the selector to the root node of this model (must not be null)untrackOnFinalize
- a flag whether the tracked node should be released on finalization- Throws:
java.lang.IllegalArgumentException
- if a required parameter is missing
-
-
Method Detail
-
addNodes
public void addNodes(java.lang.String key, java.util.Collection<? extends ImmutableNode> nodes, NodeKeyResolver<ImmutableNode> resolver)
Description copied from interface:NodeModel
Adds a collection of new nodes to this model. This operation corresponds to theaddNodes()
method of theHierarchicalConfiguration
interface. The new nodes are either added to an existing node (if the passed in key selects exactly one node) or to a newly created node. The passed inNodeKeyResolver
is used to interpret the given key.- Specified by:
addNodes
in interfaceNodeModel<ImmutableNode>
- Parameters:
key
- the keynodes
- the collection of nodes to be added (may be null)resolver
- theNodeKeyResolver
-
addProperty
public void addProperty(java.lang.String key, java.lang.Iterable<?> values, NodeKeyResolver<ImmutableNode> resolver)
Description copied from interface:NodeModel
Adds a new property to this node model consisting of an arbitrary number of values. The key for the add operation is provided. For each value a new node has to be added. The passed in resolver is queried for aNodeAddData
object defining the add operation to be performed.- Specified by:
addProperty
in interfaceNodeModel<ImmutableNode>
- Parameters:
key
- the keyvalues
- the values to be added at the position defined by the keyresolver
- theNodeKeyResolver
-
clear
public void clear(NodeKeyResolver<ImmutableNode> resolver)
Removes all data from this model. This implementation clears the sub tree spanned by the associate tracked node. This has the side effect that this in any case becomes detached.- Specified by:
clear
in interfaceNodeModel<ImmutableNode>
- Parameters:
resolver
- theNodeKeyResolver
.
-
clearProperty
public void clearProperty(java.lang.String key, NodeKeyResolver<ImmutableNode> resolver)
Description copied from interface:NodeModel
Clears the value of a property. This method is similar toNodeModel.clearTree(String, NodeKeyResolver)
: However, the nodes referenced by the passed in key are not removed completely, but only their value is set to null.- Specified by:
clearProperty
in interfaceNodeModel<ImmutableNode>
- Parameters:
key
- the key selecting the properties to be clearedresolver
- theNodeKeyResolver
-
clearTree
public java.util.List<QueryResult<ImmutableNode>> clearTree(java.lang.String key, NodeKeyResolver<ImmutableNode> resolver)
Description copied from interface:NodeModel
Removes the sub trees defined by the given key from this model. All nodes selected by this key are retrieved from the specifiedNodeKeyResolver
and removed from the model.- Specified by:
clearTree
in interfaceNodeModel<ImmutableNode>
- Parameters:
key
- the key selecting the properties to be removedresolver
- theNodeKeyResolver
- Returns:
- an object with information about the data removed
-
close
public void close()
Closes this model. This causes the tracked node this model is based upon to be released (i.e.InMemoryNodeModel.untrackNode(NodeSelector)
is called). This method should be called when this model is no longer needed. This implementation is idempotent; it is safe to callclose()
multiple times - only the first invocation has an effect. After this method has been called this model can no longer be used because there is no guarantee that the node can still be accessed from the parent model.
-
finalize
protected void finalize() throws java.lang.Throwable
This implementation callsclose()
if theuntrackOnFinalize
flag was set when this instance was constructed. While this is not 100 percent reliable, it is better than keeping the tracked node hanging around. Note that it is not a problem ifclose()
already had been invoked manually because this method is idempotent.- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.lang.Throwable
- See Also:
close()
-
getInMemoryRepresentation
public ImmutableNode getInMemoryRepresentation()
Gets a representation of the data stored in this model in form of a nodes hierarchy ofImmutableNode
objects. A concrete model implementation can use an arbitrary means to store its data. When a model's data is to be used together with other functionality of the Configuration library (e.g. when combining multiple configuration sources) it has to be transformed into a common format. This is done by this method.ImmutableNode
is a generic representation of a hierarchical structure. Thus, it should be possible to generate a corresponding structure from arbitrary model data. This implementation returns the tracked node instance acting as root node of this model.- Specified by:
getInMemoryRepresentation
in interfaceNodeModel<ImmutableNode>
- Returns:
- the root node of an in-memory hierarchy representing the data stored in this model
-
getNodeHandler
public NodeHandler<ImmutableNode> getNodeHandler()
Description copied from interface:NodeModel
Gets aNodeHandler
for dealing with the nodes managed by this model.- Specified by:
getNodeHandler
in interfaceNodeModel<ImmutableNode>
- Returns:
- the
NodeHandler
-
getParentModel
public InMemoryNodeModel getParentModel()
Gets the parent model. Operations on this model are delegated to this parent model specifying the selector to the tracked node.- Returns:
- the parent model
-
getParentModelSupport
public InMemoryNodeModelSupport getParentModelSupport()
Gets theInMemoryNodeModelSupport
object which is used to gain access to the underlying node model.- Returns:
- the associated
InMemoryNodeModelSupport
object
-
getSelector
public NodeSelector getSelector()
Gets theNodeSelector
pointing to the tracked node managed by this model.- Returns:
- the tracked node selector
-
isReleaseTrackedNodeOnFinalize
public boolean isReleaseTrackedNodeOnFinalize()
Returns the flag whether the managed tracked node is to be released when this object gets finalized. This method returns the value of the corresponding flag passed to the constructor. If result is true, the underlying model is asked to untrack the managed node when this object is claimed by the GC.- Returns:
- a flag whether the managed tracked node should be released when this object dies
- See Also:
InMemoryNodeModel.untrackNode(NodeSelector)
-
setProperty
public void setProperty(java.lang.String key, java.lang.Object value, NodeKeyResolver<ImmutableNode> resolver)
Description copied from interface:NodeModel
Changes the value of a property. This is a more complex operation as it might involve adding, updating, or deleting nodes and attributes from the model. The object representing the new value is passed to theNodeKeyResolver
which will produce a correspondingNodeUpdateData
object. Based on the content of this object, update operations are performed.- Specified by:
setProperty
in interfaceNodeModel<ImmutableNode>
- Parameters:
key
- the keyvalue
- the new value for this property (to be evaluated by theNodeKeyResolver
)resolver
- theNodeKeyResolver
-
setRootNode
public void setRootNode(ImmutableNode newRoot)
Description copied from interface:NodeModel
Sets a new root node for this model. The whole structure is replaced by the new node and its children.- Specified by:
setRootNode
in interfaceNodeModel<ImmutableNode>
- Parameters:
newRoot
- the new root node to be set (can be null, then an empty root node is set)
-
-