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

Classes in this File | Line Coverage | Branch Coverage | Complexity
ConfigurationDynaBean |
77% | 57/74
89% | 17/19
| 4.1;4,1

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.lang.reflect.Array;
21 |  |
     import java.util.Collection;
22 |  |
     import java.util.Iterator;
23 |  |
     import java.util.List;
24 |  |
25 |  |
     import org.apache.commons.beanutils.DynaBean;
26 |  |
     import org.apache.commons.beanutils.DynaClass;
27 |  |
     import org.apache.commons.configuration.Configuration;
28 |  |
     import org.apache.commons.configuration.ConfigurationMap;
29 |  |
     import org.apache.commons.configuration.ConversionException;
30 |  |
     import org.apache.commons.configuration.SubsetConfiguration;
31 |  |
     import org.apache.commons.logging.Log;
32 |  |
     import org.apache.commons.logging.LogFactory;
33 |  |
34 |  |
     /**
35 |  |
      * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
36 |  |
      * configurations properties from a wrapped configuration-collection
37 |  |
      * {@link org.apache.commons.configuration.Configuration} instance. It also
38 |  |
      * implements a {@link java.util.Map} interface so that it can be used in
39 |  |
      * JSP 2.0 Expression Language expressions.
40 |  |
      *
41 |  |
      * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
42 |  |
      * to the appropriate <code>Configuration</code> subset using the
43 |  |
      * {@link org.apache.commons.configuration.Configuration#subset}
44 |  |
      * method. Similarly, indexed properties reference lists of configuration
45 |  |
      * properties using the
46 |  |
      * {@link org.apache.commons.configuration.Configuration#getList(String)}
47 |  |
      * method. Setting an indexed property always throws an exception.</p>
48 |  |
      *
49 |  |
      * <p>Note: Some of the methods expect that a dot (&quot;.&quot;) is used as
50 |  |
      * property delimitor for the wrapped configuration. This is true for most of
51 |  |
      * the default configurations. Hierarchical configurations, for which a specific
52 |  |
      * expression engine is set, may cause problems.</p>
53 |  |
      *
54 |  |
      * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
55 |  |
      * @version $Revision: 532554 $, $Date: 2007-04-26 02:40:17 +0200 (Do, 26 Apr 2007) $
56 |  |
      * @since 1.0-rc1
57 |  |
      */
58 |  |
     public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
