P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package foundation.server.config;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
import javax.servlet.ServletContext;
 
import foundation.translator.Translator;
import foundation.util.Util;
import foundation.variant.provider.DataEvent;
import foundation.variant.provider.IVariantsProvider;
import foundation.variant.provider.VariantProviderType;
 
 
public class Configer implements IVariantsProvider {
 
    private static Configer instance;
    private static Object lock = new Object();
    
    private static String Path_Application;
    private static String Path_WebInfo;
    private static String Path_Config;
    private static String Path_SQL;
    
    private Map<String, String> params;
 
 
    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) {
        getInstance();
        
        instance.params = new HashMap<String, String>();
 
        Path_Application = servletContext.getRealPath("").replace("\\", "/");
        Path_WebInfo = servletContext.getRealPath("/WEB-INF").replace("\\", "/");
        Path_Config = Path_WebInfo + "/config/";
        Path_SQL = Path_WebInfo + "/sql/";
    }
    
    public static Configer newInstance() {
        return new Configer();
    }
 
    public static void activeInstance(Configer configer) {
        instance = configer;
    }
    
    public static void clear() {
        instance.params.clear();
    }
    
    public void addParam(String name, String value) throws Exception {
        params.put(name.toLowerCase(), value);
    }
    
    public static String getParam(String name) {
        if (name == null) {
            return null;
        }
 
        return instance.params.get(name.toLowerCase());
    }
    
    public static String getString(String name) {
        if (name == null) {
            return null;
        }
 
        name = name.toLowerCase();
        return instance.params.get(name);
    }
    
    public static String getString(String name, String defaultValue) {
        String value = getString(name);
        
        if (Util.isEmpty(value)) {
            return defaultValue;
        }
        
        return value;
    }
    
    public static int getInt(String name) throws Exception {
        String value = getString(name);
        
        if (Util.isEmpty(value)) {
            return 0;
        }
        
        return Translator.toInteger(value);
    }
    
    public static int getInt(String name, int defaultValue) {
        int result = defaultValue;
        try {
            result = getInt(name);
        }
        catch (Exception e) {
            result = defaultValue;
        }
        
        return result;
    }
 
    public static boolean getBoolean(String name, boolean defaultValue) {
        String value = getString(name);
        return Translator.toBoolean(value, defaultValue);
    }
 
    public static String getPath_WebInfo() {
        return Path_WebInfo;
    }
 
    public static String getPath_Config() {
        return Path_Config;
    }
 
    public static String getPath_TimerConfig() {
        return Path_Config + "timer.properties";
    }
 
    public static String getPath_Application() {
        return Path_Application;
    }
    
    public static String getPath_Application(String subpath) {
        if (subpath == null) {
            return Path_Application;
        }
        
        subpath = subpath.replace("\\", "/");
        
        if ('/' != subpath.charAt(0)) {
            subpath = "/" + subpath;
        }
        
        if ('/' != subpath.charAt(subpath.length() - 1)) {
            subpath = subpath + "/";
        }
        
        return Path_Application + subpath;
    }
 
    public static String getPath_LoggerConfig() {
        return Path_Config + "log4j.xml";
    }
 
    public static String getPath_ActivePeriodConfig() {
        return Path_Config + "activeperiod.properties";
    }
 
    public static String getPath_SQLConfig() {
        return Path_SQL;
    }
 
    public static String getPath_SQLDTD() {
        return Path_Config + "sql.dtd";
    }
    
    public static String getPath_ServerConfig() {
        return Path_Config + "server.xml";
    }
 
    public static String getPath_MainConfig() {
        return Path_Config + "config.xml";
    }
 
    public static String getPath_WXConfig() {
        return Path_Config + "weixin.properties";
    }
    
    //he mail.xml 20230829
    public static String getPath_MailConfig() {
        return Path_Config + "mail.xml";
    }
    
    //he message.xml 20230807
    public static String getPath_SmsConfig() {
        return Path_Config + "message.xml";
    }    
 
    public static String getPath(String name, String defaultValue) {
        String path = getString(name);
        
        if (path == null) {
            path = defaultValue;
        }
        
        return path;
    }
 
    @Override
    public VariantProviderType getProviderType() {
        return VariantProviderType.Persist;
    }
 
    @Override
    public String getProviderName() {
        return "configer";
    }
 
    @Override
    public boolean containsVariant(String variantName) {
        if (variantName == null) {
            return false;
        }
        
        variantName = variantName.toLowerCase();
        return params.containsKey(variantName);
    }
 
    @Override
    public Object getVariantValue(DataEvent dataEvent, String variantName) {
        if (variantName == null) {
            return null;
        }
        
        if ("path_application".equalsIgnoreCase(variantName)) {
            return Path_Application;
        }
        else if ("path_config".equalsIgnoreCase(variantName)) {
            return Path_Config;
        }
        else if ("path_sql".equalsIgnoreCase(variantName)) {
            return Path_SQL;
        }
        else if ("path_webinfo".equalsIgnoreCase(variantName)) {
            return Path_WebInfo;
        }
        else {
            return params.get(variantName.toLowerCase());
        }
    }
 
    @Override
    public Set<String> getVariantNames() {
        return params.keySet();
    }
 
}