vincentlu
2025-02-05 9d79b8c8d34077ebf782a7b61d54ee4e1debdba1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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;
        }
    }
 
}