From 2b2113714dea8830507ee60e492d370648bc99ee Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 11 Mar 2020 09:24:27 +0000 Subject: [PATCH] Use getTomcatWebServer to create TomcatWebServer instance TomcatReactiveWebServerFactory's getTomcatWebServer is described as being a hook point the can be overrideden to return a different TomcatWebServer or to apply additional processing to the Tomcat server. Previously, this did not work as the method was not called to create the TomcatWebServer instance. Instead, TomcatWebServer was instantiated and returned directly. This commit updates TomcatReactiveWebServerFactory to call getTomcatWebServer to create the TomcatWebServer instance. A test has also been added to verify that the method is now used. Fixes gh-20386 --- .../tomcat/TomcatReactiveWebServerFactory.java | 4 ++-- .../TomcatReactiveWebServerFactoryTests.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java index be562efd0d..bd27f6b726 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -112,7 +112,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac configureEngine(tomcat.getEngine()); TomcatHttpHandlerAdapter servlet = new TomcatHttpHandlerAdapter(httpHandler); prepareContext(tomcat.getHost(), servlet); - return new TomcatWebServer(tomcat, getPort() >= 0); + return getTomcatWebServer(tomcat); } private void configureEngine(Engine engine) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java index 9c9149722f..ed8f2e6a2a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.web.embedded.tomcat; import java.util.Arrays; +import java.util.concurrent.atomic.AtomicReference; import org.apache.catalina.Context; import org.apache.catalina.LifecycleEvent; @@ -184,4 +185,19 @@ public class TomcatReactiveWebServerFactoryTests extends AbstractReactiveWebServ .isInstanceOf(ConnectorStartFailedException.class); } + @Test + public void whenGetTomcatWebServerIsOverriddenThenWebServerCreationCanBeCustomized() { + AtomicReference webServerReference = new AtomicReference<>(); + TomcatWebServer webServer = (TomcatWebServer) new TomcatReactiveWebServerFactory() { + + @Override + protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) { + webServerReference.set(new TomcatWebServer(tomcat)); + return webServerReference.get(); + } + + }.getWebServer(new EchoHandler()); + assertThat(webServerReference).hasValue(webServer); + } + }