1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2.beanutils;
19
20 import java.lang.reflect.Array;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Objects;
24
25 import org.apache.commons.beanutils.DynaBean;
26 import org.apache.commons.beanutils.DynaClass;
27 import org.apache.commons.configuration2.Configuration;
28 import org.apache.commons.configuration2.ConfigurationMap;
29 import org.apache.commons.configuration2.SubsetConfiguration;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean {
55
56
57 private static final String PROPERTY_DELIMITER = ".";
58
59
60 private static final Log LOG = LogFactory.getLog(ConfigurationDynaBean.class);
61
62
63
64
65
66
67 public ConfigurationDynaBean(final Configuration configuration) {
68 super(configuration);
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("ConfigurationDynaBean(" + configuration + ")");
71 }
72 }
73
74 @Override
75 public void set(final String name, final Object value) {
76 if (LOG.isTraceEnabled()) {
77 LOG.trace("set(" + name + "," + value + ")");
78 }
79 Objects.requireNonNull(value, "Error trying to set property to null.");
80
81 if (value instanceof Collection) {
82 final Collection<?> collection = (Collection<?>) value;
83 collection.forEach(v -> getConfiguration().addProperty(name, v));
84 } else if (value.getClass().isArray()) {
85 final int length = Array.getLength(value);
86 for (int i = 0; i < length; i++) {
87 getConfiguration().addProperty(name, Array.get(value, i));
88 }
89 } else {
90 getConfiguration().setProperty(name, value);
91 }
92 }
93
94 @Override
95 public Object get(final String name) {
96 if (LOG.isTraceEnabled()) {
97 LOG.trace("get(" + name + ")");
98 }
99
100
101 Object result = getConfiguration().getProperty(name);
102 if (result == null) {
103
104 final Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
105 if (!subset.isEmpty()) {
106 result = new ConfigurationDynaBean(subset);
107 }
108 }
109
110 if (LOG.isDebugEnabled()) {
111 LOG.debug(name + "=[" + result + "]");
112 }
113
114 if (result == null) {
115 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
116 }
117 return result;
118 }
119
120 @Override
121 public boolean contains(final String name, final String key) {
122 final Configuration subset = getConfiguration().subset(name);
123 if (subset == null) {
124 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
125 }
126
127 return subset.containsKey(key);
128 }
129
130 @Override
131 public Object get(final String name, final int index) {
132 if (!checkIndexedProperty(name)) {
133 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
134 }
135
136 final List<Object> list = getConfiguration().getList(name);
137 return list.get(index);
138 }
139
140 @Override
141 public Object get(final String name, final String key) {
142 final Configuration subset = getConfiguration().subset(name);
143 if (subset == null) {
144 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
145 }
146
147 return subset.getProperty(key);
148 }
149
150 @Override
151 public DynaClass getDynaClass() {
152 return new ConfigurationDynaClass(getConfiguration());
153 }
154
155 @Override
156 public void remove(final String name, final String key) {
157 final Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
158 subset.setProperty(key, null);
159 }
160
161 @Override
162 public void set(final String name, final int index, final Object value) {
163 if (!checkIndexedProperty(name) && index > 0) {
164 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
165 }
166
167 final Object property = getConfiguration().getProperty(name);
168
169 if (property instanceof List) {
170
171
172 @SuppressWarnings("unchecked")
173 final List<Object> list = (List<Object>) property;
174 list.set(index, value);
175 getConfiguration().setProperty(name, list);
176 } else if (property.getClass().isArray()) {
177 Array.set(property, index, value);
178 } else if (index == 0) {
179 getConfiguration().setProperty(name, value);
180 }
181 }
182
183 @Override
184 public void set(final String name, final String key, final Object value) {
185 getConfiguration().setProperty(name + "." + key, value);
186 }
187
188
189
190
191
192
193
194
195
196 private boolean checkIndexedProperty(final String name) {
197 final Object property = getConfiguration().getProperty(name);
198
199 if (property == null) {
200 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
201 }
202
203 return property instanceof List || property.getClass().isArray();
204 }
205 }