Skip to content

Commit

Permalink
Merge pull request #40 from stuartwdouglas/explicit-config
Browse files Browse the repository at this point in the history
Add support for explicit configuration
  • Loading branch information
stuartwdouglas authored Oct 2, 2018
2 parents ff1d4f2 + ca2d4d0 commit a088465
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a088465

Please sign in to comment.