KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ActionContexCleanUp


1 package com.opensymphony.webwork.dispatcher;
2
3 import com.opensymphony.xwork.ActionContext;
4 import com.opensymphony.xwork.interceptor.component.ComponentManager;
5
6 import javax.servlet.*;
7 import java.io.IOException JavaDoc;
8
9 /**
10  * Special filter designed to work with the {@link FilterDispatcher} and allow
11  * for easier integration with SiteMesh. Normally, ordering your filters to have
12  * SiteMesh go first, and then {@link FilterDispatcher} go second is perfectly fine.
13  * However, sometimes you may wish to access WebWork-features, including the
14  * value stack, from within your SiteMesh decorators. Because {@link FilterDispatcher}
15  * cleans up the {@link ActionContext}, your decorator won't have access to the
16  * date you want.
17  * <p/>
18  * <p/>
19  * By adding this filter, the {@link FilterDispatcher} will know to not clean up and
20  * instead defer cleanup to this filter. The ordering of the filters should then be:
21  * <p/>
22  * <ul>
23  * <li>this filter</li>
24  * <li>SiteMesh filter</li>
25  * <li>{@link FilterDispatcher}</li>
26  * </ul>
27  *
28  * @author Patrick Lightbody
29  * @see FilterDispatcher
30  * @since 2.2
31  */

32 public class ActionContexCleanUp implements Filter {
33     private static final String JavaDoc CLEANUP_PRESENT = "__cleanup_present";
34
35     public void init(FilterConfig filterConfig) throws ServletException {
36     }
37
38     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException JavaDoc, ServletException {
39         try {
40             req.setAttribute(CLEANUP_PRESENT, Boolean.TRUE);
41             chain.doFilter(req, res);
42             req.setAttribute(CLEANUP_PRESENT, Boolean.FALSE);
43         } finally {
44             cleanUp(req);
45         }
46     }
47
48     protected static void cleanUp(ServletRequest req) {
49         // should we clean up yet?
50
Boolean JavaDoc dontClean = (Boolean JavaDoc) req.getAttribute(CLEANUP_PRESENT);
51         if (dontClean != null && dontClean.booleanValue()) {
52             return;
53         }
54
55         // tear down the component manager if it was created
56
ComponentManager componentManager = (ComponentManager) req.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY);
57         if (componentManager != null) {
58             componentManager.dispose();
59         }
60
61         // always dontClean up the thread request, even if an action hasn't been executed
62
ActionContext.setContext(null);
63     }
64
65     public void destroy() {
66     }
67 }
68
Popular Tags