001: // $Id: ContextSessionListener.java,v 1.1 2002/07/18 09:08:03 per_nyfelt Exp $
002:
003: package webapp;
004:
005: import javax.servlet.ServletContext;
006: import javax.servlet.ServletContextEvent;
007: import javax.servlet.ServletContextListener;
008: import javax.servlet.ServletContextAttributeListener;
009: import javax.servlet.ServletContextAttributeEvent;
010: import javax.servlet.http.HttpSessionAttributeListener;
011: import javax.servlet.http.HttpSessionBindingEvent;
012: import javax.servlet.http.HttpSessionEvent;
013: import javax.servlet.http.HttpSessionListener;
014:
015: import org.apache.log4j.Category;
016:
017: import webapp.WebApp;
018:
019: /**
020: * Listener for context-related and session-related application
021: * events. Controls the state of the "WebApp" (which manages
022: * "application global" objects like the database connection).
023: *
024: * @author James Stiefel
025: * @version $Revision: 1.1 $ $Date: 2002/07/18 09:08:03 $
026: */
027:
028: public final class ContextSessionListener implements
029: ServletContextListener, ServletContextAttributeListener,
030: HttpSessionAttributeListener, HttpSessionListener {
031: /**
032: * log4j logger
033: */
034: private static Category logger = Category
035: .getInstance(ContextSessionListener.class.getName());
036:
037: /**
038: * Initialize the application and store in the context.
039: *
040: * @param event The servlet context event
041: */
042: public void contextInitialized(ServletContextEvent event) {
043:
044: logger.info("contextInitialized()");
045:
046: try {
047:
048: WebApp.init();
049:
050: } catch (Exception e) {
051: e.printStackTrace();
052: }
053: }
054:
055: /**
056: * Clean up and close down the application
057: *
058: * @param event The servlet context event
059: */
060: public void contextDestroyed(ServletContextEvent event) {
061:
062: logger.info("contextDestroyed()");
063:
064: //close down the app
065: try {
066:
067: // close the database connection
068: WebApp.term();
069:
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073:
074: }
075:
076: /**
077: * Record the fact that a servlet context attribute was added.
078: *
079: * @param event The servlet context attribute event
080: */
081: public void attributeAdded(ServletContextAttributeEvent event) {
082:
083: logger.info("attributeAdded('" + event.getName() + "', '"
084: + event.getValue() + "')");
085: }
086:
087: /**
088: * Record the fact that a servlet context attribute was removed.
089: *
090: * @param event The servlet context attribute event
091: */
092: public void attributeRemoved(ServletContextAttributeEvent event) {
093:
094: logger.info("attributeRemoved('" + event.getName() + "', '"
095: + event.getValue() + "')");
096: }
097:
098: /**
099: * Record the fact that a servlet context attribute was replaced.
100: *
101: * @param event The servlet context attribute event
102: */
103: public void attributeReplaced(ServletContextAttributeEvent event) {
104:
105: logger.info("attributeReplaced('" + event.getName() + "', '"
106: + event.getValue() + "')");
107:
108: }
109:
110: /**
111: * Record the fact that a servlet session attribute was added.
112: *
113: * @param event The session attribute event
114: */
115: public void attributeAdded(HttpSessionBindingEvent event) {
116:
117: logger.info("attributeAdded('" + event.getSession().getId()
118: + "', '" + event.getName() + "', '" + event.getValue()
119: + "')");
120: }
121:
122: /**
123: * Record the fact that a servlet session attribute was removed.
124: *
125: * @param event The session attribute event
126: */
127: public void attributeRemoved(HttpSessionBindingEvent event) {
128:
129: logger.info("attributeRemoved('" + event.getSession().getId()
130: + "', '" + event.getName() + "', '" + event.getValue()
131: + "')");
132: }
133:
134: /**
135: * Record the fact that a servlet context attribute was replaced.
136: *
137: * @param event The session attribute event
138: */
139: public void attributeReplaced(HttpSessionBindingEvent event) {
140:
141: logger.info("attributeReplaced('" + event.getSession().getId()
142: + "', '" + event.getName() + "', '" + event.getValue()
143: + "')");
144: }
145:
146: /**
147: * Record the fact that a session has been created.
148: *
149: * @param event The session event
150: */
151: public void sessionCreated(HttpSessionEvent event) {
152:
153: logger.info("sessionCreated('" + event.getSession().getId()
154: + "')");
155: }
156:
157: /**
158: * Record the fact that a session has been destroyed.
159: *
160: * @param event The session event
161: */
162: public void sessionDestroyed(HttpSessionEvent event) {
163:
164: logger.info("sessionDestroyed('" + event.getSession().getId()
165: + "')");
166:
167: }
168:
169: }
|