1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration.beanutils;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
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;
30
31 /***
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.
35 *
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) $
38 * @since 1.0-rc1
39 */
40 public class ConfigurationDynaClass implements DynaClass
41 {
42 /*** The logger.*/
43 private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
44
45 /*** Stores the associated configuration.*/
46 private Configuration configuration;
47
48 /***
49 * Construct an instance of a <code>ConfigurationDynaClass</code>
50 * wrapping the specified <code>Configuration</code> instance.
51 * @param configuration <code>Configuration</code> instance.
52 */
53 public ConfigurationDynaClass(Configuration configuration)
54 {
55 super();
56 if (log.isTraceEnabled())
57 {
58 log.trace("ConfigurationDynaClass(" + configuration + ")");
59 }
60 this.configuration = configuration;
61 }
62
63 public DynaProperty getDynaProperty(String name)
64 {
65 if (log.isTraceEnabled())
66 {
67 log.trace("getDynaProperty(" + name + ")");
68 }
69
70 if (name == null)
71 {
72 throw new IllegalArgumentException("Property name must not be null!");
73 }
74
75 Object value = configuration.getProperty(name);
76 if (value == null)
77 {
78 return null;
79 }
80 else
81 {
82 Class type = value.getClass();
83
84 if (type == Byte.class)
85 {
86 type = Byte.TYPE;
87 }
88 if (type == Character.class)
89 {
90 type = Character.TYPE;
91 }
92 else if (type == Boolean.class)
93 {
94 type = Boolean.TYPE;
95 }
96 else if (type == Double.class)
97 {
98 type = Double.TYPE;
99 }
100 else if (type == Float.class)
101 {
102 type = Float.TYPE;
103 }
104 else if (type == Integer.class)
105 {
106 type = Integer.TYPE;
107 }
108 else if (type == Long.class)
109 {
110 type = Long.TYPE;
111 }
112 else if (type == Short.class)
113 {
114 type = Short.TYPE;
115 }
116
117 return new DynaProperty(name, type);
118 }
119 }
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 public String getName()
148 {
149 return ConfigurationDynaBean.class.getName();
150 }
151
152 public DynaBean newInstance() throws IllegalAccessException, InstantiationException
153 {
154 return new ConfigurationDynaBean(configuration);
155 }
156 }