Repository navigation
Expand file tree
/
Copy pathConfLoader.java
More file actions
168 lines (146 loc) · 4.8 KB
/
Copy pathConfLoader.java
File metadata and controls
168 lines (146 loc) · 4.8 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
package act.conf;
import act.Act;
import act.util.SysProps;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static act.conf.ConfigKey.KEY_COMMON_CONF_TAG;
/**
* Loading configurations from conf file or conf dir
*/
public abstract class ConfLoader<T extends Config> {
private static Logger logger = L.get(Act.class);
// 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);
}
return m;
}
public T load(File confFile) {
// load conf from disk
Map<String, ?> rawConf = null == confFile ? C.newMap() : loadConfFromDisk(confFile);
// load conf from System.properties
Properties sysProps = System.getProperties();
rawConf.putAll((Map) sysProps);
// strip off "act." prefix if has any
rawConf = processConf(rawConf);
// initialize the configuration with all loaded data
return create(rawConf);
}
protected abstract T create(Map<String, ?> rawConf);
protected abstract String confFileName();
private Map loadConfFromDisk(File conf) {
if (conf.isDirectory()) {
return loadConfFromDir(conf);
} else {
return loadConfFromFile(conf);
}
}
private Map loadConfFromFile(File conf) {
if (!conf.canRead()) {
logger.warn("Cannot read conf file[%s]", conf.getAbsolutePath());
return C.newMap();
}
InputStream is = null;
if (null == conf) {
ClassLoader cl = Act.class.getClassLoader();
is = cl.getResourceAsStream("/" + confFileName());
} else {
try {
is = new FileInputStream(conf);
} catch (IOException e) {
logger.warn(e, "Error opening conf file:" + conf);
}
}
if (null != is) {
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 C.newMap();
}
/**
* 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;
}
private Map loadConfFromDir(File confDir) {
if (!confDir.exists()) {
logger.warn("Cannot read conf dir[%s]", confDir.getAbsolutePath());
return C.newMap();
}
Map map = C.newMap();
/*
* try load from 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.info("Loading conf profile: %s", profile);
File taggedConfDir = new File(confDir, profile);
if (taggedConfDir.exists() && taggedConfDir.isDirectory()) {
map.putAll(loadConfFromDir_(taggedConfDir));
return map;
}
map.putAll(loadConfFromDir_(confDir));
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 = C.newMap();
int n = confFiles.length;
for (int i = 0; i < n; ++i) {
map.putAll(loadConfFromFile(confFiles[i]));
}
return map;
}
}
}