View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.configuration.SubsetConfiguration;
29  import org.apache.commons.lang.BooleanUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
35   * configurations properties from a wrapped configuration-collection
36   * {@link org.apache.commons.configuration.Configuration} instance. It also
37   * implements a {@link java.util.Map} interface so that it can be used in
38   * JSP 2.0 Expression Language expressions.
39   *
40   * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
41   * to the appropriate <code>Configuration</code> subset using the
42   * {@link org.apache.commons.configuration.Configuration#subset}
43   * method. Similarly, indexed properties reference lists of configuration
44   * properties using the
45   * {@link org.apache.commons.configuration.Configuration#getList(String)}
46   * method. Setting an indexed property always throws an exception.</p>
47   *
48   * <p>Note: Some of the methods expect that a dot (&quot;.&quot;) is used as
49   * property delimitor for the wrapped configuration. This is true for most of
50   * the default configurations. Hierarchical configurations, for which a specific
51   * expression engine is set, may cause problems.</p>
52   *
53   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
54   * @version $Revision: 492216 $, $Date: 2007-01-03 17:51:24 +0100 (Mi, 03 Jan 2007) $
55   * @since 1.0-rc1
56   */
57  public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
58  {
59      /*** Constant for the property delimiter.*/
60      private static final String PROPERTY_DELIMITER = ".";
61  
62      /*** The logger.*/
63      private static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
64  
65      /***
66       * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
67       * the configuration this bean is associated with.
68       * @param configuration the configuration
69       */
70      public ConfigurationDynaBean(Configuration configuration)
71      {
72          super(configuration);
73          if (log.isTraceEnabled())
74          {
75              log.trace("ConfigurationDynaBean(" + configuration + ")");
76          }
77      }
78  
79      public void set(String name, Object value)
80      {
81          if (log.isTraceEnabled())
82          {
83              log.trace("set(" + name + "," + value + ")");
84          }
85  
86          if (value == null)
87          {
88              throw new NullPointerException("Error trying to set property to null.");
89          }
90  
91          if (value instanceof List)
92          {
93              List list = (List) value;
94              Iterator iterator = list.iterator();
95              while (iterator.hasNext())
96              {
97                  getConfiguration().addProperty(name, iterator.next());
98              }
99          }
100         else if (value instanceof int[])
101         {
102             int[] array = (int[]) value;
103             for (int i = 0; i < array.length; i++)
104             {
105                 getConfiguration().addProperty(name, new Integer(array[i]));
106             }
107         }
108         else if (value instanceof boolean[])
109         {
110             boolean[] array = (boolean[]) value;
111             for (int i = 0; i < array.length; i++)
112             {
113                 getConfiguration().addProperty(name, BooleanUtils.toBooleanObject(array[i]));
114             }
115         }
116         else if (value instanceof char[])
117         {
118             char[] array = (char[]) value;
119             for (int i = 0; i < array.length; i++)
120             {
121                 getConfiguration().addProperty(name, new Character(array[i]));
122             }
123         }
124         else if (value instanceof byte[])
125         {
126             byte[] array = (byte[]) value;
127             for (int i = 0; i < array.length; i++)
128             {
129                 getConfiguration().addProperty(name, new Byte(array[i]));
130             }
131         }
132         else if (value instanceof short[])
133         {
134             short[] array = (short[]) value;
135             for (int i = 0; i < array.length; i++)
136             {
137                 getConfiguration().addProperty(name, new Short(array[i]));
138             }
139         }
140         else if (value instanceof long[])
141         {
142             long[] array = (long[]) value;
143             for (int i = 0; i < array.length; i++)
144             {
145                 getConfiguration().addProperty(name, new Long(array[i]));
146             }
147         }
148         else if (value instanceof float[])
149         {
150             float[] array = (float[]) value;
151             for (int i = 0; i < array.length; i++)
152             {
153                 getConfiguration().addProperty(name, new Float(array[i]));
154             }
155         }
156         else if (value instanceof double[])
157         {
158             double[] array = (double[]) value;
159             for (int i = 0; i < array.length; i++)
160             {
161                 getConfiguration().addProperty(name, new Double(array[i]));
162             }
163         }
164         else if (value instanceof Object[])
165         {
166             Object[] array = (Object[]) value;
167             for (int i = 0; i < array.length; i++)
168             {
169                 getConfiguration().addProperty(name, array[i]);
170             }
171         }
172         else
173         {
174             getConfiguration().setProperty(name, value);
175         }
176     }
177 
178     public Object get(String name)
179     {
180         if (log.isTraceEnabled())
181         {
182             log.trace("get(" + name + ")");
183         }
184 
185         // get configuration property
186         Object result = getConfiguration().getProperty(name);
187         if (result == null)
188         {
189             // otherwise attempt to create bean from configuration subset
190             Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
191             if (!subset.isEmpty())
192             {
193                 result = new ConfigurationDynaBean(subset);
194             }
195         }
196 
197         if (log.isDebugEnabled())
198         {
199             log.debug(name + "=[" + result + "]");
200         }
201 
202         if (result == null)
203         {
204             throw new IllegalArgumentException("Property '" + name + "' does not exist.");
205         }
206         return result;
207     }
208 
209     public boolean contains(String name, String key)
210     {
211         Configuration subset = getConfiguration().subset(name);
212         if (subset == null)
213         {
214             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
215         }
216 
217         return subset.containsKey(key);
218     }
219 
220     public Object get(String name, int index)
221     {
222         try
223         {
224             List list = getConfiguration().getList(name);
225             if (list.isEmpty())
226             {
227                 throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
228             }
229 
230             return list.get(index);
231         }
232         catch (ConversionException e)
233         {
234             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
235         }
236     }
237 
238     public Object get(String name, String key)
239     {
240         Configuration subset = getConfiguration().subset(name);
241         if (subset == null)
242         {
243             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
244         }
245 
246         return subset.getProperty(key);
247     }
248 
249     public DynaClass getDynaClass()
250     {
251         return new ConfigurationDynaClass(getConfiguration());
252     }
253 
254     public void remove(String name, String key)
255     {
256         Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
257         subset.setProperty(key, null);
258     }
259 
260     public void set(String name, int index, Object value)
261     {
262         try
263         {
264             Object property = getConfiguration().getProperty(name);
265 
266             if (property == null)
267             {
268                 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
269             }
270             else if (property instanceof List)
271             {
272                 List list = (List) property;
273                 list.set(index, value);
274                 getConfiguration().setProperty(name, list);
275             }
276             else if (property.getClass().isArray())
277             {
278                 Object[] array = (Object[]) property;
279                 array[index] = value;
280             }
281             else if (index == 0)
282             {
283                 getConfiguration().setProperty(name, value);
284             }
285             else
286             {
287                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
288             }
289         }
290         catch (ConversionException e)
291         {
292             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
293         }
294     }
295 
296     public void set(String name, String key, Object value)
297     {
298         getConfiguration().setProperty(name + "." + key, value);
299     }
300 }