Tidy ConfigurableEmbeddedServletContainerFactory
Rename ConfigurableEmbeddedServletContainerFactory to ConfigurableEmbeddedServletContainer and extract AbstractConfigurableEmbeddedServletContainer from AbstractEmbeddedServletContainerFactory.
This commit is contained in:
@@ -36,7 +36,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.ErrorPage;
|
||||
@@ -76,16 +76,16 @@ public class EndpointWebMvcChildContextConfiguration {
|
||||
private ManagementServerProperties managementServerProperties;
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
if (this.managementServerProperties == null) {
|
||||
this.managementServerProperties = BeanFactoryUtils
|
||||
.beanOfTypeIncludingAncestors(this.beanFactory,
|
||||
ManagementServerProperties.class);
|
||||
}
|
||||
factory.setPort(this.managementServerProperties.getPort());
|
||||
factory.setAddress(this.managementServerProperties.getAddress());
|
||||
factory.setContextPath(this.managementServerProperties.getContextPath());
|
||||
factory.addErrorPages(new ErrorPage(this.errorPath));
|
||||
container.setPort(this.managementServerProperties.getPort());
|
||||
container.setAddress(this.managementServerProperties.getAddress());
|
||||
container.setContextPath(this.managementServerProperties.getContextPath());
|
||||
container.addErrorPages(new ErrorPage(this.errorPath));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.DefaultTemplateResolverConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.ErrorPage;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -80,8 +80,8 @@ public class ErrorMvcAutoConfiguration implements EmbeddedServletContainerCustom
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
factory.addErrorPages(new ErrorPage(this.errorPath));
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
container.addErrorPages(new ErrorPage(this.errorPath));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.apache.catalina.valves.AccessLogValve;
|
||||
import org.apache.catalina.valves.RemoteIpValve;
|
||||
import org.apache.coyote.AbstractProtocol;
|
||||
import org.apache.coyote.ProtocolHandler;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
@@ -110,22 +110,22 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
Integer port = getPort();
|
||||
if (port != null) {
|
||||
factory.setPort(port);
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
if (getPort() != null) {
|
||||
container.setPort(getPort());
|
||||
}
|
||||
if (getAddress() != null) {
|
||||
factory.setAddress(getAddress());
|
||||
container.setAddress(getAddress());
|
||||
}
|
||||
if (getContextPath() != null) {
|
||||
factory.setContextPath(getContextPath());
|
||||
container.setContextPath(getContextPath());
|
||||
}
|
||||
if (getSessionTimeout() != null) {
|
||||
factory.setSessionTimeout(getSessionTimeout());
|
||||
container.setSessionTimeout(getSessionTimeout());
|
||||
}
|
||||
if (factory instanceof TomcatEmbeddedServletContainerFactory) {
|
||||
getTomcat().customizeTomcat((TomcatEmbeddedServletContainerFactory) factory);
|
||||
if (container instanceof TomcatEmbeddedServletContainerFactory) {
|
||||
getTomcat()
|
||||
.customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -33,7 +33,7 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} that configures the
|
||||
* {@link ConfigurableEmbeddedServletContainerFactory} from a {@link ServerProperties}
|
||||
* {@link ConfigurableEmbeddedServletContainer} from a {@link ServerProperties}
|
||||
* bean.
|
||||
*
|
||||
* @author Dave Syer
|
||||
@@ -59,7 +59,7 @@ public class ServerPropertiesAutoConfiguration implements ApplicationContextAwar
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
// ServerProperties handles customization, this just checks we only have
|
||||
// a single bean
|
||||
String[] serverPropertiesBeans = this.applicationContext
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory;
|
||||
@@ -211,7 +211,7 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainerFactory) {
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainer) {
|
||||
MockEmbeddedServletContainerFactory containerFactory = (MockEmbeddedServletContainerFactory) bean;
|
||||
assertNull(containerFactory.getServletContext());
|
||||
}
|
||||
@@ -229,8 +229,8 @@ public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
public static class CallbackEmbeddedContainerCustomizer implements
|
||||
EmbeddedServletContainerCustomizer {
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
factory.setPort(9000);
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
container.setPort(9000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.mockito.Mockito;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
@@ -177,9 +177,10 @@ public class ServerPropertiesAutoConfigurationTests {
|
||||
return new EmbeddedServletContainerCustomizer() {
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
factory.setPort(3000);
|
||||
public void customize(ConfigurableEmbeddedServletContainer container) {
|
||||
container.setPort(3000);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ 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.ConfigurableEmbeddedServletContainer;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -73,14 +73,14 @@ public class ServerPropertiesTests {
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcat() throws Exception {
|
||||
ConfigurableEmbeddedServletContainerFactory factory = mock(ConfigurableEmbeddedServletContainerFactory.class);
|
||||
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
|
||||
this.properties.customize(factory);
|
||||
verify(factory).setContextPath("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomizeTomcatPort() throws Exception {
|
||||
ConfigurableEmbeddedServletContainerFactory factory = mock(ConfigurableEmbeddedServletContainerFactory.class);
|
||||
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
|
||||
this.properties.setPort(8080);
|
||||
this.properties.customize(factory);
|
||||
verify(factory).setPort(8080);
|
||||
|
||||
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
* 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.boot.context.embedded;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link ConfigurableEmbeddedServletContainer} implementations.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
* @see AbstractEmbeddedServletContainerFactory
|
||||
*/
|
||||
public abstract class AbstractConfigurableEmbeddedServletContainer implements
|
||||
ConfigurableEmbeddedServletContainer {
|
||||
|
||||
private String contextPath = "";
|
||||
|
||||
private boolean registerDefaultServlet = true;
|
||||
|
||||
private boolean registerJspServlet = true;
|
||||
|
||||
private String jspServletClassName = "org.apache.jasper.servlet.JspServlet";
|
||||
|
||||
private int port = 8080;
|
||||
|
||||
private List<ServletContextInitializer> initializers = new ArrayList<ServletContextInitializer>();
|
||||
|
||||
private File documentRoot;
|
||||
|
||||
private Set<ErrorPage> errorPages = new LinkedHashSet<ErrorPage>();
|
||||
|
||||
private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT);
|
||||
|
||||
private InetAddress address;
|
||||
|
||||
private int sessionTimeout;
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractConfigurableEmbeddedServletContainer} instance.
|
||||
*/
|
||||
public AbstractConfigurableEmbeddedServletContainer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractConfigurableEmbeddedServletContainer} instance with the
|
||||
* specified port.
|
||||
* @param port the port number for the embedded servlet container
|
||||
*/
|
||||
public AbstractConfigurableEmbeddedServletContainer(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractConfigurableEmbeddedServletContainer} instance with the
|
||||
* specified context path and port.
|
||||
* @param contextPath the context path for the embedded servlet container
|
||||
* @param port the port number for the embedded servlet container
|
||||
*/
|
||||
public AbstractConfigurableEmbeddedServletContainer(String contextPath, int port) {
|
||||
checkContextPath(contextPath);
|
||||
this.contextPath = contextPath;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContextPath(String contextPath) {
|
||||
checkContextPath(contextPath);
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
private void checkContextPath(String contextPath) {
|
||||
Assert.notNull(contextPath, "ContextPath must not be null");
|
||||
if (contextPath.length() > 0) {
|
||||
if ("/".equals(contextPath)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Root ContextPath must be specified using an empty string");
|
||||
}
|
||||
if (!contextPath.startsWith("/") || contextPath.endsWith("/")) {
|
||||
throw new IllegalArgumentException(
|
||||
"ContextPath must start with '/ and not end with '/'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the context path for the embedded servlet container. The path will start
|
||||
* with "/" and not end with "/". The root context is represented by an empty string.
|
||||
*/
|
||||
public String getContextPath() {
|
||||
return this.contextPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* The port that the embedded server listens on.
|
||||
* @return the port
|
||||
*/
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAddress(InetAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the address the embedded container binds to
|
||||
*/
|
||||
public InetAddress getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSessionTimeout(int sessionTimeout) {
|
||||
this.sessionTimeout = sessionTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSessionTimeout(int sessionTimeout, TimeUnit timeUnit) {
|
||||
Assert.notNull(timeUnit, "TimeUnit must not be null");
|
||||
this.sessionTimeout = (int) timeUnit.toSeconds(sessionTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the session timeout in seconds
|
||||
*/
|
||||
public int getSessionTimeout() {
|
||||
return this.sessionTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInitializers(List<? extends ServletContextInitializer> initializers) {
|
||||
Assert.notNull(initializers, "Initializers must not be null");
|
||||
this.initializers = new ArrayList<ServletContextInitializer>(initializers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInitializers(ServletContextInitializer... initializers) {
|
||||
Assert.notNull(initializers, "Initializers must not be null");
|
||||
this.initializers.addAll(Arrays.asList(initializers));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocumentRoot(File documentRoot) {
|
||||
this.documentRoot = documentRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the document root which will be used by the web context to serve static
|
||||
* files.
|
||||
*/
|
||||
public File getDocumentRoot() {
|
||||
return this.documentRoot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorPages(Set<ErrorPage> errorPages) {
|
||||
Assert.notNull(errorPages, "ErrorPages must not be null");
|
||||
this.errorPages = new LinkedHashSet<ErrorPage>(errorPages);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addErrorPages(ErrorPage... errorPages) {
|
||||
Assert.notNull(errorPages, "ErrorPages must not be null");
|
||||
this.errorPages.addAll(Arrays.asList(errorPages));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mutable set of {@link ErrorPage}s that will be used when handling
|
||||
* exceptions.
|
||||
*/
|
||||
public Set<ErrorPage> getErrorPages() {
|
||||
return this.errorPages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMimeMappings(MimeMappings mimeMappings) {
|
||||
this.mimeMappings = new MimeMappings(mimeMappings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mime-type mappings.
|
||||
* @return the mimeMappings the mime-type mappings.
|
||||
*/
|
||||
public MimeMappings getMimeMappings() {
|
||||
return this.mimeMappings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegisterDefaultServlet(boolean registerDefaultServlet) {
|
||||
this.registerDefaultServlet = registerDefaultServlet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the JSP servlet should be registered if available on the
|
||||
* classpath.
|
||||
* @return true if the JSP servlet is to be registered
|
||||
*/
|
||||
public boolean isRegisterJspServlet() {
|
||||
return this.registerJspServlet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegisterJspServlet(boolean registerJspServlet) {
|
||||
this.registerJspServlet = registerJspServlet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the default servlet should be registered.
|
||||
* @return true if the default servlet is to be registered
|
||||
*/
|
||||
public boolean isRegisterDefaultServlet() {
|
||||
return this.registerDefaultServlet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setJspServletClassName(String jspServletClassName) {
|
||||
this.jspServletClassName = jspServletClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the JSP servlet class name
|
||||
*/
|
||||
protected String getJspServletClassName() {
|
||||
return this.jspServletClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that can be used by subclasses wishing to combine the specified
|
||||
* {@link ServletContextInitializer} parameters with those defined in this instance.
|
||||
* @param initializers the initializers to merge
|
||||
* @return a complete set of merged initializers (with the specified parameters
|
||||
* appearing first)
|
||||
*/
|
||||
protected final ServletContextInitializer[] mergeInitializers(
|
||||
ServletContextInitializer... initializers) {
|
||||
List<ServletContextInitializer> mergedInitializers = new ArrayList<ServletContextInitializer>();
|
||||
mergedInitializers.addAll(Arrays.asList(initializers));
|
||||
mergedInitializers.addAll(this.initializers);
|
||||
return mergedInitializers
|
||||
.toArray(new ServletContextInitializer[mergedInitializers.size()]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,21 +18,14 @@ package org.springframework.boot.context.embedded;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.JarURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.security.CodeSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link EmbeddedServletContainerFactory} implementations.
|
||||
@@ -40,255 +33,25 @@ import org.springframework.util.Assert;
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public abstract class AbstractEmbeddedServletContainerFactory implements
|
||||
EmbeddedServletContainerFactory, ConfigurableEmbeddedServletContainerFactory {
|
||||
public abstract class AbstractEmbeddedServletContainerFactory extends
|
||||
AbstractConfigurableEmbeddedServletContainer implements
|
||||
EmbeddedServletContainerFactory {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private static final String[] COMMON_DOC_ROOTS = { "src/main/webapp", "public",
|
||||
"static" };
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private String contextPath = "";
|
||||
|
||||
private boolean registerDefaultServlet = true;
|
||||
|
||||
private boolean registerJspServlet = true;
|
||||
|
||||
private String jspServletClassName = "org.apache.jasper.servlet.JspServlet";
|
||||
|
||||
private int port = 8080;
|
||||
|
||||
private List<ServletContextInitializer> initializers = new ArrayList<ServletContextInitializer>();
|
||||
|
||||
private File documentRoot;
|
||||
|
||||
private Set<ErrorPage> errorPages = new LinkedHashSet<ErrorPage>();
|
||||
|
||||
private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT);
|
||||
|
||||
private InetAddress address;
|
||||
|
||||
private int sessionTimeout;
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractEmbeddedServletContainerFactory} instance.
|
||||
*/
|
||||
public AbstractEmbeddedServletContainerFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractEmbeddedServletContainerFactory} instance with the
|
||||
* specified port.
|
||||
* @param port the port number for the embedded servlet container
|
||||
*/
|
||||
public AbstractEmbeddedServletContainerFactory(int port) {
|
||||
this.port = port;
|
||||
super(port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link AbstractEmbeddedServletContainerFactory} instance with the
|
||||
* specified context path and port.
|
||||
* @param contextPath the context path for the embedded servlet container
|
||||
* @param port the port number for the embedded servlet container
|
||||
*/
|
||||
public AbstractEmbeddedServletContainerFactory(String contextPath, int port) {
|
||||
checkContextPath(contextPath);
|
||||
this.contextPath = contextPath;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContextPath(String contextPath) {
|
||||
checkContextPath(contextPath);
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
private void checkContextPath(String contextPath) {
|
||||
Assert.notNull(contextPath, "ContextPath must not be null");
|
||||
if (contextPath.length() > 0) {
|
||||
if ("/".equals(contextPath)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Root ContextPath must be specified using an empty string");
|
||||
}
|
||||
if (!contextPath.startsWith("/") || contextPath.endsWith("/")) {
|
||||
throw new IllegalArgumentException(
|
||||
"ContextPath must start with '/ and not end with '/'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the context path for the embedded servlet container. The path will start
|
||||
* with "/" and not end with "/". The root context is represented by an empty string.
|
||||
*/
|
||||
public String getContextPath() {
|
||||
return this.contextPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* The port that the embedded server listens on.
|
||||
* @return the port
|
||||
*/
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAddress(InetAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the address the embedded container binds to
|
||||
*/
|
||||
public InetAddress getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSessionTimeout(int sessionTimeout) {
|
||||
this.sessionTimeout = sessionTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSessionTimeout(int sessionTimeout, TimeUnit timeUnit) {
|
||||
Assert.notNull(timeUnit, "TimeUnit must not be null");
|
||||
this.sessionTimeout = (int) timeUnit.toSeconds(sessionTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the session timeout in seconds
|
||||
*/
|
||||
public int getSessionTimeout() {
|
||||
return this.sessionTimeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInitializers(List<? extends ServletContextInitializer> initializers) {
|
||||
Assert.notNull(initializers, "Initializers must not be null");
|
||||
this.initializers = new ArrayList<ServletContextInitializer>(initializers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInitializers(ServletContextInitializer... initializers) {
|
||||
Assert.notNull(initializers, "Initializers must not be null");
|
||||
this.initializers.addAll(Arrays.asList(initializers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mutable list of {@link ServletContextInitializer} that should be applied
|
||||
* in addition to {@link #getEmbeddedServletContainer(ServletContextInitializer...)}
|
||||
* parameters.
|
||||
* @return the initializers
|
||||
*/
|
||||
public List<ServletContextInitializer> getInitializers() {
|
||||
return this.initializers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDocumentRoot(File documentRoot) {
|
||||
this.documentRoot = documentRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the document root which will be used by the web context to serve static
|
||||
* files.
|
||||
*/
|
||||
public File getDocumentRoot() {
|
||||
return this.documentRoot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorPages(Set<ErrorPage> errorPages) {
|
||||
Assert.notNull(errorPages, "ErrorPages must not be null");
|
||||
this.errorPages = new LinkedHashSet<ErrorPage>(errorPages);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addErrorPages(ErrorPage... errorPages) {
|
||||
Assert.notNull(errorPages, "ErrorPages must not be null");
|
||||
this.errorPages.addAll(Arrays.asList(errorPages));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mutable set of {@link ErrorPage}s that will be used when handling
|
||||
* exceptions.
|
||||
*/
|
||||
public Set<ErrorPage> getErrorPages() {
|
||||
return this.errorPages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMimeMappings(MimeMappings mimeMappings) {
|
||||
this.mimeMappings = new MimeMappings(mimeMappings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mime-type mappings.
|
||||
* @return the mimeMappings the mime-type mappings.
|
||||
*/
|
||||
public MimeMappings getMimeMappings() {
|
||||
return this.mimeMappings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegisterDefaultServlet(boolean registerDefaultServlet) {
|
||||
this.registerDefaultServlet = registerDefaultServlet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the JSP servlet should be registered if available on the
|
||||
* classpath.
|
||||
* @return true if the JSP servlet is to be registered
|
||||
*/
|
||||
public boolean isRegisterJspServlet() {
|
||||
return this.registerJspServlet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRegisterJspServlet(boolean registerJspServlet) {
|
||||
this.registerJspServlet = registerJspServlet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the default servlet should be registered.
|
||||
* @return true if the default servlet is to be registered
|
||||
*/
|
||||
public boolean isRegisterDefaultServlet() {
|
||||
return this.registerDefaultServlet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setJspServletClassName(String jspServletClassName) {
|
||||
this.jspServletClassName = jspServletClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the JSP servlet class name
|
||||
*/
|
||||
protected String getJspServletClassName() {
|
||||
return this.jspServletClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that can be used by subclasses wishing to combine the specified
|
||||
* {@link ServletContextInitializer} parameters with those defined in this instance.
|
||||
* @param initializers the initializers to merge
|
||||
* @return a complete set of merged initializers (with the specified parameters
|
||||
* appearing first)
|
||||
*/
|
||||
protected final ServletContextInitializer[] mergeInitializers(
|
||||
ServletContextInitializer... initializers) {
|
||||
List<ServletContextInitializer> mergedInitializers = new ArrayList<ServletContextInitializer>();
|
||||
mergedInitializers.addAll(Arrays.asList(initializers));
|
||||
mergedInitializers.addAll(getInitializers());
|
||||
return mergedInitializers
|
||||
.toArray(new ServletContextInitializer[mergedInitializers.size()]);
|
||||
super(contextPath, port);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* @see EmbeddedServletContainerFactory
|
||||
* @see EmbeddedServletContainerCustomizer
|
||||
*/
|
||||
public interface ConfigurableEmbeddedServletContainerFactory {
|
||||
public interface ConfigurableEmbeddedServletContainer {
|
||||
|
||||
/**
|
||||
* Sets the context path for the embedded servlet container. The context should start
|
||||
@@ -35,9 +35,9 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
public interface EmbeddedServletContainerCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the specified {@link ConfigurableEmbeddedServletContainerFactory}.
|
||||
* @param factory the factory to customize
|
||||
* Customize the specified {@link ConfigurableEmbeddedServletContainer}.
|
||||
* @param container the container to customize
|
||||
*/
|
||||
void customize(ConfigurableEmbeddedServletContainerFactory factory);
|
||||
void customize(ConfigurableEmbeddedServletContainer container);
|
||||
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
|
||||
/**
|
||||
* {@link BeanPostProcessor} that apply all {@link EmbeddedServletContainerCustomizer}s
|
||||
* from the bean factory to {@link ConfigurableEmbeddedServletContainerFactory} beans.
|
||||
* from the bean factory to {@link ConfigurableEmbeddedServletContainer} beans.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
@@ -50,8 +50,8 @@ public class EmbeddedServletContainerCustomizerBeanPostProcessor implements
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainerFactory) {
|
||||
postProcessBeforeInitialization((ConfigurableEmbeddedServletContainerFactory) bean);
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainer) {
|
||||
postProcessBeforeInitialization((ConfigurableEmbeddedServletContainer) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class EmbeddedServletContainerCustomizerBeanPostProcessor implements
|
||||
}
|
||||
|
||||
private void postProcessBeforeInitialization(
|
||||
ConfigurableEmbeddedServletContainerFactory bean) {
|
||||
ConfigurableEmbeddedServletContainer bean) {
|
||||
for (EmbeddedServletContainerCustomizer customizer : getCustomizers()) {
|
||||
customizer.customize(bean);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user