Merge pull request #763 from stianst/master

KEYCLOAK-755 Support env variables in keycloak-server.json
This commit is contained in:
Stian Thorgersen 2014-10-14 15:06:57 +02:00
commit 7e7ab86821
3 changed files with 32 additions and 19 deletions

View file

@ -15,19 +15,19 @@
},
"user": {
"provider": "${keycloak.user.provider:jpa}"
"provider": "jpa"
},
"userSessions": {
"provider" : "${keycloak.userSessions.provider:mem}"
"provider" : "mem"
},
"realmCache": {
"provider": "${keycloak.realm.cache.provider:mem}"
"provider": "mem"
},
"userCache": {
"provider": "${keycloak.user.cache.provider:mem}",
"provider": "mem",
"mem": {
"maxSize": 20000
}
@ -68,11 +68,5 @@
"dataSource": "java:jboss/datasources/KeycloakDS",
"databaseSchema": "update"
}
},
"connectionsInfinispan": {
"default": {
"cacheContainer" : "java:jboss/infinispan/Keycloak"
}
}
}

View file

@ -37,6 +37,8 @@ import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
@ -118,7 +120,14 @@ public class KeycloakApplication extends Application {
if (config != null) {
JsonNode node = new ObjectMapper().readTree(config);
Config.init(new JsonConfigProvider(node));
Properties properties = new Properties();
properties.putAll(System.getProperties());
for(Map.Entry<String, String> e : System.getenv().entrySet()) {
properties.put("env." + e.getKey(), e.getValue());
}
Config.init(new JsonConfigProvider(node, properties));
log.info("Loaded config from " + config);
return;

View file

@ -4,21 +4,26 @@ import org.codehaus.jackson.JsonNode;
import org.keycloak.Config;
import org.keycloak.util.StringPropertyReplacer;
import java.util.Properties;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class JsonConfigProvider implements Config.ConfigProvider {
private Properties properties;
private JsonNode config;
public JsonConfigProvider(JsonNode config) {
public JsonConfigProvider(JsonNode config, Properties properties) {
this.config = config;
this.properties = properties;
}
@Override
public String getProvider(String spi) {
JsonNode n = getNode(config, spi, "provider");
return n != null ? StringPropertyReplacer.replaceProperties(n.getTextValue()) : null;
return n != null ? replaceProperties(n.getTextValue()) : null;
}
@Override
@ -40,6 +45,10 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return n;
}
private String replaceProperties(String value) {
return StringPropertyReplacer.replaceProperties(value, properties);
}
public class JsonScope implements Config.Scope {
private JsonNode config;
@ -62,7 +71,7 @@ public class JsonConfigProvider implements Config.ConfigProvider {
if (n == null) {
return defaultValue;
}
return StringPropertyReplacer.replaceProperties(n.getTextValue());
return replaceProperties(n.getTextValue());
}
@Override
@ -77,11 +86,11 @@ public class JsonConfigProvider implements Config.ConfigProvider {
} else if (n.isArray()) {
String[] a = new String[n.size()];
for (int i = 0; i < a.length; i++) {
a[i] = StringPropertyReplacer.replaceProperties(n.get(i).getTextValue());
a[i] = replaceProperties(n.get(i).getTextValue());
}
return a;
} else {
return new String[] { StringPropertyReplacer.replaceProperties(n.getTextValue()) };
return new String[] { replaceProperties(n.getTextValue()) };
}
}
@ -100,7 +109,7 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return defaultValue;
}
if (n.isTextual()) {
return Integer.parseInt(StringPropertyReplacer.replaceProperties(n.getTextValue()));
return Integer.parseInt(replaceProperties(n.getTextValue()));
} else {
return n.getIntValue();
}
@ -121,7 +130,7 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return defaultValue;
}
if (n.isTextual()) {
return Long.parseLong(StringPropertyReplacer.replaceProperties(n.getTextValue()));
return Long.parseLong(replaceProperties(n.getTextValue()));
} else {
return n.getLongValue();
}
@ -142,7 +151,7 @@ public class JsonConfigProvider implements Config.ConfigProvider {
return defaultValue;
}
if (n.isTextual()) {
return Boolean.parseBoolean(StringPropertyReplacer.replaceProperties(n.getTextValue()));
return Boolean.parseBoolean(replaceProperties(n.getTextValue()));
} else {
return n.getBooleanValue();
}
@ -152,6 +161,7 @@ public class JsonConfigProvider implements Config.ConfigProvider {
public Config.Scope scope(String... path) {
return new JsonScope(getNode(config, path));
}
}
}