From dc98d909ab14a99aa1874770cde352c0d32d0250 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 22 Feb 2017 14:29:59 +0100 Subject: [PATCH] Support reactive web servers with LocalServerPort This commit refactors the `EmbeddedWebServerInitializedEvent` hierarchy to have one specialized event for Servlet based apps and another one for reactive apps. Each event implementation has: * a specific `ApplicationContext` implementation for the app * a custom `getServerId` implementation that differentiates the application server from the management server Closes gh-8348 --- ...ddedReactiveWebServerInitializedEvent.java | 48 +++++++++++++++++++ ...eddedServletContainerInitializedEvent.java | 12 +++++ .../EmbeddedWebServerInitializedEvent.java | 23 ++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedReactiveWebServerInitializedEvent.java diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedReactiveWebServerInitializedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedReactiveWebServerInitializedEvent.java new file mode 100644 index 0000000000..2ecc85340d --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedReactiveWebServerInitializedEvent.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.embedded; + +/** + * Event to be published after the {@link ReactiveWebApplicationContext} is + * refreshed and the {@link EmbeddedWebServer} is ready. Useful for + * obtaining the local port of a running server. + * + * @author Brian Clozel + * @author Stephane Nicoll + * @since 2.0.0 + */ +public class EmbeddedReactiveWebServerInitializedEvent extends EmbeddedWebServerInitializedEvent { + + private final ReactiveWebApplicationContext applicationContext; + + public EmbeddedReactiveWebServerInitializedEvent( + EmbeddedWebServer source, + ReactiveWebApplicationContext applicationContext) { + super(source); + this.applicationContext = applicationContext; + } + + @Override + public ReactiveWebApplicationContext getApplicationContext() { + return this.applicationContext; + } + + @Override + public String getServerId() { + return "server"; + } +} diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java index 2a2c9973fb..dc355ffd03 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java @@ -16,6 +16,8 @@ package org.springframework.boot.context.embedded; +import org.springframework.util.StringUtils; + /** * Event to be published after the {@link EmbeddedWebApplicationContext} is * refreshed and the {@link EmbeddedWebServer} is ready. Useful for @@ -44,8 +46,18 @@ public class EmbeddedServletContainerInitializedEvent extends EmbeddedWebServerI * context) before acting on the server container itself. * @return the applicationContext that the container was created from */ + @Override public EmbeddedWebApplicationContext getApplicationContext() { return this.applicationContext; } + @Override + public String getServerId() { + String name = this.applicationContext.getNamespace(); + if (StringUtils.isEmpty(name)) { + name = "server"; + } + return name; + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java index 17444971e4..0fa1a3bbb5 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java @@ -16,6 +16,7 @@ package org.springframework.boot.context.embedded; +import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; /** @@ -24,13 +25,15 @@ import org.springframework.context.ApplicationEvent; * obtaining the local port of a running server. * * @author Brian Clozel + * @author Stephane Nicoll * @since 2.0.0 * @see EmbeddedServletContainerInitializedEvent + * @see EmbeddedReactiveWebServerInitializedEvent */ @SuppressWarnings("serial") -public class EmbeddedWebServerInitializedEvent extends ApplicationEvent { +public abstract class EmbeddedWebServerInitializedEvent extends ApplicationEvent { - public EmbeddedWebServerInitializedEvent(EmbeddedWebServer source) { + protected EmbeddedWebServerInitializedEvent(EmbeddedWebServer source) { super(source); } @@ -42,6 +45,14 @@ public class EmbeddedWebServerInitializedEvent extends ApplicationEvent { return getSource(); } + /** + * Access the application context that the container was created in. Sometimes it is + * prudent to check that this matches expectations (like being equal to the current + * context) before acting on the server container itself. + * @return the applicationContext that the container was created from + */ + public abstract ApplicationContext getApplicationContext(); + /** * Access the source of the event (an {@link EmbeddedWebServer}). * @return the embedded web server @@ -50,4 +61,12 @@ public class EmbeddedWebServerInitializedEvent extends ApplicationEvent { public EmbeddedWebServer getSource() { return (EmbeddedWebServer) super.getSource(); } + + /** + * Access the {@link EmbeddedWebServer} Id used internally + * to differentiate application / management servers. + * @return the server internal Id + */ + public abstract String getServerId(); + }