Add JettyServerCustomizer for Jetty container customization
E.g. can be used to add SSL support to a Jetty container, similarly to a TomcatConnectorCustomizer. Fixes gh-345
This commit is contained in:
@@ -64,6 +64,8 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||
|
||||
private List<Configuration> configurations = new ArrayList<Configuration>();
|
||||
|
||||
private List<JettyServerCustomizer> jettyServerCustomizers = new ArrayList<JettyServerCustomizer>();
|
||||
|
||||
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<? extends JettyServerCustomizer> customizers) {
|
||||
Assert.notNull(customizers, "JettyServerCustomizers must not be null");
|
||||
this.jettyServerCustomizers = new ArrayList<JettyServerCustomizer>(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<JettyServerCustomizer> 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
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user