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
026 * managed by an {@link InMemoryNodeModel} object as root node.
027 * </p>
028 * <p>
029 * Models of this type are useful when working on specific sub trees of a nodes
030 * structure. This is the case for instance for a {@code SubnodeConfiguration}.
031 * </p>
032 * <p>
033 * An instance of this class is constructed with an
034 * {@link InMemoryNodeModelSupport} object providing a reference to the
035 * underlying {@code InMemoryNodeModel} and the {@link NodeSelector} pointing to
036 * the tracked node acting as this model's root node. The {@code NodeModel}
037 * operations are implemented by delegating to the wrapped
038 * {@code InMemoryNodeModel} object specifying the selector to the tracked node
039 * as target root node for the update transaction. Note that the tracked node
040 * can become detached at any time. This situation is handled transparently by
041 * the implementation of {@code InMemoryNodeModel}. The reason for using an
042 * {@code InMemoryNodeModelSupport} object rather than an
043 * {@code InMemoryNodeModel} directly is that this additional layer of
044 * indirection can be used for performing special initializations on the model
045 * before it is returned to the {@code TrackedNodeModel} object. This is needed
046 * by some dynamic configuration implementations, e.g. by
047 * {@code CombinedConfiguration}.
048 * </p>
049 * <p>
050 * If the tracked node acting as root node is exclusively used by this model, it
051 * should be released when this model is no longer needed. This can be done
052 * manually by calling the {@link #close()} method. It is also possible to pass
053 * a value of <strong>true</strong> to the {@code untrackOnFinalize} argument of
054 * the constructor. This causes {@code close()} to be called automatically if
055 * this object gets claimed by the garbage collector.
056 * </p>
057 * <p>
058 * As {@code InMemoryNodeModel}, this class is thread-safe.
059 * </p>
060 *
061 * @version $Id: TrackedNodeModel.java 1842194 2018-09-27 22:24:23Z ggregory $
062 * @since 2.0
063 */
064public class TrackedNodeModel implements NodeModel<ImmutableNode>
065{
066    /** Stores the underlying parent model. */
067    private final InMemoryNodeModelSupport parentModelSupport;
068
069    /** The selector for the managed tracked node. */
070    private final NodeSelector selector;
071
072    /**
073     * A flag whether the tracked not should be released when this object is
074     * finalized.
075     */
076    private final boolean releaseTrackedNodeOnFinalize;
077
078    /** A flag whether this model has already been closed. */
079    private final AtomicBoolean closed;
080
081    /**
082     * Creates a new instance of {@code TrackedNodeModel} and initializes it
083     * with the given underlying model and the selector to the root node. The
084     * boolean argument controls whether the associated tracked node should be
085     * released when this object gets finalized. This allows the underlying
086     * model to free some resources. If used as model within a
087     * {@code SubnodeConfiguration}, there is typically no way to discard the
088     * model explicitly. Therefore, it makes sense to do this automatically on
089     * finalization.
090     *
091     * @param modelSupport the underlying {@code InMemoryNodeModelSupport} (must not be
092     *        <b>null</b>)
093     * @param sel the selector to the root node of this model (must not be
094     *        <b>null</b>)
095     * @param untrackOnFinalize a flag whether the tracked node should be
096     *        released on finalization
097     * @throws IllegalArgumentException if a required parameter is missing
098     */
099    public TrackedNodeModel(final InMemoryNodeModelSupport modelSupport, final NodeSelector sel,
100            final boolean untrackOnFinalize)
101    {
102        if (modelSupport == null)
103        {
104            throw new IllegalArgumentException(
105                    "Underlying model support must not be null!");
106        }
107        if (sel == null)
108        {
109            throw new IllegalArgumentException("Selector must not be null!");
110        }
111
112        parentModelSupport = modelSupport;
113        selector = sel;
114        releaseTrackedNodeOnFinalize = untrackOnFinalize;
115        closed = new AtomicBoolean();
116    }
117
118    /**
119     * Returns the {@code InMemoryNodeModelSupport} object which is used to gain
120     * access to the underlying node model.
121     *
122     * @return the associated {@code InMemoryNodeModelSupport} object
123     */
124    public InMemoryNodeModelSupport getParentModelSupport()
125    {
126        return parentModelSupport;
127    }
128
129    /**
130     * Returns the parent model. Operations on this model are delegated to this
131     * parent model specifying the selector to the tracked node.
132     *
133     * @return the parent model
134     */
135    public InMemoryNodeModel getParentModel()
136    {
137        return getParentModelSupport().getNodeModel();
138    }
139
140    /**
141     * Returns the {@code NodeSelector} pointing to the tracked node managed by
142     * this model.
143     *
144     * @return the tracked node selector
145     */
146    public NodeSelector getSelector()
147    {
148        return selector;
149    }
150
151    /**
152     * Returns the flag whether the managed tracked node is to be released when
153     * this object gets finalized. This method returns the value of the
154     * corresponding flag passed to the constructor. If result is true, the
155     * underlying model is asked to untrack the managed node when this object is
156     * claimed by the GC.
157     *
158     * @return a flag whether the managed tracked node should be released when
159     *         this object dies
160     * @see InMemoryNodeModel#untrackNode(NodeSelector)
161     */
162    public boolean isReleaseTrackedNodeOnFinalize()
163    {
164        return releaseTrackedNodeOnFinalize;
165    }
166
167    @Override
168    public void setRootNode(final ImmutableNode newRoot)
169    {
170        getParentModel().replaceTrackedNode(getSelector(), newRoot);
171    }
172
173    @Override
174    public NodeHandler<ImmutableNode> getNodeHandler()
175    {
176        return getParentModel().getTrackedNodeHandler(getSelector());
177    }
178
179    @Override
180    public void addProperty(final String key, final Iterable<?> values,
181            final NodeKeyResolver<ImmutableNode> resolver)
182    {
183        getParentModel().addProperty(key, getSelector(), values, resolver);
184    }
185
186    @Override
187    public void addNodes(final String key, final Collection<? extends ImmutableNode> nodes,
188            final NodeKeyResolver<ImmutableNode> resolver)
189    {
190        getParentModel().addNodes(key, getSelector(), nodes, resolver);
191    }
192
193    @Override
194    public void setProperty(final String key, final Object value,
195            final NodeKeyResolver<ImmutableNode> resolver)
196    {
197        getParentModel().setProperty(key, getSelector(), value, resolver);
198    }
199
200    @Override
201    public List<QueryResult<ImmutableNode>> clearTree(final String key,
202            final NodeKeyResolver<ImmutableNode> resolver)
203    {
204        return getParentModel().clearTree(key, getSelector(), resolver);
205    }
206
207    @Override
208    public void clearProperty(final String key,
209            final NodeKeyResolver<ImmutableNode> resolver)
210    {
211        getParentModel().clearProperty(key, getSelector(), resolver);
212    }
213
214    /**
215     * {@inheritDoc} This implementation clears the sub tree spanned by the
216     * associate tracked node. This has the side effect that this in any case
217     * becomes detached.
218     *
219     * @param resolver
220     */
221    @Override
222    public void clear(final NodeKeyResolver<ImmutableNode> resolver)
223    {
224        getParentModel().clearTree(null, getSelector(), resolver);
225    }
226
227    /**
228     * {@inheritDoc} This implementation returns the tracked node instance
229     * acting as root node of this model.
230     */
231    @Override
232    public ImmutableNode getInMemoryRepresentation()
233    {
234        return getNodeHandler().getRootNode();
235    }
236
237    /**
238     * Closes this model. This causes the tracked node this model is based upon
239     * to be released (i.e. {@link InMemoryNodeModel#untrackNode(NodeSelector)}
240     * is called). This method should be called when this model is no longer
241     * needed. This implementation is idempotent; it is safe to call
242     * {@code close()} multiple times - only the first invocation has an effect.
243     * After this method has been called this model can no longer be used
244     * because there is no guarantee that the node can still be accessed from
245     * the parent model.
246     */
247    public void close()
248    {
249        if (closed.compareAndSet(false, true))
250        {
251            getParentModel().untrackNode(getSelector());
252        }
253    }
254
255    /**
256     * {@inheritDoc} This implementation calls {@code close()} if the
257     * {@code untrackOnFinalize} flag was set when this instance was
258     * constructed. While this is not 100 percent reliable, it is better than
259     * keeping the tracked node hanging around. Note that it is not a problem if
260     * {@code close()} already had been invoked manually because this method is
261     * idempotent.
262     *
263     * @see #close()
264     */
265    @Override
266    protected void finalize() throws Throwable
267    {
268        if (isReleaseTrackedNodeOnFinalize())
269        {
270            close();
271        }
272        super.finalize();
273    }
274}