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.Iterator; |
21 | |
import java.util.ArrayList; |
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 9 | public class ConfigurationDynaClass implements DynaClass |
41 | |
{ |
42 | |
|
43 | 1 | private static Log log = LogFactory.getLog(ConfigurationDynaClass.class); |
44 | |
|
45 | |
|
46 | |
private Configuration configuration; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public ConfigurationDynaClass(Configuration configuration) |
54 | |
{ |
55 | 11 | super(); |
56 | 11 | if (log.isTraceEnabled()) |
57 | |
{ |
58 | 0 | log.trace("ConfigurationDynaClass(" + configuration + ")"); |
59 | |
} |
60 | 11 | this.configuration = configuration; |
61 | 11 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public DynaProperty getDynaProperty(String name) |
67 | |
{ |
68 | 36 | if (log.isTraceEnabled()) |
69 | |
{ |
70 | 0 | log.trace("getDynaProperty(" + name + ")"); |
71 | |
} |
72 | |
|
73 | 36 | if (name == null) |
74 | |
{ |
75 | 1 | throw new IllegalArgumentException("No such property name=[" + name + "]"); |
76 | |
} |
77 | |
|
78 | 35 | Object value = configuration.getProperty(name); |
79 | 35 | if (value == null) |
80 | |
{ |
81 | 1 | return null; |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 34 | Class type = value.getClass(); |
86 | |
|
87 | 34 | if (type == Byte.class) |
88 | |
{ |
89 | 1 | type = Byte.TYPE; |
90 | |
} |
91 | 34 | if (type == Character.class) |
92 | |
{ |
93 | 1 | type = Character.TYPE; |
94 | |
} |
95 | 33 | else if (type == Boolean.class) |
96 | |
{ |
97 | 4 | type = Boolean.TYPE; |
98 | |
} |
99 | 29 | else if (type == Double.class) |
100 | |
{ |
101 | 2 | type = Double.TYPE; |
102 | |
} |
103 | 27 | else if (type == Float.class) |
104 | |
{ |
105 | 2 | type = Float.TYPE; |
106 | |
} |
107 | 25 | else if (type == Integer.class) |
108 | |
{ |
109 | 3 | type = Integer.TYPE; |
110 | |
} |
111 | 22 | else if (type == Long.class) |
112 | |
{ |
113 | 2 | type = Long.TYPE; |
114 | |
} |
115 | 20 | else if (type == Short.class) |
116 | |
{ |
117 | 2 | type = Short.TYPE; |
118 | |
} |
119 | |
|
120 | 34 | return new DynaProperty(name, type); |
121 | |
} |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public DynaProperty[] getDynaProperties() |
128 | |
{ |
129 | 1 | if (log.isTraceEnabled()) |
130 | |
{ |
131 | 0 | log.trace("getDynaProperties()"); |
132 | |
} |
133 | |
|
134 | 1 | Iterator keys = configuration.getKeys(); |
135 | 1 | List properties = new ArrayList(); |
136 | 28 | while (keys.hasNext()) |
137 | |
{ |
138 | 26 | String key = (String) keys.next(); |
139 | 26 | DynaProperty property = getDynaProperty(key); |
140 | 26 | properties.add(property); |
141 | |
} |
142 | |
|
143 | 1 | DynaProperty[] propertyArray = new DynaProperty[properties.size()]; |
144 | 1 | properties.toArray(propertyArray); |
145 | 1 | if (log.isDebugEnabled()) |
146 | |
{ |
147 | 0 | log.debug("Found " + properties.size() + " properties."); |
148 | |
} |
149 | |
|
150 | 1 | return propertyArray; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public String getName() |
157 | |
{ |
158 | 0 | return ConfigurationDynaBean.class.getName(); |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public DynaBean newInstance() throws IllegalAccessException, InstantiationException |
165 | |
{ |
166 | 0 | return new ConfigurationDynaBean(configuration); |
167 | |
} |
168 | |
|
169 | |
} |