-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.java
More file actions
182 lines (157 loc) · 4.62 KB
/
Config.java
File metadata and controls
182 lines (157 loc) · 4.62 KB
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
package uyun.show.server.domain.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uyun.whale.common.util.data.AbstractDynamicObject;
import uyun.whale.common.util.error.ErrorUtil;
import uyun.whale.common.util.system.SystemConfig;
import uyun.whale.common.util.text.TextUtil;
import java.io.*;
import java.util.*;
public class Config extends AbstractDynamicObject {
private static final String MICRO_START = "${";
private static final String MICRO_END = "}";
private static final Logger logger = LoggerFactory.getLogger(Config.class);
private static Config instance;
private Properties remoteProperties = new Properties();
private Properties localProperties = new Properties();
public synchronized static Config getInstance() {
if (instance == null) {
instance = new Config();
}
return instance;
}
public Config() {
loadLocalProperties();
}
Properties getRemoteProperties() {
return remoteProperties;
}
Properties getLocalProperties() {
return localProperties;
}
private void loadLocalProperties() {
if (!loadLocalProperties("show-server.properties") || !loadLocalProperties("common.properties"))
logger.warn("Cannot load default local properties files");
loadLocalProperties("common.properties");
loadLocalProperties("show-server.properties");
}
private boolean loadLocalProperties(String filename) {
InputStream is;
File file = searchFile(filename);
if (file == null) {
return false;
}
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
ErrorUtil.warn(logger, "file not exists: " + file, e);
logger.info("Load config from default");
is = Config.class.getResourceAsStream("config-default.properties");
}
try {
localProperties.load(is);
} catch (Throwable e) {
ErrorUtil.warn(logger, "read default config failed", e);
}
try {
is.close();
} catch (IOException e) {
ErrorUtil.warn(logger, "close file failed", e);
}
return true;
}
private static File searchFile(String filename) {
String[] searchPaths = new String[] {
"./",
"/conf/lang",
"/conf/",
"/../conf/",
"/../../conf/",
"/config/",
"/lib/",
"/../lib/",
"/../front/src/main/resources/conf/",
"/../../build/src/main/resources/conf/",
"/../../front/build/src/main/resources/conf/",
};
String userDir = SystemConfig.getUserDir();
for (String path : searchPaths) {
File file = new File(userDir, path + filename);
logger.debug("Test config file: {}", file);
if (file.exists()) {
logger.info("Load config from :" + file);
return file;
}
}
return null;
}
private static String replaceAll(String text, String findString, String replaceString) {
while (true) {
int pos = text.indexOf(findString);
if (pos < 0)
break;
text = text.substring(0, pos) + replaceString + text.substring(pos + findString.length());
}
return text;
}
private String searchProperty(String key) {
Object value = remoteProperties.get(key);
if (value == null) {
value = System.getProperty(key);
if (value == null)
value = localProperties.get(key);
}
return value == null ? null : value.toString();
}
public Object get(String key) {
String value = ConfigUtils.getValue(key, searchProperty(key));
if (value != null) {
while (true) {
String item = TextUtil.between(value, MICRO_START, MICRO_END);
if (item == null)
break;
String itemValue = searchProperty(item);
if (itemValue != null)
value = replaceAll(value, MICRO_START + item + MICRO_END, itemValue);
else
throw new RuntimeException("undefined variable: " + MICRO_START + item + MICRO_END);
}
}
return value;
}
@Override
@SuppressWarnings("unused")
public void set(String s, Object o) {
localProperties.setProperty(s, o.toString());
}
public boolean isDeveloperMode() {
return get("bat.developer.mode", false);
}
public boolean isChinese() {
return "zh_CN".equals(get("uyun.lang", "zh_CN"));
}
public void dumpConfigItem() {
if (!logger.isInfoEnabled())
return;
Enumeration<?> iter = remoteProperties.propertyNames();
List<String> names = new ArrayList<>();
while (iter.hasMoreElements()) {
names.add(iter.nextElement().toString());
}
iter = localProperties.propertyNames();
names = new ArrayList<>();
while (iter.hasMoreElements()) {
names.add(iter.nextElement().toString());
}
Collections.sort(names);
StringBuilder sb = new StringBuilder();
sb.append("config summary: \n");
int i = 1;
for (String key : names) {
String value = get(key, (String)null);
sb.append(String.format("%3d. %-40s = %s\n", i, key, value));
i++;
}
logger.info(sb.toString());
}
}