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.
18
package org.apache.commons.configuration.beanutils;
20
import java.lang.reflect.Array;
21
import java.util.Collection;
22
import java.util.List;
24
import org.apache.commons.beanutils.DynaBean;
25
import org.apache.commons.beanutils.DynaClass;
26
import org.apache.commons.configuration.Configuration;
27
import org.apache.commons.configuration.ConfigurationMap;
28
import org.apache.commons.configuration.SubsetConfiguration;
29
import org.apache.commons.logging.Log;
30
import org.apache.commons.logging.LogFactory;
33
* The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
34
* configurations properties from a wrapped configuration-collection
35
* {@link org.apache.commons.configuration.Configuration} instance. It also
36
* implements a {@link java.util.Map} interface so that it can be used in
37
* JSP 2.0 Expression Language expressions.
39
* <p>The {@code ConfigurationDynaBean} maps nested and mapped properties
40
* to the appropriate {@code Configuration} subset using the
41
* {@link org.apache.commons.configuration.Configuration#subset}
42
* method. Similarly, indexed properties reference lists of configuration
43
* properties using the
44
* {@link org.apache.commons.configuration.Configuration#getList(String)}
45
* method. Setting an indexed property is supported, too.</p>
47
* <p>Note: Some of the methods expect that a dot (".") is used as
48
* property delimiter for the wrapped configuration. This is true for most of
49
* the default configurations. Hierarchical configurations, for which a specific
50
* expression engine is set, may cause problems.</p>
52
* @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
53
* @version $Id: ConfigurationDynaBean.java 1366932 2012-07-29 20:06:31Z oheger $
56
public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
58
/** Constant for the property delimiter.*/
59
private static final String PROPERTY_DELIMITER = ".";
62
private static final Log LOG = LogFactory.getLog(ConfigurationDynaBean.class);
65
* Creates a new instance of {@code ConfigurationDynaBean} and sets
66
* the configuration this bean is associated with.
68
* @param configuration the configuration
70
public ConfigurationDynaBean(Configuration configuration)
73
if (LOG.isTraceEnabled())
75
LOG.trace("ConfigurationDynaBean(" + configuration + ")");
79
public void set(String name, Object value)
81
if (LOG.isTraceEnabled())
83
LOG.trace("set(" + name + "," + value + ")");
88
throw new NullPointerException("Error trying to set property to null.");
91
if (value instanceof Collection)
93
Collection<?> collection = (Collection<?>) value;
94
for (Object v : collection)
96
getConfiguration().addProperty(name, v);
99
else if (value.getClass().isArray())
101
int length = Array.getLength(value);
102
for (int i = 0; i < length; i++)
104
getConfiguration().addProperty(name, Array.get(value, i));
109
getConfiguration().setProperty(name, value);
113
public Object get(String name)
115
if (LOG.isTraceEnabled())
117
LOG.trace("get(" + name + ")");
120
// get configuration property
121
Object result = getConfiguration().getProperty(name);
124
// otherwise attempt to create bean from configuration subset
125
Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
126
if (!subset.isEmpty())
128
result = new ConfigurationDynaBean(subset);
132
if (LOG.isDebugEnabled())
134
LOG.debug(name + "=[" + result + "]");
139
throw new IllegalArgumentException("Property '" + name + "' does not exist.");
144
public boolean contains(String name, String key)
146
Configuration subset = getConfiguration().subset(name);
149
throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
152
return subset.containsKey(key);
155
public Object get(String name, int index)
157
if (!checkIndexedProperty(name))
159
throw new IllegalArgumentException("Property '" + name
160
+ "' is not indexed.");
163
List<Object> list = getConfiguration().getList(name);
164
return list.get(index);
167
public Object get(String name, String key)
169
Configuration subset = getConfiguration().subset(name);
172
throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
175
return subset.getProperty(key);
178
public DynaClass getDynaClass()
180
return new ConfigurationDynaClass(getConfiguration());
183
public void remove(String name, String key)
185
Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
186
subset.setProperty(key, null);
189
public void set(String name, int index, Object value)
191
if (!checkIndexedProperty(name) && index > 0)
193
throw new IllegalArgumentException("Property '" + name
194
+ "' is not indexed.");
197
Object property = getConfiguration().getProperty(name);
199
if (property instanceof List)
201
// This is safe because multiple values of a configuration property
202
// are always stored as lists of type Object.
203
@SuppressWarnings("unchecked")
204
List<Object> list = (List<Object>) property;
205
list.set(index, value);
206
getConfiguration().setProperty(name, list);
208
else if (property.getClass().isArray())
210
Array.set(property, index, value);
214
getConfiguration().setProperty(name, value);
218
public void set(String name, String key, Object value)
220
getConfiguration().setProperty(name + "." + key, value);
224
* Tests whether the given name references an indexed property. This
225
* implementation tests for properties of type list or array. If the
226
* property does not exist, an exception is thrown.
228
* @param name the name of the property to check
229
* @return a flag whether this is an indexed property
230
* @throws IllegalArgumentException if the property does not exist
232
private boolean checkIndexedProperty(String name)
234
Object property = getConfiguration().getProperty(name);
236
if (property == null)
238
throw new IllegalArgumentException("Property '" + name
239
+ "' does not exist.");
242
return (property instanceof List) || property.getClass().isArray();