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

Classes in this File | Line Coverage | Branch Coverage | Complexity
ConfigurationDynaClass |
87% | 41/47
94% | 15/16
| 4.4;4,4

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.ArrayList;
22 |  |
     import java.util.List;
23 |  |
24 |  |
     import org.apache.commons.beanutils.DynaBean;
25 |  |
     import org.apache.commons.beanutils.DynaClass;
26 |  |
     import org.apache.commons.beanutils.DynaProperty;
27 |  |
     import org.apache.commons.configuration.Configuration;
28 |  |
     import org.apache.commons.logging.Log;
29 |  |
     import org.apache.commons.logging.LogFactory;
30 |  |
31 |  |
     /**
32 |  |
      * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
33 |  |
      * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
34 |  |
      * {@link org.apache.commons.configuration.Configuration} instance.
35 |  |
      *
36 |  |
      * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
37 |  |
      * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
38 |  |
      * @since 1.0-rc1
39 |  |
      */
40 | 9 |
     public class ConfigurationDynaClass implements DynaClass
41 |  |
     {
42 |  |
         /** The logger.*/
43 | 1 |
         private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
44 |  |
45 |  |
         /** Stores the associated configuration.*/
46 |  |
         private Configuration configuration;
47 |  |
48 |  |
         /**
49 |  |
          * Construct an instance of a <code>ConfigurationDynaClass</code>
50 |  |
          * wrapping the specified <code>Configuration</code> instance.
51 |  |
          * @param configuration <code>Configuration</code> instance.
52 |  |
          */
53 |  |
         public ConfigurationDynaClass(Configuration configuration)
54 |  |
         {
55 | 11 |
             super();
56 | 11 |
             if (log.isTraceEnabled())
57 |  |
             {
58 | 0 |
                 log.trace("ConfigurationDynaClass(" + configuration + ")");
59 |  |
             }
60 | 11 |
             this.configuration = configuration;
61 | 11 |
         }
62 |  |
63 |  |
         /**
64 |  |
          * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
65 |  |
          */
66 |  |
         public DynaProperty getDynaProperty(String name)
67 |  |
         {
68 | 36 |
             if (log.isTraceEnabled())
69 |  |
             {
70 | 0 |
                 log.trace("getDynaProperty(" + name + ")");
71 |  |
             }
72 |  |
73 | 36 |
             if (name == null)
74 |  |
             {
75 | 1 |
                 throw new IllegalArgumentException("No such property name=[" + name + "]");
76 |  |
             }
77 |  |
78 | 35 |
             Object value = configuration.getProperty(name);
79 | 35 |
             if (value == null)
80 |  |
             {
81 | 1 |
                 return null;
82 |  |
             }
83 |  |
             else
84 |  |
             {
85 | 34 |
                 Class type = value.getClass();
86 |  |
87 | 34 |
                 if (type == Byte.class)
88 |  |
                 {
89 | 1 |
                     type = Byte.TYPE;
90 |  |
                 }
91 | 34 |
                 if (type == Character.class)
92 |  |
                 {
93 | 1 |
                     type = Character.TYPE;
94 |  |
                 }
95 | 33 |
                 else if (type == Boolean.class)
96 |  |
                 {
97 | 4 |
                     type = Boolean.TYPE;
98 |  |
                 }
99 | 29 |
                 else if (type == Double.class)
100 |  |
                 {
101 | 2 |
                     type = Double.TYPE;
102 |  |
                 }
103 | 27 |
                 else if (type == Float.class)
104 |  |
                 {
105 | 2 |
                     type = Float.TYPE;
106 |  |
                 }
107 | 25 |
                 else if (type == Integer.class)
108 |  |
                 {
109 | 3 |
                     type = Integer.TYPE;
110 |  |
                 }
111 | 22 |
                 else if (type == Long.class)
112 |  |
                 {
113 | 2 |
                     type = Long.TYPE;
114 |  |
                 }
115 | 20 |
                 else if (type == Short.class)
116 |  |
                 {
117 | 2 |
                     type = Short.TYPE;
118 |  |
                 }
119 |  |
120 | 34 |
                 return new DynaProperty(name, type);
121 |  |
             }
122 |  |
         }
123 |  |
124 |  |
         /**
125 |  |
          * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
126 |  |
          */
127 |  |
         public DynaProperty[] getDynaProperties()
128 |  |
         {
129 | 1 |
             if (log.isTraceEnabled())
130 |  |
             {
131 | 0 |
                 log.trace("getDynaProperties()");
132 |  |
             }
133 |  |
134 | 1 |
             Iterator keys = configuration.getKeys();
135 | 1 |
             List properties = new ArrayList();
136 | 28 |
             while (keys.hasNext())
137 |  |
             {
138 | 26 |
                 String key = (String) keys.next();
139 | 26 |
                 DynaProperty property = getDynaProperty(key);
140 | 26 |
                 properties.add(property);
141 |  |
             }
142 |  |
143 | 1 |
             DynaProperty[] propertyArray = new DynaProperty[properties.size()];
144 | 1 |
             properties.toArray(propertyArray);
145 | 1 |
             if (log.isDebugEnabled())
146 |  |
             {
147 | 0 |
                 log.debug("Found " + properties.size() + " properties.");
148 |  |
             }
149 |  |
150 | 1 |
             return propertyArray;
151 |  |
         }
152 |  |
153 |  |
         /**
154 |  |
          * @see org.apache.commons.beanutils.DynaClass#getName()
155 |  |
          */
156 |  |
         public String getName()
157 |  |
         {
158 | 0 |
             return ConfigurationDynaBean.class.getName();
159 |  |
         }
160 |  |
161 |  |
         /**
162 |  |
          * @see org.apache.commons.beanutils.DynaClass#newInstance()
163 |  |
          */
164 |  |
         public DynaBean newInstance() throws IllegalAccessException, InstantiationException
165 |  |
         {
166 | 0 |
             return new ConfigurationDynaBean(configuration);
167 |  |
         }
168 |  |
169 |  |
     }