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