[bs-78] Add port=0 option to switch off main servlet container

[Fixes #48888639]
pull/1/merge
Dave Syer 12 years ago
parent 628a8c79aa
commit e8e059bc90

@ -114,11 +114,12 @@ public abstract class AbstractEmbeddedServletContainerFactory implements
/** /**
* Sets the port that the embedded servlet container should listen on. If not * 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 * @param port the port to set
*/ */
public void setPort(int port) { 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"); throw new IllegalArgumentException("Port must be between 1 and 65535");
} }
this.port = port; this.port = port;

@ -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
}
}

@ -32,7 +32,9 @@ import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebAppContext;
import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactory; 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.EmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmptyEmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.ErrorPage; import org.springframework.bootstrap.context.embedded.ErrorPage;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
@ -91,8 +93,11 @@ public class JettyEmbeddedServletContainerFactory extends
} }
@Override @Override
public JettyEmbeddedServletContainer getEmbdeddedServletContainer( public EmbeddedServletContainer getEmbdeddedServletContainer(
ServletContextInitializer... initializers) { ServletContextInitializer... initializers) {
if (getPort() == 0) {
return new EmptyEmbeddedServletContainer();
}
Server server = new Server(getPort()); Server server = new Server(getPort());
WebAppContext context = new WebAppContext(); WebAppContext context = new WebAppContext();

@ -40,6 +40,7 @@ import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletCon
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerException; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerException;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory; import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmptyEmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.ErrorPage; import org.springframework.bootstrap.context.embedded.ErrorPage;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
@ -107,6 +108,9 @@ public class TomcatEmbeddedServletContainerFactory extends
@Override @Override
public EmbeddedServletContainer getEmbdeddedServletContainer( public EmbeddedServletContainer getEmbdeddedServletContainer(
ServletContextInitializer... initializers) { ServletContextInitializer... initializers) {
if (getPort() == 0) {
return new EmptyEmbeddedServletContainer();
}
File baseDir = (this.baseDirectory != null ? this.baseDirectory File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat")); : createTempDir("tomcat"));
this.tomcat.setBaseDir(baseDir.getAbsolutePath()); this.tomcat.setBaseDir(baseDir.getAbsolutePath());
@ -330,6 +334,9 @@ public class TomcatEmbeddedServletContainerFactory extends
public EmbeddedServletContainer getEmbdeddedServletContainer( public EmbeddedServletContainer getEmbdeddedServletContainer(
ServletContextInitializer... initializers) { ServletContextInitializer... initializers) {
if (getPort() == 0) {
return new EmptyEmbeddedServletContainer();
}
StandardService service = new StandardService(); StandardService service = new StandardService();
service.setName(name); service.setName(name);
Connector connector = new Connector("HTTP/1.1"); Connector connector = new Connector("HTTP/1.1");

@ -38,10 +38,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.mockito.InOrder; 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.HttpMethod;
import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
@ -90,6 +86,17 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
assertThat(getResponse("http://localhost:8080/hello"), equalTo("Hello World")); 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 @Test
public void stopServlet() throws Exception { public void stopServlet() throws Exception {
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();

Loading…
Cancel
Save