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.List; |
21 |
|
|
22 |
|
import org.apache.commons.beanutils.DynaBean; |
23 |
|
import org.apache.commons.beanutils.DynaClass; |
24 |
|
import org.apache.commons.configuration.Configuration; |
25 |
|
import org.apache.commons.configuration.ConversionException; |
26 |
|
import org.apache.commons.lang.BooleanUtils; |
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public class ConfigurationDynaBean implements DynaBean |
50 |
|
{ |
51 |
46 |
private final static Log log = LogFactory.getLog(ConfigurationDynaBean.class); |
52 |
|
|
53 |
|
Configuration configuration; |
54 |
|
|
55 |
|
public ConfigurationDynaBean(Configuration configuration) |
56 |
897 |
{ |
57 |
897 |
if (log.isTraceEnabled()) |
58 |
|
{ |
59 |
0 |
log.trace("ConfigurationDynaBean(" + configuration + ")"); |
60 |
|
} |
61 |
|
|
62 |
897 |
this.configuration = configuration; |
63 |
897 |
} |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public void set(String name, Object value) |
69 |
|
{ |
70 |
8947 |
if (log.isTraceEnabled()) |
71 |
|
{ |
72 |
0 |
log.trace("set(" + name + "," + value + ")"); |
73 |
|
} |
74 |
|
|
75 |
8947 |
if (value == null) |
76 |
|
{ |
77 |
23 |
throw new NullPointerException("Error trying to set property to null."); |
78 |
|
} |
79 |
|
|
80 |
8924 |
if (value instanceof List) |
81 |
|
{ |
82 |
874 |
List list = (List) value; |
83 |
874 |
Iterator iterator = list.iterator(); |
84 |
5244 |
while (iterator.hasNext()) |
85 |
|
{ |
86 |
4370 |
configuration.addProperty(name, iterator.next()); |
87 |
|
} |
88 |
|
} |
89 |
8050 |
else if (value instanceof int[]) |
90 |
|
{ |
91 |
874 |
int[] array = (class="keyword">int[]) value; |
92 |
5244 |
for (int i = 0; i < array.length; i++) |
93 |
|
{ |
94 |
4370 |
configuration.addProperty(name, new Integer(array[i])); |
95 |
|
} |
96 |
|
} |
97 |
7176 |
else if (value instanceof boolean[]) |
98 |
|
{ |
99 |
874 |
boolean[] array = (class="keyword">boolean[]) value; |
100 |
5244 |
for (int i = 0; i < array.length; i++) |
101 |
|
{ |
102 |
4370 |
configuration.addProperty(name, BooleanUtils.toBooleanObject(array[i])); |
103 |
|
} |
104 |
|
} |
105 |
6302 |
else if (value instanceof char[]) |
106 |
|
{ |
107 |
874 |
char[] array = (class="keyword">char[]) value; |
108 |
5244 |
for (int i = 0; i < array.length; i++) |
109 |
|
{ |
110 |
4370 |
configuration.addProperty(name, new Character(array[i])); |
111 |
|
} |
112 |
|
} |
113 |
5428 |
else if (value instanceof byte[]) |
114 |
|
{ |
115 |
874 |
byte[] array = (byte[]) value; |
116 |
5244 |
for (int i = 0; i < array.length; i++) |
117 |
|
{ |
118 |
4370 |
configuration.addProperty(name, new Byte(array[i])); |
119 |
|
} |
120 |
|
} |
121 |
4554 |
else if (value instanceof short[]) |
122 |
|
{ |
123 |
874 |
short[] array = (class="keyword">short[]) value; |
124 |
5244 |
for (int i = 0; i < array.length; i++) |
125 |
|
{ |
126 |
4370 |
configuration.addProperty(name, new Short(array[i])); |
127 |
|
} |
128 |
|
} |
129 |
3680 |
else if (value instanceof int[]) |
130 |
|
{ |
131 |
0 |
int[] array = (class="keyword">int[]) value; |
132 |
0 |
for (int i = 0; i < array.length; i++) |
133 |
|
{ |
134 |
0 |
configuration.addProperty(name, new Integer(array[i])); |
135 |
|
} |
136 |
|
} |
137 |
3680 |
else if (value instanceof long[]) |
138 |
|
{ |
139 |
874 |
long[] array = (class="keyword">long[]) value; |
140 |
5244 |
for (int i = 0; i < array.length; i++) |
141 |
|
{ |
142 |
4370 |
configuration.addProperty(name, new Long(array[i])); |
143 |
|
} |
144 |
|
} |
145 |
2806 |
else if (value instanceof float[]) |
146 |
|
{ |
147 |
874 |
float[] array = (class="keyword">float[]) value; |
148 |
5244 |
for (int i = 0; i < array.length; i++) |
149 |
|
{ |
150 |
4370 |
configuration.addProperty(name, new Float(array[i])); |
151 |
|
} |
152 |
|
} |
153 |
1932 |
else if (value instanceof double[]) |
154 |
|
{ |
155 |
874 |
double[] array = (class="keyword">double[]) value; |
156 |
5244 |
for (int i = 0; i < array.length; i++) |
157 |
|
{ |
158 |
4370 |
configuration.addProperty(name, new Double(array[i])); |
159 |
|
} |
160 |
|
} |
161 |
1058 |
else if (value instanceof Object[]) |
162 |
|
{ |
163 |
874 |
Object[] array = (Object[]) value; |
164 |
5244 |
for (int i = 0; i < array.length; i++) |
165 |
|
{ |
166 |
4370 |
configuration.addProperty(name, array[i]); |
167 |
|
} |
168 |
|
} |
169 |
|
else |
170 |
|
{ |
171 |
184 |
configuration.setProperty(name, value); |
172 |
|
} |
173 |
8924 |
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
public Object get(String name) |
179 |
|
{ |
180 |
598 |
if (log.isTraceEnabled()) |
181 |
|
{ |
182 |
0 |
log.trace("get(" + name + ")"); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
598 |
Object result = configuration.getProperty(name); |
187 |
598 |
if (result == null) |
188 |
|
{ |
189 |
|
|
190 |
69 |
Configuration subset = configuration.subset(name); |
191 |
69 |
if (!subset.isEmpty()) |
192 |
|
{ |
193 |
23 |
result = new ConfigurationDynaBean(configuration.subset(name)); |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
598 |
if (log.isDebugEnabled()) |
198 |
|
{ |
199 |
0 |
log.debug(name + "=[" + result + "]"); |
200 |
|
} |
201 |
|
|
202 |
598 |
if (result == null) |
203 |
|
{ |
204 |
46 |
throw new IllegalArgumentException("Property '" + name + "' does not exist."); |
205 |
|
} |
206 |
552 |
return result; |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
public boolean contains(String name, String key) |
213 |
|
{ |
214 |
138 |
Configuration subset = configuration.subset(name); |
215 |
138 |
if (subset == null) |
216 |
|
{ |
217 |
0 |
throw new IllegalArgumentException("Mapped property '" + name + "' does not exist."); |
218 |
|
} |
219 |
|
|
220 |
138 |
return subset.containsKey(key); |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
public Object get(String name, int index) |
227 |
|
{ |
228 |
|
try |
229 |
|
{ |
230 |
736 |
List list = configuration.getList(name); |
231 |
713 |
if (list.isEmpty()) |
232 |
|
{ |
233 |
0 |
throw new IllegalArgumentException("Indexed property '" + name + "' does not exist."); |
234 |
|
} |
235 |
|
|
236 |
713 |
return list.get(index); |
237 |
|
} |
238 |
23 |
catch (ConversionException e) |
239 |
|
{ |
240 |
23 |
throw new IllegalArgumentException("Property '" + name + "' is not indexed."); |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
public Object get(String name, String key) |
248 |
|
{ |
249 |
138 |
Configuration subset = configuration.subset(name); |
250 |
138 |
if (subset == null) |
251 |
|
{ |
252 |
0 |
throw new IllegalArgumentException("Mapped property '" + name + "' does not exist."); |
253 |
|
} |
254 |
|
|
255 |
138 |
return subset.getProperty(key); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
public DynaClass getDynaClass() |
262 |
|
{ |
263 |
253 |
return new ConfigurationDynaClass(configuration); |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
public void remove(String name, String key) |
270 |
|
{ |
271 |
46 |
Configuration subset = configuration.subset(name); |
272 |
46 |
if (subset == null) |
273 |
|
{ |
274 |
0 |
throw new IllegalArgumentException("Mapped property '" + name + "' does not exist."); |
275 |
|
} |
276 |
46 |
subset.setProperty(key, null); |
277 |
46 |
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
public void set(String name, int index, Object value) |
283 |
|
{ |
284 |
|
try |
285 |
|
{ |
286 |
138 |
List list = configuration.getList(name); |
287 |
138 |
if (list == null) |
288 |
|
{ |
289 |
0 |
throw new IllegalArgumentException("Property '" + name + "' does not exist."); |
290 |
|
} |
291 |
|
|
292 |
138 |
list.set(index, value); |
293 |
|
} |
294 |
0 |
catch (ConversionException e) |
295 |
|
{ |
296 |
0 |
throw new IllegalArgumentException("Property '" + name + "' is not indexed."); |
297 |
115 |
} |
298 |
115 |
} |
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
public void set(String name, String key, Object value) |
304 |
|
{ |
305 |
46 |
configuration.setProperty(name + "." + key, value); |
306 |
46 |
} |
307 |
|
|
308 |
|
} |