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.util.ArrayList;
21
import java.util.Iterator;
22
import java.util.List;
24
import org.apache.commons.beanutils.DynaBean;
25
import org.apache.commons.beanutils.DynaClass;
26
import org.apache.commons.beanutils.DynaProperty;
27
import org.apache.commons.configuration.Configuration;
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
32
* The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
33
* a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
34
* {@link org.apache.commons.configuration.Configuration} instance.
36
* @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
37
* @version $Revision: 727664 $, $Date: 2008-12-18 08:16:09 +0100 (Do, 18. Dez 2008) $
40
public class ConfigurationDynaClass implements DynaClass
43
private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
45
/** Stores the associated configuration.*/
46
private Configuration configuration;
49
* Construct an instance of a <code>ConfigurationDynaClass</code>
50
* wrapping the specified <code>Configuration</code> instance.
51
* @param configuration <code>Configuration</code> instance.
53
public ConfigurationDynaClass(Configuration configuration)
56
if (log.isTraceEnabled())
58
log.trace("ConfigurationDynaClass(" + configuration + ")");
60
this.configuration = configuration;
63
public DynaProperty getDynaProperty(String name)
65
if (log.isTraceEnabled())
67
log.trace("getDynaProperty(" + name + ")");
72
throw new IllegalArgumentException("Property name must not be null!");
75
Object value = configuration.getProperty(name);
82
Class type = value.getClass();
84
if (type == Byte.class)
88
if (type == Character.class)
90
type = Character.TYPE;
92
else if (type == Boolean.class)
96
else if (type == Double.class)
100
else if (type == Float.class)
104
else if (type == Integer.class)
108
else if (type == Long.class)
112
else if (type == Short.class)
117
return new DynaProperty(name, type);
121
public DynaProperty[] getDynaProperties()
123
if (log.isTraceEnabled())
125
log.trace("getDynaProperties()");
128
Iterator keys = configuration.getKeys();
129
List properties = new ArrayList();
130
while (keys.hasNext())
132
String key = (String) keys.next();
133
DynaProperty property = getDynaProperty(key);
134
properties.add(property);
137
DynaProperty[] propertyArray = new DynaProperty[properties.size()];
138
properties.toArray(propertyArray);
139
if (log.isDebugEnabled())
141
log.debug("Found " + properties.size() + " properties.");
144
return propertyArray;
147
public String getName()
149
return ConfigurationDynaBean.class.getName();
152
public DynaBean newInstance() throws IllegalAccessException, InstantiationException
154
return new ConfigurationDynaBean(configuration);