forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfLoader.java
More file actions
303 lines (275 loc) · 9.98 KB
/
ConfLoader.java
File metadata and controls
303 lines (275 loc) · 9.98 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package act.conf;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static act.conf.ConfigKey.KEY_COMMON_CONF_TAG;
import act.Act;
import act.app.ProjectLayout;
import act.app.RuntimeDirs;
import act.util.LogSupport;
import act.util.SysProps;
import org.osgl.util.C;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Loading configurations from conf file or conf dir
*/
public abstract class ConfLoader<T extends Config> extends LogSupport {
public T load() {
Map<String, ?> rawConf = new HashMap<>();
Properties sysProps = System.getProperties();
rawConf.putAll((Map) sysProps);
rawConf = processConf(rawConf);
return create(rawConf);
}
public T load(File resourceRoot) {
// load conf from disk
Map<String, Object> rawConf = null == resourceRoot ? new HashMap<>() : loadConfFromDisk(resourceRoot);
// load conf from System.properties
Properties sysProps = System.getProperties();
rawConf.putAll((Map) sysProps);
// load conf from Environment variable
Map<String, String> envMap = System.getenv();
for (Map.Entry<String, String> entry : envMap.entrySet()) {
String key = entry.getKey();
if (key.startsWith("act_env_")) {
key = Config.canonical(key.substring(8));
rawConf.put(key, entry.getValue());
}
}
// strip off "act." prefix if has any
// canonical all keys
rawConf = processConf(rawConf);
processScanPackage(rawConf);
// initialize the configuration with all loaded data
return create(rawConf);
}
/**
* Return the "common" configuration set name. By default it is "common"
* @return the "common" conf set name
*/
public static String common() {
String common = SysProps.get(KEY_COMMON_CONF_TAG);
if (S.blank(common)) {
common = "common";
}
return common;
}
/**
* Return the name of the current conf set
* @return the conf set name
*/
public static String confSetName() {
String profile = SysProps.get(AppConfigKey.PROFILE.key());
if (S.blank(profile)) {
profile = Act.mode().name().toLowerCase();
}
return profile;
}
protected abstract T create(Map<String, ?> rawConf);
protected abstract String confFileName();
private Map loadConfFromDisk(File conf) {
if (conf.isDirectory()) {
return loadConfFromDir(conf);
} else if (conf.getName().endsWith(".jar")) {
return loadConfFromJar(conf);
} else {
return loadConfFromFile(conf);
}
}
private Map loadConfFromJar(File jarFile) {
boolean traceEnabled = isTraceEnabled();
if (traceEnabled) {
trace("loading app conf from jar file: %s", jarFile);
}
TreeMap<String, JarEntry> map = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String path1, String path2) {
boolean path1IsRoot = path1.contains("/");
boolean path2IsRoot = path2.contains("/");
if (path1IsRoot && path2IsRoot) {
return path1.compareTo(path2);
}
if (path1IsRoot) {
return -1;
} else if (path2IsRoot) {
return 1;
}
if (path1.startsWith("conf/")) {
path1 = path1.substring(5);
path2 = path2.substring(5);
return compare(path1, path2);
}
boolean path1IsCommon = path1.startsWith("common/");
boolean path2IsCommon = path2.startsWith("common/");
if (path1IsCommon && path2IsCommon) {
return path1.compareTo(path2);
}
if (path1IsCommon) {
return -1;
} else if (path2IsCommon) {
return 1;
}
return path1.compareTo(path2);
}
});
try (JarFile jar = new JarFile(jarFile)) {
String profile = Act.profile();
for (JarEntry entry : C.enumerable(jar.entries())) {
String name = entry.getName();
if (isAppProperties(name, profile)) {
if (traceEnabled) {
trace("found jar entry for app properties: %s", name);
}
map.put(name, entry);
}
}
Map conf = new HashMap();
for (Map.Entry<String, JarEntry> entry : map.entrySet()) {
Properties p = new Properties();
try {
if (traceEnabled) {
trace("loading app properties from jar entry: %s", entry.getKey());
}
p.load(jar.getInputStream(entry.getValue()));
for (Map.Entry<Object, Object> pEntry: p.entrySet()) {
conf.put(Config.canonical(S.string(entry.getKey())), entry.getValue());
}
} catch (IOException e) {
logger.warn("Error loading %s from jar file: %s", entry.getKey(), jarFile);
}
}
return conf;
} catch (IOException e) {
warn(e, "error opening jar file: %s", jarFile);
}
return new HashMap<>();
}
private boolean isAppProperties(String name, String profile) {
if (!name.endsWith(".properties")) {
return false;
}
if (name.startsWith("conf/")) {
String name0 = name.substring(5);
if (!name0.contains("/") || name0.startsWith("common")) {
return true;
}
return !S.blank(profile) && name0.startsWith(profile + "/");
}
return !name.contains("/") && !name.startsWith("act") && !name.startsWith("build.");
}
private Map loadConfFromFile(File conf) {
if (isTraceEnabled()) {
trace("loading app conf from file: %s", conf);
}
if (!conf.canRead()) {
logger.warn("Cannot read conf file[%s]", conf.getAbsolutePath());
return new HashMap<>();
}
InputStream is = IO.inputStream(conf);
Properties p = new Properties();
try {
p.load(is);
return p;
} catch (Exception e) {
logger.warn(e, "Error loading %s", confFileName());
} finally {
IO.close(is);
}
return new HashMap<>();
}
// trim "act." from conf keys
private static Map<String, Object> processConf(Map<String, ?> conf) {
Map<String, Object> m = new HashMap<String, Object>(conf.size());
for (String s : conf.keySet()) {
Object o = conf.get(s);
if (s.startsWith("act.")) s = s.substring(4);
m.put(s, o);
m.put(Config.canonical(s), o);
}
return m;
}
private void processScanPackage(Map rawConfig) {
String scanPackage = (String) rawConfig.get(AppConfigKey.SCAN_PACKAGE.key());
if (null == scanPackage) {
Object v = rawConfig.get(AppConfigKey.SCAN_PACKAGE_SYS.key());
rawConfig.put(AppConfigKey.SCAN_PACKAGE.key(), v);
}
}
private Map loadConfFromDir(File resourceDir) {
if (!resourceDir.exists()) {
logger.warn("Cannot read conf dir[%s]", resourceDir.getAbsolutePath());
return new HashMap<>();
}
Map map = new HashMap<>();
/*
* Load from resources root
*/
map.putAll(loadConfFromDir_(resourceDir));
/*
* Load from resources/conf root
*/
File confDir = ProjectLayout.Utils.file(resourceDir, RuntimeDirs.CONF);
map.putAll(loadConfFromDir_(confDir));
/*
* try load from resources/conf/common conf
*/
String common = common();
File commonConfDir = new File(confDir, common);
if (commonConfDir.isDirectory()) {
map.putAll(loadConfFromDir_(commonConfDir));
}
/*
* try to load conf from profile conf dir, e.g. ${conf_root}/uat or
* ${conf_root}/dev etc
*/
String profile = confSetName();
logger.debug("Loading conf profile: %s", profile);
File taggedConfDir = new File(confDir, profile);
if (taggedConfDir.exists() && taggedConfDir.isDirectory()) {
map.putAll(loadConfFromDir_(taggedConfDir));
}
return map;
}
private Map loadConfFromDir_(File confDir) {
File[] confFiles = confDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".properties") || name.endsWith(".conf");
}
});
if (null == confFiles) {
return C.Map();
} else {
Map map = new HashMap<>();
int n = confFiles.length;
for (int i = 0; i < n; ++i) {
map.putAll(loadConfFromFile(confFiles[i]));
}
return map;
}
}
}