2013-10-23 47 views
0

我想加載屬性文件和命令行參數,然後動態地配置在運行時記錄,這是我以前能做到像這樣:使用apache commons-configuration PropertiesConfiguration配置java.util.logging?

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"); 
} 

偉大的屬性,但可以將它與PropertiesConfiguration做什麼?

(FYI我希望將利用哪一公地配置提供屬性的陣列)

回答

0

不。但是,你可以轉換成PropertiesConfiguration性能

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; 
} 

只要不失去這些逗號分隔值(configuration.getString將返回只是第一個)

private static String asString(Object value) { 
    if (value instanceof List) { 
     List<?> list = (List<?>) value; 
     value = StringUtils.join(list.iterator(), ","); 
    } 
    return (String) value; 
}