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