01: package com.opensymphony.webwork.plexus;
02:
03: import org.codehaus.plexus.DefaultPlexusContainer;
04: import org.codehaus.plexus.PlexusContainer;
05: import org.apache.commons.logging.Log;
06: import org.apache.commons.logging.LogFactory;
07:
08: import javax.servlet.ServletContext;
09: import javax.servlet.ServletContextEvent;
10: import javax.servlet.ServletContextListener;
11: import javax.servlet.http.HttpSession;
12: import javax.servlet.http.HttpSessionEvent;
13: import javax.servlet.http.HttpSessionListener;
14: import java.util.Collections;
15:
16: public class PlexusLifecycleListener implements ServletContextListener,
17: HttpSessionListener {
18: private static final Log log = LogFactory
19: .getLog(PlexusObjectFactory.class);
20:
21: public static boolean loaded = false;
22: public static final String KEY = "webwork.plexus.container";
23:
24: public void contextInitialized(
25: ServletContextEvent servletContextEvent) {
26: loaded = true;
27:
28: try {
29: PlexusContainer pc = new DefaultPlexusContainer();
30: PlexusUtils.configure(pc, "plexus-application.xml");
31: ServletContext ctx = servletContextEvent
32: .getServletContext();
33: ctx.setAttribute(KEY, pc);
34:
35: pc.initialize();
36: pc.start();
37: } catch (Exception e) {
38: log
39: .error(
40: "Error initializing plexus container (scope: application)",
41: e);
42: }
43: }
44:
45: public void contextDestroyed(ServletContextEvent servletContextEvent) {
46: try {
47: ServletContext ctx = servletContextEvent
48: .getServletContext();
49: PlexusContainer pc = (PlexusContainer) ctx
50: .getAttribute(KEY);
51: pc.dispose();
52: } catch (Exception e) {
53: log
54: .error(
55: "Error disposing plexus container (scope: application)",
56: e);
57: }
58: }
59:
60: public void sessionCreated(HttpSessionEvent httpSessionEvent) {
61: try {
62: HttpSession session = httpSessionEvent.getSession();
63: ServletContext ctx = session.getServletContext();
64: PlexusContainer parent = (PlexusContainer) ctx
65: .getAttribute(KEY);
66: PlexusContainer child = parent.createChildContainer(
67: "session", Collections.EMPTY_LIST,
68: Collections.EMPTY_MAP);
69: session.setAttribute(KEY, child);
70: PlexusUtils.configure(child, "plexus-session.xml");
71: child.initialize();
72: child.start();
73: } catch (Exception e) {
74: log
75: .error(
76: "Error initializing plexus container (scope: session)",
77: e);
78: }
79: }
80:
81: public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
82: try {
83: HttpSession session = httpSessionEvent.getSession();
84: PlexusContainer child = (PlexusContainer) session
85: .getAttribute(KEY);
86: child.dispose();
87: } catch (Exception e) {
88: log
89: .error(
90: "Error initializing plexus container (scope: session)",
91: e);
92: }
93: }
94: }
|