From 61dd7d1dbbc71d3569aa12dc8ae8fb07c6ccbe03 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 22 Nov 2013 16:43:35 +0000 Subject: [PATCH] Add port scan to ServerProperties (server.scan=true) Also moved ServerProperties to autoconfigure project. --- .../EndpointWebMvcAutoConfiguration.java | 2 +- .../ManagementServerProperties.java | 2 +- spring-boot-autoconfigure/pom.xml | 5 +++++ .../autoconfigure/web}/ServerProperties.java | 21 ++++++++++++++++--- .../ServerPropertiesAutoConfiguration.java | 3 +-- ...erverPropertiesAutoConfigurationTests.java | 1 - .../web}/ServerPropertiesTests.java | 14 ++++++++++++- .../JettyEmbeddedServletContainerFactory.java | 1 + ...TomcatEmbeddedServletContainerFactory.java | 1 + 9 files changed, 41 insertions(+), 9 deletions(-) rename {spring-boot/src/main/java/org/springframework/boot/context/embedded/properties => spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web}/ServerProperties.java (94%) rename {spring-boot/src/test/java/org/springframework/boot/context/embedded/properties => spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web}/ServerPropertiesTests.java (81%) 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 c1db1f57061..ee92f038909 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 @@ -40,9 +40,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; -import org.springframework.boot.context.embedded.properties.ServerProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java index 2f0f2638208..df0f847801e 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java @@ -21,7 +21,7 @@ import java.net.InetAddress; import javax.validation.constraints.NotNull; import org.springframework.boot.autoconfigure.security.SecurityPrequisite; -import org.springframework.boot.context.embedded.properties.ServerProperties; +import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.util.ClassUtils; diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 72904f87364..1cd27087eb9 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -56,6 +56,11 @@ hibernate-entitymanager true + + org.hibernate + hibernate-validator + true + org.hibernate.javax.persistence hibernate-jpa-2.0-api diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/properties/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java similarity index 94% rename from spring-boot/src/main/java/org/springframework/boot/context/embedded/properties/ServerProperties.java rename to spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index a91e8b7c23a..76caeadaf9c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/properties/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.context.embedded.properties; +package org.springframework.boot.autoconfigure.web; import java.io.File; import java.net.InetAddress; @@ -35,6 +35,7 @@ import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomize import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.SocketUtils; import org.springframework.util.StringUtils; /** @@ -53,6 +54,8 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer { private Integer sessionTimeout; + private boolean scan = false; + @NotNull private String contextPath = ""; @@ -78,6 +81,14 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer { this.port = port; } + public boolean getScan() { + return this.scan; + } + + public void setScan(boolean scan) { + this.scan = scan; + } + public InetAddress getAddress() { return this.address; } @@ -100,8 +111,12 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainerFactory factory) { - if (getPort() != null) { - factory.setPort(getPort()); + Integer port = getPort(); + if (this.scan) { + port = SocketUtils.findAvailableTcpPort(port != null ? 8080 : port); + } + if (port != null) { + factory.setPort(port); } if (getAddress() != null) { factory.setAddress(getAddress()); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java index 0559edd2ca6..3625643194e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java @@ -21,7 +21,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; -import org.springframework.boot.context.embedded.properties.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -44,7 +43,7 @@ public class ServerPropertiesAutoConfiguration implements ApplicationContextAwar private ApplicationContext applicationContext; - @Bean(name = "org.springframework.boot.context.embedded.properties.ServerProperties") + @Bean(name = "org.springframework.boot.autoconfigure.web.ServerProperties") @ConditionalOnMissingBean public ServerProperties serverProperties() { return new ServerProperties(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java index 747055692d2..71a916c4cf4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java @@ -32,7 +32,6 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomi import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory; -import org.springframework.boot.context.embedded.properties.ServerProperties; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.ApplicationContextException; import org.springframework.context.annotation.Bean; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/properties/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java similarity index 81% rename from spring-boot/src/test/java/org/springframework/boot/context/embedded/properties/ServerPropertiesTests.java rename to spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index bda19a6b82f..be56375ed67 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/properties/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.context.embedded.properties; +package org.springframework.boot.autoconfigure.web; import java.net.InetAddress; import java.util.Collections; @@ -24,9 +24,12 @@ import java.util.Map; import org.junit.Test; import org.springframework.beans.MutablePropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; +import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory; +import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Tests for {@link ServerProperties}. @@ -68,6 +71,15 @@ public class ServerPropertiesTests { .getProtocolHeader()); } + @Test + public void testPortScan() throws Exception { + this.properties.setScan(true); + this.properties.setPort(1000); + ConfigurableEmbeddedServletContainerFactory factory = new MockEmbeddedServletContainerFactory(); + this.properties.customize(factory); + assertTrue(factory.getPort() > 1000); + } + // FIXME test customize } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java index 0f0d111fb13..24ec2ee0b86 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java @@ -123,6 +123,7 @@ public class JettyEmbeddedServletContainerFactory extends postProcessWebAppContext(context); server.setHandler(context); + this.logger.info("Server initialized with port: " + getPort()); return getJettyEmbeddedServletContainer(server); } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java index a38e04a8d3c..9b578455b78 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java @@ -134,6 +134,7 @@ public class TomcatEmbeddedServletContainerFactory extends tomcat.getEngine().setBackgroundProcessorDelay(-1); prepareContext(tomcat.getHost(), initializers); + this.logger.info("Server initialized with port: " + getPort()); return getTomcatEmbeddedServletContainer(tomcat); }