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$, $Date: 2005-12-05 09:23:53 +0100 (Mon, 05 Dec 2005) $
37 * @since 1.0-rc1
38 */
39 public class ConfigurationDynaClass implements DynaClass
40 {
41 /*** The logger.*/
42 private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
43
44 /*** Stores the associated configuration.*/
45 private Configuration configuration;
46
47 /***
48 * Construct an instance of a <code>ConfigurationDynaClass</code>
49 * wrapping the specified <code>Configuration</code> instance.
50 * @param configuration <code>Configuration</code> instance.
51 */
52 public ConfigurationDynaClass(Configuration configuration)
53 {
54 super();
55 if (log.isTraceEnabled())
56 {
57 log.trace("ConfigurationDynaClass(" + configuration + ")");
58 }
59 this.configuration = configuration;
60 }
61
62 /***
63 * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
64 */
65 public DynaProperty getDynaProperty(String name)
66 {
67 if (log.isTraceEnabled())
68 {
69 log.trace("getDynaProperty(" + name + ")");
70 }
71
72 if (name == null)
73 {
74 throw new IllegalArgumentException("No such property name=[" + name + "]");
75 }
76
77 Object value = configuration.getProperty(name);
78 if (value == null)
79 {
80 return null;
81 }
82 else
83 {
84 Class type = value.getClass();
85
86 if (type == Byte.class)
87 {
88 type = Byte.TYPE;
89 }
90 if (type == Character.class)
91 {
92 type = Character.TYPE;
93 }
94 else if (type == Boolean.class)
95 {
96 type = Boolean.TYPE;
97 }
98 else if (type == Double.class)
99 {
100 type = Double.TYPE;
101 }
102 else if (type == Float.class)
103 {
104 type = Float.TYPE;
105 }
106 else if (type == Integer.class)
107 {
108 type = Integer.TYPE;
109 }
110 else if (type == Long.class)
111 {
112 type = Long.TYPE;
113 }
114 else if (type == Short.class)
115 {
116 type = Short.TYPE;
117 }
118
119 return new DynaProperty(name, type);
120 }
121 }
122
123 /***
124 * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
125 */
126 public DynaProperty[] getDynaProperties()
127 {
128 if (log.isTraceEnabled())
129 {
130 log.trace("getDynaProperties()");
131 }
132
133 Iterator keys = configuration.getKeys();
134 List properties = new ArrayList();
135 while (keys.hasNext())
136 {
137 String key = (String) keys.next();
138 DynaProperty property = getDynaProperty(key);
139 properties.add(property);
140 }
141
142 DynaProperty[] propertyArray = new DynaProperty[properties.size()];
143 properties.toArray(propertyArray);
144 if (log.isDebugEnabled())
145 {
146 log.debug("Found " + properties.size() + " properties.");
147 }
148
149 return propertyArray;
150 }
151
152 /***
153 * @see org.apache.commons.beanutils.DynaClass#getName()
154 */
155 public String getName()
156 {
157 return ConfigurationDynaBean.class.getName();
158 }
159
160 /***
161 * @see org.apache.commons.beanutils.DynaClass#newInstance()
162 */
163 public DynaBean newInstance() throws IllegalAccessException, InstantiationException
164 {
165 return new ConfigurationDynaBean(configuration);
166 }
167
168 }