View Javadoc

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