From a91e85b8487c0998fd9f3be03556df6a4b2e6e5b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 27 May 2014 19:13:05 +0100 Subject: [PATCH] Add local.management.port when on same port Add a `local.management.port` alias when the management server is running on the same port as the main server. Fixes gh-952 --- .../EndpointWebMvcAutoConfiguration.java | 72 +++++++++++++------ .../EndpointWebMvcAutoConfigurationTests.java | 39 ++++++++++ 2 files changed, 89 insertions(+), 22 deletions(-) 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 142ba4e5e9..333ef782dc 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 @@ -65,6 +65,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySource; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.servlet.DispatcherServlet; @@ -122,31 +124,17 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext() == this.applicationContext) { - if (ManagementServerPort.get(this.applicationContext) == ManagementServerPort.DIFFERENT + ManagementServerPort managementPort = ManagementServerPort + .get(this.applicationContext); + if (managementPort == ManagementServerPort.DIFFERENT && this.applicationContext instanceof WebApplicationContext) { createChildManagementContext(); } - } - } - - // Put Servlets and Filters in their own nested class so they don't force early - // instantiation of ManagementServerProperties. - @Configuration - protected static class ApplicationContextFilterConfiguration { - - @Bean - public Filter applicationContextIdFilter(ApplicationContext context) { - final String id = context.getId(); - return new OncePerRequestFilter() { - - @Override - protected void doFilterInternal(HttpServletRequest request, - HttpServletResponse response, FilterChain filterChain) - throws ServletException, IOException { - response.addHeader("X-Application-Context", id); - filterChain.doFilter(request, response); - } - }; + if (managementPort == ManagementServerPort.SAME + && this.applicationContext.getEnvironment() instanceof ConfigurableEnvironment) { + addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.applicationContext + .getEnvironment()); + } } } @@ -232,6 +220,46 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, } }; + /** + * Add an alias for 'local.management.port' that actually resolves using + * 'local.server.port'. + * @param environment the environment + */ + private void addLocalManagementPortPropertyAlias( + final ConfigurableEnvironment environment) { + environment.getPropertySources().addLast( + new PropertySource("Management Server") { + @Override + public Object getProperty(String name) { + if ("local.management.port".equals(name)) { + return environment.getProperty("local.server.port"); + } + return null; + } + }); + } + + // Put Servlets and Filters in their own nested class so they don't force early + // instantiation of ManagementServerProperties. + @Configuration + protected static class ApplicationContextFilterConfiguration { + + @Bean + public Filter applicationContextIdFilter(ApplicationContext context) { + final String id = context.getId(); + return new OncePerRequestFilter() { + + @Override + protected void doFilterInternal(HttpServletRequest request, + HttpServletResponse response, FilterChain filterChain) + throws ServletException, IOException { + response.addHeader("X-Application-Context", id); + filterChain.doFilter(request, response); + } + }; + } + } + protected static enum ManagementServerPort { DISABLE, 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 49d9c671bb..893d55ae25 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 @@ -38,6 +38,7 @@ import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebAppl import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.boot.test.ServerPortInfoApplicationContextInitializer; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; @@ -55,6 +56,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; /** @@ -172,6 +174,43 @@ public class EndpointWebMvcAutoConfigurationTests { assertAllClosed(); } + @Test + public void portPropertiesOnSamePort() throws Exception { + this.applicationContext.register(RootConfig.class, BaseConfiguration.class, + ServerPortConfig.class, EndpointWebMvcAutoConfiguration.class); + new ServerPortInfoApplicationContextInitializer() + .initialize(this.applicationContext); + this.applicationContext.refresh(); + Integer localServerPort = this.applicationContext.getEnvironment().getProperty( + "local.server.port", Integer.class); + Integer localManagementPort = this.applicationContext.getEnvironment() + .getProperty("local.management.port", Integer.class); + assertThat(localServerPort, notNullValue()); + assertThat(localManagementPort, notNullValue()); + assertThat(localServerPort, equalTo(localManagementPort)); + this.applicationContext.close(); + assertAllClosed(); + } + + @Test + public void portPropertiesOnDifferentPort() throws Exception { + new ServerPortInfoApplicationContextInitializer() + .initialize(this.applicationContext); + this.applicationContext.register(RootConfig.class, DifferentPortConfig.class, + BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, + ErrorMvcAutoConfiguration.class); + this.applicationContext.refresh(); + Integer localServerPort = this.applicationContext.getEnvironment().getProperty( + "local.server.port", Integer.class); + Integer localManagementPort = this.applicationContext.getEnvironment() + .getProperty("local.management.port", Integer.class); + assertThat(localServerPort, notNullValue()); + assertThat(localManagementPort, notNullValue()); + assertThat(localServerPort, not(equalTo(localManagementPort))); + this.applicationContext.close(); + assertAllClosed(); + } + private void assertAllClosed() throws Exception { assertContent("/controller", ports.get().server, null); assertContent("/endpoint", ports.get().server, null);