    001package org.apache.turbine.services.session;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import java.io.Serializable;
    025import javax.servlet.http.HttpSessionActivationListener;
    026import javax.servlet.http.HttpSessionEvent;
    027import javax.servlet.http.HttpSessionListener;
    028
    029/**
    030 * This class is a listener for both session creation and destruction,
    031 * and for session activation and passivation.  It must be configured
    032 * via your web application's <code>web.xml</code> deployment
    033 * descriptor as follows for the container to call it:
    034 *
    035 * <blockquote><code><pre>
    036 * &lt;listener&gt;
    037 *   &lt;listener-class&gt;
    038 *     org.apache.turbine.session.SessionListener
    039 *   &lt;/listener-class&gt;
    040 * &lt;/listener&gt;
    041 * </pre></code></blockquote>
    042 *
    043 * <code>&lt;listener&gt;</code> elemements can occur between
    044 * <code>&lt;context-param&gt;</code> and <code>&lt;servlet&gt;</code>
    045 * elements in your deployment descriptor.
    046 *
    047 * The {@link #sessionCreated(HttpSessionEvent)} callback will
    048 * automatically add an instance of this listener to any newly created
    049 * <code>HttpSession</code> for detection of session passivation and
    050 * re-activation.
    051 *
    052 * @since 2.3
    053 * @version $Id: SessionListener.java 1066925 2011-02-03 19:44:37Z ludwig $
    054 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    055 * @author <a href="mailto:dlr@apache.org">Daniel Rall</a>
    056 * @see javax.servlet.http.HttpSessionListener
    057 */
    058public class SessionListener
    059        implements HttpSessionListener, HttpSessionActivationListener, Serializable
    060{
    061    // ---- HttpSessionListener implementation -----------------------------
    062
    063    /**
    064     * Serial version.
    065     */
    066    private static final long serialVersionUID = -8083730704842809870L;
    067
    068    /**
    069     * Called by the servlet container when a new session is created
    070     *
    071     * @param event Session creation event.
    072     */
    073    public void sessionCreated(HttpSessionEvent event)
    074    {
    075        TurbineSession.addSession(event.getSession());
    076        event.getSession().setAttribute(getClass().getName(), this);
    077    }
    078
    079    /**
    080     * Called by the servlet container when a session is destroyed
    081     *
    082     * @param event Session destruction event.
    083     */
    084    public void sessionDestroyed(HttpSessionEvent event)
    085    {
    086        TurbineSession.removeSession(event.getSession());
    087    }
    088
    089
    090    // ---- HttpSessionActivationListener implementation -------------------
    091
    092    /**
    093     * Called by the servlet container when an existing session is
    094     * (re-)activated.
    095     *
    096     * @param event Session activation event.
    097     */
    098    public void sessionDidActivate(HttpSessionEvent event)
    099    {
    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}