diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactory.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactory.java index 538c2a2256..c588cb5ece 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactory.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactory.java @@ -114,11 +114,12 @@ public abstract class AbstractEmbeddedServletContainerFactory implements /** * Sets the port that the embedded servlet container should listen on. If not - * specified port '8080' will be used. + * specified port '8080' will be used. Use port 0 to switch off the server completely. + * * @param port the port to set */ public void setPort(int port) { - if (port < 1 || port > 65535) { + if (port < 0 || port > 65535) { throw new IllegalArgumentException("Port must be between 1 and 65535"); } this.port = port; diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmptyEmbeddedServletContainer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmptyEmbeddedServletContainer.java new file mode 100644 index 0000000000..d9c7627513 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmptyEmbeddedServletContainer.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.context.embedded; + +/** + * @author Dave Syer + * + */ +public class EmptyEmbeddedServletContainer implements EmbeddedServletContainer { + + @Override + public void stop() throws EmbeddedServletContainerException { + // do nothing + } + +} diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java index b76f194b1e..df6abe6dc6 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java @@ -32,7 +32,9 @@ import org.eclipse.jetty.webapp.AbstractConfiguration; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.WebAppContext; import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactory; +import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory; +import org.springframework.bootstrap.context.embedded.EmptyEmbeddedServletContainer; import org.springframework.bootstrap.context.embedded.ErrorPage; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.ResourceLoader; @@ -91,8 +93,11 @@ public class JettyEmbeddedServletContainerFactory extends } @Override - public JettyEmbeddedServletContainer getEmbdeddedServletContainer( + public EmbeddedServletContainer getEmbdeddedServletContainer( ServletContextInitializer... initializers) { + if (getPort() == 0) { + return new EmptyEmbeddedServletContainer(); + } Server server = new Server(getPort()); WebAppContext context = new WebAppContext(); diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java index 88d4aaca74..40aaf25221 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java @@ -40,6 +40,7 @@ import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletCon import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerException; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory; +import org.springframework.bootstrap.context.embedded.EmptyEmbeddedServletContainer; import org.springframework.bootstrap.context.embedded.ErrorPage; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.ResourceLoader; @@ -107,6 +108,9 @@ public class TomcatEmbeddedServletContainerFactory extends @Override public EmbeddedServletContainer getEmbdeddedServletContainer( ServletContextInitializer... initializers) { + if (getPort() == 0) { + return new EmptyEmbeddedServletContainer(); + } File baseDir = (this.baseDirectory != null ? this.baseDirectory : createTempDir("tomcat")); this.tomcat.setBaseDir(baseDir.getAbsolutePath()); @@ -330,6 +334,9 @@ public class TomcatEmbeddedServletContainerFactory extends public EmbeddedServletContainer getEmbdeddedServletContainer( ServletContextInitializer... initializers) { + if (getPort() == 0) { + return new EmptyEmbeddedServletContainer(); + } StandardService service = new StandardService(); service.setName(name); Connector connector = new Connector("HTTP/1.1"); diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 4a4844ddac..a5b473206f 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -38,10 +38,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.mockito.InOrder; -import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactory; -import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer; -import org.springframework.bootstrap.context.embedded.FilterRegistrationBean; -import org.springframework.bootstrap.context.embedded.ServletRegistrationBean; import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpResponse; @@ -90,6 +86,17 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World")); } + @Test + public void emptyServer() throws Exception { + AbstractEmbeddedServletContainerFactory factory = getFactory(); + factory.setPort(0); + this.container = factory + .getEmbdeddedServletContainer(exampleServletRegistration()); + this.thrown.expect(ConnectException.class); + this.thrown.expectMessage("Connection refused"); + getResponse("http://localhost:8080/hello"); + } + @Test public void stopServlet() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory();