Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 19:07
Show Gist options
  • Save gissuebot/984f60a0266c86563dc8 to your computer and use it in GitHub Desktop.
Save gissuebot/984f60a0266c86563dc8 to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 635, comment 4
Index: extensions/servlet/src/com/google/inject/servlet/GuiceFilter.java
===================================================================
--- extensions/servlet/src/com/google/inject/servlet/GuiceFilter.java (revision 1556)
+++ extensions/servlet/src/com/google/inject/servlet/GuiceFilter.java (working copy)
@@ -17,6 +17,7 @@
package com.google.inject.servlet;
import com.google.inject.Inject;
+import com.google.inject.Injector;
import com.google.inject.OutOfScopeException;
import java.io.IOException;
import java.lang.ref.WeakReference;
@@ -57,13 +58,19 @@
*/
public class GuiceFilter implements Filter {
static final ThreadLocal<Context> localContext = new ThreadLocal<Context>();
- static volatile FilterPipeline pipeline = new DefaultFilterPipeline();
+ static volatile boolean multipleStaticPipelines = false;
+ static volatile FilterPipeline staticPipeline = new DefaultFilterPipeline();
+ @Inject
+ private final FilterPipeline injectedPipeline = null;
/**
- * We allow both the static and dynamic versions of the pipeline to exist.
+ * The pipeline is set by:
+ *
+ * 1. GuiceServletContextListener, otherwise by
+ * 2. Field injection, otherwise by
+ * 3. GuiceFilter.setPipeline()
*/
- @Inject
- private final FilterPipeline injectedPipeline = null;
+ private FilterPipeline pipeline = null;
/** Used to inject the servlets configured via {@link ServletModule} */
static volatile WeakReference<ServletContext> servletContext =
@@ -77,23 +84,24 @@
+ "ignore this message. If this is NOT deliberate however, "
+ "your application may not work as expected.";
+ private static final Logger log = Logger.getLogger(GuiceFilter.class.getName());
+
//VisibleForTesting
@Inject
static void setPipeline(FilterPipeline pipeline) {
// This can happen if you create many injectors and they all have their own
// servlet module. This is legal, caveat a small warning.
- if (GuiceFilter.pipeline instanceof ManagedFilterPipeline) {
- Logger.getLogger(GuiceFilter.class.getName()).warning(MULTIPLE_INJECTORS_WARNING);
- }
+ if (GuiceFilter.staticPipeline instanceof ManagedFilterPipeline)
+ multipleStaticPipelines = true;
- // We overwrite the default pipeline
- GuiceFilter.pipeline = pipeline;
+ // We overwrite the static pipeline
+ GuiceFilter.staticPipeline = pipeline;
}
//VisibleForTesting
static void reset() {
- pipeline = new DefaultFilterPipeline();
+ staticPipeline = new DefaultFilterPipeline();
}
public void doFilter(ServletRequest servletRequest,
@@ -102,15 +110,12 @@
Context previous = localContext.get();
- // Prefer the injected pipeline, but fall back on the static one for web.xml users.
- FilterPipeline filterPipeline = null != injectedPipeline ? injectedPipeline : pipeline;
-
try {
localContext.set(new Context((HttpServletRequest) servletRequest,
(HttpServletResponse) servletResponse));
//dispatch across the servlet pipeline, ensuring web.xml's filterchain is honored
- filterPipeline.dispatch(servletRequest, servletResponse, filterChain);
+ pipeline.dispatch(servletRequest, servletResponse, filterChain);
} finally {
localContext.set(previous);
@@ -165,20 +170,28 @@
// Store servlet context in a weakreference, for injection
GuiceFilter.servletContext = new WeakReference<ServletContext>(servletContext);
- // In the default pipeline, this is a noop. However, if replaced
- // by a managed pipeline, a lazy init will be triggered the first time
- // dispatch occurs.
- FilterPipeline filterPipeline = null != injectedPipeline ? injectedPipeline : pipeline;
- filterPipeline.initPipeline(servletContext);
+ Injector injector = (Injector) servletContext.getAttribute(Injector.class.getName());
+ if (injector != null)
+ pipeline = injector.getInstance(FilterPipeline.class);
+ else if (injectedPipeline != null)
+ pipeline = injectedPipeline;
+ else {
+ pipeline = staticPipeline;
+ if (multipleStaticPipelines) {
+ // Only issue a warning if multiple injectors are detected and
+ // the static pipeline gets used.
+ log.warning(MULTIPLE_INJECTORS_WARNING);
+ }
+ }
+ pipeline.initPipeline(servletContext);
}
public void destroy() {
try {
// Destroy all registered filters & servlets in that order
- FilterPipeline filterPipeline = null != injectedPipeline ? injectedPipeline : pipeline;
- filterPipeline.destroyPipeline();
-
+ if (pipeline != null)
+ pipeline.destroyPipeline();
} finally {
reset();
servletContext.clear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment