Skip to content

Commit 21b1986

Browse files
committed
Support passing settings via environment variable actframework#636
1 parent ae8a388 commit 21b1986

13 files changed

Lines changed: 94 additions & 45 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# ActFramework Change Log
22

33
**1.8.8**
4+
* Support passing settings via environment variable #636
5+
* Use canonical property key #635
46
* `scan_package` setting in configuration file is not effective #634
57
* Passing multiple packages in `Act.start(String, String)` trigger `IllegalArgumentException` #633
68
* Support getting Header value from query parameters #631

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<jpa.version>1.0.0.Final</jpa.version>
6363
<joda-time.version>2.9.9</joda-time.version>
6464
<okhttp.version>3.10.0</okhttp.version>
65-
<osgl-tool.version>1.10.0</osgl-tool.version>
65+
<osgl-tool.version>1.10.1-SNAPSHOT</osgl-tool.version>
6666
<osgl-cache.version>1.3.3</osgl-cache.version>
6767
<osgl-genie.version>1.7.0</osgl-genie.version>
6868
<osgl-http.version>1.6.0-SNAPSHOT</osgl-http.version>

src/main/java/act/app/RouterRegexMacroLookup.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import act.conf.AppConfig;
24+
import act.conf.Config;
2425
import act.inject.util.ConfigResourceLoader;
2526
import act.util.LogSupport;
2627
import org.osgl.util.IO;
@@ -82,7 +83,9 @@ public RouterRegexMacroLookup(AppConfig<?> config) {
8283
InputStream is = ConfigResourceLoader.load(macroDefinitionFile, InputStream.class, true);
8384
if (null != is) {
8485
Properties properties = IO.loadProperties(is);
85-
this.macros.putAll((Map) properties);
86+
for (Map.Entry entry : properties.entrySet()) {
87+
this.macros.put(Config.canonical(S.string(entry.getKey())), S.string(entry.getValue()));
88+
}
8689
}
8790
}
8891

