Browse Source

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
pull/12305/head
Brian Clozel 9 years ago
parent
commit
a5bdbd0dc2
  1. 2
      spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java
  2. 31
      spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java
  3. 6
      spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServer.java
  4. 53
      spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java
  5. 4
      spring-boot/src/main/java/org/springframework/boot/context/embedded/ServerPortInfoApplicationContextInitializer.java
  6. 4
      spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java

2
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java

@ -896,7 +896,7 @@ public class EndpointWebMvcAutoConfigurationTests { @@ -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();
}
}

31
spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java

@ -1,5 +1,5 @@ @@ -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 @@ @@ -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.
*
* <p>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 { @@ -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

6
spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServer.java

@ -1,5 +1,5 @@ @@ -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; @@ -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
*/

53
spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebServerInitializedEvent.java

@ -0,0 +1,53 @@ @@ -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();
}
}

4
spring-boot/src/main/java/org/springframework/boot/context/embedded/ServerPortInfoApplicationContextInitializer.java

@ -1,5 +1,5 @@ @@ -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 @@ -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) {

4
spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java

@ -1,5 +1,5 @@ @@ -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 @@ -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();

Loading…
Cancel
Save