Skip to content

Commit

Permalink
Merge pull request #4934 from jaikiran/qk-4815
Browse files Browse the repository at this point in the history
Make sure hot deployment works after a failed server start in an application with non-default http port
  • Loading branch information
stuartwdouglas authored Oct 28, 2019
2 parents baa1233 + 0771078 commit 96cda30
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> supportedClassNameSuffix;

static {
final Set<String> 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()) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoutingContext> 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);
Expand Down

0 comments on commit 96cda30

Please sign in to comment.