59 |  |
     {
60 |  |
         /** Constant for the property delimiter.*/
61 |  |
         private static final String PROPERTY_DELIMITER = ".";
62 |  |
63 |  |
         /** The logger.*/
64 | 4 |
         private static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
65 |  |
66 |  |
         /**
67 |  |
          * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
68 |  |
          * the configuration this bean is associated with.
69 |  |
          *
70 |  |
          * @param configuration the configuration
71 |  |
          */
72 |  |
         public ConfigurationDynaBean(Configuration configuration)
73 |  |
         {
74 | 78 |
             super(configuration);
75 | 78 |
             if (log.isTraceEnabled())
76 |  |
             {
77 | 0 |
                 log.trace("ConfigurationDynaBean(" + configuration + ")");
78 |  |
             }
79 | 78 |
         }
80 |  |
81 |  |
         public void set(String name, Object value)
82 |  |
         {
83 | 778 |
             if (log.isTraceEnabled())
84 |  |
             {
85 | 0 |
                 log.trace("set(" + name + "," + value + ")");
86 |  |
             }
87 |  |
88 | 778 |
             if (value == null)
89 |  |
             {
90 | 2 |
                 throw new NullPointerException("Error trying to set property to null.");
91 |  |
             }
92 |  |
93 | 776 |
             if (value instanceof Collection)
94 |  |
             {
95 | 76 |
                 Collection collection = (Collection) value;
96 | 76 |
                 Iterator iterator = collection.iterator();
97 | 456 |
                 while (iterator.hasNext())
98 |  |
                 {
99 | 380 |
                     getConfiguration().addProperty(name, iterator.next());
100 | 380 |
                 }
101 | 76 |
             }
102 | 700 |
             else if (value.getClass().isArray())
103 |  |
             {
104 | 684 |
                 int length = Array.getLength(value);
105 | 4104 |
                 for (int i = 0; i < length; i++)
106 |  |
                 {
107 | 3420 |
                     getConfiguration().addProperty(name, Array.get(value, i));
108 |  |
                 }
109 | 684 |
             }
110 |  |
             else
111 |  |
             {
112 | 16 |
                 getConfiguration().setProperty(name, value);
113 |  |
             }
114 | 776 |
         }
115 |  |
116 |  |
         public Object get(String name)
117 |  |
         {
118 | 52 |
             if (log.isTraceEnabled())
119 |  |
             {
120 | 0 |
                 log.trace("get(" + name + ")");
121 |  |
             }
122 |  |
123 |  |
             // get configuration property
124 | 52 |
             Object result = getConfiguration().getProperty(name);
125 | 52 |
             if (result == null)
126 |  |
             {
127 |  |
                 // otherwise attempt to create bean from configuration subset
128 | 6 |
                 Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
129 | 6 |
                 if (!subset.isEmpty())
130 |  |
                 {
131 | 2 |
                     result = new ConfigurationDynaBean(subset);
132 |  |
                 }
133 |  |
             }
134 |  |
135 | 52 |
             if (log.isDebugEnabled())
136 |  |
             {
137 | 0 |
                 log.debug(name + "=[" + result + "]");
138 |  |
             }
139 |  |
140 | 52 |
             if (result == null)
141 |  |
             {
142 | 4 |
                 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
143 |  |
             }
144 | 48 |
             return result;
145 |  |
         }
146 |  |
147 |  |
         public boolean contains(String name, String key)
148 |  |
         {
149 | 12 |
             Configuration subset = getConfiguration().subset(name);
150 | 12 |
             if (subset == null)
151 |  |
             {
152 | 0 |
                 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
153 |  |
             }
154 |  |
155 | 12 |
             return subset.containsKey(key);
156 |  |
         }
157 |  |
158 |  |
         public Object get(String name, int index)
159 |  |
         {
160 |  |
             try
161 |  |
             {
162 | 64 |
                 List list = getConfiguration().getList(name);
163 | 62 |
                 if (list.isEmpty())
164 |  |
                 {
165 | 0 |
                     throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
166 |  |
                 }
167 |  |
168 | 62 |
                 return list.get(index);
169 |  |
             }
170 | 2 |
             catch (ConversionException e)
171 |  |
             {
172 | 2 |
                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
173 |  |
             }
174 |  |
         }
175 |  |
176 |  |
         public Object get(String name, String key)
177 |  |
         {
178 | 12 |
             Configuration subset = getConfiguration().subset(name);
179 | 12 |
             if (subset == null)
180 |  |
             {
181 | 0 |
                 throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
182 |  |
             }
183 |  |
184 | 12 |
             return subset.getProperty(key);
185 |  |
         }
186 |  |
187 |  |
         public DynaClass getDynaClass()
188 |  |
         {
189 | 22 |
             return new ConfigurationDynaClass(getConfiguration());
190 |  |
         }
191 |  |
192 |  |
         public void remove(String name, String key)
193 |  |
         {
194 | 4 |
             Configuration subset = new SubsetConfiguration(getConfiguration(), name, PROPERTY_DELIMITER);
195 | 4 |
             subset.setProperty(key, null);
196 | 4 |
         }
197 |  |
198 |  |
         public void set(String name, int index, Object value)
199 |  |
         {
200 |  |
             try
201 |  |
             {
202 | 12 |
                 Object property = getConfiguration().getProperty(name);
203 |  |
204 | 12 |
                 if (property == null)
205 |  |
                 {
206 | 0 |
                     throw new IllegalArgumentException("Property '" + name + "' does not exist.");
207 |  |
                 }
208 | 12 |
                 else if (property instanceof List)
209 |  |
                 {
210 | 12 |
                     List list = (List) property;
211 | 12 |
                     list.set(index, value);
212 | 10 |
                     getConfiguration().setProperty(name, list);
213 | 10 |
                 }
214 | 0 |
                 else if (property.getClass().isArray())
215 |  |
                 {
216 | 0 |
                     Array.set(value, index, value);
217 | 0 |
                 }
218 | 0 |
                 else if (index == 0)
219 |  |
                 {
220 | 0 |
                     getConfiguration().setProperty(name, value);
221 | 0 |
                 }
222 |  |
                 else
223 |  |
                 {
224 | 0 |
                     throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
225 |  |
                 }
226 |  |
             }
227 | 0 |
             catch (ConversionException e)
228 |  |
             {
229 | 0 |
                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
230 | 10 |
             }
231 | 10 |
         }
232 |  |
233 |  |
         public void set(String name, String key, Object value)
234 |  |
         {
235 | 4 |
             getConfiguration().setProperty(name + "." + key, value);
236 | 4 |
         }
237 |  |
     }