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