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