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 018 package listeners; 019 020 021 import javax.servlet.ServletContext; 022 import javax.servlet.ServletContextEvent; 023 import javax.servlet.ServletContextListener; 024 import javax.servlet.http.HttpSessionAttributeListener; 025 import javax.servlet.http.HttpSessionBindingEvent; 026 import javax.servlet.http.HttpSessionEvent; 027 import javax.servlet.http.HttpSessionListener; 028 029 030 /** 031 * Example listener for context-related application events, which were 032 * introduced in the 2.3 version of the Servlet API. This listener 033 * merely documents the occurrence of such events in the application log 034 * associated with our servlet context. 035 * 036 * @author Craig R. McClanahan 037 * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $ 038 */ 039 040 public final class SessionListener 041 implements ServletContextListener, 042 HttpSessionAttributeListener, HttpSessionListener { 043 044 045 // ----------------------------------------------------- Instance Variables 046 047 048 /** 049 * The servlet context with which we are associated. 050 */ 051 private ServletContext context = null; 052 053 054 // --------------------------------------------------------- Public Methods 055 056 057 /** 058 * Record the fact that a servlet context attribute was added. 059 * 060 * @param event The session attribute event 061 */ 062 public void attributeAdded(HttpSessionBindingEvent event) { 063 064 log("attributeAdded('" + event.getSession().getId() + "', '" + 065 event.getName() + "', '" + event.getValue() + "')"); 066 067 } 068 069 070 /** 071 * Record the fact that a servlet context attribute was removed. 072 * 073 * @param event The session attribute event 074 */ 075 public void attributeRemoved(HttpSessionBindingEvent event) { 076 077 log("attributeRemoved('" + event.getSession().getId() + "', '" + 078 event.getName() + "', '" + event.getValue() + "')"); 079 080 } 081 082 083 /** 084 * Record the fact that a servlet context attribute was replaced. 085 * 086 * @param event The session attribute event 087 */ 088 public void attributeReplaced(HttpSessionBindingEvent event) { 089 090 log("attributeReplaced('" + event.getSession().getId() + "', '" + 091 event.getName() + "', '" + event.getValue() + "')"); 092 093 } 094 095 096 /** 097 * Record the fact that this web application has been destroyed. 098 * 099 * @param event The servlet context event 100 */ 101 public void contextDestroyed(ServletContextEvent event) { 102 103 log("contextDestroyed()"); 104 this.context = null; 105 106 } 107 108 109 /** 110 * Record the fact that this web application has been initialized. 111 * 112 * @param event The servlet context event 113 */ 114 public void contextInitialized(ServletContextEvent event) { 115 116 this.context = event.getServletContext(); 117 log("contextInitialized()"); 118 119 } 120 121 122 /** 123 * Record the fact that a session has been created. 124 * 125 * @param event The session event 126 */ 127 public void sessionCreated(HttpSessionEvent event) { 128 129 log("sessionCreated('" + event.getSession().getId() + "')"); 130 131 } 132 133 134 /** 135 * Record the fact that a session has been destroyed. 136 * 137 * @param event The session event 138 */ 139 public void sessionDestroyed(HttpSessionEvent event) { 140 141 log("sessionDestroyed('" + event.getSession().getId() + "')"); 142 143 } 144 145 146 // -------------------------------------------------------- Private Methods 147 148 149 /** 150 * Log a message to the servlet context application log. 151 * 152 * @param message Message to be logged 153 */ 154 private void log(String message) { 155 156 if (context != null) 157 context.log("SessionListener: " + message); 158 else 159 System.out.println("SessionListener: " + message); 160 161 } 162 163 164 /** 165 * Log a message and associated exception to the servlet context 166 * application log. 167 * 168 * @param message Message to be logged 169 * @param throwable Exception to be logged 170 */ 171 private void log(String message, Throwable throwable) { 172 173 if (context != null) 174 context.log("SessionListener: " + message, throwable); 175 else { 176 System.out.println("SessionListener: " + message); 177 throwable.printStackTrace(System.out); 178 } 179 180 } 181 182 183 }