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 getDynaProperty(final String name) {
59 if (LOG.isTraceEnabled()) {
60 LOG.trace("getDynaProperty(" + name + ")");
61 }
62
63 if (name == null) {
64 throw new IllegalArgumentException("Property name must not be null!");
65 }
66
67 final Object value = configuration.getProperty(name);
68 if (value == null) {
69 return null;
70 }
71 Class<?> type = value.getClass();
72
73 if (type == Byte.class) {
74 type = Byte.TYPE;
75 }
76 if (type == Character.class) {
77 type = Character.TYPE;
78 } else if (type == Boolean.class) {
79 type = Boolean.TYPE;
80 } else if (type == Double.class) {
81 type = Double.TYPE;
82 } else if (type == Float.class) {
83 type = Float.TYPE;
84 } else if (type == Integer.class) {
85 type = Integer.TYPE;
86 } else if (type == Long.class) {
87 type = Long.TYPE;
88 } else if (type == Short.class) {
89 type = Short.TYPE;
90 }
91
92 return new DynaProperty(name, type);
93 }
94
95 @Override
96 public DynaProperty[] getDynaProperties() {
97 if (LOG.isTraceEnabled()) {
98 LOG.trace("getDynaProperties()");
99 }
100
101 final Iterator<String> keys = configuration.getKeys();
102 final List<DynaProperty> properties = new ArrayList<>();
103 while (keys.hasNext()) {
104 final String key = keys.next();
105 final DynaProperty property = getDynaProperty(key);
106 properties.add(property);
107 }
108
109 final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
110 properties.toArray(propertyArray);
111 if (LOG.isDebugEnabled()) {
112 LOG.debug("Found " + properties.size() + " properties.");
113 }
114
115 return propertyArray;
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 }