Update port file writer to support reactive servers

Closes gh-8531
pull/6863/merge
Andy Wilkinson 8 years ago
parent bdd8cb3468
commit 328bbaf17f

@ -21,12 +21,14 @@ import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
/**
* An {@link ApplicationListener} that saves embedded server port and management port into
@ -40,7 +42,7 @@ import org.springframework.util.StringUtils;
* @since 1.4.0
*/
public class EmbeddedServerPortFileWriter
implements ApplicationListener<ServletWebServerInitializedEvent> {
implements ApplicationListener<WebServerInitializedEvent> {
private static final String DEFAULT_FILE_NAME = "application.port";
@ -84,7 +86,7 @@ public class EmbeddedServerPortFileWriter
}
@Override
public void onApplicationEvent(ServletWebServerInitializedEvent event) {
public void onApplicationEvent(WebServerInitializedEvent event) {
File portFile = getPortFile(event.getApplicationContext());
try {
String port = String.valueOf(event.getWebServer().getPort());
@ -100,12 +102,12 @@ public class EmbeddedServerPortFileWriter
/**
* Return the actual port file that should be written for the given application
* context. The default implementation builds a file from the source file and the
* application context namespace.
* application context namespace if available.
* @param applicationContext the source application context
* @return the file that should be written
*/
protected File getPortFile(ServletWebServerApplicationContext applicationContext) {
String contextName = applicationContext.getNamespace();
protected File getPortFile(ApplicationContext applicationContext) {
String contextName = getContextName(applicationContext);
if (StringUtils.isEmpty(contextName)) {
return this.file;
}
@ -124,6 +126,17 @@ public class EmbeddedServerPortFileWriter
return new File(this.file.getParentFile(), name);
}
private String getContextName(ApplicationContext applicationContext) {
if (applicationContext instanceof ConfigurableWebApplicationContext) {
return ((ConfigurableWebApplicationContext) applicationContext)
.getNamespace();
}
if (applicationContext instanceof ReactiveWebApplicationContext) {
return ((ReactiveWebApplicationContext) applicationContext).getNamespace();
}
return null;
}
private boolean isUpperCase(String name) {
for (int i = 0; i < name.length(); i++) {
if (Character.isLetter(name.charAt(i))

@ -28,6 +28,8 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
public class GenericReactiveWebApplicationContext extends
AnnotationConfigApplicationContext implements ReactiveWebApplicationContext {
private String namespace;
public GenericReactiveWebApplicationContext() {
super();
}
@ -36,4 +38,14 @@ public class GenericReactiveWebApplicationContext extends
super(annotatedClasses);
}
@Override
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String getNamespace() {
return this.namespace;
}
}

@ -26,4 +26,16 @@ import org.springframework.context.ApplicationContext;
*/
public interface ReactiveWebApplicationContext extends ApplicationContext {
/**
* Set the namespace for this reactive web application context.
* @param namespace the namespace for the context
*/
void setNamespace(String namespace);
/**
* Return the namespace for this reactive web application context, if any.
* @return the namespace or {@code null}
*/
String getNamespace();
}

@ -20,13 +20,20 @@ import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiFunction;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.context.ReactiveWebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
@ -44,10 +51,44 @@ import static org.mockito.Mockito.mock;
* @author Phillip Webb
* @author Andy Wilkinson
*/
@RunWith(Parameterized.class)
public class EmbeddedServerPortFileWriterTests {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@Parameters(name = "{0}")
public static Object[] parameters() {
BiFunction<String, Integer, ? extends WebServerInitializedEvent> servletEvent = (
String name, Integer port) -> {
ServletWebServerApplicationContext applicationContext = mock(
ServletWebServerApplicationContext.class);
given(applicationContext.getNamespace()).willReturn(name);
WebServer source = mock(WebServer.class);
given(source.getPort()).willReturn(port);
ServletWebServerInitializedEvent event = new ServletWebServerInitializedEvent(
applicationContext, source);
return event;
};
BiFunction<String, Integer, ? extends WebServerInitializedEvent> reactiveEvent = (
String name, Integer port) -> {
ReactiveWebServerApplicationContext applicationContext = mock(
ReactiveWebServerApplicationContext.class);
given(applicationContext.getNamespace()).willReturn(name);
WebServer source = mock(WebServer.class);
given(source.getPort()).willReturn(port);
return new ReactiveWebServerInitializedEvent(source, applicationContext);
};
return new Object[] { new Object[] { "Servlet", servletEvent },
new Object[] { "Reactive", reactiveEvent } };
}
private final BiFunction<String, Integer, ? extends WebServerInitializedEvent> eventFactory;
public EmbeddedServerPortFileWriterTests(String name,
BiFunction<String, Integer, ? extends WebServerInitializedEvent> eventFactory) {
this.eventFactory = eventFactory;
}
@Before
@After
@ -117,15 +158,8 @@ public class EmbeddedServerPortFileWriterTests {
assertThat(collectFileNames(file.getParentFile())).contains(managementFile);
}
private ServletWebServerInitializedEvent mockEvent(String name, int port) {
ServletWebServerApplicationContext applicationContext = mock(
ServletWebServerApplicationContext.class);
WebServer source = mock(WebServer.class);
given(applicationContext.getNamespace()).willReturn(name);
given(source.getPort()).willReturn(port);
ServletWebServerInitializedEvent event = new ServletWebServerInitializedEvent(
applicationContext, source);
return event;
private WebServerInitializedEvent mockEvent(String name, int port) {
return this.eventFactory.apply(name, port);
}
private Set<String> collectFileNames(File directory) {

Loading…
Cancel
Save