Reputation: 10311
I want to load properties files and command line arguments then dynamically configure logging in runtime, which I previously could do like so:
Properties configuration;
...
ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteArrayInputStream is;
byte[] buf;
try {
configuration.store(os, "logging");
buf = os.toByteArray();
is = new ByteArrayInputStream(buf);
java.util.logging.LogManager.getLogManager().readConfiguration(is);
} catch (IOException e) {
System.err.println("Failed to configure java.util.logging.LogManager");
}
Great with Properties but can it be done with PropertiesConfiguration?
(FYI I was hoping to utilise arrays of properties which commons-configuration provides)
Upvotes: 0
Views: 467
Reputation: 3277
Use ConfigurationConverter to convert PropertiesConfiguration to standard poperties file
Upvotes: 1
Reputation: 10311
Nope. But you can convert PropertiesConfiguration into Properties
public static Properties configurationAsProperties(){
Properties fromConfiguration = new Properties();
Iterator<String> keys = configuration.getKeys();
while (keys.hasNext()) {
String key = keys.next();
String value = asString(configuration.getProperty(key));
fromConfiguration.setProperty(key,value);
// System.out.println(key + " = " + value);
}
return fromConfiguration;
}
Just don't lose those comma separated values (configuration.getString would return just the first)
private static String asString(Object value) {
if (value instanceof List) {
List<?> list = (List<?>) value;
value = StringUtils.join(list.iterator(), ",");
}
return (String) value;
}
Upvotes: 0