com.ajjpj.asysmon.config.wiring.ConfigPropsFile Maven / Gradle / Ivy
package com.ajjpj.asysmon.config.wiring;
import com.ajjpj.asysmon.config.log.ASysMonLogger;
import com.ajjpj.asysmon.config.log.ASysMonLoggerFactory;
import com.ajjpj.abase.collection.immutable.AOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* @author arno
*/
public class ConfigPropsFile {
private final Properties props;
private final ASysMonLogger log;
public ConfigPropsFile(Properties props, ASysMonLoggerFactory log) {
this.props = props;
this.log = log.getLogger(ConfigPropsFile.class);
}
public T get(String key, Class type, Class>... paramTypes) {
return get(key, AOption.none(), type, paramTypes);
}
public T get(String key, AOption extends T> defaultValue, Class type, Class>... paramTypes) {
final ConfigValueResolver r = new ConfigValueResolver(props, key, type, paramTypes);
return (T) r.get(defaultValue);
}
public List getList(String key, Class type) {
final ConfigValueResolver r = new ConfigValueResolver(props, key, List.class, new Class[] {type});
return (List) r.get(AOption.some(Collections.emptyList()));
}
public List getListRaw(String key) {
final List result = new ArrayList();
final String raw = props.getProperty(key);
if(raw == null || raw.trim().isEmpty()) {
return result;
}
final String[] split = raw.split(",");
for(String s: split) {
final String trimmed = s.trim();
if(trimmed.isEmpty()) {
log.warn("configuration error: empty list entry @ " + key + " - skipping");
}
else {
result.add(trimmed);
}
}
return result;
}
}