forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AliasConfigSource.java
78 lines (64 loc) · 2.64 KB
/
AliasConfigSource.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
package edu.harvard.iq.dataverse.settings.source;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigSource;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
public final class AliasConfigSource implements ConfigSource {
private static final Logger logger = Logger.getLogger(AliasConfigSource.class.getName());
private final ConcurrentHashMap<String, String> aliases = new ConcurrentHashMap<>();
private final String ALIASES_PROP_FILE = "aliases-config.properties";
public AliasConfigSource() {
try {
// get resource from classpath
ClassLoader classLoader = this.getClass().getClassLoader();
URL aliasesResource = classLoader.getResource(ALIASES_PROP_FILE);
// load properties from file resource (parsing included)
Properties aliasProps = new Properties();
aliasProps.load(aliasesResource.openStream());
// store in our aliases map
aliasProps.forEach((key, value) -> aliases.put(key.toString(), value.toString()));
} catch (IOException e) {
logger.info("Could not read from "+ALIASES_PROP_FILE+". Skipping MPCONFIG alias setup.");
}
}
@Override
public Map<String, String> getProperties() {
// No, just no. We're not going to drop a list of stuff. We're only
// dealiasing on getValue();
return new HashMap<>();
}
@Override
public Set<String> getPropertyNames() {
// It does not make sense to retrieve the aliased names...
return new HashSet<>();
}
@Override
public int getOrdinal() {
// Any other config source can override us.
// As soon as someone is starting to use the new property name, this alias becomes pointless.
return Integer.MIN_VALUE;
}
@Override
public String getValue(String key) {
String value = null;
if (this.aliases.contains(key)) {
String oldKey = this.aliases.get(key);
value = ConfigProvider.getConfig().getOptionalValue(oldKey, String.class).orElse(null);
if (value != null) {
logger.warning("Detected deprecated config option '"+oldKey+"' in use. Please update your config to use '"+key+"'.");
}
}
return value;
}
@Override
public String getName() {
return "Alias";
}
}