From a5bdbd0dc2eb274f1903cf3ab3f1c82157ea6105 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 14 Feb 2017 14:30:49 +0100 Subject: [PATCH] Introduce EmbeddedWebServerInitializedEvent This commit adds `EmbeddedWebServerInitializedEvent`, which holds the `EmbeddedWebServer` information when the application context starts. The `EmbeddedServletContainerInitializedEvent` now inherits from that type and holds additional information, the `EmbeddedWebApplicationContext`. Closes gh-8208 --- .../EndpointWebMvcAutoConfigurationTests.java | 2 +- ...eddedServletContainerInitializedEvent.java | 31 +++-------- .../context/embedded/EmbeddedWebServer.java | 6 +-- .../EmbeddedWebServerInitializedEvent.java | 53 +++++++++++++++++++ ...PortInfoApplicationContextInitializer.java | 4 +- .../system/EmbeddedServerPortFileWriter.java | 4 +- 6 files changed, 68 insertions(+), 32 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java index 18767797d3b..c938c3901dd 100755 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java @@ -896,7 +896,7 @@ public class EndpointWebMvcAutoConfigurationTests { @Override public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { if (event.getApplicationContext() != this.rootContext) { - this.servletContainer = event.getEmbeddedServletContainer(); + this.servletContainer = event.getEmbeddedWebServer(); } } 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 55ec9537851..2a2c9973fba 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * 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. @@ -16,18 +16,18 @@ package org.springframework.boot.context.embedded; -import org.springframework.context.ApplicationEvent; - /** - * Event to be published after the context is refreshed and the - * {@link EmbeddedWebServer} is ready. Useful for obtaining the local port of a - * running server. Normally it will have been started, but listeners are free to inspect + * Event to be published after the {@link EmbeddedWebApplicationContext} is + * refreshed and the {@link EmbeddedWebServer} is ready. Useful for + * obtaining the local port of a running server. + * + *

Normally it will have been started, but listeners are free to inspect * the server and stop and start it if they want to. * * @author Dave Syer */ @SuppressWarnings("serial") -public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent { +public class EmbeddedServletContainerInitializedEvent extends EmbeddedWebServerInitializedEvent { private final EmbeddedWebApplicationContext applicationContext; @@ -38,23 +38,6 @@ public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent { this.applicationContext = applicationContext; } - /** - * Access the {@link EmbeddedWebServer}. - * @return the embedded servlet container - */ - public EmbeddedWebServer getEmbeddedServletContainer() { - return getSource(); - } - - /** - * Access the source of the event (an {@link EmbeddedWebServer}). - * @return the embedded servlet container - */ - @Override - public EmbeddedWebServer getSource() { - return (EmbeddedWebServer) super.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 diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServer.java index 1e099f85d96..fdca13164d3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * 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. @@ -31,14 +31,14 @@ package org.springframework.boot.context.embedded; public interface EmbeddedWebServer { /** - * Starts the embedded servlet container. Calling this method on an already started + * Starts the embedded web server. Calling this method on an already started * container has no effect. * @throws EmbeddedWebServerException if the container cannot be started */ void start() throws EmbeddedWebServerException; /** - * Stops the embedded servlet container. Calling this method on an already stopped + * Stops the embedded web server. Calling this method on an already stopped * container has no effect. * @throws EmbeddedWebServerException if the container cannot be stopped */ 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 new file mode 100644 index 00000000000..17444971e4f --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java @@ -0,0 +1,53 @@ +/* + * 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; + +import org.springframework.context.ApplicationEvent; + +/** + * Event to be published after the application context is + * refreshed and the {@link EmbeddedWebServer} is ready. Useful for + * obtaining the local port of a running server. + * + * @author Brian Clozel + * @since 2.0.0 + * @see EmbeddedServletContainerInitializedEvent + */ +@SuppressWarnings("serial") +public class EmbeddedWebServerInitializedEvent extends ApplicationEvent { + + public EmbeddedWebServerInitializedEvent(EmbeddedWebServer source) { + super(source); + } + + /** + * Access the {@link EmbeddedWebServer}. + * @return the embedded web server + */ + public EmbeddedWebServer getEmbeddedWebServer() { + return getSource(); + } + + /** + * Access the source of the event (an {@link EmbeddedWebServer}). + * @return the embedded web server + */ + @Override + public EmbeddedWebServer getSource() { + return (EmbeddedWebServer) super.getSource(); + } +} diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServerPortInfoApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServerPortInfoApplicationContextInitializer.java index a7fa35e0954..5a700b3ca9c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServerPortInfoApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ServerPortInfoApplicationContextInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * 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. @@ -69,7 +69,7 @@ public class ServerPortInfoApplicationContextInitializer protected void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { String propertyName = getPropertyName(event.getApplicationContext()); setPortProperty(event.getApplicationContext(), propertyName, - event.getEmbeddedServletContainer().getPort()); + event.getEmbeddedWebServer().getPort()); } protected String getPropertyName(EmbeddedWebApplicationContext context) { diff --git a/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java b/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java index 0d46956a3f0..6ba2f51f55f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java +++ b/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * 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. @@ -87,7 +87,7 @@ public class EmbeddedServerPortFileWriter public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) { File portFile = getPortFile(event.getApplicationContext()); try { - String port = String.valueOf(event.getEmbeddedServletContainer().getPort()); + String port = String.valueOf(event.getEmbeddedWebServer().getPort()); createParentFolder(portFile); FileCopyUtils.copy(port.getBytes(), portFile); portFile.deleteOnExit();