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
75
76
77
78
79
80
81
82 private boolean checkIndexedProperty(final String name) {
83 final Object property = getConfiguration().getProperty(name);
84
85 if (property == null) {
86 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
87 }
88
89 return property instanceof List || property.getClass().isArray();
90 }
91
92 @Override
93 public boolean contains(final String name, final String key) {
94 final Configuration subset = getConfiguration().subset(name);
95 if (subset == null) {
96 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
97 }
98
99 return subset.containsKey(key);
100 }
101
102 @Override
103 public Object get(final String name) {
104 if (LOG.isTraceEnabled()) {
105 LOG.trace("get(" + name + ")");
106 }
107
108
109 Object result = getConfiguration().getProperty(name);
110 if (result == null) {
111
112 final Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
113 if (!subset.isEmpty()) {
114 result = new ConfigurationDynaBean(subset);
115 }
116 }
117
118 if (LOG.isDebugEnabled()) {
119 LOG.debug(name + "=[" + result + "]");
120 }
121
122 if (result == null) {
123 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
124 }
125 return result;
126 }
127
128 @Override
129 public Object get(final String name, final int index) {
130 if (!checkIndexedProperty(name)) {
131 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
132 }
133
134 final List<Object> list = getConfiguration().getList(name);
135 return list.get(index);
136 }
137
138 @Override
139 public Object get(final String name, final String key) {
140 final Configuration subset = getConfiguration().subset(name);
141 if (subset == null) {
142 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
143 }
144
145 return subset.getProperty(key);
146 }
147
148 @Override
149 public DynaClass getDynaClass() {
150 return new ConfigurationDynaClass(getConfiguration());
151 }
152
153 @Override
154 public void remove(final String name, final String key) {
155 final Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
156 subset.setProperty(key, null);
157 }
158
159 @Override
160 public void set(final String name, final int index, final Object value) {
161 if (!checkIndexedProperty(name) && index > 0) {
162 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
163 }
164
165 final Object property = getConfiguration().getProperty(name);
166
167 if (property instanceof List) {
168
169
170 @SuppressWarnings("unchecked")
171 final List<Object> list = (List<Object>) property;
172 list.set(index, value);
173 getConfiguration().setProperty(name, list);
174 } else if (property.getClass().isArray()) {
175 Array.set(property, index, value);
176 } else if (index == 0) {
177 getConfiguration().setProperty(name, value);
178 }
179 }
180
181 @Override
182 public void set(final String name, final Object value) {
183 if (LOG.isTraceEnabled()) {
184 LOG.trace("set(" + name + "," + value + ")");
185 }
186 Objects.requireNonNull(value, "value");
187
188 if (value instanceof Collection) {
189 final Collection<?> collection = (Collection<?>) value;
190 collection.forEach(v -> getConfiguration().addProperty(name, v));
191 } else if (value.getClass().isArray()) {
192 final int length = Array.getLength(value);
193 for (int i = 0; i < length; i++) {
194 getConfiguration().addProperty(name, Array.get(value, i));
195 }
196 } else {
197 getConfiguration().setProperty(name, value);
198 }
199 }
200
201 @Override
202 public void set(final String name, final String key, final Object value) {
203 getConfiguration().setProperty(name + "." + key, value);
204 }
205 }