From f76479fb99c168e6129a1173aa2ef0126a4c131c Mon Sep 17 00:00:00 2001 From: tinexw Date: Thu, 5 Oct 2017 22:15:11 +0200 Subject: [PATCH 1/2] Log context path at startup See gh-10544 --- .../boot/web/embedded/jetty/JettyWebServer.java | 14 +++++++++++++- .../boot/web/embedded/tomcat/TomcatWebServer.java | 13 ++++++++++++- .../undertow/UndertowServletWebServer.java | 4 +++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java index 5d92fc79c2..931fa5adb5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java @@ -17,7 +17,9 @@ package org.springframework.boot.web.embedded.jetty; import java.net.BindException; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -25,6 +27,7 @@ import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerWrapper; import org.eclipse.jetty.util.component.AbstractLifeCycle; @@ -44,6 +47,7 @@ import org.springframework.util.StringUtils; * @author David Liu * @author Eddú Meléndez * @author Brian Clozel + * @author Kristine Jetzke * @since 2.0.0 * @see JettyReactiveWebServerFactory */ @@ -151,7 +155,8 @@ public class JettyWebServer implements WebServer { } this.started = true; JettyWebServer.logger - .info("Jetty started on port(s) " + getActualPortsDescription()); + .info("Jetty started on port(s) " + getActualPortsDescription() + + " with context path " + getContextPath()); } catch (WebServerException ex) { throw ex; @@ -190,6 +195,13 @@ public class JettyWebServer implements WebServer { return " (" + StringUtils.collectionToDelimitedString(protocols, ", ") + ")"; } + private String getContextPath() { + return Arrays.stream(this.server.getHandlers()) + .filter(ContextHandler.class::isInstance) + .map(handler -> ((ContextHandler) handler).getContextPath()) + .collect(Collectors.joining(" ")); + } + private void handleDeferredInitialize(Handler... handlers) throws Exception { for (Handler handler : handlers) { if (handler instanceof JettyEmbeddedWebAppContext) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java index 019f11b2d1..33942f070a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java @@ -16,9 +16,11 @@ package org.springframework.boot.web.embedded.tomcat; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; import javax.naming.NamingException; @@ -43,6 +45,7 @@ import org.springframework.util.Assert; * should be created using the {@link TomcatReactiveWebServerFactory} and not directly. * * @author Brian Clozel + * @author Kristine Jetzke * @since 2.0.0 */ public class TomcatWebServer implements WebServer { @@ -191,7 +194,8 @@ public class TomcatWebServer implements WebServer { checkThatConnectorsHaveStarted(); this.started = true; TomcatWebServer.logger - .info("Tomcat started on port(s): " + getPortsDescription(true)); + .info("Tomcat started on port(s): " + getPortsDescription(true) + + " with context path " + getContextPath()); } catch (ConnectorStartFailedException ex) { stopSilently(); @@ -322,6 +326,13 @@ public class TomcatWebServer implements WebServer { return 0; } + private String getContextPath() { + return Arrays.stream(this.tomcat.getHost().findChildren()) + .filter(TomcatEmbeddedContext.class::isInstance) + .map(context -> ((TomcatEmbeddedContext) context).getPath()) + .collect(Collectors.joining(" ")); + } + /** * Returns access to the underlying Tomcat server. * @return the Tomcat server diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java index a48b973416..518ef52f00 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java @@ -62,6 +62,7 @@ import org.springframework.util.StringUtils; * @author Andy Wilkinson * @author Eddú Meléndez * @author Christoph Dreis + * @author Kristine Jetzke * @since 2.0.0 * @see UndertowServletWebServerFactory */ @@ -156,7 +157,8 @@ public class UndertowServletWebServer implements WebServer { this.undertow.start(); this.started = true; UndertowServletWebServer.logger - .info("Undertow started on port(s) " + getPortsDescription()); + .info("Undertow started on port(s) " + getPortsDescription() + + " with context path " + this.contextPath); } catch (Exception ex) { try { From dbea25fa8b60d283183c9bfb2bce9230836656dc Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 6 Nov 2017 12:48:51 +0100 Subject: [PATCH 2/2] Polish "Log context path at startup" Closes gh-10544 --- .../boot/web/embedded/jetty/JettyWebServer.java | 2 +- .../boot/web/embedded/tomcat/TomcatWebServer.java | 2 +- .../web/embedded/undertow/UndertowServletWebServer.java | 2 +- .../server/AbstractServletWebServerFactoryTests.java | 9 +++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java index 931fa5adb5..0fcf476891 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java @@ -156,7 +156,7 @@ public class JettyWebServer implements WebServer { this.started = true; JettyWebServer.logger .info("Jetty started on port(s) " + getActualPortsDescription() - + " with context path " + getContextPath()); + + " with context path '" + getContextPath() + "'"); } catch (WebServerException ex) { throw ex; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java index 33942f070a..85e79d062f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java @@ -195,7 +195,7 @@ public class TomcatWebServer implements WebServer { this.started = true; TomcatWebServer.logger .info("Tomcat started on port(s): " + getPortsDescription(true) - + " with context path " + getContextPath()); + + " with context path '" + getContextPath() + "'"); } catch (ConnectorStartFailedException ex) { stopSilently(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java index 518ef52f00..1c9bd97991 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java @@ -158,7 +158,7 @@ public class UndertowServletWebServer implements WebServer { this.started = true; UndertowServletWebServer.logger .info("Undertow started on port(s) " + getPortsDescription() - + " with context path " + this.contextPath); + + " with context path '" + this.contextPath + "'"); } catch (Exception ex) { try { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index ca779a44c4..b08c996757 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -263,6 +263,15 @@ public abstract class AbstractServletWebServerFactoryTests { assertThat(getResponse(getLocalUrl("/say/hello"))).isEqualTo("Hello World"); } + @Test + public void contextPathIsLoggedOnStartup() { + AbstractServletWebServerFactory factory = getFactory(); + factory.setContextPath("/custom"); + this.webServer = factory.getWebServer(exampleServletRegistration()); + this.webServer.start(); + assertThat(this.output.toString()).containsOnlyOnce("with context path '/custom'"); + } + @Test public void contextPathMustStartWithSlash() throws Exception { this.thrown.expect(IllegalArgumentException.class);