View Javadoc

1   package org.apache.turbine.services.session;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  import javax.servlet.http.HttpSessionActivationListener;
24  import javax.servlet.http.HttpSessionEvent;
25  import javax.servlet.http.HttpSessionListener;
26  
27  /***
28   * This class is a listener for both session creation and destruction,
29   * and for session activation and passivation.  It must be configured
30   * via your web application's <code>web.xml</code> deployment
31   * descriptor as follows for the container to call it:
32   *
33   * <blockquote><code><pre>
34   * &lt;listener&gt;
35   *   &lt;listener-class&gt;
36   *     org.apache.turbine.session.SessionListener
37   *   &lt;/listener-class&gt;
38   * &lt;/listener&gt;
39   * </pre></code></blockquote>
40   *
41   * <code>&lt;listener&gt;</code> elemements can occur between
42   * <code>&lt;context-param&gt;</code> and <code>&lt;servlet&gt;</code>
43   * elements in your deployment descriptor.
44   *
45   * The {@link #sessionCreated(HttpSessionEvent)} callback will
46   * automatically add an instance of this listener to any newly created
47   * <code>HttpSession</code> for detection of session passivation and
48   * re-activation.
49   *
50   * @since 2.3
51   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
52   * @author <a href="mailto:dlr@apache.org">Daniel Rall</a>
53   * @version $Id: SessionListener.java 534527 2007-05-02 16:10:59Z tv $
54   * @see javax.servlet.http.HttpSessionListener
55   */
56  public class SessionListener
57          implements HttpSessionListener, HttpSessionActivationListener, Serializable
58  {
59      /*** Serial Version UID */
60      private static final long serialVersionUID = -8083730704842809870L;
61  
62      // ---- HttpSessionListener implementation -----------------------------
63  
64      /***
65       * Called by the servlet container when a new session is created
66       *
67       * @param event Session creation event.
68       */
69      public void sessionCreated(HttpSessionEvent event)
70      {
71          TurbineSession.addSession(event.getSession());
72          event.getSession().setAttribute(getClass().getName(), this);
73      }
74  
75      /***
76       * Called by the servlet container when a session is destroyed
77       *
78       * @param event Session destruction event.
79       */
80      public void sessionDestroyed(HttpSessionEvent event)
81      {
82          TurbineSession.removeSession(event.getSession());
83      }
84  
85  
86      // ---- HttpSessionActivationListener implementation -------------------
87  
88      /***
89       * Called by the servlet container when an existing session is
90       * (re-)activated.
91       *
92       * @param event Session activation event.
93       */
94      public void sessionDidActivate(HttpSessionEvent event)
95      {
96          TurbineSession.addSession(event.getSession());
97      }
98  
99      /***
100      * Called by the servlet container when a an existing session is
101      * passivated.
102      *
103      * @param event Session passivation event.
104      */
105     public void sessionWillPassivate(HttpSessionEvent event)
106     {
107         TurbineSession.removeSession(event.getSession());
108     }
109 }