001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package listeners;
018:
019: import javax.servlet.ServletContext;
020: import javax.servlet.ServletContextEvent;
021: import javax.servlet.ServletContextListener;
022: import javax.servlet.http.HttpSessionAttributeListener;
023: import javax.servlet.http.HttpSessionBindingEvent;
024: import javax.servlet.http.HttpSessionEvent;
025: import javax.servlet.http.HttpSessionListener;
026:
027: /**
028: * Example listener for context-related application events, which were
029: * introduced in the 2.3 version of the Servlet API. This listener
030: * merely documents the occurrence of such events in the application log
031: * associated with our servlet context.
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $
035: */
036:
037: public final class SessionListener implements ServletContextListener,
038: HttpSessionAttributeListener, HttpSessionListener {
039:
040: // ----------------------------------------------------- Instance Variables
041:
042: /**
043: * The servlet context with which we are associated.
044: */
045: private ServletContext context = null;
046:
047: // --------------------------------------------------------- Public Methods
048:
049: /**
050: * Record the fact that a servlet context attribute was added.
051: *
052: * @param event The session attribute event
053: */
054: public void attributeAdded(HttpSessionBindingEvent event) {
055:
056: log("attributeAdded('" + event.getSession().getId() + "', '"
057: + event.getName() + "', '" + event.getValue() + "')");
058:
059: }
060:
061: /**
062: * Record the fact that a servlet context attribute was removed.
063: *
064: * @param event The session attribute event
065: */
066: public void attributeRemoved(HttpSessionBindingEvent event) {
067:
068: log("attributeRemoved('" + event.getSession().getId() + "', '"
069: + event.getName() + "', '" + event.getValue() + "')");
070:
071: }
072:
073: /**
074: * Record the fact that a servlet context attribute was replaced.
075: *
076: * @param event The session attribute event
077: */
078: public void attributeReplaced(HttpSessionBindingEvent event) {
079:
080: log("attributeReplaced('" + event.getSession().getId() + "', '"
081: + event.getName() + "', '" + event.getValue() + "')");
082:
083: }
084:
085: /**
086: * Record the fact that this web application has been destroyed.
087: *
088: * @param event The servlet context event
089: */
090: public void contextDestroyed(ServletContextEvent event) {
091:
092: log("contextDestroyed()");
093: this .context = null;
094:
095: }
096:
097: /**
098: * Record the fact that this web application has been initialized.
099: *
100: * @param event The servlet context event
101: */
102: public void contextInitialized(ServletContextEvent event) {
103:
104: this .context = event.getServletContext();
105: log("contextInitialized()");
106:
107: }
108:
109: /**
110: * Record the fact that a session has been created.
111: *
112: * @param event The session event
113: */
114: public void sessionCreated(HttpSessionEvent event) {
115:
116: log("sessionCreated('" + event.getSession().getId() + "')");
117:
118: }
119:
120: /**
121: * Record the fact that a session has been destroyed.
122: *
123: * @param event The session event
124: */
125: public void sessionDestroyed(HttpSessionEvent event) {
126:
127: log("sessionDestroyed('" + event.getSession().getId() + "')");
128:
129: }
130:
131: // -------------------------------------------------------- Private Methods
132:
133: /**
134: * Log a message to the servlet context application log.
135: *
136: * @param message Message to be logged
137: */
138: private void log(String message) {
139:
140: if (context != null)
141: context.log("SessionListener: " + message);
142: else
143: System.out.println("SessionListener: " + message);
144:
145: }
146:
147: /**
148: * Log a message and associated exception to the servlet context
149: * application log.
150: *
151: * @param message Message to be logged
152: * @param throwable Exception to be logged
153: */
154: private void log(String message, Throwable throwable) {
155:
156: if (context != null)
157: context.log("SessionListener: " + message, throwable);
158: else {
159: System.out.println("SessionListener: " + message);
160: throwable.printStackTrace(System.out);
161: }
162:
163: }
164:
165: }
|