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.ServletContextAttributeEvent; 024 import javax.servlet.ServletContextAttributeListener; 025 import javax.servlet.ServletContextEvent; 026 import javax.servlet.ServletContextListener; 027 028 029 /** 030 * Example listener for context-related application events, which were 031 * introduced in the 2.3 version of the Servlet API. This listener 032 * merely documents the occurrence of such events in the application log 033 * associated with our servlet context. 034 * 035 * @author Craig R. McClanahan 036 * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 037 */ 038 039 public final class ContextListener 040 implements ServletContextAttributeListener, ServletContextListener { 041 042 // ----------------------------------------------------- Instance Variables 043 044 045 /** 046 * The servlet context with which we are associated. 047 */ 048 private ServletContext context = null; 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 // -------------------------------------------------------- Private Methods 118 119 120 /** 121 * Log a message to the servlet context application log. 122 * 123 * @param message Message to be logged 124 */ 125 private void log(String message) { 126 127 if (context != null) 128 context.log("ContextListener: " + message); 129 else 130 System.out.println("ContextListener: " + message); 131 132 } 133 134 135 /** 136 * Log a message and associated exception to the servlet context 137 * application log. 138 * 139 * @param message Message to be logged 140 * @param throwable Exception to be logged 141 */ 142 private void log(String message, Throwable throwable) { 143 144 if (context != null) 145 context.log("ContextListener: " + message, throwable); 146 else { 147 System.out.println("ContextListener: " + message); 148 throwable.printStackTrace(System.out); 149 } 150 151 } 152 153 154 }