View Javadoc

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