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