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