View Javadoc

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