1 public class ConfigurationUtils {
2 private static Map<String, String> config;
3 private static Map<String,String> sourceConfig;
4
5 private static volatile boolean inited = false;
6
7 public synchronized static final void init(Properties properties) {
8 if (inited) {
9 throw new ConfigException("ConfigurationUtils has been inited!");
10 } else {
11 Map<String, String> map = new HashMap<String, String>();
12 Map<String,String> sourceMap = new HashMap<String,String>();
13 if (properties != null) {
14 Iterator<String> iterator = properties.stringPropertyNames()
15 .iterator();
16 while (iterator.hasNext()) {
17 String key = iterator.next();
18 String value = properties.getProperty(key, "");
19 map.put(key.toUpperCase(), value);
20 sourceMap.put(key, value);
21 }
22 }
23
24 config = Collections.unmodifiableMap(map);
25 sourceConfig = Collections.unmodifiableMap(sourceMap);
26 inited = true;
27 }
28 }
29
30 public static Properties getProperties() {
31 checkInit();
32 return copyProperties(config);
33 }
34
35 public static Map<String, String> getMap() {
36 checkInit();
37 return copyMap(config);
38 }
39
40 public static Properties getSourceProperties() {
41 checkInit();
42 return copyProperties(sourceConfig);
43 }
44
45 public static Map<String, String> getSourceMap() {
46 checkInit();
47 return copyMap(sourceConfig);
48 }
49
50 /**
51 * 根据键直接获取字符串,在获取字符串时会依据内外网标识取不同的值
52 * @param key
53 * @return
54 */
55 public static String getConfig(String key){
56 checkInit();
57 if (StringUtils.isEmpty(key)) {
58 return null;
59 }
60 //根据内外网标识获取不同的配置信息
61 Boolean outside = ClientIsOutside.get();
62 key = key.trim().toUpperCase();
63 String result = null;
64 if (outside) {
65 result = config.get(getOutsideKey(key).toUpperCase());
66 } else {
67 result = config.get(key);
68 }
69
70 return result;
71 }
72
73 /**
74 * 根据键直接获取字符串,在获取字符串时会依据内外网标识取不同的值;如果指定键的值不存在,则返回默认值
75 * @param key
76 * @return
77 */
78 public static String getConfig(String key,String defaultValue){
79 return getValueOrDefault(getConfig(key), defaultValue);
80 }
81
82 /**
83 * 根据键直接获取字符串,在获取字符串时会依据内外网标识取不同的值;如果指定键的值不存在,则抛出异常
84 * @param key
85 * @return
86 */
87 public static String getMustExistsConfig(String key) {
88 String value = getConfig(key);
89 checkExists(key, value);
90 return value;
91 }
92
93 public static String getSystemConfig(String key){
94 return getConfig(getSystemKey(key));
95 }
96
97 public static String getSystemConfig(String key,String defaultValue){
98 return getConfig(getSystemKey(key), defaultValue);
99 }
100
101 public static String getMustExistsSystemConfig(String key) {
102 return getMustExistsConfig(getSystemKey(key));
103 }
104
105 /**
106 * 根据键直接获取字符串
107 *
108 * @param key
109 * @return
110 */
111 public static String getStringConfig(String key) {
112 checkInit();
113 if (StringUtils.isEmpty(key)) {
114 return null;
115 }
116 return config.get(key.toUpperCase());
117 }
118
119 /**
120 * 根据键直接获取字符串,如果不存在,则返回默认值
121 *
122 * @param key
123 * @return
124 */
125 public static String getStringConfig(String key, String defaultValue) {
126 return getValueOrDefault(getStringConfig(key), defaultValue);
127 }
128
129 /**
130 * 根据键直接获取字符串,如果不存在则抛出异常
131 *
132 * @param key
133 * @return
134 */
135 public static String getMustExistsStringConfig(String key) {
136 String value = getStringConfig(key);
137 checkExists(key, value);
138 return value;
139 }
140
141 public static String getStringSystemConfig(String key) {
142 return getStringConfig(getSystemKey(key));
143 }
144
145 public static String getStringSystemConfig(String key, String defaultValue) {
146 return getStringConfig(getSystemKey(key), defaultValue);
147 }
148
149 public static String getMustExistsStringSystemConfig(String key) {
150 return getMustExistsStringConfig(getSystemKey(key));
151 }
152
153 public static Integer getIntegerConfig(String key) {
154 String value = getStringConfig(key);
155 if (!StringUtils.isEmpty(value)) {
156 return Integer.parseInt(value);
157 }
158
159 return null;
160 }
161
162 public static Integer getIntegerConfig(String key, Integer defaultValue) {
163 return getValueOrDefault(getIntegerConfig(key), defaultValue);
164 }
165
166 public static Integer getMustExistsIntegerConfig(String key) {
167 Integer value = getIntegerConfig(key);
168 checkExists(key, value);
169 return value;
170 }
171
172 public static Integer getIntegerSystemConfig(String key){
173 return getIntegerConfig(getSystemKey(key));
174 }
175
176 public static Integer getIntegerSystemConfig(String key, Integer defaultValue) {
177 return getIntegerConfig(getSystemKey(key), defaultValue);
178 }
179
180 public static Integer getMustExistsIntegerSystemConfig(String key) {
181 return getMustExistsIntegerConfig(getSystemKey(key));
182 }
183
184 public static Long getLongConfig(String key) {
185 String value = getStringConfig(key);
186 if (!StringUtils.isEmpty(value)) {
187 return Long.parseLong(value);
188 }
189
190 return null;
191 }
192
193 public static Long getLongConfig(String key, Long defaultValue) {
194 return getValueOrDefault(getLongConfig(key), defaultValue);
195 }
196
197 public static Long getMustExistsLongConfig(String key) {
198 Long value = getLongConfig(key);
199 checkExists(key, value);
200 return value;
201 }
202
203 public static Long getLongSystemConfig(String key) {
204 return getLongConfig(getSystemKey(key));
205 }
206
207 public static Long getLongSystemConfig(String key, Long defaultValue) {
208 return getLongConfig(getSystemKey(key), defaultValue);
209 }
210
211 public static Long getMustExistsLongSystemConfig(String key) {
212 return getMustExistsLongConfig(getSystemKey(key));
213 }
214
215 public static Double getDoubleConfig(String key) {
216 String value = getStringConfig(key);
217 if (!StringUtils.isEmpty(value)) {
218 return Double.parseDouble(value);
219 }
220
221 return null;
222 }
223
224 public static Double getDoubleConfig(String key, Double defaultValue) {
225 return getValueOrDefault(getDoubleConfig(key), defaultValue);
226 }
227
228 public static Double getMustExistsDoubleConfig(String key) {
229 Double value = getDoubleConfig(key);
230 checkExists(key, value);
231 return value;
232 }
233
234 public static Double getDoubleSystemConfig(String key) {
235 return getDoubleConfig(getSystemKey(key));
236 }
237
238 public static Double getDoubleSystemConfig(String key, Double defaultValue) {
239 return getDoubleConfig(getSystemKey(key), defaultValue);
240 }
241
242 public static Double getMustExistsDoubleSystemConfig(String key) {
243 return getMustExistsDoubleConfig(getSystemKey(key));
244 }
245
246 public static Boolean getBooleanConfig(String key) {
247 String value = getStringConfig(key);
248 if (!StringUtils.isEmpty(value)) {
249 return Boolean.parseBoolean(value);
250 }
251
252 return null;
253 }
254
255 public static boolean getBooleanConfig(String key, boolean defaultValue) {
256 return getValueOrDefault(getBooleanConfig(key), defaultValue);
257 }
258
259 public static Boolean getMustExistsBooleanConfig(String key) {
260 Boolean value = getBooleanConfig(key);
261 checkExists(key, value);
262 return value;
263 }
264
265 public static Boolean getBooleanSystemConfig(String key) {
266 return getBooleanConfig(getSystemKey(key));
267 }
268
269 public static boolean getBooleanSystemConfig(String key, boolean defaultValue) {
270 return getBooleanConfig(getSystemKey(key), defaultValue);
271 }
272
273 public static Boolean getMustExistsBooleanSystemConfig(String key) {
274 return getMustExistsBooleanConfig(getSystemKey(key));
275 }
276
277 public static boolean checkConfig(String key){
278 checkInit();
279 if(StringUtils.isEmpty(key)){
280 return false;
281 }
282 String value = config.get(key.toUpperCase());
283 String outsideValue = config.get(getOutsideKey(key).toUpperCase());
284 return (!StringUtils.isEmpty(value)) || (!StringUtils.isEmpty(outsideValue));
285 }
286
287 public static boolean checkSystemConfig(String key){
288 return checkConfig(getSystemKey(key));
289 }
290
291 private static void checkInit() {
292 if (!inited) {
293 throw new ConfigException(
294 "ConfigurationUtils has not been inited yet!");
295 }
296 }
297
298 private static void checkExists(String key,Object value){
299 if (value == null) {
300 throw new ConfigException("The Configuration " + key
301 + " must be configed!");
302 }
303 }
304
305 private static <T> T getValueOrDefault(T value,T defaultValue){
306 return value == null ? defaultValue : value;
307 }
308
309 public static String getSystemKey(String key){
310 key = key == null ? "" : key;
311 StringBuilder builder = new StringBuilder();
312 builder.append(
313 getMustExistsStringConfig(ConfigurationConstants.PLATFORM_SYSTEM_IDENTIFIER).toUpperCase())
314 .append(".").append(key);
315 return builder.toString();
316 }
317
318 public static String getOutsideKey(String key){
319 key = key == null ? "" : key;
320 StringBuilder str = new StringBuilder();
321 str.append(key).append(
322 ConfigurationConstants.PLATFORM_SUFFIX_CONFIG.toUpperCase());
323 return str.toString();
324 }
325
326 private static Properties copyProperties(Map<String,String> map) {
327 Properties result = new Properties();
328 Iterator<String> iterator = map.keySet().iterator();
329 while (iterator.hasNext()) {
330 String key = iterator.next();
331 String value = map.get(key);
332 result.setProperty(key, value);
333 }
334 return result;
335 }
336
337 private static Map<String,String> copyMap(Map<String,String> map) {
338 Map<String,String> result = new HashMap<String,String>();
339 Iterator<String> iterator = map.keySet().iterator();
340 while (iterator.hasNext()) {
341 String key = iterator.next();
342 String value = map.get(key);
343 result.put(key, value);
344 }
345
346 return result;
347 }
348 }