Permalink
Browse files

RF-11953: left PushHandlerFilter Serializable, but provided method to…

… ensure ServletContext is available
  • Loading branch information...
lfryc committed Feb 15, 2012
1 parent 184e778 commit 1578987bfe91ab5e74e7014201df578b131bbb0e
Showing with 25 additions and 16 deletions.
  1. +25 −16 impl/src/main/java/org/richfaces/webapp/PushHandlerFilter.java
@@ -25,7 +25,6 @@
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Collections;
-import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -43,7 +42,6 @@
import org.richfaces.application.push.PushContext;
import org.richfaces.application.push.Request;
import org.richfaces.application.push.Session;
-import org.richfaces.application.push.SessionManager;
import org.richfaces.application.push.impl.RequestImpl;
import org.richfaces.log.Logger;
import org.richfaces.log.RichfacesLogger;
@@ -60,9 +58,13 @@
private static final long serialVersionUID = 5724886106704391903L;
private static final String PUSH_SESSION_ID_PARAM = "pushSessionId";
private static final Logger LOGGER = RichfacesLogger.WEBAPP.getLogger();
- private AtomicReference<SessionManager> sessionManagerReference = new AtomicReference<SessionManager>();
+
+ private int servletMajorVersion;
+ private transient ServletContext servletContext;
public void init(FilterConfig filterConfig) throws ServletException {
+ servletContext = filterConfig.getServletContext();
+ servletMajorVersion = servletContext.getMajorVersion();
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
@@ -74,14 +76,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
chain.doFilter(request, response);
if ("GET".equals(httpReq.getMethod())) {
- Meteor meteor = Meteor.build(httpReq, SCOPE.REQUEST, Collections.<BroadcastFilter>emptyList(), null);
+ Meteor meteor = Meteor.build(httpReq, SCOPE.REQUEST, Collections.<BroadcastFilter> emptyList(), null);
String pushSessionId = httpReq.getParameter(PUSH_SESSION_ID_PARAM);
Session session = null;
if (pushSessionId != null) {
- session = getSessionManager(request).getPushSession(pushSessionId);
+ ensureServletContextAvailable(request);
+ PushContext pushContext = (PushContext) servletContext.getAttribute(PushContext.INSTANCE_KEY_NAME);
+ session = pushContext.getSessionManager().getPushSession(pushSessionId);
}
if (session == null) {
@@ -111,23 +115,28 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
/**
- * Get instance of {@link SessionManager}.
+ * Ensures that servletContext instance is available, or throws exception.
*
- * Instance of {@link SessionManager} is initialized from Current {@link SessionManager} is stored in
- * {@link #sessionManagerReference}, since it needs to be initialized lazily.
+ * This method ensures compatibility with Servlet &lt;3.0, which doesn't support obtaining {@link ServletContext} from
+ * {@link ServletRequest}.
*
- * It can be initialized by providing {@link ServletRequest}.
+ * @param request {@link ServletRequest}
+ * @throws {@link IllegalStateException} when {@link ServletContext} won't be available in Servlets &lt;3.0 environments.
+ * This can happen when this filter was serialized.
*/
- private SessionManager getSessionManager(ServletRequest request) {
- if (sessionManagerReference.get() == null) {
- ServletContext servletContext = request.getServletContext();
- PushContext pushContext = (PushContext) servletContext.getAttribute(PushContext.INSTANCE_KEY_NAME);
- sessionManagerReference.compareAndSet(null, pushContext.getSessionManager());
+ private void ensureServletContextAvailable(ServletRequest request) {
+ if (servletContext == null) {
+ if (servletMajorVersion >= 3) {
+ servletContext = request.getServletContext();
+ } else {
+ throw new IllegalStateException(
+ "ServletContext is not available (you are using Servlets API <3.0; it might be caused by "
+ + PushHandlerFilter.class.getName() + " in distributed environment)");
+ }
}
- return sessionManagerReference.get();
}
public void destroy() {
- sessionManagerReference.set(null);
+ servletContext = null;
}
}

0 comments on commit 1578987

Please sign in to comment.