/
ConfigManager.java
307 lines (283 loc) · 11.1 KB
/
ConfigManager.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
package org.vufind.index;
/**
* VuFind configuration manager
*
* Copyright (C) Villanova University 2017.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
import java.io.File;
import java.io.FileReader;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import org.solrmarc.index.indexer.ValueIndexerFactory;
import org.solrmarc.tools.PropertyUtils;
import org.solrmarc.tools.SolrMarcIndexerException;
import org.ini4j.Ini;
import org.apache.log4j.Logger;
/**
* VuFind configuration manager
*/
public class ConfigManager
{
// Initialize logging category
static Logger logger = Logger.getLogger(ConfigManager.class.getName());
private static Map<String, Ini> configCache = new ConcurrentHashMap<>();
private static Map<String, ConcurrentHashMap<String, String>> sanitizedConfigCache = new ConcurrentHashMap<>();
private Properties vuFindConfigs = null;
private static ThreadLocal<ConfigManager> managerCache =
new ThreadLocal<ConfigManager>()
{
@Override
protected ConfigManager initialValue()
{
return new ConfigManager();
}
};
public ConfigManager()
{
try {
vuFindConfigs = PropertyUtils.loadProperties(ValueIndexerFactory.instance().getHomeDirs(), "vufind.properties");
} catch (IllegalArgumentException e) {
// If the properties load failed, don't worry about it -- we'll use defaults.
}
}
public static ConfigManager instance()
{
return managerCache.get();
}
/**
* Given the base name of a configuration file, locate the full path.
* @param filename base name of a configuration file
*/
private File findConfigFile(String filename) throws IllegalStateException
{
// Find VuFind's home directory in the environment; if it's not available,
// try using a relative path on the assumption that we are currently in
// VuFind's import subdirectory:
String vufindHome = System.getenv("VUFIND_HOME");
if (vufindHome == null) {
// this shouldn't happen since import-marc.sh and .bat always set VUFIND_HOME
throw new IllegalStateException("VUFIND_HOME must be set");
}
// Check for VuFind 2.0's local directory environment variable:
String vufindLocal = System.getenv("VUFIND_LOCAL_DIR");
// Get the relative VuFind path from the properties file, defaulting to
// the 2.0-style config/vufind if necessary.
String relativeConfigPath = PropertyUtils.getProperty(
vuFindConfigs, "vufind.config.relative_path", "config/vufind"
);
// Try several different locations for the file -- VuFind 2 local dir,
// VuFind 2 base dir, VuFind 1 base dir.
File file;
if (vufindLocal != null) {
file = new File(vufindLocal + "/" + relativeConfigPath + "/" + filename);
if (file.exists()) {
return file;
}
}
file = new File(vufindHome + "/" + relativeConfigPath + "/" + filename);
if (file.exists()) {
return file;
}
file = new File(vufindHome + "/web/conf/" + filename);
return file;
}
/**
* Sanitize a VuFind configuration setting.
* @param str configuration setting
*/
private String sanitizeConfigSetting(String str)
{
// Work on a copy of the string.
// We do not want the sanitizer to update the cache, because it might
// cause problems when executing them multiple times, like
// e.g. in multithreaded scenarios.
String retVal = new String(str);
// Drop comments if necessary; if the semi-colon is inside quotes, leave
// it alone. TODO: handle complex cases with comment AND quoted semi-colon
int pos = retVal.indexOf(';');
if (pos >= 0 && !retVal.matches("\"[^\"]*;[^\"]*\"")) {
retVal = retVal.substring(0, pos).trim();
}
// Strip wrapping quotes if necessary (the ini reader won't do this for us):
if (retVal.startsWith("\"")) {
retVal = retVal.substring(1, retVal.length());
}
if (retVal.endsWith("\"")) {
retVal = retVal.substring(0, retVal.length() - 1);
}
return retVal;
}
/**
* Load an ini file.
* @param filename name of {@code .ini} file
*/
public Ini loadConfigFile(String filename)
{
// Retrieve the file if it is not already cached.
if (!configCache.containsKey(filename)) {
Ini ini = new Ini();
File configFile = null;
try {
configFile = findConfigFile(filename);
} catch (IllegalStateException e) {
dieWithError("Illegal State: " + e.getMessage());
} catch (Throwable e) {
dieWithError(
"Unable to locate " + filename
+ " (" + e.getMessage() + ")"
);
}
try {
if (configFile != null) {
ini.load(new FileReader(configFile));
configCache.putIfAbsent(filename, ini);
}
} catch (Throwable e) {
dieWithError(
"Unable to access " + configFile.getAbsolutePath()
+ " (" + e.getMessage() + ")"
);
}
}
return configCache.get(filename);
}
/**
* Get a section from a VuFind configuration file and sanitize all the values.
* @param filename configuration file name
* @param section section name within the file
*/
public Map<String, String> getConfigSection(String filename, String section)
{
String sanitizedCacheKey = filename + "#" + section;
return sanitizedConfigCache.computeIfAbsent(sanitizedCacheKey, retVal -> {
Map<String, String> rawSection = getRawConfigSection(filename, section);
if (rawSection == null) {
return new ConcurrentHashMap<>();
}
// Sanitize a copy of the section.
// We do not want the sanitizer to update the cache, because it might
// cause problems when executing them multiple times, like
// e.g. in multithreaded scenarios.
ConcurrentHashMap<String, String> sanitizedSection = new ConcurrentHashMap<>();
for (Map.Entry<String, String> entry : rawSection.entrySet()) {
sanitizedSection.put(entry.getKey(), sanitizeConfigSetting(entry.getValue()));
}
return sanitizedSection;
});
}
/**
* Get a section from a VuFind configuration file.
* @param filename configuration file name
* @param section section name within the file
*/
public Map<String, String> getRawConfigSection(String filename, String section)
{
// Grab the ini file.
Ini ini = loadConfigFile(filename);
Map<String, String> retVal = ini.get(section);
String parent = ini.get("Parent_Config", "path");
while (parent != null) {
Ini parentIni = loadConfigFile(parent);
Map<String, String> parentSection = parentIni.get(section);
for (String key : parentSection.keySet()) {
if (!retVal.containsKey(key)) {
retVal.put(key, parentSection.get(key));
}
}
parent = parentIni.get("Parent_Config", "path");
}
// Check to see if we need to worry about an override file:
String override = ini.get("Extra_Config", "local_overrides");
if (override != null) {
Map<String, String> overrideSection = loadConfigFile(override).get(section);
for (String key : overrideSection.keySet()) {
retVal.put(key, overrideSection.get(key));
}
}
return retVal;
}
/**
* @deprecated Please use getConfigSection instead, or getRawConfigSection
* if you would like to get the non-sanitized values.
*/
@Deprecated
public Map<String, String> getSanitizedConfigSection(String filename, String section)
{
return getConfigSection(filename, section);
}
/**
* Get a setting from a VuFind configuration file and sanitize the value.
* @param filename configuration file name
* @param section section name within the file
* @param setting setting name within the section
*/
public String getConfigSetting(String filename, String section, String setting)
{
String retVal = getRawConfigSetting(filename, section, setting);
return retVal == null ? retVal : sanitizeConfigSetting(retVal);
}
/**
* Get a setting from a VuFind configuration file.
* @param filename configuration file name
* @param section section name within the file
* @param setting setting name within the section
*/
public String getRawConfigSetting(String filename, String section, String setting)
{
Map<String, String> sectionMap = getRawConfigSection(filename, section);
return sectionMap == null ? null : sectionMap.get(setting);
}
/**
* @deprecated Please use getConfigSetting instead, or getRawConfigSetting
* if you would like to get the non-sanitized value.
*/
@Deprecated
public String getSanitizedConfigSetting(String filename, String section, String setting)
{
return getConfigSetting(filename, section, setting);
}
/**
* Get a Boolean setting from a VuFind configuration file; match PHP's string to Boolean logic.
* @param filename configuration file name
* @param section section name within the file
* @param setting setting name within the section
* @param default defaultValue value to use if setting is missing
*/
public boolean getBooleanConfigSetting(String filename, String section, String setting, boolean defaultValue)
{
String config = getConfigSetting(filename, section, setting);
if (config == null) {
return defaultValue;
}
switch (config.trim().toLowerCase()) {
case "false":
case "0":
case "":
return false;
}
return true;
}
/**
* Log an error message and throw a fatal exception.
* @param msg message to log
*/
private void dieWithError(String msg)
{
logger.error(msg);
throw new SolrMarcIndexerException(SolrMarcIndexerException.EXIT, msg);
}
}