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: import javax.servlet.ServletContext;
020: import javax.servlet.ServletContextAttributeEvent;
021: import javax.servlet.ServletContextAttributeListener;
022: import javax.servlet.ServletContextEvent;
023: import javax.servlet.ServletContextListener;
024:
025: /**
026: * Example listener for context-related application events, which were
027: * introduced in the 2.3 version of the Servlet API. This listener
028: * merely documents the occurrence of such events in the application log
029: * associated with our servlet context.
030: *
031: * @author Craig R. McClanahan
032: * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $
033: */
034:
035: public final class ContextListener implements
036: ServletContextAttributeListener, ServletContextListener {
037:
038: // ----------------------------------------------------- Instance Variables
039:
040: /**
041: * The servlet context with which we are associated.
042: */
043: private ServletContext context = null;
044:
045: // --------------------------------------------------------- Public Methods
046:
047: /**
048: * Record the fact that a servlet context attribute was added.
049: *
050: * @param event The servlet context attribute event
051: */
052: public void attributeAdded(ServletContextAttributeEvent event) {
053:
054: log("attributeAdded('" + event.getName() + "', '"
055: + event.getValue() + "')");
056:
057: }
058:
059: /**
060: * Record the fact that a servlet context attribute was removed.
061: *
062: * @param event The servlet context attribute event
063: */
064: public void attributeRemoved(ServletContextAttributeEvent event) {
065:
066: log("attributeRemoved('" + event.getName() + "', '"
067: + event.getValue() + "')");
068:
069: }
070:
071: /**
072: * Record the fact that a servlet context attribute was replaced.
073: *
074: * @param event The servlet context attribute event
075: */
076: public void attributeReplaced(ServletContextAttributeEvent event) {
077:
078: log("attributeReplaced('" + event.getName() + "', '"
079: + event.getValue() + "')");
080:
081: }
082:
083: /**
084: * Record the fact that this web application has been destroyed.
085: *
086: * @param event The servlet context event
087: */
088: public void contextDestroyed(ServletContextEvent event) {
089:
090: log("contextDestroyed()");
091: this .context = null;
092:
093: }
094:
095: /**
096: * Record the fact that this web application has been initialized.
097: *
098: * @param event The servlet context event
099: */
100: public void contextInitialized(ServletContextEvent event) {
101:
102: this .context = event.getServletContext();
103: log("contextInitialized()");
104:
105: }
106:
107: // -------------------------------------------------------- Private Methods
108:
109: /**
110: * Log a message to the servlet context application log.
111: *
112: * @param message Message to be logged
113: */
114: private void log(String message) {
115:
116: if (context != null)
117: context.log("ContextListener: " + message);
118: else
119: System.out.println("ContextListener: " + message);
120:
121: }
122:
123: /**
124: * Log a message and associated exception to the servlet context
125: * application log.
126: *
127: * @param message Message to be logged
128: * @param throwable Exception to be logged
129: */
130: private void log(String message, Throwable throwable) {
131:
132: if (context != null)
133: context.log("ContextListener: " + message, throwable);
134: else {
135: System.out.println("ContextListener: " + message);
136: throwable.printStackTrace(System.out);
137: }
138:
139: }
140:
141: }
|