Skip to content

Commit

Permalink
Fixed problem where ServletContext was lost with invalidated sessions…
Browse files Browse the repository at this point in the history
… for cleaning up the conversation store.
  • Loading branch information
drallen committed Mar 6, 2010
1 parent d0808f7 commit a0b5426
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
Expand Up @@ -18,6 +18,7 @@

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.jboss.weld.context.SessionContext;
Expand All @@ -36,8 +37,10 @@ public class HttpSessionBeanStore extends AbstractAttributeBackedBeanStore

private static final NamingScheme NAMING_SCHEME = new NamingScheme(SessionContext.class.getName(), "#");

// The HTTP session context to use as backing map
// The HTTP session to use as backing map
private HttpSession session;
// The ServletContext associated with the session
private ServletContext servletContext;

/**
* Attaches this bean store to a session dynamically. This allows the session
Expand All @@ -50,6 +53,7 @@ public class HttpSessionBeanStore extends AbstractAttributeBackedBeanStore
public void attachToSession(HttpSession session)
{
this.session = session;
this.servletContext = session.getServletContext();
}

/**
Expand Down Expand Up @@ -100,4 +104,9 @@ protected NamingScheme getNamingScheme()
return NAMING_SCHEME;
}

public ServletContext getServletContext()
{
return servletContext;
}

}
48 changes: 18 additions & 30 deletions impl/src/main/java/org/jboss/weld/servlet/ServletLifecycle.java
Expand Up @@ -22,10 +22,12 @@
*/
package org.jboss.weld.servlet;

import static org.jboss.weld.jsf.JsfHelper.getServletContext;
import static org.jboss.weld.logging.messages.ServletMessage.REQUEST_SCOPE_BEAN_STORE_MISSING;
import static org.jboss.weld.servlet.BeanProvider.conversationManager;
import static org.jboss.weld.servlet.BeanProvider.httpSessionManager;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

Expand Down Expand Up @@ -172,38 +174,24 @@ public void beginRequest(HttpServletRequest request)
*/
public void endRequest(HttpServletRequest request)
{
if (request.getAttribute(REQUEST_ATTRIBUTE_NAME) == null)
{
return;
}
teardownSession(request);
teardownRequest(request);
lifecycle.getConversationContext().setBeanStore(null);
lifecycle.getConversationContext().setActive(false);
}

private void teardownSession(HttpServletRequest request)
{
HttpPassThruSessionBeanStore sessionBeanStore = (HttpPassThruSessionBeanStore) lifecycle.getSessionContext().getBeanStore();
if ((sessionBeanStore != null) && (sessionBeanStore.isInvalidated()))
{
conversationManager(request.getSession().getServletContext()).teardownContext();
lifecycle.endSession(request.getRequestedSessionId(), sessionBeanStore);
}
lifecycle.getSessionContext().setActive(false);
lifecycle.getSessionContext().setBeanStore(null);

}

private void teardownRequest(HttpServletRequest request)
{
BeanStore beanStore = (BeanStore) request.getAttribute(REQUEST_ATTRIBUTE_NAME);
if (beanStore == null)
if (request.getAttribute(REQUEST_ATTRIBUTE_NAME) != null)
{
throw new ForbiddenStateException(REQUEST_SCOPE_BEAN_STORE_MISSING);
HttpPassThruSessionBeanStore sessionBeanStore = (HttpPassThruSessionBeanStore) lifecycle.getSessionContext().getBeanStore();
if ((sessionBeanStore != null) && (sessionBeanStore.isInvalidated()))
{
conversationManager(sessionBeanStore.getServletContext()).teardownContext();
lifecycle.endSession(request.getRequestedSessionId(), sessionBeanStore);
}
lifecycle.getSessionContext().setActive(false);
lifecycle.getSessionContext().setBeanStore(null);
BeanStore beanStore = (BeanStore) request.getAttribute(REQUEST_ATTRIBUTE_NAME);
if (beanStore == null)
{
throw new ForbiddenStateException(REQUEST_SCOPE_BEAN_STORE_MISSING);
}
lifecycle.endRequest(request.getRequestURI(), beanStore);
request.removeAttribute(REQUEST_ATTRIBUTE_NAME);
}
lifecycle.endRequest(request.getRequestURI(), beanStore);
request.removeAttribute(REQUEST_ATTRIBUTE_NAME);
}

}

0 comments on commit a0b5426

Please sign in to comment.