1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2.beanutils;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
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.configuration2.Configuration;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
32
33
34
35
36
37 public class ConfigurationDynaClass implements DynaClass {
38
39
40 private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);
41
42
43 private final Configuration configuration;
44
45
46
47
48
49
50 public ConfigurationDynaClass(final Configuration configuration) {
51 if (LOG.isTraceEnabled()) {
52 LOG.trace("ConfigurationDynaClass(" + configuration + ")");
53 }
54 this.configuration = configuration;
55 }
56
57 @Override
58 public DynaProperty[] getDynaProperties() {
59 if (LOG.isTraceEnabled()) {
60 LOG.trace("getDynaProperties()");
61 }
62
63 final Iterator<String> keys = configuration.getKeys();
64 final List<DynaProperty> properties = new ArrayList<>();
65 while (keys.hasNext()) {
66 final String key = keys.next();
67 final DynaProperty property = getDynaProperty(key);
68 properties.add(property);
69 }
70
71 final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
72 properties.toArray(propertyArray);
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("Found " + properties.size() + " properties.");
75 }
76
77 return propertyArray;
78 }
79
80 @Override
81 public DynaProperty getDynaProperty(final String name) {
82 if (LOG.isTraceEnabled()) {
83 LOG.trace("getDynaProperty(" + name + ")");
84 }
85
86 if (name == null) {
87 throw new IllegalArgumentException("Property name must not be null!");
88 }
89
90 final Object value = configuration.getProperty(name);
91 if (value == null) {
92 return null;
93 }
94 Class<?> type = value.getClass();
95
96 if (type == Byte.class) {
97 type = Byte.TYPE;
98 }
99 if (type == Character.class) {
100 type = Character.TYPE;
101 } else if (type == Boolean.class) {
102 type = Boolean.TYPE;
103 } else if (type == Double.class) {
104 type = Double.TYPE;
105 } else if (type == Float.class) {
106 type = Float.TYPE;
107 } else if (type == Integer.class) {
108 type = Integer.TYPE;
109 } else if (type == Long.class) {
110 type = Long.TYPE;
111 } else if (type == Short.class) {
112 type = Short.TYPE;
113 }
114
115 return new DynaProperty(name, type);
116 }
117
118 @Override
119 public String getName() {
120 return ConfigurationDynaBean.class.getName();
121 }
122
123 @Override
124 public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
125 return new ConfigurationDynaBean(configuration);
126 }
127 }