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