View Javadoc

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  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: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $
36   */
37  
38  public final class ContextListener
39          implements ServletContextAttributeListener, ServletContextListener {
40  
41      // ----------------------------------------------------- Instance Variables
42  
43  
44      /**
45       * The servlet context with which we are associated.
46       */
47      private ServletContext context = null;
48  
49      // --------------------------------------------------------- Public Methods
50  
51  
52      /**
53       * Record the fact that a servlet context attribute was added.
54       *
55       * @param event The servlet context attribute event
56       */
57      public void attributeAdded(ServletContextAttributeEvent event) {
58  
59          log("attributeAdded('" + event.getName() + "', '" +
60                  event.getValue() + "')");
61  
62      }
63  
64  
65      /**
66       * Record the fact that a servlet context attribute was removed.
67       *
68       * @param event The servlet context attribute event
69       */
70      public void attributeRemoved(ServletContextAttributeEvent event) {
71  
72          log("attributeRemoved('" + event.getName() + "', '" +
73                  event.getValue() + "')");
74  
75      }
76  
77  
78      /**
79       * Record the fact that a servlet context attribute was replaced.
80       *
81       * @param event The servlet context attribute event
82       */
83      public void attributeReplaced(ServletContextAttributeEvent event) {
84  
85          log("attributeReplaced('" + event.getName() + "', '" +
86                  event.getValue() + "')");
87  
88      }
89  
90  
91      /**
92       * Record the fact that this web application has been destroyed.
93       *
94       * @param event The servlet context event
95       */
96      public void contextDestroyed(ServletContextEvent event) {
97  
98          log("contextDestroyed()");
99          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 }