diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java index 829299b917..69aa441d06 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactory.java @@ -64,6 +64,8 @@ public class JettyEmbeddedServletContainerFactory extends private List configurations = new ArrayList(); + private List jettyServerCustomizers = new ArrayList(); + private ResourceLoader resourceLoader; /** @@ -124,6 +126,10 @@ public class JettyEmbeddedServletContainerFactory extends server.setHandler(context); this.logger.info("Server initialized with port: " + port); + for (JettyServerCustomizer customizer : getServerCustomizers()) { + customizer.customize(server); + } + return getJettyEmbeddedServletContainer(server); } @@ -249,6 +255,36 @@ public class JettyEmbeddedServletContainerFactory extends this.resourceLoader = resourceLoader; } + /** + * Sets {@link JettyServerCustomizer}s that will be applied to the {@link Server} + * before it is started. Calling this method will replace any existing configurations. + * @param customizers the Jetty customizers to apply + */ + public void setServerCustomizers( + Collection customizers) { + Assert.notNull(customizers, "JettyServerCustomizers must not be null"); + this.jettyServerCustomizers = new ArrayList(customizers); + } + + /** + * Returns a mutable collection of Jetty {@link Configuration}s that will be applied + * to the {@link WebAppContext} before the server is created. + * @return the Jetty {@link Configuration}s + */ + public Collection getServerCustomizers() { + return this.jettyServerCustomizers; + } + + /** + * Add {@link JettyServerCustomizer}s that will be applied to the {@link Server} + * before it is started. + * @param customizers the customizers to add + */ + public void addServerCustomizers(JettyServerCustomizer... customizers) { + Assert.notNull(customizers, "Configurations must not be null"); + this.jettyServerCustomizers.addAll(Arrays.asList(customizers)); + } + /** * Sets Jetty {@link Configuration}s that will be applied to the {@link WebAppContext} * before the server is created. Calling this method will replace any existing diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyServerCustomizer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyServerCustomizer.java new file mode 100644 index 0000000000..128e67b0b2 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/jetty/JettyServerCustomizer.java @@ -0,0 +1,34 @@ +/* + * 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.jetty; + +import org.eclipse.jetty.server.Server; + +/** + * Callback interface that can be used to customize a Jetty {@link Server}. + * + * @author Dave Syer + * @see JettyEmbeddedServletContainerFactory + */ +public interface JettyServerCustomizer { + + /** + * @param server the server to customize + */ + void customize(Server server); + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java index 4fc4ffc0e0..8ff9db9261 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/jetty/JettyEmbeddedServletContainerFactoryTests.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.WebAppContext; import org.junit.Test; @@ -62,6 +63,22 @@ public class JettyEmbeddedServletContainerFactoryTests extends } } + @Test + public void jettyCustomizations() throws Exception { + JettyEmbeddedServletContainerFactory factory = getFactory(); + JettyServerCustomizer[] configurations = new JettyServerCustomizer[4]; + for (int i = 0; i < configurations.length; i++) { + configurations[i] = mock(JettyServerCustomizer.class); + } + factory.setServerCustomizers(Arrays.asList(configurations[0], configurations[1])); + factory.addServerCustomizers(configurations[2], configurations[3]); + this.container = factory.getEmbeddedServletContainer(); + InOrder ordered = inOrder((Object[]) configurations); + for (JettyServerCustomizer configuration : configurations) { + ordered.verify(configuration).customize((Server) anyObject()); + } + } + @Test public void sessionTimeout() throws Exception { JettyEmbeddedServletContainerFactory factory = getFactory();