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 * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
36 * configurations properties from a wrapped configuration-collection
37 * {@link org.apache.commons.configuration.Configuration} instance. It also
38 * implements a {@link java.util.Map} interface so that it can be used in
39 * JSP 2.0 Expression Language expressions.
40 *
41 * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
42 * to the appropriate <code>Configuration</code> subset using the
43 * {@link org.apache.commons.configuration.Configuration#subset}
44 * method. Similarly, indexed properties reference lists of configuration
45 * properties using the
46 * {@link org.apache.commons.configuration.Configuration#getList(String)}
47 * method. Setting an indexed property always throws an exception.</p>
48 *
49 * <p>Note: Some of the methods expect that a dot (".") is used as
50 * property delimitor for the wrapped configuration. This is true for most of
51 * the default configurations. Hierarchical configurations, for which a specific
52 * expression engine is set, may cause problems.</p>
53 *
54 * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
55 * @version $Revision: 532554 $, $Date: 2007-04-26 02:40:17 +0200 (Do, 26 Apr 2007) $
56 * @since 1.0-rc1
57 */
58 public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
59 {
60 /*** Constant for the property delimiter.*/
61 private static final String PROPERTY_DELIMITER = ".";
62
63 /*** The logger.*/
64 private static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
65
66 /***
67 * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
68 * the configuration this bean is associated with.
69 *
70 * @param configuration the configuration
71 */
72 public ConfigurationDynaBean(Configuration configuration)
73 {
74 super(configuration);
75 if (log.isTraceEnabled())
76 {
77 log.trace("ConfigurationDynaBean(" + configuration + ")");
78 }
79 }
80
81 public void set(String name, Object value)
82 {
83 if (log.isTraceEnabled())
84 {
85 log.trace("set(" + name + "," + value + ")");
86 }
87
88 if (value == null)
89 {
90 throw new NullPointerException("Error trying to set property to null.");
91 }
92
93 if (value instanceof Collection)
94 {
95 Collection collection = (Collection) value;
96 Iterator iterator = collection.iterator();
97 while (iterator.hasNext())
98 {
99 getConfiguration().addProperty(name, iterator.next());
100 }
101 }
102 else if (value.getClass().isArray())
103 {
104 int length = Array.getLength(value);
105 for (int i = 0; i < length; i++)
106 {
107 getConfiguration().addProperty(name, Array.get(value, i));
108 }
109 }
110 else
111 {
112 getConfiguration().setProperty(name, value);
113 }
114 }
115
116 public Object get(String name)
117 {
118 if (log.isTraceEnabled())
119 {
120 log.trace("get(" + name + ")");
121 }
122
123
124 Object result = getConfiguration().getProperty(name);
125 if (result == null)
126 {
127
128 Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
129 if (!subset.isEmpty())
130 {
131 result = new ConfigurationDynaBean(subset);
132 }
133 }
134
135 if (log.isDebugEnabled())
136 {
137 log.debug(name + "=[" + result + "]");
138 }
139
140 if (result == null)
141 {
142 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
143 }
144 return result;
145 }
146
147 public boolean contains(String name, String key)
148 {
149 Configuration subset = getConfiguration().subset(name);
150 if (subset == null)
151 {
152 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
153 }
154
155 return subset.containsKey(key);
156 }
157
158 public Object get(String name, int index)
159 {
160 try
161 {
162 List list = getConfiguration().getList(name);
163 if (list.isEmpty())
164 {
165 throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
166 }
167
168 return list.get(index);
169 }
170 catch (ConversionException e)
171 {
172 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
173 }
174 }
175
176 public Object get(String name, String key)
177 {
178 Configuration subset = getConfiguration().subset(name);
179 if (subset == null)
180 {
181 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
182 }
183
184 return subset.getProperty(key);
185 }
186
187 public DynaClass getDynaClass()
188 {
189 return new ConfigurationDynaClass(getConfiguration());
190 }
191
192 public void remove(String name, String key)
193 {
194 Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
195 subset.setProperty(key, null);
196 }
197
198 public void set(String name, int index, Object value)
199 {
200 try
201 {
202 Object property = getConfiguration().getProperty(name);
203
204 if (property == null)
205 {
206 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
207 }
208 else if (property instanceof List)
209 {
210 List list = (List) property;
211 list.set(index, value);
212 getConfiguration().setProperty(name, list);
213 }
214 else if (property.getClass().isArray())
215 {
216 Array.set(value, index, value);
217 }
218 else if (index == 0)
219 {
220 getConfiguration().setProperty(name, value);
221 }
222 else
223 {
224 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
225 }
226 }
227 catch (ConversionException e)
228 {
229 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
230 }
231 }
232
233 public void set(String name, String key, Object value)
234 {
235 getConfiguration().setProperty(name + "." + key, value);
236 }
237 }