Support ServletContextListener with Jetty 9

Call `context.getServletContext().setExtendedListenerTypes(true)` to
ensure that ServletContextListeners can be registered with Jetty 9.

Fixes gh-2058
This commit is contained in:
Phillip Webb 2014-12-09 09:17:51 -08:00
parent 16164b30a9
commit a69afa0dca
2 changed files with 50 additions and 3 deletions

View File

@ -0,0 +1,40 @@
/*
* Copyright 2012-2014 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 sample.jetty;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.stereotype.Component;
/**
* Simple {@link ServletContextListener} to test gh-2058.
*/
@Component
public class ExampleServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("*** contextInitialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("*** contextDestroyed");
}
}

View File

@ -118,7 +118,6 @@ public class JettyEmbeddedServletContainerFactory extends
configureWebAppContext(context, initializers);
server.setHandler(context);
this.logger.info("Server initialized with port: " + port);
if (getSsl() != null) {
SslContextFactory sslContextFactory = new SslContextFactory();
configureSsl(sslContextFactory, getSsl());
@ -126,11 +125,9 @@ public class JettyEmbeddedServletContainerFactory extends
server, sslContextFactory, port);
server.setConnectors(new Connector[] { connector });
}
for (JettyServerCustomizer customizer : getServerCustomizers()) {
customizer.customize(server);
}
return getJettyEmbeddedServletContainer(server);
}
@ -224,6 +221,7 @@ public class JettyEmbeddedServletContainerFactory extends
protected final void configureWebAppContext(WebAppContext context,
ServletContextInitializer... initializers) {
Assert.notNull(context, "Context must not be null");
setExtendedListenerTypes(context);
if (this.resourceLoader != null) {
context.setClassLoader(this.resourceLoader.getClassLoader());
}
@ -248,6 +246,15 @@ public class JettyEmbeddedServletContainerFactory extends
postProcessWebAppContext(context);
}
private void setExtendedListenerTypes(WebAppContext context) {
try {
context.getServletContext().setExtendedListenerTypes(true);
}
catch (NoSuchMethodError ex) {
// Not available on Jetty 8
}
}
private void configureDocumentRoot(WebAppContext handler) {
File root = getValidDocumentRoot();
if (root != null) {