|
@@ -29,30 +29,66 @@ public class ConfigLoader {
|
29
|
29
|
private static String START_PLACEHOLDER = "${";
|
30
|
30
|
private static String END_PLACEHOLDER = "}";
|
31
|
31
|
|
32
|
|
-
|
33
|
|
- private enum ConfigProtocol {
|
|
32
|
+ private enum MapperFactoryEnum {
|
34
|
33
|
/**
|
35
|
|
- * 文件
|
|
34
|
+ * yml
|
36
|
35
|
*/
|
37
|
|
- FILE(1),
|
|
36
|
+ YML("yml", "com.fasterxml.jackson.dataformat.yaml.YAMLFactory"),
|
38
|
37
|
/**
|
39
|
|
- * 类路径
|
|
38
|
+ * yaml
|
40
|
39
|
*/
|
41
|
|
- CLASSPATH(0);
|
42
|
|
-
|
|
40
|
+ YAML("yaml", "com.fasterxml.jackson.dataformat.yaml.YAMLFactory"),
|
|
41
|
+ /**
|
|
42
|
+ * properties
|
|
43
|
+ */
|
|
44
|
+ PROPERTIES("properties", "com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper"),
|
43
|
45
|
/**
|
44
|
|
- * 配置文件优先级
|
|
46
|
+ * default json
|
45
|
47
|
*/
|
46
|
|
- private int order;
|
|
48
|
+ JSON("json", ObjectMapper.class.getCanonicalName());
|
|
49
|
+ private String name;
|
|
50
|
+ private Class<?> factory;
|
|
51
|
+
|
|
52
|
+ MapperFactoryEnum(String name, String className) {
|
|
53
|
+ this.name = name;
|
|
54
|
+ this.factory = findClass(className);
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ private static Class<?> findClass(String classname) {
|
|
58
|
+ try {
|
|
59
|
+ return Class.forName(classname);
|
|
60
|
+ } catch (ClassNotFoundException ignored) {
|
|
61
|
+ }
|
|
62
|
+ return null;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public String getName() {
|
|
66
|
+ return name;
|
|
67
|
+ }
|
47
|
68
|
|
48
|
|
- ConfigProtocol(int order) {
|
49
|
|
- this.order = order;
|
|
69
|
+ public Class<?> getFactory() {
|
|
70
|
+ return factory;
|
50
|
71
|
}
|
51
|
72
|
|
52
|
|
- public static List<ConfigProtocol> sortList() {
|
53
|
|
- List<ConfigProtocol> configProtocols = new ArrayList<>(Arrays.asList(values()));
|
54
|
|
- configProtocols.sort((o1, o2) -> o2.order - o1.order);
|
55
|
|
- return configProtocols;
|
|
73
|
+ public ObjectMapper getObjectMapper() {
|
|
74
|
+ if (factory == null) {
|
|
75
|
+ return null;
|
|
76
|
+ }
|
|
77
|
+ try {
|
|
78
|
+ final Object o = factory.newInstance();
|
|
79
|
+ ObjectMapper objectMapper;
|
|
80
|
+ if (this == MapperFactoryEnum.YML || this == MapperFactoryEnum.YAML) {
|
|
81
|
+ objectMapper = new ObjectMapper((JsonFactory) o);
|
|
82
|
+ } else {
|
|
83
|
+ objectMapper = (ObjectMapper) o;
|
|
84
|
+ }
|
|
85
|
+ objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
86
|
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
87
|
+ return objectMapper;
|
|
88
|
+ } catch (InstantiationException | IllegalAccessException e) {
|
|
89
|
+ e.printStackTrace();
|
|
90
|
+ }
|
|
91
|
+ return null;
|
56
|
92
|
}
|
57
|
93
|
}
|
58
|
94
|
|
|
@@ -63,38 +99,35 @@ public class ConfigLoader {
|
63
|
99
|
* @param profile
|
64
|
100
|
*/
|
65
|
101
|
void activeProfile(String profile);
|
|
102
|
+
|
66
|
103
|
}
|
67
|
104
|
|
|
105
|
+
|
68
|
106
|
/**
|
69
|
107
|
* 返回数据流
|
70
|
108
|
*
|
71
|
|
- * @param configProtocol
|
72
|
109
|
* @param path
|
73
|
110
|
* @return 结果可能为 null
|
74
|
111
|
*/
|
75
|
|
- private static InputStream getConfigFileAsStream(ConfigProtocol configProtocol, String path) {
|
76
|
|
-
|
77
|
|
- switch (configProtocol) {
|
78
|
|
- case FILE:
|
79
|
|
- try {
|
80
|
|
- return new FileInputStream(new File(DEFAULT_FILE_CONFIG_FOLDER + File.separator + path));
|
81
|
|
- } catch (FileNotFoundException ignored) {
|
82
|
|
- }
|
83
|
|
- return null;
|
84
|
|
- case CLASSPATH:
|
85
|
|
- return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
|
86
|
|
- default:
|
87
|
|
- throw new Error("not support this path = " + path);
|
|
112
|
+ private static InputStream getConfigFileAsStream(String path) {
|
|
113
|
+ try {
|
|
114
|
+ // from config path
|
|
115
|
+ final File file = new File(DEFAULT_FILE_CONFIG_FOLDER + File.separator + path);
|
|
116
|
+ if (file.exists()) {
|
|
117
|
+ return new FileInputStream(file);
|
|
118
|
+ }
|
|
119
|
+ } catch (FileNotFoundException ignored) {
|
88
|
120
|
}
|
|
121
|
+ // from class path
|
|
122
|
+ return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
|
89
|
123
|
}
|
90
|
124
|
|
91
|
|
-
|
92
|
125
|
public static <T> T parseConfig(Class<T> clazz) {
|
93
|
126
|
return parseConfig(null, clazz, null);
|
94
|
127
|
}
|
95
|
128
|
|
96
|
129
|
public static <T> T parseConfig(String[] args, Class<T> clazz, ConfigListener configListener) {
|
97
|
|
- LinkedHashMap map = loaderConfig(args, configListener);
|
|
130
|
+ Map map = loaderConfig(args, configListener);
|
98
|
131
|
return getObjectMapper().convertValue(map, clazz);
|
99
|
132
|
}
|
100
|
133
|
|
|
@@ -104,7 +137,7 @@ public class ConfigLoader {
|
104
|
137
|
}
|
105
|
138
|
|
106
|
139
|
public static <T> T parseConfig(String[] args, TypeReference<T> toValueTypeRef, ConfigListener configListener) {
|
107
|
|
- LinkedHashMap map = loaderConfig(args, configListener);
|
|
140
|
+ Map map = loaderConfig(args, configListener);
|
108
|
141
|
return getObjectMapper().convertValue(map, toValueTypeRef);
|
109
|
142
|
}
|
110
|
143
|
|
|
@@ -120,90 +153,75 @@ public class ConfigLoader {
|
120
|
153
|
*
|
121
|
154
|
* @return
|
122
|
155
|
*/
|
123
|
|
- public static LinkedHashMap loaderConfig() {
|
|
156
|
+ public static Map loaderConfig() {
|
124
|
157
|
return loaderConfig(null, null);
|
125
|
158
|
}
|
126
|
159
|
|
|
160
|
+
|
127
|
161
|
/**
|
128
|
162
|
* 加载指定 profile 的配置文件
|
129
|
163
|
*
|
130
|
164
|
* @param args
|
131
|
165
|
* @return
|
132
|
166
|
*/
|
133
|
|
- public static LinkedHashMap loaderConfig(String[] args, ConfigListener configListener) {
|
134
|
|
-
|
135
|
|
- List<ConfigProtocol> configProtocols = ConfigProtocol.sortList();
|
136
|
|
- LinkedHashMap defaultProperties = null;
|
137
|
|
- ConfigProtocol matchProtocol = null;
|
138
|
|
- for (ConfigProtocol protocol : configProtocols) {
|
139
|
|
- LinkedHashMap temp = selectConfig(protocol, DEFAULT_APPLICATION);
|
140
|
|
- if (temp != null) {
|
141
|
|
- defaultProperties = temp;
|
142
|
|
- matchProtocol = protocol;
|
143
|
|
- break;
|
144
|
|
- }
|
145
|
|
- }
|
|
167
|
+ public static Map loaderConfig(String[] args, ConfigListener configListener) {
|
|
168
|
+
|
|
169
|
+ Map defaultProperties = selectConfigPosition(DEFAULT_APPLICATION);
|
146
|
170
|
if (defaultProperties == null) {
|
147
|
171
|
return null;
|
148
|
172
|
}
|
149
|
|
-
|
150
|
173
|
String profile = getProfileByArgs(args);
|
151
|
|
-
|
152
|
174
|
if (profile == null || profile.length() == 0) {
|
153
|
175
|
profile = getActiveProfile(defaultProperties);
|
154
|
176
|
}
|
155
|
177
|
if (configListener != null) {
|
|
178
|
+ // callback
|
156
|
179
|
configListener.activeProfile(profile);
|
157
|
180
|
}
|
158
|
|
- LinkedHashMap map = selectConfig(matchProtocol, DEFAULT_APPLICATION + "-" + profile);
|
159
|
|
- if (map != null) {
|
160
|
|
- removeActiveProfile(map);
|
|
181
|
+ Map profileMap = selectConfigPosition(DEFAULT_APPLICATION + "-" + profile);
|
|
182
|
+ if (profileMap != null) {
|
|
183
|
+ removeActiveProfile(profileMap);
|
161
|
184
|
//noinspection unchecked
|
162
|
|
- defaultProperties.putAll(map);
|
|
185
|
+ defaultProperties.putAll(profileMap);
|
163
|
186
|
}
|
164
|
187
|
return propertiesHandler(defaultProperties);
|
165
|
188
|
}
|
166
|
189
|
|
167
|
190
|
|
168
|
|
- private static LinkedHashMap selectConfig(ConfigProtocol configProtocol, String filePrefix) {
|
169
|
|
- InputStream configFileAsStream = getConfigFileAsStream(configProtocol, filePrefix + ".yml");
|
170
|
|
- if (configFileAsStream == null) {
|
171
|
|
- configFileAsStream = getConfigFileAsStream(configProtocol, filePrefix + ".yaml");
|
172
|
|
- }
|
173
|
|
- Object yamlFactory = null;
|
174
|
|
- try {
|
175
|
|
- Class<?> factory = Class.forName("com.fasterxml.jackson.dataformat.yaml.YAMLFactory");
|
176
|
|
- yamlFactory = factory.newInstance();
|
177
|
|
- } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
|
178
|
|
- // e.printStackTrace();
|
179
|
|
- }
|
180
|
|
- ObjectMapper objectMapper = null;
|
181
|
|
- if (configFileAsStream != null && yamlFactory != null) {
|
182
|
|
- objectMapper = new ObjectMapper((JsonFactory) yamlFactory);
|
183
|
|
- } else {
|
184
|
|
- configFileAsStream = getConfigFileAsStream(configProtocol, filePrefix + ".properties");
|
185
|
|
- try {
|
186
|
|
- Class<?> javaProps = Class.forName("com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper");
|
187
|
|
- objectMapper = (ObjectMapper) javaProps.newInstance();
|
188
|
|
- } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
|
189
|
|
- // e.printStackTrace();
|
|
191
|
+ private static Map selectConfigPosition(String baseFilename) {
|
|
192
|
+ InputStream configFileAsStream = null;
|
|
193
|
+ MapperFactoryEnum parseType = null;
|
|
194
|
+ for (MapperFactoryEnum value : MapperFactoryEnum.values()) {
|
|
195
|
+ if (value.getFactory() == null) {
|
|
196
|
+ continue;
|
|
197
|
+ }
|
|
198
|
+ configFileAsStream = getConfigFileAsStream(baseFilename + "." + value.getName());
|
|
199
|
+ if (configFileAsStream != null) {
|
|
200
|
+ parseType = value;
|
|
201
|
+ break;
|
190
|
202
|
}
|
191
|
203
|
}
|
192
|
|
- if (objectMapper == null) {
|
193
|
|
- throw new Error("jackson dependencies at least container [jackson-dataformat-yaml] or [jackson-dataformat-properties].");
|
|
204
|
+ if (parseType == null) {
|
|
205
|
+ return null;
|
194
|
206
|
}
|
195
|
|
- if (configFileAsStream == null) {
|
|
207
|
+ ObjectMapper objectMapper = parseType.getObjectMapper();
|
|
208
|
+ if (objectMapper == null) {
|
196
|
209
|
return null;
|
197
|
210
|
}
|
198
|
211
|
try {
|
199
|
212
|
return objectMapper.readValue(configFileAsStream, LinkedHashMap.class);
|
200
|
213
|
} catch (IOException e) {
|
201
|
214
|
e.printStackTrace();
|
|
215
|
+ } finally {
|
|
216
|
+ try {
|
|
217
|
+ configFileAsStream.close();
|
|
218
|
+ } catch (IOException e) {
|
|
219
|
+ e.printStackTrace();
|
|
220
|
+ }
|
202
|
221
|
}
|
203
|
222
|
return null;
|
204
|
223
|
}
|
205
|
224
|
|
206
|
|
-
|
207
|
225
|
private static String getActiveProfile(Map fileMap) {
|
208
|
226
|
Object profile = fileMap.get(PROFILE);
|
209
|
227
|
if (profile instanceof Map) {
|
|
@@ -228,7 +246,7 @@ public class ConfigLoader {
|
228
|
246
|
* @param map
|
229
|
247
|
* @return
|
230
|
248
|
*/
|
231
|
|
- private static LinkedHashMap propertiesHandler(LinkedHashMap map) {
|
|
249
|
+ private static Map propertiesHandler(Map map) {
|
232
|
250
|
Map<Object, String> preHandler = new LinkedHashMap<>();
|
233
|
251
|
// find placeholder value
|
234
|
252
|
findPreResolveProperties(map, preHandler, null);
|
|
@@ -358,8 +376,8 @@ public class ConfigLoader {
|
358
|
376
|
if (args != null && args.length > 0) {
|
359
|
377
|
for (String arg : args) {
|
360
|
378
|
arg = arg.trim();
|
361
|
|
- if (arg.startsWith("--profile=")) {
|
362
|
|
- return arg.substring("--profile=".length());
|
|
379
|
+ if (arg.startsWith("--profile.active=")) {
|
|
380
|
+ return arg.substring("--profile.active=".length());
|
363
|
381
|
}
|
364
|
382
|
}
|
365
|
383
|
}
|