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:
parent
16164b30a9
commit
a69afa0dca
|
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -118,7 +118,6 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||||
configureWebAppContext(context, initializers);
|
configureWebAppContext(context, initializers);
|
||||||
server.setHandler(context);
|
server.setHandler(context);
|
||||||
this.logger.info("Server initialized with port: " + port);
|
this.logger.info("Server initialized with port: " + port);
|
||||||
|
|
||||||
if (getSsl() != null) {
|
if (getSsl() != null) {
|
||||||
SslContextFactory sslContextFactory = new SslContextFactory();
|
SslContextFactory sslContextFactory = new SslContextFactory();
|
||||||
configureSsl(sslContextFactory, getSsl());
|
configureSsl(sslContextFactory, getSsl());
|
||||||
|
@ -126,11 +125,9 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||||
server, sslContextFactory, port);
|
server, sslContextFactory, port);
|
||||||
server.setConnectors(new Connector[] { connector });
|
server.setConnectors(new Connector[] { connector });
|
||||||
}
|
}
|
||||||
|
|
||||||
for (JettyServerCustomizer customizer : getServerCustomizers()) {
|
for (JettyServerCustomizer customizer : getServerCustomizers()) {
|
||||||
customizer.customize(server);
|
customizer.customize(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getJettyEmbeddedServletContainer(server);
|
return getJettyEmbeddedServletContainer(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,6 +221,7 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||||
protected final void configureWebAppContext(WebAppContext context,
|
protected final void configureWebAppContext(WebAppContext context,
|
||||||
ServletContextInitializer... initializers) {
|
ServletContextInitializer... initializers) {
|
||||||
Assert.notNull(context, "Context must not be null");
|
Assert.notNull(context, "Context must not be null");
|
||||||
|
setExtendedListenerTypes(context);
|
||||||
if (this.resourceLoader != null) {
|
if (this.resourceLoader != null) {
|
||||||
context.setClassLoader(this.resourceLoader.getClassLoader());
|
context.setClassLoader(this.resourceLoader.getClassLoader());
|
||||||
}
|
}
|
||||||
|
@ -248,6 +246,15 @@ public class JettyEmbeddedServletContainerFactory extends
|
||||||
postProcessWebAppContext(context);
|
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) {
|
private void configureDocumentRoot(WebAppContext handler) {
|
||||||
File root = getValidDocumentRoot();
|
File root = getValidDocumentRoot();
|
||||||
if (root != null) {
|
if (root != null) {
|
||||||
|
|
Loading…
Reference in New Issue