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.lang.reflect.Array; |
21 | |
import java.util.Collection; |
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.configuration.Configuration; |
27 | |
import org.apache.commons.configuration.ConfigurationMap; |
28 | |
import org.apache.commons.configuration.SubsetConfiguration; |
29 | |
import org.apache.commons.logging.Log; |
30 | |
import org.apache.commons.logging.LogFactory; |
31 | |
|
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 | |
public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean |
57 | |
{ |
58 | |
|
59 | |
private static final String PROPERTY_DELIMITER = "."; |
60 | |
|
61 | |
|
62 | 1 | private static final Log LOG = LogFactory.getLog(ConfigurationDynaBean.class); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public ConfigurationDynaBean(Configuration configuration) |
71 | |
{ |
72 | 88 | super(configuration); |
73 | 88 | if (LOG.isTraceEnabled()) |
74 | |
{ |
75 | 0 | LOG.trace("ConfigurationDynaBean(" + configuration + ")"); |
76 | |
} |
77 | 88 | } |
78 | |
|
79 | |
public void set(String name, Object value) |
80 | |
{ |
81 | 860 | if (LOG.isTraceEnabled()) |
82 | |
{ |
83 | 0 | LOG.trace("set(" + name + "," + value + ")"); |
84 | |
} |
85 | |
|
86 | 860 | if (value == null) |
87 | |
{ |
88 | 2 | throw new NullPointerException("Error trying to set property to null."); |
89 | |
} |
90 | |
|
91 | 858 | if (value instanceof Collection) |
92 | |
{ |
93 | 84 | Collection<?> collection = (Collection<?>) value; |
94 | 84 | for (Object v : collection) |
95 | |
{ |
96 | 420 | getConfiguration().addProperty(name, v); |
97 | |
} |
98 | 84 | } |
99 | 774 | else if (value.getClass().isArray()) |
100 | |
{ |
101 | 756 | int length = Array.getLength(value); |
102 | 4536 | for (int i = 0; i < length; i++) |
103 | |
{ |
104 | 3780 | getConfiguration().addProperty(name, Array.get(value, i)); |
105 | |
} |
106 | 756 | } |
107 | |
else |
108 | |
{ |
109 | 18 | getConfiguration().setProperty(name, value); |
110 | |
} |
111 | 858 | } |
112 | |
|
113 | |
public Object get(String name) |
114 | |
{ |
115 | 52 | if (LOG.isTraceEnabled()) |
116 | |
{ |
117 | 0 | LOG.trace("get(" + name + ")"); |
118 | |
} |
119 | |
|
120 | |
|
121 | 52 | Object result = getConfiguration().getProperty(name); |
122 | 52 | if (result == null) |
123 | |
{ |
124 | |
|
125 | 6 | Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER); |
126 | 6 | if (!subset.isEmpty()) |
127 | |
{ |
128 | 2 | result = new ConfigurationDynaBean(subset); |
129 | |
} |
130 | |
} |
131 | |
|
132 | 52 | if (LOG.isDebugEnabled()) |
133 | |
{ |
134 | 0 | LOG.debug(name + "=[" + result + "]"); |
135 | |
} |
136 | |
|
137 | 52 | if (result == null) |
138 | |
{ |
139 | 4 | throw new IllegalArgumentException("Property '" + name + "' does not exist."); |
140 | |
} |
141 | 48 | return result; |
142 | |
} |
143 | |
|
144 | |
public boolean contains(String name, String key) |
145 | |
{ |
146 | 12 | Configuration subset = getConfiguration().subset(name); |
147 | 12 | if (subset == null) |
148 | |
{ |
149 | 0 | throw new IllegalArgumentException("Mapped property '" + name + "' does not exist."); |
150 | |
} |
151 | |
|
152 | 12 | return subset.containsKey(key); |
153 | |
} |
154 | |
|
155 | |
public Object get(String name, int index) |
156 | |
{ |
157 | 70 | if (!checkIndexedProperty(name)) |
158 | |
{ |
159 | 4 | throw new IllegalArgumentException("Property '" + name |
160 | |
+ "' is not indexed."); |
161 | |
} |
162 | |
|
163 | 64 | List<Object> list = getConfiguration().getList(name); |
164 | 64 | return list.get(index); |
165 | |
} |
166 | |
|
167 | |
public Object get(String name, String key) |
168 | |
{ |
169 | 12 | Configuration subset = getConfiguration().subset(name); |
170 | 12 | if (subset == null) |
171 | |
{ |
172 | 0 | throw new IllegalArgumentException("Mapped property '" + name + "' does not exist."); |
173 | |
} |
174 | |
|
175 | 12 | return subset.getProperty(key); |
176 | |
} |
177 | |
|
178 | |
public DynaClass getDynaClass() |
179 | |
{ |
180 | 22 | return new ConfigurationDynaClass(getConfiguration()); |
181 | |
} |
182 | |
|
183 | |
public void remove(String name, String key) |
184 | |
{ |
185 | 4 | Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER); |
186 | 4 | subset.setProperty(key, null); |
187 | 4 | } |
188 | |
|
189 | |
public void set(String name, int index, Object value) |
190 | |
{ |
191 | 16 | if (!checkIndexedProperty(name) && index > 0) |
192 | |
{ |
193 | 2 | throw new IllegalArgumentException("Property '" + name |
194 | |
+ "' is not indexed."); |
195 | |
} |
196 | |
|
197 | 14 | Object property = getConfiguration().getProperty(name); |
198 | |
|
199 | 14 | if (property instanceof List) |
200 | |
{ |
201 | |
|
202 | |
|
203 | |
@SuppressWarnings("unchecked") |
204 | 12 | List<Object> list = (List<Object>) property; |
205 | 12 | list.set(index, value); |
206 | 10 | getConfiguration().setProperty(name, list); |
207 | 10 | } |
208 | 2 | else if (property.getClass().isArray()) |
209 | |
{ |
210 | 2 | Array.set(property, index, value); |
211 | |
} |
212 | 0 | else if (index == 0) |
213 | |
{ |
214 | 0 | getConfiguration().setProperty(name, value); |
215 | |
} |
216 | 12 | } |
217 | |
|
218 | |
public void set(String name, String key, Object value) |
219 | |
{ |
220 | 4 | getConfiguration().setProperty(name + "." + key, value); |
221 | 4 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
private boolean checkIndexedProperty(String name) |
233 | |
{ |
234 | 86 | Object property = getConfiguration().getProperty(name); |
235 | |
|
236 | 86 | if (property == null) |
237 | |
{ |
238 | 2 | throw new IllegalArgumentException("Property '" + name |
239 | |
+ "' does not exist."); |
240 | |
} |
241 | |
|
242 | 84 | return (property instanceof List) || property.getClass().isArray(); |
243 | |
} |
244 | |
} |