1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.beanutils;
18
19 import java.util.Iterator;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.beanutils.DynaBean;
24 import org.apache.commons.beanutils.DynaClass;
25 import org.apache.commons.beanutils.DynaProperty;
26 import org.apache.commons.configuration.Configuration;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /***
31 * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
32 * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
33 * {@link org.apache.commons.configuration.Configuration} instance.
34 *
35 * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
36 * @version $Revision: 1.4 $, $Date: 2004/09/21 17:49:39 $
37 * @since 1.0-rc1
38 */
39 public class ConfigurationDynaClass implements DynaClass
40 {
41 private final static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
42
43 Configuration configuration;
44
45 /***
46 * Construct an instance of a <code>ConfigurationDynaClass</code>
47 * wrapping the specified <code>Configuration</code> instance.
48 * @param configuration <code>Configuration</code> instance.
49 */
50 public ConfigurationDynaClass(Configuration configuration)
51 {
52 super();
53 if (log.isTraceEnabled()) log.trace("ConfigurationDynaClass(" + configuration + ")");
54 this.configuration = configuration;
55 }
56
57 /***
58 * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
59 */
60 public DynaProperty getDynaProperty(String name)
61 {
62 if (log.isTraceEnabled())
63 {
64 log.trace("getDynaProperty(" + name + ")");
65 }
66
67 if (name == null)
68 {
69 throw new IllegalArgumentException("No such property name=[" + name + "]");
70 }
71
72 Object value = configuration.getProperty(name);
73 if (value == null)
74 {
75 return null;
76 }
77 else
78 {
79 Class type = value.getClass();
80
81 if (type == Byte.class)
82 {
83 type = Byte.TYPE;
84 }
85 if (type == Character.class)
86 {
87 type = Character.TYPE;
88 }
89 else if (type == Boolean.class)
90 {
91 type = Boolean.TYPE;
92 }
93 else if (type == Double.class)
94 {
95 type = Double.TYPE;
96 }
97 else if (type == Float.class)
98 {
99 type = Float.TYPE;
100 }
101 else if (type == Integer.class)
102 {
103 type = Integer.TYPE;
104 }
105 else if (type == Long.class)
106 {
107 type = Long.TYPE;
108 }
109 else if (type == Short.class)
110 {
111 type = Short.TYPE;
112 }
113
114 return new DynaProperty(name, type);
115 }
116 }
117
118 /***
119 * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
120 */
121 public DynaProperty[] getDynaProperties()
122 {
123 if (log.isTraceEnabled())
124 {
125 log.trace("getDynaProperties()");
126 }
127
128 Iterator keys = configuration.getKeys();
129 List properties = new ArrayList();
130 while (keys.hasNext())
131 {
132 String key = (String) keys.next();
133 DynaProperty property = getDynaProperty(key);
134 properties.add(property);
135 }
136
137 DynaProperty[] propertyArray = new DynaProperty[properties.size()];
138 properties.toArray(propertyArray);
139 if (log.isDebugEnabled())
140 {
141 log.debug("Found " + properties.size() + " properties.");
142 }
143
144 return propertyArray;
145 }
146
147 /***
148 * @see org.apache.commons.beanutils.DynaClass#getName()
149 */
150 public String getName()
151 {
152 return ConfigurationDynaBean.class.getName();
153 }
154
155 /***
156 * @see org.apache.commons.beanutils.DynaClass#newInstance()
157 */
158 public DynaBean newInstance() throws IllegalAccessException, InstantiationException
159 {
160 return new ConfigurationDynaBean(configuration);
161 }
162
163 }