/
AppConfig.java
332 lines (299 loc) 路 13.9 KB
/
AppConfig.java
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* This file is part of Openrouteservice.
*
* Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this library;
* if not, see <https://www.gnu.org/licenses/>.
*/
package org.heigit.ors.config;
import com.graphhopper.util.Helper;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigValue;
import org.apache.log4j.Logger;
import org.heigit.ors.util.FileUtility;
import org.heigit.ors.util.StringUtility;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AppConfig {
public static final String PREFIX_ORS_SERVICES = "ors.services.";
private Config config;
private static AppConfig global;
private static String osmMd5Hash = null;
private static final Logger LOGGER = Logger.getLogger(AppConfig.class.getName());
private static final String SERVICE_NAME_ROUTING = "routing";
private static final String SERVICE_NAME_ISOCHRONES = "isochrones";
public AppConfig(String path) {
File file = new File(path);
config = ConfigFactory.parseFile(file);
}
public AppConfig() {
try {
File configFile;
String appConfigResource = "app.config";
if (System.getProperty("ors_config") != null) {
configFile = new FileSystemResource(System.getProperty("ors_config")).getFile();
LOGGER.info("System property 'ors_config' used as configuration path");
} else if (System.getProperty("ors_app_config") != null) {
configFile = new FileSystemResource(System.getProperty("ors_app_config")).getFile();
LOGGER.info("System property 'ors_app_config' used as configuration path");
LOGGER.warn("""
DEPRECATION NOTICE: The system property 'ors_app_config' will be not be supported in the\
future\
""");
LOGGER.warn("Use 'ors_config' instead");
} else if (System.getenv("ORS_CONFIG") != null) {
configFile = new FileSystemResource(System.getenv("ORS_CONFIG")).getFile();
LOGGER.info("Environment variable 'ORS_CONFIG' used as configuration path");
} else if (System.getenv("ORS_APP_CONFIG") != null) {
configFile = new ClassPathResource(System.getenv("ORS_APP_CONFIG")).getFile();
LOGGER.info("Environment variable 'ORS_APP_CONFIG' used as configuration path");
LOGGER.warn("""
DEPRECATION NOTICE: The Environment variable 'ORS_APP_CONFIG' will be not be supported\
in the future\
""");
LOGGER.warn("Use 'ORS_CONFIG' instead");
} else if (new ClassPathResource("ors-config.json").isFile()) {
configFile = new ClassPathResource("ors-config.json").getFile();
LOGGER.info("Default path of 'ors-config.json' used for configuration");
if (new ClassPathResource(appConfigResource).isFile()) {
LOGGER.warn("""
DEPRECATION NOTICE: You seem to have an unused 'app.config' file, which won't be \
supported in the future\
""");
}
} else if (new ClassPathResource(appConfigResource).isFile()) {
configFile = new ClassPathResource(appConfigResource).getFile();
LOGGER.info("Deprecated path of 'app.config' used");
LOGGER.warn("""
DEPRECATION NOTICE: The used 'app.config' configuration path will not be supported in the \
future.\
""");
LOGGER.warn("Use 'ors-config.json' instead.");
} else {
throw new IOException("""
No valid configuration file found in 'ors-api/src/main/resources'. \
Did you copy ors-config-sample.json to ors-config.json?\
""");
}
LOGGER.info("Loading configuration from " + configFile);
config = ConfigFactory.parseFile(configFile);
config = overrideFromEnvVariables(config);
LOGGER.warn("Deprecation notice: Old configuration method with JSON files is deprecated. Switch to ors-config.yml files!");
} catch (IOException ioe) {
// no deprecated JSON config found
}
//Modification by H Leuschner: Save md5 hash of map file in static String for access with every request
String graphPath = getServiceParameter("routing.profiles.default_params", "graphs_root_path");
if (graphPath != null) {
File graphsDir = new File(graphPath);
File[] md5Files = graphsDir.listFiles(pathname -> pathname.getName().endsWith(".md5"));
if (md5Files != null && md5Files.length == 1) {
try {
osmMd5Hash = FileUtility.readFile(md5Files[0].toString()).trim();
} catch (IOException e) {
LOGGER.error(e);
}
}
}
}
public static AppConfig getGlobal() {
if (global == null)
global = new AppConfig();
return global;
}
public String getParameter(String section, String paramName) {
try {
return config.getString("ors." + section + "." + paramName);
} catch (Exception e) {
// IGNORE
}
return null;
}
public List<String> getStringList(String path) {
try {
return config.getStringList(path);
} catch (Exception e) {
// IGNORE
}
return new ArrayList<>();
}
public String getServiceParameter(String serviceName, String paramName) {
try {
return config.getString(PREFIX_ORS_SERVICES + serviceName + "." + paramName);
} catch (Exception e) {
// IGNORE
}
return null;
}
private Config overrideFromEnvVariables(Config baseConfig) {
if (System.getenv("GRAPHS_FOLDER") != null) {
LOGGER.info("Environment variable 'GRAPHS_FOLDER' used as graphs folder path");
String graphsFolderPath = System.getenv("GRAPHS_FOLDER");
Config newConfig = ConfigFactory.parseString("ors.services.routing.profiles.default_params.graphs_root_path=".concat(graphsFolderPath));
baseConfig = newConfig.withFallback(baseConfig);
}
if (System.getenv("ELEVATION_CACHE_FOLDER") != null) {
LOGGER.info("Environment variable 'ELEVATION_CACHE_FOLDER' used as elevation cache folder path");
String elevationCacheFolder = System.getenv("ELEVATION_CACHE_FOLDER");
Config newConfig = ConfigFactory.parseString("ors.services.routing.profiles.default_params.elevation_cache_path=".concat(elevationCacheFolder));
baseConfig = newConfig.withFallback(baseConfig);
}
if (System.getenv("PBF_FILE_PATH") != null) {
LOGGER.info("Environment variable 'PBF_FILE_PATH' used as pbf file path");
String pbfPath = System.getenv("PBF_FILE_PATH");
Config newConfig = ConfigFactory.parseString("ors.services.routing.sources=[".concat(pbfPath).concat("]"));
baseConfig = newConfig.withFallback(baseConfig);
}
if (System.getenv("LOGS_FOLDER") != null) {
LOGGER.info("Environment variable 'LOGS_FOLDER' used as logs folder path");
String logsFolder = System.getenv("LOGS_FOLDER");
Config newConfig = ConfigFactory.parseString("ors.logging.location=".concat(logsFolder));
baseConfig = newConfig.withFallback(baseConfig);
}
return baseConfig;
}
public String getRoutingProfileParameter(String profile, String paramName) {
try {
String rootPath = PREFIX_ORS_SERVICES + "routing.profiles";
ConfigObject configObj = config.getObject(rootPath);
for (String key : configObj.keySet()) {
if (key.startsWith("profile-")) {
String profileName = getServiceParameter(SERVICE_NAME_ROUTING, "profiles." + key + ".profiles");
if (profile.equals(profileName)) {
String profileValue = getServiceParameter(SERVICE_NAME_ROUTING, "profiles." + key + ".parameters." + paramName);
if (profileValue != null) {
return profileValue;
}
}
}
}
return config.getString(rootPath + ".default_params." + paramName);
} catch (Exception e) {
// IGNORE
}
return null;
}
public List<? extends ConfigObject> getObjectList(String serviceName, String paramName) {
try {
return config.getObjectList(PREFIX_ORS_SERVICES + serviceName + "." + paramName);
} catch (Exception e) {
// IGNORE
}
return new ArrayList<>();
}
public boolean getBoolean(String path) {
try {
ConfigObject configObj = config.getObject("ors");
return configObj.toConfig().getBoolean(path);
} catch (Exception e) {
// IGNORE
}
return false;
}
public double getDouble(String path) {
try {
ConfigObject configObj = config.getObject("ors");
return configObj.toConfig().getDouble(path);
} catch (Exception e) {
// IGNORE
}
return Double.NaN;
}
public List<? extends ConfigObject> getObjectList(String paramName) {
try {
return config.getObjectList("ors." + paramName);
} catch (Exception e) {
// IGNORE
}
return new ArrayList<>();
}
public List<Double> getDoubleList(String serviceName, String paramName) {
try {
return config.getDoubleList(PREFIX_ORS_SERVICES + serviceName + "." + paramName);
} catch (Exception e) {
// IGNORE
}
return new ArrayList<>();
}
public List<String> getServiceParametersList(String serviceName, String paramName) {
try {
return config.getStringList(PREFIX_ORS_SERVICES + serviceName + "." + paramName);
} catch (Exception e) {
// IGNORE
}
return new ArrayList<>();
}
public static boolean hasValidMD5Hash() {
return osmMd5Hash != null;
}
public static String getMD5Hash() {
return osmMd5Hash;
}
public Map<String, Object> getServiceParametersMap(String serviceName, String paramName, boolean quotedStrings) {
Map<String, Object> result = null;
try {
String rootPath = PREFIX_ORS_SERVICES + serviceName + "." + paramName;
ConfigObject configObj = config.getObject(rootPath);
result = new HashMap<>();
for (String key : configObj.keySet()) {
Object value = null;
ConfigValue paramValue = config.getValue(rootPath + "." + key);
switch (paramValue.valueType()) {
case NUMBER:
case LIST:
case BOOLEAN:
value = paramValue.unwrapped();
break;
case OBJECT:
value = getServiceParametersMap(serviceName, paramName + "." + key, quotedStrings);
break;
case STRING:
if (quotedStrings)
value = paramValue.render();
else
value = StringUtility.trim(paramValue.render(), '"');
break;
default:
break;
}
result.put(key, value);
}
} catch (Exception ex) {
// IGNORE
}
return result;
}
public static String getRoutingParameter(String paramName) {
return getGlobal().getServiceParameter(SERVICE_NAME_ROUTING, paramName);
}
public static String getRoutingParameter(String paramName, boolean notNull) {
String value = getGlobal().getServiceParameter(SERVICE_NAME_ROUTING, paramName);
if (notNull && Helper.isEmpty(value))
throw new IllegalArgumentException("Parameter '" + paramName + "' must not be null or empty.");
return value;
}
public static List<String> getRoutingParametersList(String paramName) {
return getGlobal().getServiceParametersList(SERVICE_NAME_ROUTING, paramName);
}
public static Map<String, Object> getRoutingParametersMap(String paramName, boolean quotedStrings) {
return getGlobal().getServiceParametersMap(SERVICE_NAME_ROUTING, paramName, quotedStrings);
}
public static List<String> getIsochronesParametersList(String paramName) {
return getGlobal().getServiceParametersList(SERVICE_NAME_ISOCHRONES, paramName);
}
public static Map<String, Object> getIsochronesParametersMap(String paramName, boolean quotedStrings) {
return getGlobal().getServiceParametersMap(SERVICE_NAME_ISOCHRONES, paramName, quotedStrings);
}
}