Use '-1' to disable management port

Update `EndpointWebMvcAutoConfiguration` to use '-1' to indicate that
the management context should be disabled. This brings consistency
between the server settings and the management settings and allows '0'
to be used to pick a random port.

Fixed gh-311
This commit is contained in:
Phillip Webb
2014-02-04 11:30:16 -08:00
parent 9049291d9a
commit 676cacd308
6 changed files with 85 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -82,8 +82,6 @@ import org.springframework.web.servlet.DispatcherServlet;
public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
ApplicationListener<ContextRefreshedEvent> {
private static final Integer DISABLED_PORT = Integer.valueOf(0);
private ApplicationContext applicationContext;
@Autowired
@@ -210,18 +208,18 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware,
managementServerProperties = new ManagementServerProperties();
}
if (DISABLED_PORT.equals(managementServerProperties.getPort())) {
Integer port = managementServerProperties.getPort();
if (port != null && port < 0) {
return DISABLE;
}
if (!(beanFactory instanceof WebApplicationContext)) {
// Current context is not a webapp
return DIFFERENT;
}
return managementServerProperties.getPort() == null
|| serverProperties.getPort() == null
&& managementServerProperties.getPort().equals(8080)
|| managementServerProperties.getPort().equals(
serverProperties.getPort()) ? SAME : DIFFERENT;
return ((port == null)
|| (serverProperties.getPort() == null && port.equals(8080))
|| (port != 0 && port.equals(serverProperties.getPort())) ? SAME
: DIFFERENT);
}
};
}

View File

@@ -33,7 +33,11 @@ import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfi
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
@@ -46,6 +50,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
/**
@@ -101,6 +106,27 @@ public class EndpointWebMvcAutoConfigurationTests {
assertAllClosed();
}
@Test
public void onRandomPort() throws Exception {
this.applicationContext.register(RootConfig.class, RandomPortConfig.class,
PropertyPlaceholderAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class);
GrabManagementPort grabManagementPort = new GrabManagementPort(
this.applicationContext);
this.applicationContext.addApplicationListener(grabManagementPort);
this.applicationContext.refresh();
int managementPort = grabManagementPort.getServletContainer().getPort();
assertThat(managementPort, not(equalTo(8080)));
assertContent("/controller", 8080, "controlleroutput");
assertContent("/endpoint", 8080, null);
assertContent("/controller", managementPort, null);
assertContent("/endpoint", managementPort, "endpointoutput");
}
@Test
public void disabled() throws Exception {
this.applicationContext.register(RootConfig.class, DisableConfig.class,
@@ -230,7 +256,7 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@Configuration
public static class DisableConfig {
public static class RandomPortConfig {
@Bean
public ManagementServerProperties managementServerProperties() {
@@ -241,6 +267,18 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@Configuration
public static class DisableConfig {
@Bean
public ManagementServerProperties managementServerProperties() {
ManagementServerProperties properties = new ManagementServerProperties();
properties.setPort(-1);
return properties;
}
}
public static class TestEndpoint implements MvcEndpoint {
@RequestMapping
@@ -267,4 +305,27 @@ public class EndpointWebMvcAutoConfigurationTests {
}
private static class GrabManagementPort implements
ApplicationListener<EmbeddedServletContainerInitializedEvent> {
private ApplicationContext rootContext;
private EmbeddedServletContainer servletContainer;
public GrabManagementPort(ApplicationContext rootContext) {
this.rootContext = rootContext;
}
@Override
public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
if (event.getApplicationContext() != this.rootContext) {
this.servletContainer = event.getEmbeddedServletContainer();
}
}
public EmbeddedServletContainer getServletContainer() {
return this.servletContainer;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -42,7 +42,8 @@ public interface ConfigurableEmbeddedServletContainerFactory extends
/**
* Sets the port that the embedded servlet container should listen on. If not
* specified port '8080' will be used. Use port 0 to switch off the server completely.
* specified port '8080' will be used. Use port -1 to disable auto-start (i.e start
* the web application context but not have it listen to any port).
* @param port the port to set
*/
void setPort(int port);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -66,7 +66,7 @@ public interface EmbeddedServletContainer {
void stop() throws EmbeddedServletContainerException;
/**
* @return the port this server is listening on (or zero if none)
* @return the port this server is listening on (or -1 if none)
*/
int getPort();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.
@@ -37,9 +37,16 @@ public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent {
this.applicationContext = applicationContext;
}
/**
* Access the {@link EmbeddedServletContainer}.
* @return the embedded servlet container
*/
public EmbeddedServletContainer getEmbeddedServletContainer() {
return getSource();
}
/**
* Access the source of the event (an {@link EmbeddedServletContainer}).
*
* @return the embedded servlet container
*/
@Override
@@ -51,7 +58,6 @@ public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent {
* 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 ApplicationContext getApplicationContext() {

View File

@@ -60,11 +60,12 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
/**
* Create a new {@link TomcatEmbeddedServletContainer} instance.
* @param tomcat the underlying Tomcat server
* @param autoStart if the server should be started
*/
public TomcatEmbeddedServletContainer(Tomcat tomcat, boolean autoStart) {
this.autoStart = autoStart;
Assert.notNull(tomcat, "Tomcat Server must not be null");
this.tomcat = tomcat;
this.autoStart = autoStart;
initialize();
}