org.yelong.support.yaml.DefaultYamlProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yelong-support Show documentation
Show all versions of yelong-support Show documentation
对各种开源框架的包装、支持、拓展。这里也包含的yelong-core与orm框架的整合。
默认对所有依赖为 scope 为 provided 。您需要针对自己的需要进行再次依赖
/**
*
*/
package org.yelong.support.yaml;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import org.yelong.core.annotation.Nullable;
import org.yelong.support.ognl.OgnlWrapper;
import ognl.OgnlException;
/**
* @author PengFei
*/
public class DefaultYamlProperties implements YamlProperties {
@Nullable
private String name;
private final Map sourceMap;
private Properties properties;
private YamlMapToProperties yamlMapToProperties = new YamlMapToProperties();
public DefaultYamlProperties(Map sourceMap) {
this(null, sourceMap);
}
public DefaultYamlProperties(@Nullable String name, Map sourceMap) {
this.name = name;
this.sourceMap = sourceMap;
this.properties = yamlMapToProperties.as(sourceMap);
}
@Override
public String getProperty(String key) {
return this.properties.getProperty(key);
}
@Override
public String getProperty(String key, String defaultValue) {
return this.properties.getProperty(key, defaultValue);
}
@SuppressWarnings("unchecked")
@Override
public T as(String prefix, Class type) {
OgnlWrapper ognlWrapper = new OgnlWrapper();
try {
ognlWrapper.setRoot(type.newInstance());
Collection keys = getKeys();
for (String key : keys) {
if (key.startsWith(prefix + ".")) {
try {
ognlWrapper.setValue(key.substring((prefix + ".").length()), this.properties.get(key));
} catch (OgnlException e) {
}
}
}
return (T) ognlWrapper.getRoot();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
@Override
public Map getSourceMap() {
return this.sourceMap;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Map getMap() {
return (Map) this.properties;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Collection getKeys() {
return (Collection) this.properties.keySet();
}
@Override
public String getName() {
return this.name;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy