Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure hot deployment works after a failed server start in an application with non-default http port #4934

Merged
merged 2 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind avoiding using final on local variables. I don't think we use that approach elsewhere and in your case it shows artificial line changes compared to the original code which makes it harder to read.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emmanuelbernard, I'll keep that in mind for unchanged lines going forward.

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