From 22c9364237c4b576ae6792fac84766683ed2818e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 2 Apr 2019 10:41:36 +0100 Subject: [PATCH] Only use AprLifecycleListener when native library is available Closes gh-16040 --- .../tomcat/TomcatReactiveWebServerFactory.java | 11 ++++++++--- .../tomcat/TomcatServletWebServerFactory.java | 10 ++++++++-- .../tomcat/TomcatReactiveWebServerFactoryTests.java | 9 +++++++-- .../tomcat/TomcatServletWebServerFactoryTests.java | 9 +++++++-- 4 files changed, 30 insertions(+), 9 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 814ed4cec02..f2e9f4b3280 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 @@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.List; import org.apache.catalina.Context; @@ -67,8 +66,7 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac private List engineValves = new ArrayList<>(); - private List contextLifecycleListeners = new ArrayList<>( - Collections.singleton(new AprLifecycleListener())); + private List contextLifecycleListeners = getDefaultLifecycleListeners(); private List tomcatContextCustomizers = new ArrayList<>(); @@ -95,6 +93,13 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac super(port); } + private static List getDefaultLifecycleListeners() { + AprLifecycleListener aprLifecycleListener = new AprLifecycleListener(); + return AprLifecycleListener.isAprAvailable() + ? new ArrayList<>(Arrays.asList(aprLifecycleListener)) + : new ArrayList<>(); + } + @Override public WebServer getWebServer(HttpHandler httpHandler) { Tomcat tomcat = new Tomcat(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java index b3b7d3c324b..0d03687c31a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java @@ -111,8 +111,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto private List contextValves = new ArrayList<>(); - private List contextLifecycleListeners = new ArrayList<>( - Collections.singleton(new AprLifecycleListener())); + private List contextLifecycleListeners = getDefaultLifecycleListeners(); private List tomcatContextCustomizers = new ArrayList<>(); @@ -155,6 +154,13 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto super(contextPath, port); } + private static List getDefaultLifecycleListeners() { + AprLifecycleListener aprLifecycleListener = new AprLifecycleListener(); + return AprLifecycleListener.isAprAvailable() + ? new ArrayList<>(Arrays.asList(aprLifecycleListener)) + : new ArrayList<>(); + } + @Override public WebServer getWebServer(ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); 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 e731385ecf4..ca44a9ee0cf 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 @@ -82,8 +82,13 @@ public class TomcatReactiveWebServerFactoryTests @Test public void defaultTomcatListeners() { TomcatReactiveWebServerFactory factory = getFactory(); - assertThat(factory.getContextLifecycleListeners()).hasSize(1).first() - .isInstanceOf(AprLifecycleListener.class); + if (AprLifecycleListener.isAprAvailable()) { + assertThat(factory.getContextLifecycleListeners()).hasSize(1).first() + .isInstanceOf(AprLifecycleListener.class); + } + else { + assertThat(factory.getContextLifecycleListeners()).isEmpty(); + } } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java index b80312128a2..e3b8ed42275 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java @@ -134,8 +134,13 @@ public class TomcatServletWebServerFactoryTests @Test public void defaultTomcatListeners() { TomcatServletWebServerFactory factory = getFactory(); - assertThat(factory.getContextLifecycleListeners()).hasSize(1).first() - .isInstanceOf(AprLifecycleListener.class); + if (AprLifecycleListener.isAprAvailable()) { + assertThat(factory.getContextLifecycleListeners()).hasSize(1).first() + .isInstanceOf(AprLifecycleListener.class); + } + else { + assertThat(factory.getContextLifecycleListeners()).isEmpty(); + } } @Test