diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index 5ce278f591..1b5816b44f 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -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 { - 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); } }; } 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 2b02b9aabc..05dc56ba3d 100644 --- 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 @@ -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 { + + 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; + } + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ConfigurableEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ConfigurableEmbeddedServletContainerFactory.java index 1bbbef8887..32f396b6bb 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/ConfigurableEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/ConfigurableEmbeddedServletContainerFactory.java @@ -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); diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainer.java index 091e1c65b8..7481c7a110 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainer.java @@ -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(); 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 cf2b6af5f4..0fd38ddb9c 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-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() { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java index e2decf8547..6d2c73f902 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java @@ -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(); }