From 76335ec4a120efa880767b9e56da4f7c0be96286 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Mon, 28 Oct 2019 20:50:03 +0530 Subject: [PATCH 1/2] Fix config instantiation to account for both "Config" and "Configuration" class name suffixes --- .../configuration/ConfigInstantiator.java | 69 ++++++++++++++++--- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigInstantiator.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigInstantiator.java index 27e3d4f5d86e6..ad3d262b6ffbb 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigInstantiator.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigInstantiator.java @@ -4,11 +4,14 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.OptionalDouble; import java.util.OptionalInt; import java.util.OptionalLong; +import java.util.Set; import java.util.regex.Pattern; import org.eclipse.microprofile.config.ConfigProvider; @@ -28,19 +31,33 @@ public class ConfigInstantiator { private static final Pattern COMMA_PATTERN = Pattern.compile(","); + // certain well-known classname suffixes that we support + private static Set supportedClassNameSuffix; + + static { + final Set suffixes = new HashSet<>(); + suffixes.add("Config"); + suffixes.add("Configuration"); + supportedClassNameSuffix = Collections.unmodifiableSet(suffixes); + } public static void handleObject(Object o) { - SmallRyeConfig config = (SmallRyeConfig) ConfigProvider.getConfig(); - Class cls = o.getClass(); - String name = dashify(cls.getSimpleName().substring(0, cls.getSimpleName().length() - "Config".length())); + final SmallRyeConfig config = (SmallRyeConfig) ConfigProvider.getConfig(); + final Class cls = o.getClass(); + final String clsNameSuffix = getClassNameSuffix(o); + if (clsNameSuffix == null) { + // unsupported object type + return; + } + final String name = dashify(cls.getSimpleName().substring(0, cls.getSimpleName().length() - clsNameSuffix.length())); handleObject("quarkus." + name, o, config); } private static void handleObject(String prefix, Object o, SmallRyeConfig config) { try { - Class cls = o.getClass(); - if (!cls.getName().endsWith("Config") && !cls.getName().endsWith("Configuration")) { + final Class cls = o.getClass(); + if (!isClassNameSuffixSupported(o)) { return; } for (Field field : cls.getDeclaredFields()) { @@ -110,14 +127,48 @@ private static void handleObject(String prefix, Object o, SmallRyeConfig config) } } + // Configuration keys are normally derived from the field names that they are tied to. + // This is done by de-camel-casing the name and then joining the segments with hyphens (-). + // Some examples: + // bindAddress becomes bind-address + // keepAliveTime becomes keep-alive-time + // requestDNSTimeout becomes request-dns-timeout private static String dashify(String substring) { - StringBuilder ret = new StringBuilder(); - for (char i : substring.toCharArray()) { - if (i >= 'A' && i <= 'Z') { + final StringBuilder ret = new StringBuilder(); + final char[] chars = substring.toCharArray(); + for (int i = 0; i < chars.length; i++) { + final char c = chars[i]; + if (i != 0 && i != (chars.length - 1) && c >= 'A' && c <= 'Z') { ret.append('-'); } - ret.append(Character.toLowerCase(i)); + ret.append(Character.toLowerCase(c)); } return ret.toString(); } + + private static String getClassNameSuffix(final Object o) { + if (o == null) { + return null; + } + final String klassName = o.getClass().getName(); + for (final String supportedSuffix : supportedClassNameSuffix) { + if (klassName.endsWith(supportedSuffix)) { + return supportedSuffix; + } + } + return null; + } + + private static boolean isClassNameSuffixSupported(final Object o) { + if (o == null) { + return false; + } + final String klassName = o.getClass().getName(); + for (final String supportedSuffix : supportedClassNameSuffix) { + if (klassName.endsWith(supportedSuffix)) { + return true; + } + } + return false; + } } From 0771078512615a4faa4ae1b1f45ecd6623d65447 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Mon, 28 Oct 2019 20:50:55 +0530 Subject: [PATCH 2/2] Re-use the hot deployment handler, in case of failed server start, to allow for hot deploying any fixes that caused the server start failure --- .../io/quarkus/vertx/http/runtime/VertxHttpRecorder.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java index ad80e36d940fb..7120dec51c7ec 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/VertxHttpRecorder.java @@ -99,7 +99,12 @@ public static void startServerAfterFailedStart() { if (closeTask != null) { //it is possible start failed after the server was started //we shut it down in this case, as we have no idea what state it is in + final Handler prevHotReplacementHandler = hotReplacementHandler; shutDownDevMode(); + // reset back to the older hot replacement handler, so that it can be used + // to watch any artifacts that need hot deployment to fix the reason which caused + // the server start to fail + hotReplacementHandler = prevHotReplacementHandler; } VertxConfiguration vertxConfiguration = new VertxConfiguration(); ConfigInstantiator.handleObject(vertxConfiguration);