##### Coverage Report - org.apache.commons.configuration.beanutils.ConfigurationDynaBean

Classes in this File | Line Coverage | Branch Coverage | Complexity
ConfigurationDynaBean |
84% | 85/101
94% | 34/36
| 5.9;5,9

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 | 1 |
     public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
52 |  |
     {
53 |  |
         /** The logger.*/
54 | 1 |
         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 | 39 |
             super(configuration);
64 | 39 |
             if (log.isTraceEnabled())
65 |  |
             {
66 | 0 |
                 log.trace("ConfigurationDynaBean(" + configuration + ")");
67 |  |
             }
68 | 39 |
         }
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 | 389 |
             if (log.isTraceEnabled())
76 |  |
             {
77 | 0 |
                 log.trace("set(" + name + "," + value + ")");
78 |  |
             }
79 |  |
80 | 389 |
             if (value == null)
81 |  |
             {
82 | 1 |
                 throw new NullPointerException("Error trying to set property to null.");
83 |  |
             }
84 |  |
85 | 388 |
             if (value instanceof List)
86 |  |
             {
87 | 38 |
                 List list = (List) value;
88 | 38 |
                 Iterator iterator = list.iterator();
89 | 266 |
                 while (iterator.hasNext())
90 |  |
                 {
91 | 190 |
                     getConfiguration().addProperty(name, iterator.next());
92 |  |
                 }
93 |  |
             }
94 | 350 |
             else if (value instanceof int[])
95 |  |
             {
96 | 38 |
                 int[] array = (int[]) value;
97 | 228 |
                 for (int i = 0; i < array.length; i++)
98 |  |
                 {
99 | 190 |
                     getConfiguration().addProperty(name, new Integer(array[i]));
100 |  |
                 }
101 |  |
             }
102 | 312 |
             else if (value instanceof boolean[])
103 |  |
             {
104 | 38 |
                 boolean[] array = (boolean[]) value;
105 | 228 |
                 for (int i = 0; i < array.length; i++)
106 |  |
                 {
107 | 190 |
                     getConfiguration().addProperty(name, BooleanUtils.toBooleanObject(array[i]));
108 |  |
                 }
109 |  |
             }
110 | 274 |
             else if (value instanceof char[])
111 |  |
             {
112 | 38 |
                 char[] array = (char[]) value;
113 | 228 |
                 for (int i = 0; i < array.length; i++)
114 |  |
                 {
115 | 190 |
                     getConfiguration().addProperty(name, new Character(array[i]));
116 |  |
                 }
117 |  |
             }
118 | 236 |
             else if (value instanceof byte[])
119 |  |
             {
120 | 38 |
                 byte[] array = (byte[]) value;
121 | 228 |
                 for (int i = 0; i < array.length; i++)
122 |  |
                 {
123 | 190 |
                     getConfiguration().addProperty(name, new Byte(array[i]));
124 |  |
                 }
125 |  |
             }
126 | 198 |
             else if (value instanceof short[])
127 |  |
             {
128 | 38 |
                 short[] array = (short[]) value;
129 | 228 |
                 for (int i = 0; i < array.length; i++)
130 |  |
                 {
131 | 190 |
                     getConfiguration().addProperty(name, new Short(array[i]));
132 |  |
                 }
133 |  |
             }
134 | 160 |
             else if (value instanceof long[])
135 |  |
             {
136 | 38 |
                 long[] array = (long[]) value;
137 | 228 |
                 for (int i = 0; i < array.length; i++)
138 |  |
                 {
139 | 190 |
                     getConfiguration().addProperty(name, new Long(array[i]));
140 |  |
                 }
141 |  |
             }
142 | 122 |
             else if (value instanceof float[])
143 |  |
             {
144 | 38 |
                 float[] array = (float[]) value;
145 | 228 |
                 for (int i = 0; i < array.length; i++)
146 |  |
                 {
147 | 190 |
                     getConfiguration().addProperty(name, new Float(array[i]));
148 |  |
                 }
149 |  |
             }
150 | 84 |
             else if (value instanceof double[])
151 |  |
             {
152 | 38 |
                 double[] array = (double[]) value;
153 | 228 |
                 for (int i = 0; i < array.length; i++)
154 |  |
                 {
155 | 190 |
                     getConfiguration().addProperty(name, new Double(array[i]));
156 |  |
                 }
157 |  |
             }
158 | 46 |
             else if (value instanceof Object[])
