01: package com.opensymphony.webwork.plexus;
02:
03: import org.apache.commons.logging.Log;
04: import org.apache.commons.logging.LogFactory;
05: import org.codehaus.plexus.PlexusContainer;
06:
07: import javax.servlet.*;
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpSession;
10: import java.io.IOException;
11: import java.util.Collections;
12:
13: /**
14: * @author Patrick Lightbody (plightbo at gmail dot com)
15: */
16: public class PlexusFilter implements Filter {
17: private static final Log log = LogFactory
18: .getLog(PlexusObjectFactory.class);
19:
20: public static boolean loaded = false;
21:
22: private ServletContext ctx;
23:
24: public void init(FilterConfig filterConfig) throws ServletException {
25: ctx = filterConfig.getServletContext();
26: loaded = true;
27: }
28:
29: public void doFilter(ServletRequest req, ServletResponse res,
30: FilterChain chain) throws IOException, ServletException {
31: PlexusContainer child = null;
32: try {
33: HttpServletRequest request = (HttpServletRequest) req;
34: HttpSession session = request.getSession(false);
35: PlexusContainer parent;
36: if (session != null) {
37: parent = (PlexusContainer) session
38: .getAttribute(PlexusLifecycleListener.KEY);
39: } else {
40: parent = (PlexusContainer) ctx
41: .getAttribute(PlexusLifecycleListener.KEY);
42: }
43:
44: child = parent.createChildContainer("request",
45: Collections.EMPTY_LIST, Collections.EMPTY_MAP);
46: PlexusUtils.configure(child, "plexus-request.xml");
47: child.initialize();
48: child.start();
49: PlexusThreadLocal.setPlexusContainer(child);
50: } catch (Exception e) {
51: log
52: .error(
53: "Error initializing plexus container (scope: request)",
54: e);
55: }
56:
57: chain.doFilter(req, res);
58:
59: try {
60: if (child != null) {
61: child.dispose();
62: }
63: PlexusThreadLocal.setPlexusContainer(null);
64: } catch (Exception e) {
65: log
66: .error(
67: "Error disposing plexus container (scope: request)",
68: e);
69: }
70: }
71:
72: public void destroy() {
73: }
74: }
|