@@ -104,7 +107,7 @@ public String expand(String macro) {
104107
if (!isMacro(macro)) {
105108
return macro;
106109
}
107-
String definition = macros.get(macro);
110+
String definition = macros.get(Config.canonical(macro));
108111
if (null == definition) {
109112
warn("possible missing definition of macro[%s]", macro);
110113
}

src/main/java/act/conf/ActConfigKey.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public enum ActConfigKey implements ConfigKey {
106106
}
107107

108108
ActConfigKey(String key, Object defVal) {
109-
this.key = key;
109+
this.key = Config.canonical(key);
110110
this.defVal = defVal;
111111
}
112112

@@ -166,7 +166,7 @@ public <T> T val(Map<String, ?> configuration) {
166166

167167
static {
168168
for (ActConfigKey k : values()) {
169-
lookup.put(k.key().toUpperCase(), k);
169+
lookup.put(k.key(), k);
170170
}
171171
}
172172

@@ -178,7 +178,7 @@ public <T> T val(Map<String, ?> configuration) {
178178
*/
179179
public static ActConfigKey valueOfIgnoreCase(String s) {
180180
if (S.empty(s)) throw new IllegalArgumentException();
181-
return lookup.get(s.trim().toUpperCase());
181+
return lookup.get(Config.canonical(s));
182182
}
183183

184184
}

src/main/java/act/conf/AppConfigKey.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ public <T> T val(Map<String, ?> configuration) {
11591159
}
11601160

11611161
AppConfigKey(String key, Object defVal) {
1162-
this.key = key;
1162+
this.key = Config.canonical(key);
11631163
this.defVal = defVal;
11641164
}
11651165

@@ -1237,7 +1237,7 @@ public <T> T val(Map<String, ?> configuration) {
12371237
if (S.notBlank(suffix) && suffixes.contains(suffix) && !nonAliasSuffixes.contains(suffix)) {
12381238
Set<String> aliases = ConfigKeyHelper.aliases(key, suffix);
12391239
for (String alias : aliases) {
1240-
lookup.put(alias, k);
1240+
lookup.put(Config.canonical(alias), k);
12411241
}
12421242
}
12431243
}
@@ -1251,7 +1251,7 @@ public <T> T val(Map<String, ?> configuration) {
12511251
*/
12521252
public static AppConfigKey valueOfIgnoreCase(String s) {
12531253
E.illegalArgumentIf(S.blank(s), "config key cannot be empty");
1254-
return lookup.get(s.trim().toUpperCase());
1254+
return lookup.get(Config.canonical(s));
12551255
}
12561256

12571257
}

src/main/java/act/conf/ConfLoader.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,24 @@ public T load() {
5555

5656
public T load(File resourceRoot) {
5757
// load conf from disk
58-
Map<String, ?> rawConf = null == resourceRoot ? new HashMap<>() : loadConfFromDisk(resourceRoot);
58+
Map<String, Object> rawConf = null == resourceRoot ? new HashMap<>() : loadConfFromDisk(resourceRoot);
5959

6060
// load conf from System.properties
6161
Properties sysProps = System.getProperties();
6262
rawConf.putAll((Map) sysProps);
6363

64+
// load conf from Environment variable
65+
Map<String, String> envMap = System.getenv();
66+
for (Map.Entry<String, String> entry : envMap.entrySet()) {
67+
String key = entry.getKey();
68+
if (key.startsWith("act_env_")) {
69+
key = Config.canonical(key.substring(8));
70+
rawConf.put(key, entry.getValue());
71+
}
72+
}
73+
6474
// strip off "act." prefix if has any
75+
// canonical all keys
6576
rawConf = processConf(rawConf);
6677
processScanPackage(rawConf);
6778

@@ -162,7 +173,9 @@ public int compare(String path1, String path2) {
162173
trace("loading app properties from jar entry: %s", entry.getKey());
163174
}
164175
p.load(jar.getInputStream(entry.getValue()));
165-
conf.putAll(p);
176+
for (Map.Entry<Object, Object> pEntry: p.entrySet()) {
177+
conf.put(Config.canonical(S.string(entry.getKey())), entry.getValue());
178+
}
166179
} catch (IOException e) {
167180
logger.warn("Error loading %s from jar file: %s", entry.getKey(), jarFile);
168181
}
@@ -196,7 +209,7 @@ private Map loadConfFromFile(File conf) {
196209
logger.warn("Cannot read conf file[%s]", conf.getAbsolutePath());
197210
return new HashMap<>();
198211
}
199-
InputStream is = IO.is(conf);
212+
InputStream is = IO.inputStream(conf);
200213
Properties p = new Properties();
201214
try {
202215
p.load(is);
@@ -215,7 +228,7 @@ private static Map<String, Object> processConf(Map<String, ?> conf) {
215228
for (String s : conf.keySet()) {
216229
Object o = conf.get(s);
217230
if (s.startsWith("act.")) s = s.substring(4);
218-
m.put(s, o);
231+
m.put(Config.canonical(s), o);
219232
}
220233
return m;
221234
}

src/main/java/act/conf/Config.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
*/
2222

2323
import act.util.DestroyableBase;
24+
import org.osgl.$;
2425
import org.osgl.exception.ConfigurationException;
2526
import org.osgl.exception.UnexpectedNewInstanceException;
26-
import org.osgl.util.C;
27+
import org.osgl.util.Keyword;
2728
import org.osgl.util.S;
2829

2930
import java.util.HashMap;
@@ -157,7 +158,7 @@ public <T> List<T> getList(AppConfigKey key, Class<T> c) {
157158
/**
158159
* Look up configuration by a <code>String<code/> key. If the String key
159160
* can be converted into {@link AppConfigKey rythm configuration key}, then
160-
* it is converted and call to {@link #get(ConfigKey)} method. Otherwise
161+
* it is converted and call to {@link #get(ConfigKey, Object)} method. Otherwise
161162
* the original configuration map is used to fetch the value from the string key
162163
*
163164
* @param key
@@ -177,37 +178,16 @@ public <T> T get(String key) {
177178
}
178179

179180
public <T> T getIgnoreCase(String key) {
180-
if (key.startsWith(PREFIX)) {
181-
key = key.substring(PREFIX_LEN);
182-
}
183-
T t = get(key);
184-
if (null != t) {
185-
return t;
186-
}
187-
key = key.toUpperCase();
188-
for (Map.Entry<String, Object> entries : raw.entrySet()) {
189-
if (entries.getKey().toUpperCase().equals(key)) {
190-
Object o = entries.getValue();
191-
if (o instanceof String) {
192-
return (T) AppConfigKey.helper.evaluate(o.toString(), raw);
193-
}
194-
return (T) o;
195-
} else if (entries.getKey().replace('_', '.').equals(key.replace('_', '.'))) {
196-
Object o = entries.getValue();
197-
if (o instanceof String) {
198-
return (T) AppConfigKey.helper.evaluate(o.toString(), raw);
199-
}
200-
return (T) o;
201-
}
202-
}
203-
return null;
181+
// because of #635 we just return get(key)
182+
return get(key);
204183
}
205184

206185
public Map<String, Object> rawConfiguration() {
207186
return raw;
208187
}
209188

210189
public Map<String, Object> subSet(String namespace) {
190+
namespace = Config.canonical(namespace);
211191
if (!namespace.endsWith(".")) {
212192
namespace = namespace + ".";
213193
}
@@ -234,10 +214,19 @@ public Map<String, Object> subSet(String namespace) {
234214

235215
protected abstract ConfigKey keyOf(String s);
236216

217+
public static String canonical(String key) {
218+
return Keyword.of(key).dotted();
219+
}
220+
221+
public static boolean matches(String k1, String k2) {
222+
return $.eq(Keyword.of(k1), Keyword.of(k2));
223+
}
224+
237225
private static final Object NULL = new Object() {
238226
@Override
239227
public String toString() {
240228
return "null";
241229
}
242230
};
231+
243232
}

src/main/java/act/conf/ConfigKeyHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public Object apply() throws NotAppliedException, $.Break {
7171
return getConfiguration(key, defVal, configuration);
7272
}
7373
<T> T getConfiguration(String key, $.F0<?> defVal, Map<String, ?> configuration) {
74+
key = Config.canonical(key);
7475
if (key.endsWith(".enabled") || key.endsWith(".disabled")) {
7576
String key0 = S.beforeLast(key, ".");
7677
Boolean B = getEnabled(key0, configuration, defVal);
@@ -390,7 +391,11 @@ public String evaluate(String s, Map<String, ?> map) {
390391
}
391392
String expression = s.substring(n0, n);
392393
if (S.notBlank(expression)) {
393-
Object o = getConfiguration(expression, null, map);
394+
// in case getting from Env variable
395+
Object o = map.get(expression);
396+
if (null == o) {
397+
o = getConfiguration(expression, null, map);
398+
}
394399
if (null != o) {
395400
sb.append(o);
396401
} else {

src/main/java/act/inject/genie/GenieProviders.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import act.app.App;
2525
import act.app.AppClassLoader;
2626
import act.conf.AppConfig;
27+
import act.conf.Config;
2728
import act.inject.DefaultValue;
2829
import act.inject.DependencyInjector;
2930
import act.util.ClassNode;
@@ -60,7 +61,7 @@ protected List<Class<?>> load(Class<? extends Annotation> aClass, final boolean
6061
return C.list();
6162
}
6263
final List<Class<?>> list = new ArrayList<>();
63-
Osgl.Visitor<ClassNode> visitor = new Osgl.Visitor<ClassNode>() {
64+
$.Visitor<ClassNode> visitor = new Osgl.Visitor<ClassNode>() {
6465
@Override
6566
public void visit(ClassNode classNode) throws Osgl.Break {
6667
Class c = $.classForName(classNode.name(), cl);
@@ -111,7 +112,7 @@ protected void initialized() {
111112
public Object get() {
112113
final AppConfig appConfig = app().config();
113114
DependencyInjector injector = Act.injector();
114-
final String confKey = value().toString();
115+
final String confKey = Config.canonical(value().toString());
115116
boolean isImpl = confKey.endsWith(".impl");
116117
if (this.spec.isInstanceOf(Map.class)) {
117118
String prefix = confKey;
@@ -122,7 +123,7 @@ public Object get() {
122123
BeanSpec valSpec = BeanSpec.of(valType, injector);
123124
int pos = prefix.length() + 1;
124125
for (String key : confMap.keySet()) {
125-
if (S.eq(key, prefix)) {
126+
if (Config.matches(key, prefix)) {
126127
continue;
127128
}
128129
Object val = confMap.get(key);

testapp/src/main/bin/start

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ DEBUG_PORT=5005
1010
PROFILE=
1111
SYS_PROP=
1212
HELP=0
13+
export act_env_gh636_conf=bar
1314

1415
# As long as there is at least one more argument, keep looping
1516
while [[ $# -gt 0 ]]; do
@@ -62,4 +63,4 @@ else
6263
else
6364
java $JAVA_OPTS -Dapp.mode=prod -Dprofile=$PROFILE $SYS_PROP -cp "$CP" $APP_ENTRY
6465
fi
65-
fi
66+
fi

0 commit comments

Comments
 (0)