1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 19 package listeners; 20 21 22 import javax.servlet.ServletContext; 23 import javax.servlet.ServletContextAttributeEvent; 24 import javax.servlet.ServletContextAttributeListener; 25 import javax.servlet.ServletContextEvent; 26 import javax.servlet.ServletContextListener; 27 28 29 /** 30 * Example listener for context-related application events, which were 31 * introduced in the 2.3 version of the Servlet API. This listener 32 * merely documents the occurrence of such events in the application log 33 * associated with our servlet context. 34 * 35 * @author Craig R. McClanahan 36 * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $ 37 */ 38 39 public final class ContextListener 40 implements ServletContextAttributeListener, ServletContextListener { 41 42 // ----------------------------------------------------- Instance Variables 43 44 45 /** 46 * The servlet context with which we are associated. 47 */ 48 private ServletContext context = null; 49 50 // --------------------------------------------------------- Public Methods 51 52 53 /** 54 * Record the fact that a servlet context attribute was added. 55 * 56 * @param event The servlet context attribute event 57 */ 58 public void attributeAdded(ServletContextAttributeEvent event) { 59 60 log("attributeAdded('" + event.getName() + "', '" + 61 event.getValue() + "')"); 62 63 } 64 65 66 /** 67 * Record the fact that a servlet context attribute was removed. 68 * 69 * @param event The servlet context attribute event 70 */ 71 public void attributeRemoved(ServletContextAttributeEvent event) { 72 73 log("attributeRemoved('" + event.getName() + "', '" + 74 event.getValue() + "')"); 75 76 } 77 78 79 /** 80 * Record the fact that a servlet context attribute was replaced. 81 * 82 * @param event The servlet context attribute event 83 */ 84 public void attributeReplaced(ServletContextAttributeEvent event) { 85 86 log("attributeReplaced('" + event.getName() + "', '" + 87 event.getValue() + "')"); 88 89 } 90 91 92 /** 93 * Record the fact that this web application has been destroyed. 94 * 95 * @param event The servlet context event 96 */ 97 public void contextDestroyed(ServletContextEvent event) { 98 99 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 }