diff --git a/core/deployment/src/main/java/org/jboss/shamrock/deployment/codegen/BytecodeRecorderImpl.java b/core/deployment/src/main/java/org/jboss/shamrock/deployment/codegen/BytecodeRecorderImpl.java index 62dc4868d313a..d99e6e65b5ae6 100644 --- a/core/deployment/src/main/java/org/jboss/shamrock/deployment/codegen/BytecodeRecorderImpl.java +++ b/core/deployment/src/main/java/org/jboss/shamrock/deployment/codegen/BytecodeRecorderImpl.java @@ -36,6 +36,7 @@ import org.jboss.protean.gizmo.ResultHandle; import org.jboss.shamrock.deployment.ClassOutput; import org.jboss.shamrock.deployment.ShamrockConfig; +import org.jboss.shamrock.runtime.ConfiguredValue; import org.jboss.shamrock.runtime.ContextObject; import org.jboss.shamrock.runtime.InjectionInstance; import org.jboss.shamrock.runtime.RuntimeInjector; @@ -303,9 +304,13 @@ private ResultHandle loadObjectInstance(MethodCreator method, Object param, Map< throw new RuntimeException("Failed to substitute " + param, e); } + } else if (param instanceof ConfiguredValue) { + ConfiguredValue val = (ConfiguredValue) param; + String value = val.getValue(); + String key = val.getKey(); + out = method.newInstance(ofConstructor(ConfiguredValue.class, String.class, String.class), method.load(key), method.load(value)); } else if (param instanceof String) { String configParam = ShamrockConfig.getConfigKey((String) param); - out = method.load((String) param); if (configParam != null) { ResultHandle config = method.invokeStaticMethod(ofMethod(ConfigProvider.class, "getConfig", Config.class)); ResultHandle propName = method.load(configParam); diff --git a/core/runtime/src/main/java/org/jboss/shamrock/runtime/ConfiguredValue.java b/core/runtime/src/main/java/org/jboss/shamrock/runtime/ConfiguredValue.java new file mode 100644 index 0000000000000..0af29e60176a0 --- /dev/null +++ b/core/runtime/src/main/java/org/jboss/shamrock/runtime/ConfiguredValue.java @@ -0,0 +1,42 @@ +package org.jboss.shamrock.runtime; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; + +/** + * Represents a value that can be read from MicroProfile config. It is designed to be passed + * to Template objects, and as such can be configured at both build and run time. + * + * The value is determined as follows: + * + * - If the key is present in MPConfig at runtime, then the runtime value is used + * - If the key is present at deployment time, then the deployment time value is used + * - Otherwise the default value is used, or null if it was not provided + * + * TODO: this is a more explicit alternative to the transparent config option provided by ShamrockConfig. We should probably only have one, this is something to revisit later + * + */ +public class ConfiguredValue { + + private static final Config config = ConfigProvider.getConfig(); + + private final String key; + private final String defaultValue; + + public ConfiguredValue(String key, String defaultValue) { + this.key = key; + this.defaultValue = defaultValue; + } + + public String getValue() { + return config.getOptionalValue(key, String.class).orElse(defaultValue); + } + + public String getKey() { + return key; + } + + public String getDefaultValue() { + return defaultValue; + } +} diff --git a/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/ServletResourceProcessor.java b/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/ServletResourceProcessor.java index 564400417742d..eec18a4733fe4 100644 --- a/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/ServletResourceProcessor.java +++ b/undertow/deployment/src/main/java/org/jboss/shamrock/undertow/ServletResourceProcessor.java @@ -65,6 +65,7 @@ import org.jboss.shamrock.deployment.RuntimePriority; import org.jboss.shamrock.deployment.ShamrockConfig; import org.jboss.shamrock.deployment.codegen.BytecodeRecorder; +import org.jboss.shamrock.runtime.ConfiguredValue; import org.jboss.shamrock.runtime.InjectionInstance; import org.jboss.shamrock.undertow.runtime.UndertowDeploymentTemplate; @@ -207,7 +208,7 @@ public void process(ArchiveContext archiveContext, ProcessorContext processorCon try (BytecodeRecorder context = processorContext.addDeploymentTask(RuntimePriority.UNDERTOW_START)) { UndertowDeploymentTemplate template = context.getRecordingProxy(UndertowDeploymentTemplate.class); - template.startUndertow(null, null, config.getConfig("http.port", "8080")); + template.startUndertow(null, null, new ConfiguredValue("http.port", "8080")); } } diff --git a/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java b/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java index ad42757e607c0..7ac42316eb8b3 100644 --- a/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java +++ b/undertow/runtime/src/main/java/org/jboss/shamrock/undertow/runtime/UndertowDeploymentTemplate.java @@ -11,6 +11,7 @@ import javax.servlet.Servlet; import javax.servlet.ServletException; +import org.jboss.shamrock.runtime.ConfiguredValue; import org.jboss.shamrock.runtime.ContextObject; import org.jboss.shamrock.runtime.InjectionInstance; import org.jboss.shamrock.runtime.StartupContext; @@ -124,12 +125,12 @@ public void addServletContextParameter(@ContextObject("deploymentInfo") Deployme info.addInitParameter(name, value); } - public void startUndertow(StartupContext startupContext, @ContextObject("servletHandler") HttpHandler handler, String port) throws ServletException { + public void startUndertow(StartupContext startupContext, @ContextObject("servletHandler") HttpHandler handler, ConfiguredValue port) throws ServletException { if (undertow == null) { try { log.log(Level.INFO, "Starting Undertow on port " + port); undertow = Undertow.builder() - .addHttpListener(Integer.parseInt(port), "localhost") + .addHttpListener(Integer.parseInt(port.getValue()), "localhost") .setHandler(new CanonicalPathHandler(ROOT_HANDLER)) .build(); undertow.start();