Skip to content

Commit

Permalink
context cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nickarls committed Mar 5, 2010
1 parent dc0186d commit d0808f7
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions impl/src/main/java/org/jboss/weld/servlet/ServletLifecycle.java
Expand Up @@ -22,12 +22,10 @@
*/
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 All @@ -52,8 +50,11 @@ public class ServletLifecycle

public static final String REQUEST_ATTRIBUTE_NAME = ServletLifecycle.class.getName() + ".requestBeanStore";


/**
* Constructor
*
* @param lifecycle The lifecycle to work against
*/
public ServletLifecycle(ContextLifecycle lifecycle)
{
Expand Down Expand Up @@ -149,7 +150,7 @@ protected BeanStore restoreSessionContext(HttpSession session)
}

/**
* Begins a HTTP request Sets the session into the session context
* Begins a HTTP request. Sets the session into the session context
*
* @param request The request
*/
Expand All @@ -171,24 +172,38 @@ public void beginRequest(HttpServletRequest request)
*/
public void endRequest(HttpServletRequest request)
{
if (request.getAttribute(REQUEST_ATTRIBUTE_NAME) != null)
if (request.getAttribute(REQUEST_ATTRIBUTE_NAME) == null)
{
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);
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);
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)
{
throw new ForbiddenStateException(REQUEST_SCOPE_BEAN_STORE_MISSING);
}
lifecycle.endRequest(request.getRequestURI(), beanStore);
request.removeAttribute(REQUEST_ATTRIBUTE_NAME);
}

}

0 comments on commit d0808f7

Please sign in to comment.