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: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 039 */ 040 041 public final class SessionListener 042 implements ServletContextListener, 043 HttpSessionAttributeListener, HttpSessionListener { 044 045 // ----------------------------------------------------- Instance Variables 046 047 048 /** 049 * The servlet context with which we are associated. 050 */ 051 private ServletContext context = null; 052 053 // --------------------------------------------------------- Public Methods 054 055 056 /** 057 * Record the fact that a servlet context attribute was added. 058 * 059 * @param event The session attribute event 060 */ 061 public void attributeAdded(HttpSessionBindingEvent event) { 062 063 log("attributeAdded('" + event.getSession().getId() + "', '" + 064 event.getName() + "', '" + event.getValue() + "')"); 065 066 } 067 068 069 /** 070 * Record the fact that a servlet context attribute was removed. 071 * 072 * @param event The session attribute event 073 */ 074 public void attributeRemoved(HttpSessionBindingEvent event) { 075 076 log("attributeRemoved('" + event.getSession().getId() + "', '" + 077 event.getName() + "', '" + event.getValue() + "')"); 078 079 } 080 081 082 /** 083 * Record the fact that a servlet context attribute was replaced. 084 * 085 * @param event The session attribute event 086 */ 087 public void attributeReplaced(HttpSessionBindingEvent event) { 088 089 log("attributeReplaced('" + event.getSession().getId() + "', '" + 090 event.getName() + "', '" + event.getValue() + "')"); 091 092 } 093 094 095 /** 096 * Record the fact that this web application has been destroyed. 097 * 098 * @param event The servlet context event 099 */ 100 public void contextDestroyed(ServletContextEvent event) { 101 102 log("contextDestroyed()"); 103 this.context = null; 104 105 } 106 107 108 /** 109 * Record the fact that this web application has been initialized. 110 * 111 * @param event The servlet context event 112 */ 113 public void contextInitialized(ServletContextEvent event) { 114 115 this.context = event.getServletContext(); 116 log("contextInitialized()"); 117 118 } 119 120 121 /** 122 * Record the fact that a session has been created. 123 * 124 * @param event The session event 125 */ 126 public void sessionCreated(HttpSessionEvent event) { 127 128 log("sessionCreated('" + event.getSession().getId() + "')"); 129 130 } 131 132 133 /** 134 * Record the fact that a session has been destroyed. 135 * 136 * @param event The session event 137 */ 138 public void sessionDestroyed(HttpSessionEvent event) { 139 140 log("sessionDestroyed('" + event.getSession().getId() + "')"); 141 142 } 143 144 // -------------------------------------------------------- Private Methods 145 146 147 /** 148 * Log a message to the servlet context application log. 149 * 150 * @param message Message to be logged 151 */ 152 private void log(String message) { 153 154 if (context != null) 155 context.log("SessionListener: " + message); 156 else 157 System.out.println("SessionListener: " + message); 158 159 } 160 161 162 /** 163 * Log a message and associated exception to the servlet context 164 * application log. 165 * 166 * @param message Message to be logged 167 * @param throwable Exception to be logged 168 */ 169 private void log(String message, Throwable throwable) { 170 171 if (context != null) 172 context.log("SessionListener: " + message, throwable); 173 else { 174 System.out.println("SessionListener: " + message); 175 throwable.printStackTrace(System.out); 176 } 177 178 } 179 180 181 }