prompto.config.YamlConfigurationReader Maven / Gradle / Ivy
The newest version!
package prompto.config;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import prompto.store.IStore;
import prompto.store.IStoreFactory;
import prompto.utils.Logger;
import com.esotericsoftware.yamlbeans.YamlReader;
public class YamlConfigurationReader implements IConfigurationReader {
static Logger logger = new Logger();
@SuppressWarnings("unchecked")
static Map parseYaml(InputStream input) {
try {
YamlReader reader = new YamlReader(new InputStreamReader(input));
return reader.read(Map.class);
} catch(Throwable t) {
throw new RuntimeException(t);
}
}
Map data;
public YamlConfigurationReader(InputStream input) {
this(parseYaml(input));
}
@SuppressWarnings("unchecked")
public YamlConfigurationReader(Map data) {
/// TODO remove once yamlbeans is upgraded
if(data.containsKey("<<")) {
this.data = new HashMap();
this.data.putAll((Map)data.get("<<"));
this.data.putAll(data);
this.data.remove("<<");
} else
this.data = data;
}
@Override
public String toString() {
return data.toString();
}
@Override
public boolean hasKey(String key) {
return data.containsKey(key);
}
@Override
public String getString(String key) {
Object value = data.get(key);
return value==null ? null : value.toString();
}
@Override
public Boolean getBoolean(String key) {
Object value = data.get(key);
return value==null ? null : Boolean.parseBoolean(value.toString());
}
@Override
public Integer getInteger(String key) {
Object value = data.get(key);
return value==null ? null : Integer.parseInt(value.toString());
}
@SuppressWarnings("unchecked")
@Override
public Collection getArray(String key) {
Object value = data.get(key);
if(value instanceof List)
return (List) value;
else
return null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Collection getObjectsArray(String key) {
Object value = data.get(key);
if(value instanceof List) {
return (Collection)((List)value).stream()
.filter(o -> o instanceof Map)
.map(o -> (Map)o)
.map(m -> new YamlConfigurationReader((Map)m))
.collect(Collectors.toList());
} else
return null;
}
@SuppressWarnings("unchecked")
@Override
public IConfigurationReader getObject(String key) {
Object value = data.get(key);
return value instanceof Map ? new YamlConfigurationReader((Map)value): null;
}
public static boolean checkStoreConnection(String yamlConfig) {
try {
logger.info(()->yamlConfig);
IConfigurationReader reader = new YamlConfigurationReader(new ByteArrayInputStream(yamlConfig.getBytes()));
IStoreConfiguration storeConfig = reader.readStoreConfiguration();
IStore store = IStoreFactory.newStoreFromConfig(storeConfig);
boolean valid = store.checkConnection();
store.close();
return valid;
} catch(Throwable t) {
logger.error(()->"Failed to connect!", t);
return false;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy