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.lang.BooleanUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /***
33   * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
34   * configurations properties from a wrapped configuration-collection
35   * {@link org.apache.commons.configuration.Configuration} instance. It also
36   * implements a {@link java.util.Map} interface so that it can be used in
37   * JSP 2.0 Expression Language expressions.
38   *
39   * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
40   * to the appropriate <code>Configuration</code> subset using the
41   * {@link org.apache.commons.configuration.Configuration#subset}
42   * method. Similarly, indexed properties reference lists of configuration
43   * properties using the
44   * {@link org.apache.commons.configuration.Configuration#getList(String)}
45   * method. Setting an indexed property always throws an exception.</p>
46   *
47   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
48   * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
49   * @since 1.0-rc1
50   */
51  public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
52  {
53      /*** The logger.*/
54      private static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
55  
56      /***
57       * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
58       * the configuration this bean is associated with.
59       * @param configuration the configuration
60       */
61      public ConfigurationDynaBean(Configuration configuration)
62      {
63          super(configuration);
64          if (log.isTraceEnabled())
65          {
66              log.trace("ConfigurationDynaBean(" + configuration + ")");
67          }
68      }
69  
70      /***
71       * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.Object)
72       */
73      public void set(String name, Object value)
74      {
75          if (log.isTraceEnabled())
76          {
77              log.trace("set(" + name + "," + value + ")");
78          }
79  
80          if (value == null)
81          {
82              throw new NullPointerException("Error trying to set property to null.");
83          }
84  
85          if (value instanceof List)
86          {
87              List list = (List) value;
88              Iterator iterator = list.iterator();
89              while (iterator.hasNext())
90              {
91                  getConfiguration().addProperty(name, iterator.next());
92              }
93          }
94          else if (value instanceof int[])
95          {
96              int[] array = (int[]) value;
97              for (int i = 0; i < array.length; i++)
98              {
99                  getConfiguration().addProperty(name, new Integer(array[i]));
100             }
101         }
102         else if (value instanceof boolean[])
103         {
104             boolean[] array = (boolean[]) value;
105             for (int i = 0; i < array.length; i++)
106             {
107                 getConfiguration().addProperty(name, BooleanUtils.toBooleanObject(array[i]));
108             }
109         }
110         else if (value instanceof char[])
111         {
112             char[] array = (char[]) value;
113             for (int i = 0; i < array.length; i++)
114             {
115                 getConfiguration().addProperty(name, new Character(array[i]));
116             }
117         }
118         else if (value instanceof byte[])
119         {
120             byte[] array = (byte[]) value;
121             for (int i = 0; i < array.length; i++)
122             {
123                 getConfiguration().addProperty(name, new Byte(array[i]));
124             }
125         }
126         else if (value instanceof short[])
127         {
128             short[] array = (short[]) value;
129             for (int i = 0; i < array.length; i++)
130             {
131                 getConfiguration().addProperty(name, new Short(array[i]));
132             }
133         }
134         else if (value instanceof long[])
135         {
136             long[] array = (long[]) value;
137             for (int i = 0; i < array.length; i++)
138             {
139                 getConfiguration().addProperty(name, new Long(array[i]));
140             }
141         }
142         else if (value instanceof float[])
143         {
144             float[] array = (float[]) value;
145             for (int i = 0; i < array.length; i++)
146             {
147                 getConfiguration().addProperty(name, new Float(array[i]));
148             }
149         }
150         else if (value instanceof double[])
151         {
152             double[] array = (double[]) value;
153             for (int i = 0; i < array.length; i++)
154             {
155                 getConfiguration().addProperty(name, new Double(array[i]));
156             }
157         }
158         else if (value instanceof Object[])
159         {
160             Object[] array = (Object[]) value;
161             for (int i = 0; i < array.length; i++)
162             {
163                 getConfiguration().addProperty(name, array[i]);
164             }
165         }
166         else
167         {
168             getConfiguration().setProperty(name, value);
169         }
170     }
171 
172     /***
173      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String)
174      */
175     public Object get(String name)
176     {
177         if (log.isTraceEnabled())
178         {
179             log.trace("get(" + name + ")");
180         }
181 
182         // get configuration property
183         Object result = getConfiguration().getProperty(name);
184         if (result == null)
185         {
186             // otherwise attempt to create bean from configuration subset
187             Configuration subset = getConfiguration().subset(name);
188             if (!subset.isEmpty())
189             {
190                 result = new ConfigurationDynaBean(getConfiguration().subset(name));
191             }
192         }
193 
194         if (log.isDebugEnabled())
195         {
196             log.debug(name + "=[" + result + "]");
197         }
198 
199         if (result == null)
200         {
201             throw new IllegalArgumentException("Property '" + name + "' does not exist.");
202         }
203         return result;
204     }
205 
206     /***
207      * @see org.apache.commons.beanutils.DynaBean#contains(java.lang.String, java.lang.String)
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     /***
221      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, int)
222      */
223     public Object get(String name, int index)
224     {
225         try
226         {
227             List list = getConfiguration().getList(name);
228             if (list.isEmpty())
229             {
230                 throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
231             }
232 
233             return list.get(index);
234         }
235         catch (ConversionException e)
236         {
237             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
238         }
239     }
240 
241     /***
242      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, java.lang.String)
243      */
244     public Object get(String name, String key)
245     {
246         Configuration subset = getConfiguration().subset(name);
247         if (subset == null)
248         {
249             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
250         }
251 
252         return subset.getProperty(key);
253     }
254 
255     /***
256      * @see org.apache.commons.beanutils.DynaBean#getDynaClass()
257      */
258     public DynaClass getDynaClass()
259     {
260         return new ConfigurationDynaClass(getConfiguration());
261     }
262 
263     /***
264      * @see org.apache.commons.beanutils.DynaBean#remove(java.lang.String, java.lang.String)
265      */
266     public void remove(String name, String key)
267     {
268         Configuration subset = getConfiguration().subset(name);
269         if (subset == null)
270         {
271             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
272         }
273         subset.setProperty(key, null);
274     }
275 
276     /***
277      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, int, java.lang.Object)
278      */
279     public void set(String name, int index, Object value)
280     {
281         try
282         {
283             Object property = getConfiguration().getProperty(name);
284 
285             if (property == null)
286             {
287                 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
288             }
289             else if (property instanceof List)
290             {
291                 List list = (List) property;
292                 list.set(index, value);
293             }
294             else if (property.getClass().isArray())
295             {
296                 Object[] array = (Object[]) property;
297                 array[index] = value;
298             }
299             else if (index == 0)
300             {
301                 getConfiguration().setProperty(name, value);
302             }
303             else
304             {
305                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
306             }
307         }
308         catch (ConversionException e)
309         {
310             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
311         }
312     }
313 
314     /***
315      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.String, java.lang.Object)
316      */
317     public void set(String name, String key, Object value)
318     {
319         getConfiguration().setProperty(name + "." + key, value);
320     }
321 
322 }