作者:じ☆ve宝贝
发布时间:2017-12-13T17:48:18
package cn.studyjava.canal.common.util;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import org.apache.logging.log4j.core.config.plugins.util.ResolverUtil.Test;
import org.yaml.snakeyaml.Yaml;
import com.google.common.collect.Maps;
/**
* @author zsljava 2017年12月13日 下午2:16:49
* @since 1.0.0
*/
public class YmlUtil {
private static Map<String, Object> ymlMap = null;
static{
ymlMap = Maps.newConcurrentMap();
getApplicationYml();
}
/**
*
*/
public YmlUtil(){
super();
}
@SuppressWarnings("unchecked")
private static Map<String, Object> getApplicationYml(){
try {
Yaml yaml = new Yaml();
URL url = ClassLoader.getSystemResource("application.yml");
if (url != null) {
Map<String, Object> map = yaml.loadAs(new FileInputStream(url.getFile()), Map.class);
switchToMap(null, map);
}
} catch (Exception e) {
e.printStackTrace();
}
return ymlMap;
}
@SuppressWarnings("unchecked")
private static void switchToMap(String myKey,Map<String, Object> map){
Iterator<String> it = map.keySet().iterator();
myKey = myKey == null ? "" : myKey;
String tmpkey = myKey;
while(it.hasNext()){
String key = it.next();
myKey = tmpkey +key;
Object value = map.get(key);
if(value instanceof Map){
switchToMap(myKey.concat("."), (Map<String, Object>)value);
}else{
if(null != value){
ymlMap.put(myKey, value);
}
// System.out.println(myKey+"->"+map.get(key));
}
}
}
@SuppressWarnings("unchecked")
public static <T> T get(String key){
return (T) ymlMap.get(key);
}
public static String getStr(String key){
return String.valueOf(ymlMap.get(key));
}
}