001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.configuration2.tree;
018
019import java.util.Collection;
020import java.util.List;
021import java.util.concurrent.atomic.AtomicBoolean;
022
023/**
024 * <p>
025 * A specialized {@code NodeModel} implementation that uses a tracked node managed by an {@link InMemoryNodeModel}
026 * object as root node.
027 * </p>
028 * <p>
029 * Models of this type are useful when working on specific sub trees of a nodes structure. This is the case for instance
030 * for a {@code SubnodeConfiguration}.
031 * </p>
032 * <p>
033 * An instance of this class is constructed with an {@link InMemoryNodeModelSupport} object providing a reference to the
034 * underlying {@code InMemoryNodeModel} and the {@link NodeSelector} pointing to the tracked node acting as this model's
035 * root node. The {@code NodeModel} operations are implemented by delegating to the wrapped {@code InMemoryNodeModel}
036 * object specifying the selector to the tracked node as target root node for the update transaction. Note that the
037 * tracked node can become detached at any time. This situation is handled transparently by the implementation of
038 * {@code InMemoryNodeModel}. The reason for using an {@code InMemoryNodeModelSupport} object rather than an
039 * {@code InMemoryNodeModel} directly is that this additional layer of indirection can be used for performing special
040 * initializations on the model before it is returned to the {@code TrackedNodeModel} object. This is needed by some
041 * dynamic configuration implementations, e.g. by {@code CombinedConfiguration}.
042 * </p>
043 * <p>
044 * If the tracked node acting as root node is exclusively used by this model, it should be released when this model is
045 * no longer needed. This can be done manually by calling the {@link #close()} method. It is also possible to pass a
046 * value of <strong>true</strong> to the {@code untrackOnFinalize} argument of the constructor. This causes
047 * {@code close()} to be called automatically if this object gets claimed by the garbage collector.
048 * </p>
049 * <p>
050 * As {@code InMemoryNodeModel}, this class is thread-safe.
051 * </p>
052 *
053 * @since 2.0
054 */
055public class TrackedNodeModel implements NodeModel<ImmutableNode> {
056    /** Stores the underlying parent model. */
057    private final InMemoryNodeModelSupport parentModelSupport;
058
059    /** The selector for the managed tracked node. */
060    private final NodeSelector selector;
061
062    /**
063     * A flag whether the tracked not should be released when this object is finalized.
064     */
065    private final boolean releaseTrackedNodeOnFinalize;
066
067    /** A flag whether this model has already been closed. */
068    private final AtomicBoolean closed;
069
070    /**
071     * Creates a new instance of {@code TrackedNodeModel} and initializes it with the given underlying model and the
072     * selector to the root node. The boolean argument controls whether the associated tracked node should be released when
073     * this object gets finalized. This allows the underlying model to free some resources. If used as model within a
074     * {@code SubnodeConfiguration}, there is typically no way to discard the model explicitly. Therefore, it makes sense to
075     * do this automatically on finalization.
076     *
077     * @param modelSupport the underlying {@code InMemoryNodeModelSupport} (must not be <b>null</b>)
078     * @param sel the selector to the root node of this model (must not be <b>null</b>)
079     * @param untrackOnFinalize a flag whether the tracked node should be released on finalization
080     * @throws IllegalArgumentException if a required parameter is missing
081     */
082    public TrackedNodeModel(final InMemoryNodeModelSupport modelSupport, final NodeSelector sel, final boolean untrackOnFinalize) {
083        if (modelSupport == null) {
084            throw new IllegalArgumentException("Underlying model support must not be null!");
085        }
086        if (sel == null) {
087            throw new IllegalArgumentException("Selector must not be null!");
088        }
089
090        parentModelSupport = modelSupport;
091        selector = sel;
092        releaseTrackedNodeOnFinalize = untrackOnFinalize;
093        closed = new AtomicBoolean();
094    }
095
096    @Override
097    public void addNodes(final String key, final Collection<? extends ImmutableNode> nodes, final NodeKeyResolver<ImmutableNode> resolver) {
098        getParentModel().addNodes(key, getSelector(), nodes, resolver);
099    }
100
101    @Override
102    public void addProperty(final String key, final Iterable<?> values, final NodeKeyResolver<ImmutableNode> resolver) {
103        getParentModel().addProperty(key, getSelector(), values, resolver);
104    }
105
106    /**
107     * {@inheritDoc} This implementation clears the sub tree spanned by the associate tracked node. This has the side effect
108     * that this in any case becomes detached.
109     *
110     * @param resolver the {@code NodeKeyResolver}.
111     */
112    @Override
113    public void clear(final NodeKeyResolver<ImmutableNode> resolver) {
114        getParentModel().clearTree(null, getSelector(), resolver);
115    }
116
117    @Override
118    public void clearProperty(final String key, final NodeKeyResolver<ImmutableNode> resolver) {
119        getParentModel().clearProperty(key, getSelector(), resolver);
120    }
121
122    @Override
123    public List<QueryResult<ImmutableNode>> clearTree(final String key, final NodeKeyResolver<ImmutableNode> resolver) {
124        return getParentModel().clearTree(key, getSelector(), resolver);
125    }
126
127    /**
128     * Closes this model. This causes the tracked node this model is based upon to be released (i.e.
129     * {@link InMemoryNodeModel#untrackNode(NodeSelector)} is called). This method should be called when this model is no
130     * longer needed. This implementation is idempotent; it is safe to call {@code close()} multiple times - only the first
131     * invocation has an effect. After this method has been called this model can no longer be used because there is no
132     * guarantee that the node can still be accessed from the parent model.
133     */
134    public void close() {
135        if (closed.compareAndSet(false, true)) {
136            getParentModel().untrackNode(getSelector());
137        }
138    }
139
140    /**
141     * {@inheritDoc} This implementation calls {@code close()} if the {@code untrackOnFinalize} flag was set when this
142     * instance was constructed. While this is not 100 percent reliable, it is better than keeping the tracked node hanging
143     * around. Note that it is not a problem if {@code close()} already had been invoked manually because this method is
144     * idempotent.
145     *
146     * @see #close()
147     */
148    @Override
149    protected void finalize() throws Throwable {
150        if (isReleaseTrackedNodeOnFinalize()) {
151            close();
152        }
153        super.finalize();
154    }
155
156    /**
157     * {@inheritDoc} This implementation returns the tracked node instance acting as root node of this model.
158     */
159    @Override
160    public ImmutableNode getInMemoryRepresentation() {
161        return getNodeHandler().getRootNode();
162    }
163
164    @Override
165    public NodeHandler<ImmutableNode> getNodeHandler() {
166        return getParentModel().getTrackedNodeHandler(getSelector());
167    }
168
169    /**
170     * Gets the parent model. Operations on this model are delegated to this parent model specifying the selector to the
171     * tracked node.
172     *
173     * @return the parent model
174     */
175    public InMemoryNodeModel getParentModel() {
176        return getParentModelSupport().getNodeModel();
177    }
178
179    /**
180     * Gets the {@code InMemoryNodeModelSupport} object which is used to gain access to the underlying node model.
181     *
182     * @return the associated {@code InMemoryNodeModelSupport} object
183     */
184    public InMemoryNodeModelSupport getParentModelSupport() {
185        return parentModelSupport;
186    }
187
188    /**
189     * Gets the {@code NodeSelector} pointing to the tracked node managed by this model.
190     *
191     * @return the tracked node selector
192     */
193    public NodeSelector getSelector() {
194        return selector;
195    }
196
197    /**
198     * Returns the flag whether the managed tracked node is to be released when this object gets finalized. This method
199     * returns the value of the corresponding flag passed to the constructor. If result is true, the underlying model is
200     * asked to untrack the managed node when this object is claimed by the GC.
201     *
202     * @return a flag whether the managed tracked node should be released when this object dies
203     * @see InMemoryNodeModel#untrackNode(NodeSelector)
204     */
205    public boolean isReleaseTrackedNodeOnFinalize() {
206        return releaseTrackedNodeOnFinalize;
207    }
208
209    @Override
210    public void setProperty(final String key, final Object value, final NodeKeyResolver<ImmutableNode> resolver) {
211        getParentModel().setProperty(key, getSelector(), value, resolver);
212    }
213
214    @Override
215    public void setRootNode(final ImmutableNode newRoot) {
216        getParentModel().replaceTrackedNode(getSelector(), newRoot);
217    }
218}