|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ContextListener.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright 2004 The Apache Software Foundation | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | ||
| 17 | ||
| 18 | package listeners; | |
| 19 | ||
| 20 | ||
| 21 | import javax.servlet.ServletContext; | |
| 22 | import javax.servlet.ServletContextAttributeEvent; | |
| 23 | import javax.servlet.ServletContextAttributeListener; | |
| 24 | import javax.servlet.ServletContextEvent; | |
| 25 | import javax.servlet.ServletContextListener; | |
| 26 | ||
| 27 | ||
| 28 | /** | |
| 29 | * Example listener for context-related application events, which were | |
| 30 | * introduced in the 2.3 version of the Servlet API. This listener | |
| 31 | * merely documents the occurrence of such events in the application log | |
| 32 | * associated with our servlet context. | |
| 33 | * | |
| 34 | * @author Craig R. McClanahan | |
| 35 | * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $ | |
| 36 | */ | |
| 37 | ||
| 38 | public final class ContextListener | |
| 39 | implements ServletContextAttributeListener, ServletContextListener { | |
| 40 | ||
| 41 | ||
| 42 | // ----------------------------------------------------- Instance Variables | |
| 43 | ||
| 44 | ||
| 45 | /** | |
| 46 | * The servlet context with which we are associated. | |
| 47 | */ | |
| 48 | private ServletContext context = null; | |
| 49 | ||
| 50 | ||
| 51 | // --------------------------------------------------------- Public Methods | |
| 52 | ||
| 53 | ||
| 54 | /** | |
| 55 | * Record the fact that a servlet context attribute was added. | |
| 56 | * | |
| 57 | * @param event The servlet context attribute event | |
| 58 | */ | |
| 59 | 0 | public void attributeAdded(ServletContextAttributeEvent event) { |
| 60 | ||
| 61 | 0 | log("attributeAdded('" + event.getName() + "', '" + |
| 62 | event.getValue() + "')"); | |
| 63 | ||
| 64 | } | |
| 65 | ||
| 66 | ||
| 67 | /** | |
| 68 | * Record the fact that a servlet context attribute was removed. | |
| 69 | * | |
| 70 | * @param event The servlet context attribute event | |
| 71 | */ | |
| 72 | 0 | public void attributeRemoved(ServletContextAttributeEvent event) { |
| 73 | ||
| 74 | 0 | log("attributeRemoved('" + event.getName() + "', '" + |
| 75 | event.getValue() + "')"); | |
| 76 | ||
| 77 | } | |
| 78 | ||
| 79 | ||
| 80 | /** | |
| 81 | * Record the fact that a servlet context attribute was replaced. | |
| 82 | * | |
| 83 | * @param event The servlet context attribute event | |
| 84 | */ | |
| 85 | 0 | public void attributeReplaced(ServletContextAttributeEvent event) { |
| 86 | ||
| 87 | 0 | log("attributeReplaced('" + event.getName() + "', '" + |
| 88 | event.getValue() + "')"); | |
| 89 | ||
| 90 | } | |
| 91 | ||
| 92 | ||
| 93 | /** | |
| 94 | * Record the fact that this web application has been destroyed. | |
| 95 | * | |
| 96 | * @param event The servlet context event | |
| 97 | */ | |
| 98 | 0 | public void contextDestroyed(ServletContextEvent event) { |
| 99 | ||
| 100 | 0 | log("contextDestroyed()"); |
| 101 | 0 | 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 | 0 | public void contextInitialized(ServletContextEvent event) { |
| 112 | ||
| 113 | 0 | this.context = event.getServletContext(); |
| 114 | 0 | 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 | 0 | private void log(String message) { |
| 128 | ||
| 129 | 0 | if (context != null) |
| 130 | 0 | context.log("ContextListener: " + message); |
| 131 | else | |
| 132 | 0 | 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 | 0 | private void log(String message, Throwable throwable) { |
| 145 | ||
| 146 | 0 | if (context != null) |
| 147 | 0 | context.log("ContextListener: " + message, throwable); |
| 148 | else { | |
| 149 | 0 | System.out.println("ContextListener: " + message); |
| 150 | 0 | throwable.printStackTrace(System.out); |
| 151 | } | |
| 152 | ||
| 153 | } | |
| 154 | ||
| 155 | ||
| 156 | } |
|
||||||||||