Browse Source

Add port scan to ServerProperties (server.scan=true)

Also moved ServerProperties to autoconfigure project.
pull/132/head
Dave Syer 13 years ago
parent
commit
61dd7d1dbb
  1. 2
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java
  2. 2
      spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java
  3. 5
      spring-boot-autoconfigure/pom.xml
  4. 21
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java
  5. 3
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java
  6. 1
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java
  7. 14
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java
  8. 1
      spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java
  9. 1
      spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java

2
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

@ -40,9 +40,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean @@ -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;

2
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ManagementServerProperties.java

@ -21,7 +21,7 @@ import java.net.InetAddress; @@ -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;

5
spring-boot-autoconfigure/pom.xml

@ -56,6 +56,11 @@ @@ -56,6 +56,11 @@
<artifactId>hibernate-entitymanager</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>

21
spring-boot/src/main/java/org/springframework/boot/context/embedded/properties/ServerProperties.java → spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

@ -14,7 +14,7 @@ @@ -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 @@ -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 { @@ -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 { @@ -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 { @@ -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());

3
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.java

@ -21,7 +21,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -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 @@ -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();

1
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java

@ -32,7 +32,6 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomi @@ -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;

14
spring-boot/src/test/java/org/springframework/boot/context/embedded/properties/ServerPropertiesTests.java → spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

@ -14,7 +14,7 @@ @@ -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; @@ -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 { @@ -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
}

1
spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java

@ -123,6 +123,7 @@ public class JettyEmbeddedServletContainerFactory extends @@ -123,6 +123,7 @@ public class JettyEmbeddedServletContainerFactory extends
postProcessWebAppContext(context);
server.setHandler(context);
this.logger.info("Server initialized with port: " + getPort());
return getJettyEmbeddedServletContainer(server);
}

1
spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java

@ -134,6 +134,7 @@ public class TomcatEmbeddedServletContainerFactory extends @@ -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);
}

Loading…
Cancel
Save