1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 18 | public class ConfigurationDynaClass implements DynaClass |
41 | |
{ |
42 | |
|
43 | 2 | private static Log log = LogFactory.getLog(ConfigurationDynaClass.class); |
44 | |
|
45 | |
|
46 | |
private Configuration configuration; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public ConfigurationDynaClass(Configuration configuration) |
54 | |
{ |
55 | 22 | super(); |
56 | 22 | if (log.isTraceEnabled()) |
57 | |
{ |
58 | 0 | log.trace("ConfigurationDynaClass(" + configuration + ")"); |
59 | |
} |
60 | 22 | this.configuration = configuration; |
61 | 22 | } |
62 | |
|
63 | |
public DynaProperty getDynaProperty(String name) |
64 | |
{ |
65 | 72 | if (log.isTraceEnabled()) |
66 | |
{ |
67 | 0 | log.trace("getDynaProperty(" + name + ")"); |
68 | |
} |
69 | |
|
70 | 72 | if (name == null) |
71 | |
{ |
72 | 2 | throw new IllegalArgumentException("No such property name=[" + name + "]"); |
73 | |
} |
74 | |
|
75 | 70 | Object value = configuration.getProperty(name); |
76 | 70 | if (value == null) |
77 | |
{ |
78 | 2 | return null; |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 68 | Class type = value.getClass(); |
83 | |
|
84 | 68 | if (type == Byte.class) |
85 | |
{ |
86 | 2 | type = Byte.TYPE; |
87 | |
} |
88 | 68 | if (type == Character.class) |
89 | |
{ |
90 | 2 | type = Character.TYPE; |
91 | |
} |
92 | 66 | else if (type == Boolean.class) |
93 | |
{ |
94 | 8 | type = Boolean.TYPE; |
95 | |
} |
96 | 58 | else if (type == Double.class) |
97 | |
{ |
98 | 4 | type = Double.TYPE; |
99 | |
} |
100 | 54 | else if (type == Float.class) |
101 | |
{ |
102 | 4 | type = Float.TYPE; |
103 | |
} |
104 | 50 | else if (type == Integer.class) |
105 | |
{ |
106 | 6 | type = Integer.TYPE; |
107 | |
} |
108 | 44 | else if (type == Long.class) |
109 | |
{ |
110 | 4 | type = Long.TYPE; |
111 | |
} |
112 | 40 | else if (type == Short.class) |
113 | |
{ |
114 | 4 | type = Short.TYPE; |
115 | |
} |
116 | |
|
117 | 68 | return new DynaProperty(name, type); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
public DynaProperty[] getDynaProperties() |
122 | |
{ |
123 | 2 | if (log.isTraceEnabled()) |
124 | |
{ |
125 | 0 | log.trace("getDynaProperties()"); |
126 | |
} |
127 | |
|
128 | 2 | Iterator keys = configuration.getKeys(); |
129 | 2 | List properties = new ArrayList(); |
130 | 56 | while (keys.hasNext()) |
131 | |
{ |
132 | 52 | String key = (String) keys.next(); |
133 | 52 | DynaProperty property = getDynaProperty(key); |
134 | 52 | properties.add(property); |
135 | |
} |
136 | |
|
137 | 2 | DynaProperty[] propertyArray = new DynaProperty[properties.size()]; |
138 | 2 | properties.toArray(propertyArray); |
139 | 2 | if (log.isDebugEnabled()) |
140 | |
{ |
141 | 0 | log.debug("Found " + properties.size() + " properties."); |
142 | |
} |
143 | |
|
144 | 2 | return propertyArray; |
145 | |
} |
146 | |
|
147 | |
public String getName() |
148 | |
{ |
149 | 0 | return ConfigurationDynaBean.class.getName(); |
150 | |
} |
151 | |
|
152 | |
public DynaBean newInstance() throws IllegalAccessException, InstantiationException |
153 | |
{ |
154 | 0 | return new ConfigurationDynaBean(configuration); |
155 | |
} |
156 | |
} |