View Javadoc

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  package listeners;
18  
19  
20  import javax.servlet.ServletContext;
21  import javax.servlet.ServletContextAttributeEvent;
22  import javax.servlet.ServletContextAttributeListener;
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  
26  
27  /**
28   * Example listener for context-related application events, which were
29   * introduced in the 2.3 version of the Servlet API.  This listener
30   * merely documents the occurrence of such events in the application log
31   * associated with our servlet context.
32   *
33   * @author Craig R. McClanahan
34   * @version $Revision: 267129 $ $Date: 2004-03-18 08:40:35 -0800 (Thu, 18 Mar 2004) $
35   */
36  
37  public final class ContextListener
38      implements ServletContextAttributeListener, ServletContextListener {
39  
40  
41      // ----------------------------------------------------- Instance Variables
42  
43  
44      /**
45       * The servlet context with which we are associated.
46       */
47      private ServletContext context = null;
48  
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 
118     // -------------------------------------------------------- Private Methods
119 
120 
121     /**
122      * Log a message to the servlet context application log.
123      *
124      * @param message Message to be logged
125      */
126     private void log(String message) {
127 
128 	if (context != null)
129 	    context.log("ContextListener: " + message);
130 	else
131 	    System.out.println("ContextListener: " + message);
132 
133     }
134 
135 
136     /**
137      * Log a message and associated exception to the servlet context
138      * application log.
139      *
140      * @param message Message to be logged
141      * @param throwable Exception to be logged
142      */
143     private void log(String message, Throwable throwable) {
144 
145 	if (context != null)
146 	    context.log("ContextListener: " + message, throwable);
147 	else {
148 	    System.out.println("ContextListener: " + message);
149 	    throwable.printStackTrace(System.out);
150 	}
151 
152     }
153 
154 
155 }