Skip to content

Commit 4a9186b

Browse files
committed
Merge pull request #10544 from tinexw:9299
* pr/10544: Polish "Log context path at startup" Log context path at startup
2 parents 60afbdc + dbea25f commit 4a9186b

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
package org.springframework.boot.web.embedded.jetty;
1818

1919
import java.net.BindException;
20+
import java.util.Arrays;
2021
import java.util.List;
22+
import java.util.stream.Collectors;
2123

2224
import org.apache.commons.logging.Log;
2325
import org.apache.commons.logging.LogFactory;
2426
import org.eclipse.jetty.server.Connector;
2527
import org.eclipse.jetty.server.Handler;
2628
import org.eclipse.jetty.server.NetworkConnector;
2729
import org.eclipse.jetty.server.Server;
30+
import org.eclipse.jetty.server.handler.ContextHandler;
2831
import org.eclipse.jetty.server.handler.HandlerCollection;
2932
import org.eclipse.jetty.server.handler.HandlerWrapper;
3033
import org.eclipse.jetty.util.component.AbstractLifeCycle;
@@ -44,6 +47,7 @@
4447
* @author David Liu
4548
* @author Eddú Meléndez
4649
* @author Brian Clozel
50+
* @author Kristine Jetzke
4751
* @since 2.0.0
4852
* @see JettyReactiveWebServerFactory
4953
*/
@@ -151,7 +155,8 @@ public void start() throws WebServerException {
151155
}
152156
this.started = true;
153157
JettyWebServer.logger
154-
.info("Jetty started on port(s) " + getActualPortsDescription());
158+
.info("Jetty started on port(s) " + getActualPortsDescription()
159+
+ " with context path '" + getContextPath() + "'");
155160
}
156161
catch (WebServerException ex) {
157162
throw ex;
@@ -190,6 +195,13 @@ private String getProtocols(Connector connector) {
190195
return " (" + StringUtils.collectionToDelimitedString(protocols, ", ") + ")";
191196
}
192197

198+
private String getContextPath() {
199+
return Arrays.stream(this.server.getHandlers())
200+
.filter(ContextHandler.class::isInstance)
201+
.map(handler -> ((ContextHandler) handler).getContextPath())
202+
.collect(Collectors.joining(" "));
203+
}
204+
193205
private void handleDeferredInitialize(Handler... handlers) throws Exception {
194206
for (Handler handler : handlers) {
195207
if (handler instanceof JettyEmbeddedWebAppContext) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatWebServer.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package org.springframework.boot.web.embedded.tomcat;
1818

19+
import java.util.Arrays;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122
import java.util.concurrent.atomic.AtomicInteger;
23+
import java.util.stream.Collectors;
2224

2325
import javax.naming.NamingException;
2426

@@ -43,6 +45,7 @@
4345
* should be created using the {@link TomcatReactiveWebServerFactory} and not directly.
4446
*
4547
* @author Brian Clozel
48+
* @author Kristine Jetzke
4649
* @since 2.0.0
4750
*/
4851
public class TomcatWebServer implements WebServer {
@@ -191,7 +194,8 @@ public void start() throws WebServerException {
191194
checkThatConnectorsHaveStarted();
192195
this.started = true;
193196
TomcatWebServer.logger
194-
.info("Tomcat started on port(s): " + getPortsDescription(true));
197+
.info("Tomcat started on port(s): " + getPortsDescription(true)
198+
+ " with context path '" + getContextPath() + "'");
195199
}
196200
catch (ConnectorStartFailedException ex) {
197201
stopSilently();
@@ -322,6 +326,13 @@ public int getPort() {
322326
return 0;
323327
}
324328

329+
private String getContextPath() {
330+
return Arrays.stream(this.tomcat.getHost().findChildren())
331+
.filter(TomcatEmbeddedContext.class::isInstance)
332+
.map(context -> ((TomcatEmbeddedContext) context).getPath())
333+
.collect(Collectors.joining(" "));
334+
}
335+
325336
/**
326337
* Returns access to the underlying Tomcat server.
327338
* @return the Tomcat server

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* @author Andy Wilkinson
6363
* @author Eddú Meléndez
6464
* @author Christoph Dreis
65+
* @author Kristine Jetzke
6566
* @since 2.0.0
6667
* @see UndertowServletWebServerFactory
6768
*/
@@ -156,7 +157,8 @@ public void start() throws WebServerException {
156157
this.undertow.start();
157158
this.started = true;
158159
UndertowServletWebServer.logger
159-
.info("Undertow started on port(s) " + getPortsDescription());
160+
.info("Undertow started on port(s) " + getPortsDescription()
161+
+ " with context path '" + this.contextPath + "'");
160162
}
161163
catch (Exception ex) {
162164
try {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ public void specificContextRoot() throws Exception {
263263
assertThat(getResponse(getLocalUrl("/say/hello"))).isEqualTo("Hello World");
264264
}
265265

266+
@Test
267+
public void contextPathIsLoggedOnStartup() {
268+
AbstractServletWebServerFactory factory = getFactory();
269+
factory.setContextPath("/custom");
270+
this.webServer = factory.getWebServer(exampleServletRegistration());
271+
this.webServer.start();
272+
assertThat(this.output.toString()).containsOnlyOnce("with context path '/custom'");
273+
}
274+
266275
@Test
267276
public void contextPathMustStartWithSlash() throws Exception {
268277
this.thrown.expect(IllegalArgumentException.class);

0 commit comments

Comments
 (0)