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  
19  package listeners;
20  
21  
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletContextEvent;
24  import javax.servlet.ServletContextListener;
25  import javax.servlet.http.HttpSessionAttributeListener;
26  import javax.servlet.http.HttpSessionBindingEvent;
27  import javax.servlet.http.HttpSessionEvent;
28  import javax.servlet.http.HttpSessionListener;
29  
30  
31  /**
32   * Example listener for context-related application events, which were
33   * introduced in the 2.3 version of the Servlet API.  This listener
34   * merely documents the occurrence of such events in the application log
35   * associated with our servlet context.
36   *
37   * @author Craig R. McClanahan
38   * @version $Revision: 664175 $ $Date: 2008-06-06 18:43:44 -0400 (Fri, 06 Jun 2008) $
39   */
40  
41  public final class SessionListener
42          implements ServletContextListener,
43          HttpSessionAttributeListener, HttpSessionListener {
44  
45      // ----------------------------------------------------- Instance Variables
46  
47  
48      /**
49       * The servlet context with which we are associated.
50       */
51      private ServletContext context = null;
52  
53      // --------------------------------------------------------- Public Methods
54  
55  
56      /**
57       * Record the fact that a servlet context attribute was added.
58       *
59       * @param event The session attribute event
60       */
61      public void attributeAdded(HttpSessionBindingEvent event) {
62  
63          log("attributeAdded('" + event.getSession().getId() + "', '" +
64                  event.getName() + "', '" + event.getValue() + "')");
65  
66      }
67  
68  
69      /**
70       * Record the fact that a servlet context attribute was removed.
71       *
72       * @param event The session attribute event
73       */
74      public void attributeRemoved(HttpSessionBindingEvent event) {
75  
76          log("attributeRemoved('" + event.getSession().getId() + "', '" +
77                  event.getName() + "', '" + event.getValue() + "')");
78  
79      }
80  
81  
82      /**
83       * Record the fact that a servlet context attribute was replaced.
84       *
85       * @param event The session attribute event
86       */
87      public void attributeReplaced(HttpSessionBindingEvent event) {
88  
89          log("attributeReplaced('" + event.getSession().getId() + "', '" +
90                  event.getName() + "', '" + event.getValue() + "')");
91  
92      }
93  
94  
95      /**
96       * Record the fact that this web application has been destroyed.
97       *
98       * @param event The servlet context event
99       */
100     public void contextDestroyed(ServletContextEvent event) {
101 
102         log("contextDestroyed()");
103         this.context = null;
104 
105     }
106 
107 
108     /**
109      * Record the fact that this web application has been initialized.
110      *
111      * @param event The servlet context event
112      */
113     public void contextInitialized(ServletContextEvent event) {
114 
115         this.context = event.getServletContext();
116         log("contextInitialized()");
117 
118     }
119 
120 
121     /**
122      * Record the fact that a session has been created.
123      *
124      * @param event The session event
125      */
126     public void sessionCreated(HttpSessionEvent event) {
127 
128         log("sessionCreated('" + event.getSession().getId() + "')");
129 
130     }
131 
132 
133     /**
134      * Record the fact that a session has been destroyed.
135      *
136      * @param event The session event
137      */
138     public void sessionDestroyed(HttpSessionEvent event) {
139 
140         log("sessionDestroyed('" + event.getSession().getId() + "')");
141 
142     }
143 
144     // -------------------------------------------------------- Private Methods
145 
146 
147     /**
148      * Log a message to the servlet context application log.
149      *
150      * @param message Message to be logged
151      */
152     private void log(String message) {
153 
154         if (context != null)
155             context.log("SessionListener: " + message);
156         else
157             System.out.println("SessionListener: " + message);
158 
159     }
160 
161 
162     /**
163      * Log a message and associated exception to the servlet context
164      * application log.
165      *
166      * @param message   Message to be logged
167      * @param throwable Exception to be logged
168      */
169     private void log(String message, Throwable throwable) {
170 
171         if (context != null)
172             context.log("SessionListener: " + message, throwable);
173         else {
174             System.out.println("SessionListener: " + message);
175             throwable.printStackTrace(System.out);
176         }
177 
178     }
179 
180 
181 }