package com.vincent.rsf.common.utils;
|
|
import com.vincent.rsf.framework.common.SpringUtils;
|
import org.springframework.context.ApplicationContext;
|
import org.springframework.core.env.Environment;
|
import org.yaml.snakeyaml.Yaml;
|
|
import java.io.BufferedInputStream;
|
import java.io.InputStream;
|
import java.nio.charset.StandardCharsets;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.Properties;
|
|
public class ConfigHelper {
|
|
public static Properties m_props = new Properties();
|
public static String m_charset = "GBK";
|
public static Yaml m_yaml = new Yaml();
|
public static Map<String, Object> m_root = new HashMap<>();
|
public static Environment environment;
|
|
static {
|
try {
|
if (ClassLoader.class.getResource("/application.properties") != null) {
|
m_charset = getCharset(ClassLoader.class.getResourceAsStream("/application.properties"));
|
m_props.load(ClassLoader.class.getResourceAsStream("/application.properties"));
|
} else if ( Thread.currentThread().getContextClassLoader().getResource("/application.properties")!=null) {
|
m_charset = getCharset(Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.properties"));
|
m_props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.properties"));
|
} else if (ConfigHelper.class.getResource("/application.properties")!=null){
|
m_charset=getCharset(ConfigHelper.class.getResourceAsStream("/application.properties"));
|
m_props.load(ConfigHelper.class.getResourceAsStream("/application.properties"));
|
}
|
} catch (Exception ex){
|
System.err.println("ERR - undefined application.properties");
|
}
|
try {
|
if (ClassLoader.class.getResource("/application.yml") != null) {
|
InputStream as = ClassLoader.class.getResourceAsStream("/application.yml");
|
if(as!=null){
|
m_root = (Map) m_yaml.load(as);
|
}
|
} else if(Thread.currentThread().getContextClassLoader().getResource("/application.yml")!=null) {
|
InputStream as = Thread.currentThread().getContextClassLoader().getResourceAsStream("/application.yml");
|
if(as!=null){
|
m_root = (Map) m_yaml.load(as);
|
}
|
} else if(ConfigHelper.class.getResource("/application.yml")!=null){
|
InputStream as = ConfigHelper.class.getResourceAsStream("/application.yml");
|
if(as!=null){
|
m_root = (Map) m_yaml.load(as);
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
System.err.println("ERR - undefined application.yml");
|
}
|
}
|
|
public static void main(String...strings){
|
System.out.println(getString("redis.session.index"));
|
}
|
|
public static Boolean contains(String key){
|
return getString(key)!=null;
|
}
|
|
public static String getString(String key) {
|
if (environment==null) {
|
ApplicationContext applicationContext = null;
|
try {
|
applicationContext = SpringUtils.getApplicationContext();
|
} catch (Exception ignore) {}
|
if ( applicationContext != null) {
|
environment = SpringUtils.getApplicationContext().getEnvironment();
|
}
|
}
|
if (environment!=null) {
|
String value = environment.getProperty(key);
|
if(value!=null){
|
return value;
|
}
|
}
|
String value = m_props.getProperty(key);
|
try {
|
if (value!=null){
|
value = new String(value.getBytes(StandardCharsets.ISO_8859_1),m_charset);
|
} else if(m_root!=null) {
|
String[] items = key.split("\\.");
|
Map map = m_root;
|
for(int i=0;i<items.length-1;i++){
|
String item = items[i];
|
if(map.get(item)!=null && map.get(item) instanceof Map){
|
map = (Map) map.get(item);
|
}else{
|
map = null;
|
break;
|
}
|
}
|
if(map!=null){
|
Object v = map.get(items[items.length-1]);
|
if(v!=null){
|
return v.toString();
|
}
|
}
|
}
|
} catch(Exception ex) {
|
System.err.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
|
}
|
return value;
|
}
|
|
public static Long getLong(String key) {
|
Long l=null;
|
try {
|
l = Long.parseLong(getString(key));
|
} catch (Exception ex) {
|
System.err.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
|
}
|
return l;
|
}
|
|
public static Integer getInteger(String key) {
|
Integer i=null;
|
try{
|
i=Integer.parseInt(getString(key));
|
}catch(Exception ex){
|
//ex.printStackTrace();
|
System.out.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
|
}
|
return i;
|
}
|
|
public static Boolean getBoolean(String key) {
|
boolean b=false;
|
try{
|
String s=getString(key);
|
b=Boolean.parseBoolean(s)||s.equals("1");
|
}catch(Exception ex){
|
//ex.printStackTrace();
|
System.out.println("获取配置文件属性["+key+"]时出错 - "+ ex.getMessage());
|
}
|
return b;
|
}
|
|
public static Environment getEnvironment() {
|
return environment;
|
}
|
|
public static void setEnvironment(Environment environment) {
|
ConfigHelper.environment = environment;
|
}
|
|
/**
|
* 获取文件编码
|
* ANSI:无格式定义;
|
* Unicode: 前两个字节为0xFFFE;
|
* Unicode big endian:前两字节为0xFEFF;
|
* UTF-8:前两字节为0xEFBB;
|
*/
|
public static String getCharset( InputStream is ) {
|
String charset = "GBK";
|
byte[] first3Bytes = new byte[3];
|
try {
|
boolean checked=false;
|
BufferedInputStream bis = new BufferedInputStream(is);
|
bis.mark( 0 );
|
int read = bis.read( first3Bytes, 0, 3 );
|
if ( read == -1 ) return charset;
|
if ( first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE ) {
|
charset = "UTF-16LE";
|
checked = true;
|
}
|
else if ( first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF ) {
|
charset = "UTF-16BE";
|
checked = true;
|
}
|
else if ( first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF ) {
|
charset = "UTF-8";
|
checked = true;
|
}
|
bis.reset();
|
if ( !checked ) {
|
int loc = 0;
|
while ( (read = bis.read()) != -1 ) {
|
loc++;
|
if ( read >= 0xF0 ) break;
|
if ( 0x80 <= read && read <= 0xBF ) // 单独出现BF以下的,也算是GBK
|
break;
|
if ( 0xC0 <= read && read <= 0xDF ) {
|
read = bis.read();
|
if ( !(0x80 <= read && read <= 0xBF) ) // 双字节 (0xC0 - 0xDF) (0x80
|
// - 0xBF),也可能在GB编码内
|
{
|
break;
|
}
|
}
|
else if (0xE0 <= read) {// 也有可能出错,但是几率较小
|
read = bis.read();
|
if ( 0x80 <= read && read <= 0xBF ) {
|
read = bis.read();
|
if ( 0x80 <= read && read <= 0xBF ) {
|
charset = "UTF-8";
|
break;
|
}
|
else break;
|
}
|
else break;
|
}
|
}
|
bis.close();
|
}
|
return charset;
|
} catch ( Exception e ) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
}
|