Fix Gerrit plugins under Tomcat by avoiding Guice static filter

The static GuiceFilter breaks when a plugin loads and configures
its own web environment. Follow the pattern used by JettyServer
to initialize and load GuiceFilter in a non-static way, allowing
Gerrit to always have its own filter stack during any request.

Bug: issue 1966
Change-Id: I864f3badc4ec90154a276d35e6a5a3fa75058933
This commit is contained in:
Shawn Pearce 2013-08-08 17:46:09 -07:00
parent da062b0bec
commit 09a35b9725
2 changed files with 22 additions and 10 deletions

View File

@ -59,6 +59,7 @@ import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.spi.Message;
@ -67,16 +68,24 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
/** Configures the web application environment for Gerrit Code Review. */
public class WebAppInitializer extends GuiceServletContextListener {
public class WebAppInitializer extends GuiceServletContextListener
implements Filter {
private static final Logger log =
LoggerFactory.getLogger(WebAppInitializer.class);
@ -87,6 +96,13 @@ public class WebAppInitializer extends GuiceServletContextListener {
private Injector webInjector;
private Injector sshInjector;
private LifecycleManager manager;
private GuiceFilter filter;
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
filter.doFilter(req, res, chain);
}
private synchronized void init() {
if (manager == null) {
@ -139,6 +155,7 @@ public class WebAppInitializer extends GuiceServletContextListener {
.setHttpServletRequest(
webInjector.getProvider(HttpServletRequest.class));
filter = webInjector.getInstance(GuiceFilter.class);
manager = new LifecycleManager();
manager.add(dbInjector);
manager.add(cfgInjector);
@ -286,18 +303,17 @@ public class WebAppInitializer extends GuiceServletContextListener {
}
@Override
public void contextInitialized(final ServletContextEvent event) {
super.contextInitialized(event);
public void init(FilterConfig cfg) throws ServletException {
contextInitialized(new ServletContextEvent(cfg.getServletContext()));
init();
manager.start();
}
@Override
public void contextDestroyed(final ServletContextEvent event) {
public void destroy() {
if (manager != null) {
manager.stop();
manager = null;
}
super.contextDestroyed(event);
}
}

View File

@ -8,14 +8,10 @@
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
<filter-class>com.google.gerrit.httpd.WebAppInitializer</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.google.gerrit.httpd.WebAppInitializer</listener-class>
</listener>
</web-app>