1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.configuration2.tree; 18 19 import java.util.Collection; 20 import java.util.List; 21 import java.util.concurrent.atomic.AtomicBoolean; 22 23 /** 24 * <p> 25 * A specialized {@code NodeModel} implementation that uses a tracked node managed by an {@link InMemoryNodeModel} 26 * object as root node. 27 * </p> 28 * <p> 29 * Models of this type are useful when working on specific sub trees of a nodes structure. This is the case for instance 30 * for a {@code SubnodeConfiguration}. 31 * </p> 32 * <p> 33 * An instance of this class is constructed with an {@link InMemoryNodeModelSupport} object providing a reference to the 34 * underlying {@code InMemoryNodeModel} and the {@link NodeSelector} pointing to the tracked node acting as this model's 35 * root node. The {@code NodeModel} operations are implemented by delegating to the wrapped {@code InMemoryNodeModel} 36 * object specifying the selector to the tracked node as target root node for the update transaction. Note that the 37 * tracked node can become detached at any time. This situation is handled transparently by the implementation of 38 * {@code InMemoryNodeModel}. The reason for using an {@code InMemoryNodeModelSupport} object rather than an 39 * {@code InMemoryNodeModel} directly is that this additional layer of indirection can be used for performing special 40 * initializations on the model before it is returned to the {@code TrackedNodeModel} object. This is needed by some 41 * dynamic configuration implementations, e.g. by {@code CombinedConfiguration}. 42 * </p> 43 * <p> 44 * If the tracked node acting as root node is exclusively used by this model, it should be released when this model is 45 * no longer needed. This can be done manually by calling the {@link #close()} method. It is also possible to pass a 46 * value of <strong>true</strong> to the {@code untrackOnFinalize} argument of the constructor. This causes 47 * {@code close()} to be called automatically if this object gets claimed by the garbage collector. 48 * </p> 49 * <p> 50 * As {@code InMemoryNodeModel}, this class is thread-safe. 51 * </p> 52 * 53 * @since 2.0 54 */ 55 public class TrackedNodeModel implements NodeModel<ImmutableNode> { 56 /** Stores the underlying parent model. */ 57 private final InMemoryNodeModelSupport parentModelSupport; 58 59 /** The selector for the managed tracked node. */ 60 private final NodeSelector selector; 61 62 /** 63 * A flag whether the tracked not should be released when this object is finalized. 64 */ 65 private final boolean releaseTrackedNodeOnFinalize; 66 67 /** A flag whether this model has already been closed. */ 68 private final AtomicBoolean closed; 69 70 /** 71 * Creates a new instance of {@code TrackedNodeModel} and initializes it with the given underlying model and the 72 * selector to the root node. The boolean argument controls whether the associated tracked node should be released when 73 * this object gets finalized. This allows the underlying model to free some resources. If used as model within a 74 * {@code SubnodeConfiguration}, there is typically no way to discard the model explicitly. Therefore, it makes sense to 75 * do this automatically on finalization. 76 * 77 * @param modelSupport the underlying {@code InMemoryNodeModelSupport} (must not be <b>null</b>) 78 * @param sel the selector to the root node of this model (must not be <b>null</b>) 79 * @param untrackOnFinalize a flag whether the tracked node should be released on finalization 80 * @throws IllegalArgumentException if a required parameter is missing 81 */ 82 public TrackedNodeModel(final InMemoryNodeModelSupport modelSupport, final NodeSelector sel, final boolean untrackOnFinalize) { 83 if (modelSupport == null) { 84 throw new IllegalArgumentException("Underlying model support must not be null!"); 85 } 86 if (sel == null) { 87 throw new IllegalArgumentException("Selector must not be null!"); 88 } 89 90 parentModelSupport = modelSupport; 91 selector = sel; 92 releaseTrackedNodeOnFinalize = untrackOnFinalize; 93 closed = new AtomicBoolean(); 94 } 95 96 /** 97 * Gets the {@code InMemoryNodeModelSupport} object which is used to gain access to the underlying node model. 98 * 99 * @return the associated {@code InMemoryNodeModelSupport} object 100 */ 101 public InMemoryNodeModelSupport getParentModelSupport() { 102 return parentModelSupport; 103 } 104 105 /** 106 * Gets the parent model. Operations on this model are delegated to this parent model specifying the selector to the 107 * tracked node. 108 * 109 * @return the parent model 110 */ 111 public InMemoryNodeModel getParentModel() { 112 return getParentModelSupport().getNodeModel(); 113 } 114 115 /** 116 * Gets the {@code NodeSelector} pointing to the tracked node managed by this model. 117 * 118 * @return the tracked node selector 119 */ 120 public NodeSelector getSelector() { 121 return selector; 122 } 123 124 /** 125 * Returns the flag whether the managed tracked node is to be released when this object gets finalized. This method 126 * returns the value of the corresponding flag passed to the constructor. If result is true, the underlying model is 127 * asked to untrack the managed node when this object is claimed by the GC. 128 * 129 * @return a flag whether the managed tracked node should be released when this object dies 130 * @see InMemoryNodeModel#untrackNode(NodeSelector) 131 */ 132 public boolean isReleaseTrackedNodeOnFinalize() { 133 return releaseTrackedNodeOnFinalize; 134 } 135 136 @Override 137 public void setRootNode(final ImmutableNode newRoot) { 138 getParentModel().replaceTrackedNode(getSelector(), newRoot); 139 } 140 141 @Override 142 public NodeHandler<ImmutableNode> getNodeHandler() { 143 return getParentModel().getTrackedNodeHandler(getSelector()); 144 } 145 146 @Override 147 public void addProperty(final String key, final Iterable<?> values, final NodeKeyResolver<ImmutableNode> resolver) { 148 getParentModel().addProperty(key, getSelector(), values, resolver); 149 } 150 151 @Override 152 public void addNodes(final String key, final Collection<? extends ImmutableNode> nodes, final NodeKeyResolver<ImmutableNode> resolver) { 153 getParentModel().addNodes(key, getSelector(), nodes, resolver); 154 } 155 156 @Override 157 public void setProperty(final String key, final Object value, final NodeKeyResolver<ImmutableNode> resolver) { 158 getParentModel().setProperty(key, getSelector(), value, resolver); 159 } 160 161 @Override 162 public List<QueryResult<ImmutableNode>> clearTree(final String key, final NodeKeyResolver<ImmutableNode> resolver) { 163 return getParentModel().clearTree(key, getSelector(), resolver); 164 } 165 166 @Override 167 public void clearProperty(final String key, final NodeKeyResolver<ImmutableNode> resolver) { 168 getParentModel().clearProperty(key, getSelector(), resolver); 169 } 170 171 /** 172 * {@inheritDoc} This implementation clears the sub tree spanned by the associate tracked node. This has the side effect 173 * that this in any case becomes detached. 174 * 175 * @param resolver the {@code NodeKeyResolver}. 176 */ 177 @Override 178 public void clear(final NodeKeyResolver<ImmutableNode> resolver) { 179 getParentModel().clearTree(null, getSelector(), resolver); 180 } 181 182 /** 183 * {@inheritDoc} This implementation returns the tracked node instance acting as root node of this model. 184 */ 185 @Override 186 public ImmutableNode getInMemoryRepresentation() { 187 return getNodeHandler().getRootNode(); 188 } 189 190 /** 191 * Closes this model. This causes the tracked node this model is based upon to be released (i.e. 192 * {@link InMemoryNodeModel#untrackNode(NodeSelector)} is called). This method should be called when this model is no 193 * longer needed. This implementation is idempotent; it is safe to call {@code close()} multiple times - only the first 194 * invocation has an effect. After this method has been called this model can no longer be used because there is no 195 * guarantee that the node can still be accessed from the parent model. 196 */ 197 public void close() { 198 if (closed.compareAndSet(false, true)) { 199 getParentModel().untrackNode(getSelector()); 200 } 201 } 202 203 /** 204 * {@inheritDoc} This implementation calls {@code close()} if the {@code untrackOnFinalize} flag was set when this 205 * instance was constructed. While this is not 100 percent reliable, it is better than keeping the tracked node hanging 206 * around. Note that it is not a problem if {@code close()} already had been invoked manually because this method is 207 * idempotent. 208 * 209 * @see #close() 210 */ 211 @Override 212 protected void finalize() throws Throwable { 213 if (isReleaseTrackedNodeOnFinalize()) { 214 close(); 215 } 216 super.finalize(); 217 } 218 }