[bs-78] Add port=0 option to switch off main servlet container
[Fixes #48888639]
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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.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();
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user