Frage

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)

War es hilfreich?

Lösung

Use ConfigurationConverter to convert PropertiesConfiguration to standard poperties file

Andere Tipps

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top