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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.configuration;
19
import java.io.Reader;
20
import java.io.Writer;
21
import java.math.BigDecimal;
22
import java.math.BigInteger;
23
import java.util.Collection;
24
import java.util.Iterator;
25
import java.util.List;
26
import java.util.Properties;
28
import org.apache.commons.configuration.event.ConfigurationErrorListener;
29
import org.apache.commons.configuration.event.ConfigurationListener;
30
import org.apache.commons.configuration.tree.ConfigurationNode;
31
import org.apache.commons.configuration.tree.ExpressionEngine;
34
* Wraps a HierarchicalConfiguration and allows subtrees to be access via a configured path with
35
* replaceable tokens derived from the ConfigurationInterpolator. When used with injection frameworks
36
* such as Spring it allows components to be injected with subtrees of the configuration.
39
* href="http://commons.apache.org/configuration/team-list.html">Commons
40
* Configuration team</a>
41
* @version $Id: PatternSubtreeConfigurationWrapper.java 823891 2009-10-10 17:17:44Z rgoers $
43
public class PatternSubtreeConfigurationWrapper extends AbstractHierarchicalFileConfiguration
46
* Prevent recursion while resolving unprefixed properties.
48
private static ThreadLocal recursive = new ThreadLocal()
50
protected synchronized Object initialValue()
56
/** The wrapped configuration */
57
private final AbstractHierarchicalFileConfiguration config;
59
/** The path to the subtree */
60
private final String path;
62
/** True if the path ends with '/', false otherwise */
63
private final boolean trailing;
65
/** True if the constructor has finished */
70
* @param config The Configuration to be wrapped.
71
* @param path The base path pattern.
73
public PatternSubtreeConfigurationWrapper(AbstractHierarchicalFileConfiguration config, String path)
77
this.trailing = path.endsWith("/");
81
public Object getReloadLock()
83
return config.getReloadLock();
86
public void addProperty(String key, Object value)
88
config.addProperty(makePath(key), value);
96
public void clearProperty(String key)
98
config.clearProperty(makePath(key));
101
public boolean containsKey(String key)
103
return config.containsKey(makePath(key));
106
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
108
return config.getBigDecimal(makePath(key), defaultValue);
111
public BigDecimal getBigDecimal(String key)
113
return config.getBigDecimal(makePath(key));
116
public BigInteger getBigInteger(String key, BigInteger defaultValue)
118
return config.getBigInteger(makePath(key), defaultValue);
121
public BigInteger getBigInteger(String key)
123
return config.getBigInteger(makePath(key));
126
public boolean getBoolean(String key, boolean defaultValue)
128
return config.getBoolean(makePath(key), defaultValue);
131
public Boolean getBoolean(String key, Boolean defaultValue)
133
return config.getBoolean(makePath(key), defaultValue);
136
public boolean getBoolean(String key)
138
return config.getBoolean(makePath(key));
141
public byte getByte(String key, byte defaultValue)
143
return config.getByte(makePath(key), defaultValue);
146
public Byte getByte(String key, Byte defaultValue)
148
return config.getByte(makePath(key), defaultValue);
151
public byte getByte(String key)
153
return config.getByte(makePath(key));
156
public double getDouble(String key, double defaultValue)
158
return config.getDouble(makePath(key), defaultValue);
161
public Double getDouble(String key, Double defaultValue)
163
return config.getDouble(makePath(key), defaultValue);
166
public double getDouble(String key)
168
return config.getDouble(makePath(key));
171
public float getFloat(String key, float defaultValue)
173
return config.getFloat(makePath(key), defaultValue);
176
public Float getFloat(String key, Float defaultValue)
178
return config.getFloat(makePath(key), defaultValue);
181
public float getFloat(String key)
183
return config.getFloat(makePath(key));
186
public int getInt(String key, int defaultValue)
188
return config.getInt(makePath(key), defaultValue);
191
public int getInt(String key)
193
return config.getInt(makePath(key));
196
public Integer getInteger(String key, Integer defaultValue)
198
return config.getInteger(makePath(key), defaultValue);
201
public Iterator getKeys()
203
return config.getKeys(makePath());
206
public Iterator getKeys(String prefix)
208
return config.getKeys(makePath(prefix));
211
public List getList(String key, List defaultValue)
213
return config.getList(makePath(key), defaultValue);
216
public List getList(String key)
218
return config.getList(makePath(key));
221
public long getLong(String key, long defaultValue)
223
return config.getLong(makePath(key), defaultValue);
226
public Long getLong(String key, Long defaultValue)
228
return config.getLong(makePath(key), defaultValue);
231
public long getLong(String key)
233
return config.getLong(makePath(key));
236
public Properties getProperties(String key)
238
return config.getProperties(makePath(key));
241
public Object getProperty(String key)
243
return config.getProperty(makePath(key));
246
public short getShort(String key, short defaultValue)
248
return config.getShort(makePath(key), defaultValue);
251
public Short getShort(String key, Short defaultValue)
253
return config.getShort(makePath(key), defaultValue);
256
public short getShort(String key)
258
return config.getShort(makePath(key));
261
public String getString(String key, String defaultValue)
263
return config.getString(makePath(key), defaultValue);
266
public String getString(String key)
268
return config.getString(makePath(key));
271
public String[] getStringArray(String key)
273
return config.getStringArray(makePath(key));
276
public boolean isEmpty()
278
return getConfig().isEmpty();
281
public void setProperty(String key, Object value)
283
getConfig().setProperty(key, value);
286
public Configuration subset(String prefix)
288
return getConfig().subset(prefix);
291
public Node getRoot()
293
return getConfig().getRoot();
296
public void setRoot(Node node)
300
getConfig().setRoot(node);
308
public ConfigurationNode getRootNode()
310
return getConfig().getRootNode();
313
public void setRootNode(ConfigurationNode rootNode)
317
getConfig().setRootNode(rootNode);
321
super.setRootNode(rootNode);
325
public ExpressionEngine getExpressionEngine()
327
return config.getExpressionEngine();
330
public void setExpressionEngine(ExpressionEngine expressionEngine)
334
config.setExpressionEngine(expressionEngine);
338
super.setExpressionEngine(expressionEngine);
342
public void addNodes(String key, Collection nodes)
344
getConfig().addNodes(key, nodes);
347
public SubnodeConfiguration configurationAt(String key, boolean supportUpdates)
349
return config.configurationAt(makePath(key), supportUpdates);
352
public SubnodeConfiguration configurationAt(String key)
354
return config.configurationAt(makePath(key));
357
public List configurationsAt(String key)
359
return config.configurationsAt(makePath(key));
362
public void clearTree(String key)
364
config.clearTree(makePath(key));
367
public int getMaxIndex(String key)
369
return config.getMaxIndex(makePath(key));
372
public Configuration interpolatedConfiguration()
374
return getConfig().interpolatedConfiguration();
377
public void addConfigurationListener(ConfigurationListener l)
379
getConfig().addConfigurationListener(l);
382
public boolean removeConfigurationListener(ConfigurationListener l)
384
return getConfig().removeConfigurationListener(l);
387
public Collection getConfigurationListeners()
389
return getConfig().getConfigurationListeners();
392
public void clearConfigurationListeners()
394
getConfig().clearConfigurationListeners();
397
public void addErrorListener(ConfigurationErrorListener l)
399
getConfig().addErrorListener(l);
402
public boolean removeErrorListener(ConfigurationErrorListener l)
404
return getConfig().removeErrorListener(l);
407
public void clearErrorListeners()
409
getConfig().clearErrorListeners();
412
public void save(Writer writer) throws ConfigurationException
417
public void load(Reader reader) throws ConfigurationException
422
public Collection getErrorListeners()
424
return getConfig().getErrorListeners();
427
protected Object resolveContainerStore(String key)
429
if (((Boolean) recursive.get()).booleanValue())
433
recursive.set(Boolean.TRUE);
436
return super.resolveContainerStore(key);
440
recursive.set(Boolean.FALSE);
444
private HierarchicalConfiguration getConfig()
446
return config.configurationAt(makePath());
449
private String makePath()
451
String pathPattern = trailing ? path.substring(0, path.length() - 1) : path;
452
return getSubstitutor().replace(pathPattern);
456
* Resolve the root expression and then add the item being retrieved. Insert a
457
* separator character as required.
459
private String makePath(String item)
462
if ((item.length() == 0 || item.startsWith("/")) && trailing)
464
pathPattern = path.substring(0, path.length() - 1);
466
else if (!item.startsWith("/") || !trailing)
468
pathPattern = path + "/";
474
return getSubstitutor().replace(pathPattern) + item;