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