159 |  |
             {
160 | 38 |
                 Object[] array = (Object[]) value;
161 | 228 |
                 for (int i = 0; i < array.length; i++)
162 |  |
                 {
163 | 190 |
                     getConfiguration().addProperty(name, array[i]);
164 |  |
                 }
165 |  |
             }
166 |  |
             else
167 |  |
             {
168 | 8 |
                 getConfiguration().setProperty(name, value);
169 |  |
             }
170 | 388 |
         }
171 |  |
172 |  |
         /**
173 |  |
          * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String)
174 |  |
          */
175 |  |
         public Object get(String name)
176 |  |
         {
177 | 26 |
             if (log.isTraceEnabled())
178 |  |
             {
179 | 0 |
                 log.trace("get(" + name + ")");
180 |  |
             }
181 |  |
182 |  |
             // get configuration property
183 | 26 |
             Object result = getConfiguration().getProperty(name);
184 | 26 |
             if (result == null)
185 |  |
             {
186 |  |
                 // otherwise attempt to create bean from configuration subset
187 | 3 |
                 Configuration subset = getConfiguration().subset(name);
188 | 3 |
                 if (!subset.isEmpty())
189 |  |
                 {
190 | 1 |
                     result = new ConfigurationDynaBean(getConfiguration().subset(name));
191 |  |
                 }
192 |  |
             }
193 |  |
194 | 26 |
             if (log.isDebugEnabled())
195 |  |
             {
196 | 0 |
                 log.debug(name + "=[" + result + "]");
197 |  |
             }
198 |  |
199 | 26 |
             if (result == null)
200 |  |
             {
201 | 2 |
                 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
202 |  |
             }
203 | 24 |
             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 | 6 |
             Configuration subset = getConfiguration().subset(name);
212 | 6 |
             if (subset == null)
213 |  |
             {
214 | 0 |
                 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
215 |  |
             }
216 |  |
217 | 6 |
             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 | 32 |
                 List list = getConfiguration().getList(name);
228 | 31 |
                 if (list.isEmpty())
229 |  |
                 {
230 | 0 |
                     throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
231 |  |
                 }
232 |  |
233 | 31 |
                 return list.get(index);
234 |  |
             }
235 |  |
             catch (ConversionException e)
236 |  |
             {
237 | 1 |
                 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 | 6 |
             Configuration subset = getConfiguration().subset(name);
247 | 6 |
             if (subset == null)
248 |  |
             {
249 | 0 |
                 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
250 |  |
             }
251 |  |
252 | 6 |
             return subset.getProperty(key);
253 |  |
         }
254 |  |
255 |  |
         /**
256 |  |
          * @see org.apache.commons.beanutils.DynaBean#getDynaClass()
257 |  |
          */
258 |  |
         public DynaClass getDynaClass()
259 |  |
         {
260 | 11 |
             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 | 2 |
             Configuration subset = getConfiguration().subset(name);
269 | 2 |
             if (subset == null)
270 |  |
             {
271 | 0 |
                 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
272 |  |
             }
273 | 2 |
             subset.setProperty(key, null);
274 | 2 |
         }
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 | 6 |
                 Object property = getConfiguration().getProperty(name);
284 |  |
285 | 6 |
                 if (property == null)
286 |  |
                 {
287 | 0 |
                     throw new IllegalArgumentException("Property '" + name + "' does not exist.");
288 |  |
                 }
289 | 6 |
                 else if (property instanceof List)
290 |  |
                 {
291 | 6 |
                     List list = (List) property;
292 | 6 |
                     list.set(index, value);
293 |  |
                 }
294 | 0 |
                 else if (property.getClass().isArray())
295 |  |
                 {
296 | 0 |
                     Object[] array = (Object[]) property;
297 | 0 |
                     array[index] = value;
298 |  |
                 }
299 | 0 |
                 else if (index == 0)
300 |  |
                 {
301 | 0 |
                     getConfiguration().setProperty(name, value);
302 |  |
                 }
303 |  |
                 else
304 |  |
                 {
305 | 0 |
                     throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
306 |  |
                 }
307 | 5 |
             }
308 |  |
             catch (ConversionException e)
309 |  |
             {
310 | 0 |
                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
311 |  |
             }
312 | 5 |
         }
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 | 2 |
             getConfiguration().setProperty(name + "." + key, value);
320 | 2 |
         }
321 |  |
322 |  |
     }