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: */
017:
018: package org.apache.commons.configuration.beanutils;
019:
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.commons.beanutils.DynaBean;
024: import org.apache.commons.beanutils.DynaClass;
025: import org.apache.commons.configuration.Configuration;
026: import org.apache.commons.configuration.ConfigurationMap;
027: import org.apache.commons.configuration.ConversionException;
028: import org.apache.commons.configuration.SubsetConfiguration;
029: import org.apache.commons.lang.BooleanUtils;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
035: * configurations properties from a wrapped configuration-collection
036: * {@link org.apache.commons.configuration.Configuration} instance. It also
037: * implements a {@link java.util.Map} interface so that it can be used in
038: * JSP 2.0 Expression Language expressions.
039: *
040: * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
041: * to the appropriate <code>Configuration</code> subset using the
042: * {@link org.apache.commons.configuration.Configuration#subset}
043: * method. Similarly, indexed properties reference lists of configuration
044: * properties using the
045: * {@link org.apache.commons.configuration.Configuration#getList(String)}
046: * method. Setting an indexed property always throws an exception.</p>
047: *
048: * <p>Note: Some of the methods expect that a dot (".") is used as
049: * property delimitor for the wrapped configuration. This is true for most of
050: * the default configurations. Hierarchical configurations, for which a specific
051: * expression engine is set, may cause problems.</p>
052: *
053: * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
054: * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
055: * @since 1.0-rc1
056: */
057: public class ConfigurationDynaBean extends ConfigurationMap implements
058: DynaBean {
059: /** Constant for the property delimiter.*/
060: private static final String PROPERTY_DELIMITER = ".";
061:
062: /** The logger.*/
063: private static Log log = LogFactory
064: .getLog(ConfigurationDynaBean.class);
065:
066: /**
067: * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
068: * the configuration this bean is associated with.
069: * @param configuration the configuration
070: */
071: public ConfigurationDynaBean(Configuration configuration) {
072: super (configuration);
073: if (log.isTraceEnabled()) {
074: log.trace("ConfigurationDynaBean(" + configuration + ")");
075: }
076: }
077:
078: public void set(String name, Object value) {
079: if (log.isTraceEnabled()) {
080: log.trace("set(" + name + "," + value + ")");
081: }
082:
083: if (value == null) {
084: throw new NullPointerException(
085: "Error trying to set property to null.");
086: }
087:
088: if (value instanceof List) {
089: List list = (List) value;
090: Iterator iterator = list.iterator();
091: while (iterator.hasNext()) {
092: getConfiguration().addProperty(name, iterator.next());
093: }
094: } else if (value instanceof int[]) {
095: int[] array = (int[]) value;
096: for (int i = 0; i < array.length; i++) {
097: getConfiguration().addProperty(name,
098: new Integer(array[i]));
099: }
100: } else if (value instanceof boolean[]) {
101: boolean[] array = (boolean[]) value;
102: for (int i = 0; i < array.length; i++) {
103: getConfiguration().addProperty(name,
104: BooleanUtils.toBooleanObject(array[i]));
105: }
106: } else if (value instanceof char[]) {
107: char[] array = (char[]) value;
108: for (int i = 0; i < array.length; i++) {
109: getConfiguration().addProperty(name,
110: new Character(array[i]));
111: }
112: } else if (value instanceof byte[]) {
113: byte[] array = (byte[]) value;
114: for (int i = 0; i < array.length; i++) {
115: getConfiguration()
116: .addProperty(name, new Byte(array[i]));
117: }
118: } else if (value instanceof short[]) {
119: short[] array = (short[]) value;
120: for (int i = 0; i < array.length; i++) {
121: getConfiguration().addProperty(name,
122: new Short(array[i]));
123: }
124: } else if (value instanceof long[]) {
125: long[] array = (long[]) value;
126: for (int i = 0; i < array.length; i++) {
127: getConfiguration()
128: .addProperty(name, new Long(array[i]));
129: }
130: } else if (value instanceof float[]) {
131: float[] array = (float[]) value;
132: for (int i = 0; i < array.length; i++) {
133: getConfiguration().addProperty(name,
134: new Float(array[i]));
135: }
136: } else if (value instanceof double[]) {
137: double[] array = (double[]) value;
138: for (int i = 0; i < array.length; i++) {
139: getConfiguration().addProperty(name,
140: new Double(array[i]));
141: }
142: } else if (value instanceof Object[]) {
143: Object[] array = (Object[]) value;
144: for (int i = 0; i < array.length; i++) {
145: getConfiguration().addProperty(name, array[i]);
146: }
147: } else {
148: getConfiguration().setProperty(name, value);
149: }
150: }
151:
152: public Object get(String name) {
153: if (log.isTraceEnabled()) {
154: log.trace("get(" + name + ")");
155: }
156:
157: // get configuration property
158: Object result = getConfiguration().getProperty(name);
159: if (result == null) {
160: // otherwise attempt to create bean from configuration subset
161: Configuration subset = new SubsetConfiguration(
162: getConfiguration(), name, PROPERTY_DELIMITER);
163: if (!subset.isEmpty()) {
164: result = new ConfigurationDynaBean(subset);
165: }
166: }
167:
168: if (log.isDebugEnabled()) {
169: log.debug(name + "=[" + result + "]");
170: }
171:
172: if (result == null) {
173: throw new IllegalArgumentException("Property '" + name
174: + "' does not exist.");
175: }
176: return result;
177: }
178:
179: public boolean contains(String name, String key) {
180: Configuration subset = getConfiguration().subset(name);
181: if (subset == null) {
182: throw new IllegalArgumentException("Mapped property '"
183: + name + "' does not exist.");
184: }
185:
186: return subset.containsKey(key);
187: }
188:
189: public Object get(String name, int index) {
190: try {
191: List list = getConfiguration().getList(name);
192: if (list.isEmpty()) {
193: throw new IllegalArgumentException("Indexed property '"
194: + name + "' does not exist.");
195: }
196:
197: return list.get(index);
198: } catch (ConversionException e) {
199: throw new IllegalArgumentException("Property '" + name
200: + "' is not indexed.");
201: }
202: }
203:
204: public Object get(String name, String key) {
205: Configuration subset = getConfiguration().subset(name);
206: if (subset == null) {
207: throw new IllegalArgumentException("Mapped property '"
208: + name + "' does not exist.");
209: }
210:
211: return subset.getProperty(key);
212: }
213:
214: public DynaClass getDynaClass() {
215: return new ConfigurationDynaClass(getConfiguration());
216: }
217:
218: public void remove(String name, String key) {
219: Configuration subset = new SubsetConfiguration(
220: getConfiguration(), name, PROPERTY_DELIMITER);
221: subset.setProperty(key, null);
222: }
223:
224: public void set(String name, int index, Object value) {
225: try {
226: Object property = getConfiguration().getProperty(name);
227:
228: if (property == null) {
229: throw new IllegalArgumentException("Property '" + name
230: + "' does not exist.");
231: } else if (property instanceof List) {
232: List list = (List) property;
233: list.set(index, value);
234: getConfiguration().setProperty(name, list);
235: } else if (property.getClass().isArray()) {
236: Object[] array = (Object[]) property;
237: array[index] = value;
238: } else if (index == 0) {
239: getConfiguration().setProperty(name, value);
240: } else {
241: throw new IllegalArgumentException("Property '" + name
242: + "' is not indexed.");
243: }
244: } catch (ConversionException e) {
245: throw new IllegalArgumentException("Property '" + name
246: + "' is not indexed.");
247: }
248: }
249:
250: public void set(String name, String key, Object value) {
251: getConfiguration().setProperty(name + "." + key, value);
252: }
253: }
|