KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webapp > ContextSessionListener


1 // $Id: ContextSessionListener.java,v 1.1 2002/07/18 09:08:03 per_nyfelt Exp $
2

3 package webapp;
4
5 import javax.servlet.ServletContextEvent JavaDoc;
6 import javax.servlet.ServletContextListener JavaDoc;
7 import javax.servlet.ServletContextAttributeListener JavaDoc;
8 import javax.servlet.ServletContextAttributeEvent JavaDoc;
9 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
10 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
11 import javax.servlet.http.HttpSessionEvent JavaDoc;
12 import javax.servlet.http.HttpSessionListener JavaDoc;
13
14 import org.apache.log4j.Category;
15
16 import webapp.WebApp;
17
18
19 /**
20  * Listener for context-related and session-related application
21  * events. Controls the state of the "WebApp" (which manages
22  * "application global" objects like the database connection).
23  *
24  * @author James Stiefel
25  * @version $Revision: 1.1 $ $Date: 2002/07/18 09:08:03 $
26  */

27
28 public final class ContextSessionListener
29     implements ServletContextListener JavaDoc, ServletContextAttributeListener JavaDoc,
30                 HttpSessionAttributeListener JavaDoc, HttpSessionListener JavaDoc {
31     /**
32      * log4j logger
33      */

34     private static Category logger = Category.getInstance(ContextSessionListener.class.getName());
35
36     /**
37      * Initialize the application and store in the context.
38      *
39      * @param event The servlet context event
40      */

41     public void contextInitialized(ServletContextEvent JavaDoc event) {
42
43         logger.info("contextInitialized()");
44
45         try {
46
47             WebApp.init();
48
49         } catch (Exception JavaDoc e) {
50             e.printStackTrace();
51         }
52     }
53
54     /**
55      * Clean up and close down the application
56      *
57      * @param event The servlet context event
58      */

59     public void contextDestroyed(ServletContextEvent JavaDoc event) {
60
61         logger.info("contextDestroyed()");
62
63         //close down the app
64
try {
65
66             // close the database connection
67
WebApp.term();
68
69         } catch (Exception JavaDoc e) {
70             e.printStackTrace();
71         }
72
73     }
74
75     /**
76      * Record the fact that a servlet context attribute was added.
77      *
78      * @param event The servlet context attribute event
79      */

80     public void attributeAdded(ServletContextAttributeEvent JavaDoc event) {
81
82         logger.info("attributeAdded('" + event.getName() + "', '" +
83             event.getValue() + "')");
84     }
85
86     /**
87      * Record the fact that a servlet context attribute was removed.
88      *
89      * @param event The servlet context attribute event
90      */

91     public void attributeRemoved(ServletContextAttributeEvent JavaDoc event) {
92
93         logger.info("attributeRemoved('" + event.getName() + "', '" +
94             event.getValue() + "')");
95     }
96
97     /**
98      * Record the fact that a servlet context attribute was replaced.
99      *
100      * @param event The servlet context attribute event
101      */

102     public void attributeReplaced(ServletContextAttributeEvent JavaDoc event) {
103
104         logger.info("attributeReplaced('" + event.getName() + "', '" +
105             event.getValue() + "')");
106
107     }
108
109     /**
110      * Record the fact that a servlet session attribute was added.
111      *
112      * @param event The session attribute event
113      */

114     public void attributeAdded(HttpSessionBindingEvent JavaDoc event) {
115
116         logger.info("attributeAdded('" + event.getSession().getId() + "', '" +
117             event.getName() + "', '" + event.getValue() + "')");
118     }
119
120
121     /**
122      * Record the fact that a servlet session attribute was removed.
123      *
124      * @param event The session attribute event
125      */

126     public void attributeRemoved(HttpSessionBindingEvent JavaDoc event) {
127
128         logger.info("attributeRemoved('" + event.getSession().getId() + "', '" +
129             event.getName() + "', '" + event.getValue() + "')");
130     }
131
132
133     /**
134      * Record the fact that a servlet context attribute was replaced.
135      *
136      * @param event The session attribute event
137      */

138     public void attributeReplaced(HttpSessionBindingEvent JavaDoc event) {
139
140         logger.info("attributeReplaced('" + event.getSession().getId() + "', '" +
141             event.getName() + "', '" + event.getValue() + "')");
142     }
143
144
145
146
147     /**
148      * Record the fact that a session has been created.
149      *
150      * @param event The session event
151      */

152     public void sessionCreated(HttpSessionEvent JavaDoc event) {
153
154         logger.info("sessionCreated('" + event.getSession().getId() + "')");
155     }
156
157
158     /**
159      * Record the fact that a session has been destroyed.
160      *
161      * @param event The session event
162      */

163     public void sessionDestroyed(HttpSessionEvent JavaDoc event) {
164
165         logger.info("sessionDestroyed('" + event.getSession().getId() + "')");
166
167     }
168
169 }
170
Popular Tags