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;
}
}
}