Repository navigation
Expand file tree
/
Copy pathConfig.java
More file actions
151 lines (135 loc) · 3.97 KB
/
Copy pathConfig.java
File metadata and controls
151 lines (135 loc) · 3.97 KB
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
package act.conf;
import act.util.DestroyableBase;
import org.osgl.util.C;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Base class for XxConfig
*/
public abstract class Config<E extends ConfigKey> extends DestroyableBase {
static final String PREFIX = "act.";
static final int PREFIX_LEN = PREFIX.length();
protected Map<String, Object> raw;
protected Map<ConfigKey, Object> data;
/**
* Construct a <code>AppConfig</code> with a map. The map is copied to
* the original map of the configuration instance
*
* @param configuration
*/
public Config(Map<String, ?> configuration) {
raw = new HashMap<String, Object>(configuration);
data = new HashMap<ConfigKey, Object>(configuration.size());
}
public Config() {
this((Map) System.getProperties());
}
@Override
protected void releaseResources() {
raw.clear();
data.clear();
super.releaseResources();
}
/**
* Return configuration by {@link AppConfigKey configuration key}
*
* @param key
* @param <T>
* @return the configured item
*/
public <T> T get(ConfigKey key) {
Object o = data.get(key);
if (null == o) {
o = key.val(raw);
if (null != o) {
data.put(key, o);
} else {
data.put(key, NULL);
}
}
if (o == NULL) {
return null;
} else {
return (T) o;
}
}
/**
* Return a configuration value as list
*
* @param key
* @param c
* @param <T>
* @return the list
*/
public <T> List<T> getList(AppConfigKey key, Class<T> c) {
Object o = data.get(key);
if (null == o) {
List<T> l = key.implList(key.key(), raw, c);
data.put(key, l);
return l;
} else {
return (List) o;
}
}
/**
* Look up configuration by a <code>String<code/> key. If the String key
* can be converted into {@link AppConfigKey rythm configuration key}, then
* it is converted and call to {@link #get(ConfigKey)} method. Otherwise
* the original configuration map is used to fetch the value from the string key
*
* @param key
* @param <T>
* @return the configured item
*/
public <T> T get(String key) {
if (key.startsWith(PREFIX)) {
key = key.substring(PREFIX_LEN);
}
ConfigKey rk = keyOf(key);
if (null != rk) {
return get(rk);
} else {
return (T) raw.get(key);
}
}
public <T> T getIgnoreCase(String key) {
if (key.startsWith(PREFIX)) {
key = key.substring(PREFIX_LEN);
}
T t = get(key);
if (null != t) {
return t;
}
key = key.toLowerCase();
for (Map.Entry<String, Object> entries: raw.entrySet()) {
if (entries.getKey().toLowerCase().equals(key)) {
return (T) entries.getValue();
} else if (entries.getKey().replace('_', '.').equals(key.replace('_', '.'))) {
return (T) entries.getValue();
}
}
return null;
}
public Map rawConfiguration() {
return raw;
}
public Map subSet(String prefix) {
String prefix2 = "act." + prefix;
Map<String, Object> subset = C.newMap();
for (String key : raw.keySet()) {
if (key.startsWith(prefix) || key.startsWith(prefix2)) {
Object o = data.get(key);
if (null == o) o = raw.get(key);
if (key.startsWith("act.")) {
key = key.substring(4);
}
if (subset.containsKey(key)) continue;
subset.put(key, o);
}
}
return subset;
}
protected abstract ConfigKey keyOf(String s);
private static final Object NULL = new Object();
}