From 4e5ac2c1102b7c893aafeeee7be02c6fbbb4c922 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 19 Dec 2014 14:44:33 -0800 Subject: [PATCH] Log port(s) used by Undertow Fixes gh-2195 --- .../UndertowEmbeddedServletContainer.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java index 3c0c1a32e3..ca0ab605e1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java @@ -22,10 +22,16 @@ import io.undertow.Undertow.Builder; import io.undertow.server.HttpHandler; import io.undertow.servlet.api.DeploymentManager; +import java.lang.reflect.Field; +import java.util.List; + import javax.servlet.ServletException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerException; +import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; /** @@ -40,6 +46,9 @@ import org.springframework.util.StringUtils; */ public class UndertowEmbeddedServletContainer implements EmbeddedServletContainer { + private static final Log logger = LogFactory + .getLog(UndertowEmbeddedServletContainer.class); + private final DeploymentManager manager; private final Builder builder; @@ -73,6 +82,33 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine } this.undertow.start(); this.started = true; + UndertowEmbeddedServletContainer.logger.info("Undertow started on port(s) " + + getPortsDescription()); + } + + @SuppressWarnings("rawtypes") + private String getPortsDescription() { + try { + // Use reflection if possible to get the actual listener configuration + Field listenersField = ReflectionUtils.findField(Undertow.class, "listeners"); + listenersField.setAccessible(true); + List listeners = (List) listenersField.get(this.undertow); + StringBuilder ports = new StringBuilder(); + for (Object listener : listeners) { + Field portField = ReflectionUtils.findField(listener.getClass(), "port"); + portField.setAccessible(true); + Field typeField = ReflectionUtils.findField(listener.getClass(), "type"); + typeField.setAccessible(true); + ports.append(ports.length() == 0 ? "" : ", "); + ports.append(portField.get(listener) + " (" + + typeField.get(listener).toString().toLowerCase() + ")"); + } + return ports.toString(); + } + catch (Exception ex) { + } + // We at least know our port + return String.valueOf(this.port); } private Undertow createUndertowServer() { @@ -92,7 +128,6 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine return servletHandler; } return Handlers.path().addPrefixPath(this.contextPath, servletHandler); - } @Override