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