KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > lifecycle > SessionLifecycleListener


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.lifecycle;
6
7 import com.opensymphony.xwork.interceptor.component.ComponentConfiguration;
8 import com.opensymphony.xwork.interceptor.component.ComponentManager;
9 import com.opensymphony.xwork.interceptor.component.DefaultComponentManager;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.ServletContext JavaDoc;
14 import javax.servlet.http.*;
15 import java.io.Serializable JavaDoc;
16
17
18 /**
19  * A filter to handle the lifecycle of an HTTP session-based XWork component manager.
20  *
21  * @author <a HREF="mailto:joew@thoughtworks.com">Joe Walnes</a>
22  * @author Cameron Braid
23  * @author Bill Lynch (docs)
24  */

25 public class SessionLifecycleListener implements HttpSessionListener, Serializable JavaDoc {
26     //~ Static fields/initializers /////////////////////////////////////////////
27

28     private static final Log log = LogFactory.getLog(SessionLifecycleListener.class);
29
30     //~ Methods ////////////////////////////////////////////////////////////////
31

32     /**
33      * Initializes an XWork component manager for the lifetime of the user's session.
34      *
35      * @param event an HttpSessionEvent object.
36      */

37     public void sessionCreated(HttpSessionEvent event) {
38         if (log.isDebugEnabled()) {
39             log.debug("Session DefaultComponentManager : init");
40         }
41
42         HttpSession session = event.getSession();
43         ComponentManager container = createComponentManager();
44         ServletContext JavaDoc application = getServletContext(session);
45         ComponentManager fallback = (ComponentManager) application.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY);
46         if (fallback != null) {
47              container.setFallback(fallback);
48         }
49
50         ComponentConfiguration config = (ComponentConfiguration) application.getAttribute("ComponentConfiguration");
51         if (config != null) {
52             config.configure(container, "session");
53             session.setAttribute(ComponentManager.COMPONENT_MANAGER_KEY, container);
54         }
55     }
56
57     /**
58      * Does nothing - when the session is destroyed the component manager reference will go away as well.
59      *
60      * @param event an HttpSessionEvent object.
61      */

62     public void sessionDestroyed(HttpSessionEvent event) {
63     }
64
65     /**
66      * Servlet 2.3 specifies that the servlet context can be retrieved from the session. Unfortunately, some
67      * versions of WebLogic can only retrieve the servlet context from the filter config. Hence, this method
68      * enables subclasses to retrieve the servlet context from other sources.
69      *
70      * @param session the HTTP session where, in Servlet 2.3, the servlet context can be retrieved
71      * @return the servlet context.
72      */

73     protected ServletContext JavaDoc getServletContext(HttpSession session) {
74         return session.getServletContext();
75     }
76
77     /**
78      * Returns a new <tt>DefaultComponentManager</tt> instance. This method is useful for developers
79      * wishing to subclass this class and provide a different implementation of <tt>DefaultComponentManager</tt>.
80      *
81      * @return a new <tt>DefaultComponentManager</tt> instance.
82      */

83     protected DefaultComponentManager createComponentManager() {
84         return new SessionComponentManager();
85     }
86
87     //~ Inner Classes //////////////////////////////////////////////////////////
88

89     class SessionComponentManager extends DefaultComponentManager implements HttpSessionBindingListener {
90         public void valueBound(HttpSessionBindingEvent event) {
91         }
92
93         public void valueUnbound(HttpSessionBindingEvent event) {
94             if (log.isDebugEnabled()) {
95                 log.debug("Session DefaultComponentManager : destroy");
96             }
97
98             this.dispose();
99         }
100     }
101 }
102
Popular Tags