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