FileDocCategorySizeDatePackage
ActionContextCleanUp.javaAPI DocExample5030Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

ActionContextCleanUp

public class ActionContextCleanUp extends Object implements Filter
Special filter designed to work with the {@link FilterDispatcher} and allow for easier integration with SiteMesh. Normally, ordering your filters to have SiteMesh go first, and then {@link FilterDispatcher} go second is perfectly fine. However, sometimes you may wish to access Struts features, including the value stack, from within your SiteMesh decorators. Because {@link FilterDispatcher} cleans up the {@link ActionContext}, your decorator won't have access to the data you want.

By adding this filter, the {@link FilterDispatcher} will know to not clean up and instead defer cleanup to this filter. The ordering of the filters should then be:

  • this filter
  • SiteMesh filter
  • {@link FilterDispatcher}
see
FilterDispatcher
see
AbstractFilter
see
Dispatcher
version
$Date: 2006-12-08 14:57:59 -0500 (Fri, 08 Dec 2006) $ $Id: ActionContextCleanUp.java 484717 2006-12-08 19:57:59Z mrdon $

Fields Summary
private static final Log
LOG
private static final String
COUNTER
Constructors Summary
Methods Summary
protected static voidcleanUp(javax.servlet.ServletRequest req)
Clean up the request of threadlocals if this is the last execution

param
req The servlet request

        // should we clean up yet?
        Integer count = (Integer) req.getAttribute(COUNTER);
        if (count != null && count > 0 ) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("skipping cleanup counter="+count);
            }
            return;
        }

        // always dontClean up the thread request, even if an action hasn't been executed
        ActionContext.setContext(null);
        Dispatcher.setInstance(null);
    
public voiddestroy()

    
public voiddoFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res, javax.servlet.FilterChain chain)

see
javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)


             
               

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String timerKey = "ActionContextCleanUp_doFilter: ";
        try {
            UtilTimerStack.push(timerKey);

            try {
                Integer count = (Integer)request.getAttribute(COUNTER);
                if (count == null) {
                    count = new Integer(1);
                }
                else {
                    count = new Integer(count.intValue()+1);
                }
                request.setAttribute(COUNTER, count);

                //LOG.debug("filtering counter="+count);

                chain.doFilter(request, response);
            } finally {
                int counterVal = ((Integer)request.getAttribute(COUNTER)).intValue();
                counterVal -= 1;
                request.setAttribute(COUNTER, new Integer(counterVal));
                cleanUp(request);
            }
        }
        finally {
            UtilTimerStack.pop(timerKey);
        }
    
public voidinit(javax.servlet.FilterConfig arg0)