Add port scan to ServerProperties (server.scan=true)
Also moved ServerProperties to autoconfigure project.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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());
|
||||
@@ -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();
|
||||
|
||||
@@ -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,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
|
||||
|
||||
}
|
||||
@@ -123,6 +123,7 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||
postProcessWebAppContext(context);
|
||||
|
||||
server.setHandler(context);
|
||||
this.logger.info("Server initialized with port: " + getPort());
|
||||
return getJettyEmbeddedServletContainer(server);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user