/
EnvironmentSpecificProperties.java
119 lines (100 loc) · 3.98 KB
/
EnvironmentSpecificProperties.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
package org.jumpmind.properties;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.exception.IoException;
/**
* This extension to {@link Properties} reads in a properties file and looks for
* any properties whose prefix matches any combination of supplied environment
* tokens. Any matches it finds, it removes the prefix and keeps a reference to
* the new property.
* <p/>
*/
public class EnvironmentSpecificProperties extends TypedProperties {
private static final long serialVersionUID = 1L;
protected Set<String> propertiesForEnvironment;
protected String systemPropertyName;
protected Properties original;
public EnvironmentSpecificProperties(URL fileUrl, String systemPropertyName,
String... propertiesForEnv) {
this(new URL[] { fileUrl }, systemPropertyName, propertiesForEnv);
}
public EnvironmentSpecificProperties(URL[] fileUrls, String systemPropertyName,
String... propertiesForEnv) {
this.propertiesForEnvironment = new HashSet<String>();
this.systemPropertyName = systemPropertyName;
if (propertiesForEnv != null) {
for (String property : propertiesForEnv) {
if (property != null) {
this.propertiesForEnvironment.add(property);
}
}
}
try {
this.original = new Properties();
for (URL fileUrl : fileUrls) {
this.original.load(fileUrl.openStream());
}
this.original.putAll(System.getProperties());
activate();
} catch (IOException e) {
throw new IoException(e);
}
}
@Override
public synchronized void load(InputStream inStream) throws IOException {
throw new NotImplementedException();
}
public synchronized void load(Reader reader) throws IOException {
throw new NotImplementedException();
}
protected void activate() {
this.clear();
if (this.original != null) {
Set<String> properties = new HashSet<String>(this.propertiesForEnvironment);
if (StringUtils.isNotBlank(this.systemPropertyName)) {
String additionalActivationKeys = System.getProperty(systemPropertyName);
if (StringUtils.isBlank(additionalActivationKeys)) {
additionalActivationKeys = this.original.getProperty(systemPropertyName);
}
if (StringUtils.isNotBlank(additionalActivationKeys)) {
String[] tokens = additionalActivationKeys.split(",");
for (String token : tokens) {
properties.add(token);
}
}
}
Set<Object> keys = original.keySet();
for (Object originalKey : keys) {
String keyName = originalKey.toString();
boolean foundMatch = true;
while (foundMatch) {
foundMatch = false;
for (String property : properties) {
if (keyName.startsWith(property + ".")) {
keyName = keyName.substring(property.length() + 1, keyName.length());
foundMatch = true;
}
}
}
setProperty(keyName, original.getProperty(originalKey.toString()));
}
}
}
protected Set<String> getPropertyKeysThatBeginWith(String prefix) {
HashSet<String> set = new HashSet<String>();
Set<Object> keys = keySet();
for (Object key : keys) {
if (key.toString().startsWith(prefix)) {
set.add(key.toString());
}
}
return set;
}
}