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.ArrayList;
022: import java.util.List;
023:
024: import org.apache.commons.beanutils.DynaBean;
025: import org.apache.commons.beanutils.DynaClass;
026: import org.apache.commons.beanutils.DynaProperty;
027: import org.apache.commons.configuration.Configuration;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
033: * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
034: * {@link org.apache.commons.configuration.Configuration} instance.
035: *
036: * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
037: * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
038: * @since 1.0-rc1
039: */
040: public class ConfigurationDynaClass implements DynaClass {
041: /** The logger.*/
042: private static Log log = LogFactory
043: .getLog(ConfigurationDynaClass.class);
044:
045: /** Stores the associated configuration.*/
046: private Configuration configuration;
047:
048: /**
049: * Construct an instance of a <code>ConfigurationDynaClass</code>
050: * wrapping the specified <code>Configuration</code> instance.
051: * @param configuration <code>Configuration</code> instance.
052: */
053: public ConfigurationDynaClass(Configuration configuration) {
054: super ();
055: if (log.isTraceEnabled()) {
056: log.trace("ConfigurationDynaClass(" + configuration + ")");
057: }
058: this .configuration = configuration;
059: }
060:
061: public DynaProperty getDynaProperty(String name) {
062: if (log.isTraceEnabled()) {
063: log.trace("getDynaProperty(" + name + ")");
064: }
065:
066: if (name == null) {
067: throw new IllegalArgumentException(
068: "No such property name=[" + name + "]");
069: }
070:
071: Object value = configuration.getProperty(name);
072: if (value == null) {
073: return null;
074: } else {
075: Class type = value.getClass();
076:
077: if (type == Byte.class) {
078: type = Byte.TYPE;
079: }
080: if (type == Character.class) {
081: type = Character.TYPE;
082: } else if (type == Boolean.class) {
083: type = Boolean.TYPE;
084: } else if (type == Double.class) {
085: type = Double.TYPE;
086: } else if (type == Float.class) {
087: type = Float.TYPE;
088: } else if (type == Integer.class) {
089: type = Integer.TYPE;
090: } else if (type == Long.class) {
091: type = Long.TYPE;
092: } else if (type == Short.class) {
093: type = Short.TYPE;
094: }
095:
096: return new DynaProperty(name, type);
097: }
098: }
099:
100: public DynaProperty[] getDynaProperties() {
101: if (log.isTraceEnabled()) {
102: log.trace("getDynaProperties()");
103: }
104:
105: Iterator keys = configuration.getKeys();
106: List properties = new ArrayList();
107: while (keys.hasNext()) {
108: String key = (String) keys.next();
109: DynaProperty property = getDynaProperty(key);
110: properties.add(property);
111: }
112:
113: DynaProperty[] propertyArray = new DynaProperty[properties
114: .size()];
115: properties.toArray(propertyArray);
116: if (log.isDebugEnabled()) {
117: log.debug("Found " + properties.size() + " properties.");
118: }
119:
120: return propertyArray;
121: }
122:
123: public String getName() {
124: return ConfigurationDynaBean.class.getName();
125: }
126:
127: public DynaBean newInstance() throws IllegalAccessException,
128: InstantiationException {
129: return new ConfigurationDynaBean(configuration);
130: }
131: }
|