hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.server;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletContext;
 
import frame.variant.translator.Translator;
 
public class Configer {
 
    private static Configer instance;
    private static Object lock = new Object();
    
    private Map<String, String> params;
    private String Path_Application;
    private String Path_WebInfo;
    private String Path_Config;
    private String Path_SQL;
 
    private Configer() {
        params = new HashMap<String, String>();
    }
    
    public static Configer getInstance() {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new Configer();
                }
            }
        }
        
        return instance;
    }
    
    public static void init(ServletContext servletContext) {
        try {
            getInstance();
            
            instance.params = new HashMap<String, String>();
 
            instance.Path_Application = servletContext.getRealPath("").replace("\\", "/");
            instance.Path_WebInfo = servletContext.getRealPath("/WEB-INF").replace("\\", "/");
            instance.Path_Config = instance.Path_WebInfo + "/config/";
            instance.Path_SQL = instance.Path_WebInfo + "/sql/";
            
            instance.load();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
 
    private void load() throws Exception {
        File file = new File(Path_Config + "/wildfirechat.conf");
        
        Reader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        
        try {
            String line;
            
            while ((line = br.readLine()) != null) {
                line = line.trim();
                
                if (line.startsWith("#")) {
                    continue;
                }
 
                loadOneLine(line);
            }
        } 
        finally {
            reader.close();
        }
    }
 
    private void loadOneLine(String line) {
        if (line == null) {
            return;
        }
        
        line = line.trim();
        
        int delimiterIdx = line.indexOf(' ');
        
        if (delimiterIdx <= 0) {
            return;
        }
        
        String key = line.substring(0, delimiterIdx).trim();
        String value = line.substring(delimiterIdx).trim();
        
        params.put(key, value);
    }
 
    public static String getPath_WebInfo() {
        return instance.Path_WebInfo;
    }
 
    public static String getPath_Config() {
        return instance.Path_Config;
    }
 
    public static String getPath_TimerConfig() {
        return instance.Path_Config + "timer.properties";
    }
 
    public static String getWebserviceURI() {
        return "http://www.jydatas.com/";
    }
 
    public static String getPath_Application() {
        return instance.Path_Application;
    }
    
    public static String getPath_Application(String subpath) {
        if (subpath == null) {
            return instance.Path_Application;
        }
        
        subpath = subpath.replace("\\", "/");
        
        if ('/' != subpath.charAt(0)) {
            subpath = "/" + subpath;
        }
        
        if ('/' != subpath.charAt(subpath.length() - 1)) {
            subpath = subpath + "/";
        }
        
        return instance.Path_Application + subpath;
    }
 
    public static String getPath_LoggerConfig() {
        return instance.Path_Config + "log4j.properties";
    }
 
    public static String getPath_SQLConfig() {
        return instance.Path_SQL;
    }
 
    public static String getPath_SQLDTD() {
        return instance.Path_Config + "sql.dtd";
    }
 
    public static String getPath_MainConfig() {
        return instance.Path_Config + "config.xml";
    }
 
    public static String getPath_Datasource() {
        return instance.Path_Config + "datasource.xml";
    }
 
    public static String getPath_Upload(String username) {
        return instance.Path_Application + "/upload/" + username;
    }
 
    public static String getPath_Temp() {
        return instance.Path_Application + "/temp";
    }
    
    public static String getPath_Temp(String username) {
        return instance.Path_Application + "/temp/" + username;
    }
 
    public static String getPath_Template() {
        return instance.Path_Application + "/template";
    }
 
    public static String getPath_WXConfig() {
        return instance.Path_Config + "weixin.properties";
    }
 
    public static Integer getInteger(String name, Integer defaultValue) {
        String value = getString(name);
        return Translator.toInteger(value, defaultValue);
    }
 
    public static boolean getBoolean(String name, boolean defaultValue) {
        String value = getString(name);
        return Translator.toBoolean(value, defaultValue);
    }
 
    public static String getString(String name) {
        if (name == null) {
            return null;
        }
        
        name = name.toLowerCase();
        String result = instance.params.get(name);
        
        return result;
    }
 
}