-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationUtils.java
executable file
·285 lines (261 loc) · 10.1 KB
/
ConfigurationUtils.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* This file is part of RS Library (Base Library).
*
* RS Library is free software: you can redistribute it
* and/or modify it under the terms of version 3 of the GNU
* Lesser General Public License as published by the Free Software
* Foundation.
*
* RS Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with RS Library. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package rs.baselib.configuration;
import java.io.File;
import java.net.URL;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.HierarchicalConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.SubnodeConfiguration;
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.builder.fluent.PropertiesBuilderParameters;
import org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.tree.ImmutableNode;
import rs.baselib.lang.LangUtils;
/**
* Useful methods for configuration issues.
* @author ralph
*
*/
public class ConfigurationUtils {
/**
* Loads an object from a configuration.
* The object is configured if it is an instance of {@link IConfigurable}.
* The class will be taken from attribute <code>[@class]</code>.
* @param config the configuration to apply
* @param configure whether the object shall be configured (if it is a {@link IConfigurable}).
* @return the object
*/
public static Object load(Configuration config, boolean configure) {
String className = config.getString("[@class]");
return load(className, config, configure);
}
/**
* Loads an object from a configuration.
* The object is configured if it is an instance of {@link IConfigurable}.
* @param className the name of class to be instantiated
* @param config the configuration to apply
* @param configure whether the object shall be configured (if it is a {@link IConfigurable}).
* @return the object
*/
public static Object load(String className, Configuration config, boolean configure) {
try {
Class<?> clazz = LangUtils.forName(className);
return load(clazz, config, configure);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot load class from configuration", e);
}
}
/**
* Loads an object from a configuration.
* The object is configured if it is an instance of {@link IConfigurable}.
* @param clazz the class to be instantiated
* @param config the configuration to apply
* @param configure whether the object shall be configured (if it is a {@link IConfigurable}).
* @param <T> the type of the object to be instantiated
* @return the object
*/
@SuppressWarnings("unchecked")
public static <T> T load(Class<T> clazz, Configuration config, boolean configure) {
try {
Object rc = clazz.getConstructor().newInstance();
if (configure && (rc instanceof IConfigurable)) {
configure((IConfigurable)rc, config);
}
return (T)rc;
} catch (Exception e) {
throw new RuntimeException("Cannot load class from configuration", e);
}
}
/**
* Configure the object accordingly.
* @param configurable the object to be configured
* @param config the configuration to apply
* @throws ConfigurationException when a problem occurs
*/
public static void configure(IConfigurable configurable, Configuration config) throws ConfigurationException {
configurable.configure(config);
}
/**
* Returns the config parameter with given value for attribute name.
* @param config config
* @param name name of param
* @return value of param
*/
public static String getParam(SubnodeConfiguration config, String name) {
if (config == null) return null;
int index = 0;
while (true) {
try {
SubnodeConfiguration cfg = (SubnodeConfiguration) config.configurationAt("param("+index+")");
if (cfg == null) return null;
String n = cfg.getString("[@name]");
if (name.equals(n)) return config.getString("param("+index+")");
index++;
} catch (Exception e) {
break;
}
}
return null;
}
/**
* Returns the config parameter with given value for attribute name.
* @param config config
* @param name name of param
* @return value of param
*/
public static String getParam(HierarchicalConfiguration<ImmutableNode> config, String name) {
if (config == null) return null;
int index = 0;
while (true) {
try {
HierarchicalConfiguration<ImmutableNode> cfg = config.configurationAt("param("+index+")");
if (cfg == null) return null;
String n = cfg.getString("[@name]");
if (name.equals(n)) return config.getString("param("+index+")");
index++;
} catch (Exception e) {
break;
}
}
return null;
}
/**
* Creates the basic Properties Builder parameters
* @param encoding - loading character encoding
* @return basic builder params
*/
public static PropertiesBuilderParameters createPropertiesBuilderParams(String encoding) {
Parameters params = new Parameters();
return params.properties()
.setThrowExceptionOnMissing(false)
.setEncoding(encoding);
}
/**
* Creates the Properties configuration object based on the given parameters.
* @param params - the Properties builder parameters
* @return the Properties configuration
* @throws ConfigurationException when an error occurs
*/
public static PropertiesConfiguration getPropertiesConfiguration(PropertiesBuilderParameters params) throws ConfigurationException {
FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class).configure(params);
return builder.getConfiguration();
}
/**
* Create a Properties configuration object with UTF-8.
* @param file - file to load from
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static PropertiesConfiguration getPropertiesConfiguration(File file) throws ConfigurationException {
return getPropertiesConfiguration(file, "UTF-8");
}
/**
* Create a Properties configuration object.
* @param file - file to load from
* @param encoding - encoding, e.g. UTF-8
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static PropertiesConfiguration getPropertiesConfiguration(File file, String encoding) throws ConfigurationException {
return getPropertiesConfiguration(createPropertiesBuilderParams(encoding).setFile(file));
}
/**
* Create a Properties configuration object with UTF-8.
* @param url - URL to load from
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static PropertiesConfiguration getPropertiesConfiguration(URL url) throws ConfigurationException {
return getPropertiesConfiguration(url, "UTF-8");
}
/**
* Create a Properties configuration object.
* @param url - URL to load from
* @param encoding - encoding, e.g. UTF-8
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static PropertiesConfiguration getPropertiesConfiguration(URL url, String encoding) throws ConfigurationException {
return getPropertiesConfiguration(createPropertiesBuilderParams(encoding).setURL(url));
}
/**
* Creates the basic XML Builder parameters
* @param encoding - loading character encoding
* @return basic builder params
*/
public static XMLBuilderParameters createXMLBuilderParams(String encoding) {
Parameters params = new Parameters();
return params.xml()
.setThrowExceptionOnMissing(false)
.setValidating(false)
.setEncoding(encoding);
//.setExpressionEngine(new XPathExpressionEngine());
}
/**
* Creates the XML configuration object based on the given parameters.
* @param params - the XML builder parameters
* @return the XML configuration
* @throws ConfigurationException when an error occurs
*/
public static XMLConfiguration getXmlConfiguration(XMLBuilderParameters params) throws ConfigurationException {
FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class).configure(params);
return builder.getConfiguration();
}
/**
* Create a XML configuration object with UTF-8.
* @param file - file to load from
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static XMLConfiguration getXmlConfiguration(File file) throws ConfigurationException {
return getXmlConfiguration(file, "UTF-8");
}
/**
* Create a XML configuration object.
* @param file - file to load from
* @param encoding - encoding, e.g. UTF-8
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static XMLConfiguration getXmlConfiguration(File file, String encoding) throws ConfigurationException {
return getXmlConfiguration(createXMLBuilderParams(encoding).setFile(file));
}
/**
* Create a XML configuration object with UTF-8.
* @param configUrl - URL to load from
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static XMLConfiguration getXmlConfiguration(URL configUrl) throws ConfigurationException {
return getXmlConfiguration(configUrl, "UTF-8");
}
/**
* Create a XML configuration object.
* @param configUrl - URL to load from
* @param encoding - encoding, e.g. UTF-8
* @return the configuration object
* @throws ConfigurationException when an error occurs
*/
public static XMLConfiguration getXmlConfiguration(URL configUrl, String encoding) throws ConfigurationException {
return getXmlConfiguration(createXMLBuilderParams(encoding).setURL(configUrl));
}
}