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