-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathConfigurationDynaClass.java
More file actions
127 lines (109 loc) · 4.23 KB
/
Copy pathConfigurationDynaClass.java
File metadata and controls
127 lines (109 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.configuration2.beanutils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils2.DynaBean;
import org.apache.commons.beanutils2.DynaClass;
import org.apache.commons.beanutils2.DynaProperty;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* The {@code ConfigurationDynaClass} dynamically determines properties for a {@code ConfigurationDynaBean} from a
* wrapped configuration-collection {@link org.apache.commons.configuration2.Configuration} instance.
*
* @since 1.0-rc1
*/
public class ConfigurationDynaClass implements DynaClass {
/** The logger. */
private static final Log LOG = LogFactory.getLog(ConfigurationDynaClass.class);
/** Stores the associated configuration. */
private final Configuration configuration;
/**
* Constructs an instance of a {@code ConfigurationDynaClass} wrapping the specified {@code Configuration} instance.
*
* @param configuration {@code Configuration} instance.
*/
public ConfigurationDynaClass(final Configuration configuration) {
if (LOG.isTraceEnabled()) {
LOG.trace("ConfigurationDynaClass(" + configuration + ")");
}
this.configuration = configuration;
}
@Override
public DynaProperty[] getDynaProperties() {
if (LOG.isTraceEnabled()) {
LOG.trace("getDynaProperties()");
}
final Iterator<String> keys = configuration.getKeys();
final List<DynaProperty> properties = new ArrayList<>();
while (keys.hasNext()) {
final String key = keys.next();
final DynaProperty property = getDynaProperty(key);
properties.add(property);
}
final DynaProperty[] propertyArray = new DynaProperty[properties.size()];
properties.toArray(propertyArray);
if (LOG.isDebugEnabled()) {
LOG.debug("Found " + properties.size() + " properties.");
}
return propertyArray;
}
@Override
public DynaProperty getDynaProperty(final String name) {
if (LOG.isTraceEnabled()) {
LOG.trace("getDynaProperty(" + name + ")");
}
if (name == null) {
throw new IllegalArgumentException("Property name must not be null!");
}
final Object value = configuration.getProperty(name);
if (value == null) {
return null;
}
Class<?> type = value.getClass();
if (type == Byte.class) {
type = Byte.TYPE;
}
if (type == Character.class) {
type = Character.TYPE;
} else if (type == Boolean.class) {
type = Boolean.TYPE;
} else if (type == Double.class) {
type = Double.TYPE;
} else if (type == Float.class) {
type = Float.TYPE;
} else if (type == Integer.class) {
type = Integer.TYPE;
} else if (type == Long.class) {
type = Long.TYPE;
} else if (type == Short.class) {
type = Short.TYPE;
}
return new DynaProperty(name, type);
}
@Override
public String getName() {
return ConfigurationDynaBean.class.getName();
}
@Override
public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
return new ConfigurationDynaBean(configuration);
}
}