bug(platform-vertx) Ensure all leaf nodes have variable substitution performed.
MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/common/config/VertxEngineConfig.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/common/config/VertxEngineConfig.java
@@ -43,8 +43,6 @@ import io.apiman.gateway.platforms.vertx3.engine.VertxPluginRegistry;
import io.apiman.gateway.platforms.vertx3.i18n.Messages;
import io.apiman.gateway.platforms.vertx3.logging.VertxLoggerDelegate;
import io.vertx.core.json.JsonObject;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.text.StrSubstitutor;
import java.util.AbstractMap;
import java.util.Collections;
@@ -55,6 +53,9 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.text.StrSubstitutor;
+
/**
* Engine configuration, read simplistically from Vert'x JSON config.
@@ -97,6 +98,7 @@ public class VertxEngineConfig implements IEngineConfig {
public VertxEngineConfig(JsonObject config) {
this.config = config;
+ substituteLeafVars(config);
}
public JsonObject getConfig() {
@@ -254,7 +256,7 @@ public class VertxEngineConfig implements IEngineConfig {
}
public int getPort(String name) {
- return getVerticleConfig(name).getInteger(VERTICLE_PORT, -1);
+ return getValueAsInteger(getVerticleConfig(name), VERTICLE_PORT, -1);
}
public int getPort(VerticleType verticleType) {
@@ -268,7 +270,7 @@ public class VertxEngineConfig implements IEngineConfig {
if ("auto".equalsIgnoreCase(getVerticleConfig(verticleType.name()).getValue(VERTICLE_COUNT).toString())) {
return Runtime.getRuntime().availableProcessors();
}
- return getVerticleConfig(verticleType.name()).getInteger(VERTICLE_COUNT);
+ return getValueAsInteger(getVerticleConfig(verticleType.name()), VERTICLE_COUNT, 1);
}
public boolean isSSL() {
@@ -300,9 +302,7 @@ public class VertxEngineConfig implements IEngineConfig {
return Collections.emptyMap();
Map<String, String> outMap = new LinkedHashMap<>();
- // TODO figure out why this workaround is necessary.
jsonMapToProperties("", new JsonObject(jsonObject.encode()).getMap(), outMap);
- substituteValues(outMap);
return outMap;
}
@@ -356,6 +356,20 @@ public class VertxEngineConfig implements IEngineConfig {
return toFlatStringMap(obj.getJsonObject(prefix, new JsonObject()).getJsonObject(GATEWAY_CONFIG));
}
+ private int getValueAsInteger(JsonObject obj, String key, int defaultValue) {
+ Object val = obj.getValue(key);
+ if (val == null) {
+ return defaultValue;
+ } else {
+ if (val instanceof Number) {
+ return (int) val;
+ } else if (val instanceof String) {
+ return Integer.valueOf((String)val);
+ }
+ throw new IllegalArgumentException("Expected integer, got " + obj.getClass().getName());
+ }
+ }
+
/**
* @return a loaded class
*/
@@ -390,11 +404,6 @@ public class VertxEngineConfig implements IEngineConfig {
return str == null ? defaultValue : str;
}
- protected Boolean boolConfigWithDefault(String name, Boolean defaultValue) {
- Boolean bool = config.containsKey(name);
- return bool == null ? defaultValue : bool;
- }
-
public JsonObject getVerticleConfig(String verticleType) {
return config.getJsonObject(VERTICLES).getJsonObject(verticleType.toLowerCase());
}
@@ -423,18 +432,27 @@ public class VertxEngineConfig implements IEngineConfig {
.collect(Collectors.toList());
}
- private void substituteValues(Map<String, String> map) {
- map.entrySet().stream()
- .forEach(pair -> map.put(pair.getKey(), SUBSTITUTOR.replace(pair.getValue())));
+ protected void substituteLeafVars(JsonObject node) {
+ node.forEach(elem -> {
+ Object value = elem.getValue();
+ if (value instanceof JsonObject) {
+ substituteLeafVars((JsonObject) value);
+ } else if (value instanceof String) {
+ node.put(elem.getKey(), SUBSTITUTOR.replace((String) value));
+ }
+ });
}
private final StrSubstitutor SUBSTITUTOR = new StrSubstitutor(new VertxEngineStrSubstitutor());
private class VertxEngineStrSubstitutor extends ApimanStrLookup {
+
@Override
public String lookup(String key) {
+ // Upside of this approach is that we can capture any dynamically defined variables.
+ // Downside is that it's slightly expensive but should only happen at startup.
Map<String, String> flattenedMap = new LinkedHashMap<>();
- jsonMapToProperties("", new JsonObject(getVariables().encode()).getMap(), flattenedMap); // TODO tidy up
+ jsonMapToProperties("", new JsonObject(getVariables().encode()).getMap(), flattenedMap);
if (flattenedMap.containsKey(key)) {
return flattenedMap.get(key);
} else {

bug(platform-vertx) Ensure all leaf nodes have variable substitution performed.