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  
26  import javax.servlet.http.HttpSessionActivationListener;
27  import javax.servlet.http.HttpSessionEvent;
28  import javax.servlet.http.HttpSessionListener;
29  
30  import org.apache.turbine.services.TurbineServices;
31  
32  /**
33   * This class is a listener for both session creation and destruction,
34   * and for session activation and passivation.  It must be configured
35   * via your web application's <code>web.xml</code> deployment
36   * descriptor as follows for the container to call it:
37   *
38   * <pre>
39   * &lt;listener&gt;
40   *   &lt;listener-class&gt;
41   *     org.apache.turbine.session.SessionListener
42   *   &lt;/listener-class&gt;
43   * &lt;/listener&gt;
44   * </pre>
45   *
46   * <code>&lt;listener&gt;</code> elements can occur between
47   * <code>&lt;context-param&gt;</code> and <code>&lt;servlet&gt;</code>
48   * elements in your deployment descriptor.
49   *
50   * The {@link #sessionCreated(HttpSessionEvent)} callback will
51   * automatically add an instance of this listener to any newly created
52   * <code>HttpSession</code> for detection of session passivation and
53   * re-activation.
54   *
55   * @since 2.3
56   * @version $Id: SessionListener.java 1773378 2016-12-09 13:19:59Z tv $
57   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
58   * @author <a href="mailto:dlr@apache.org">Daniel Rall</a>
59   * @see javax.servlet.http.HttpSessionListener
60   */
61  public class SessionListener
62          implements HttpSessionListener, HttpSessionActivationListener, Serializable
63  {
64      // ---- HttpSessionListener implementation -----------------------------
65  
66      /**
67       * Serial version.
68       */
69      private static final long serialVersionUID = -8083730704842809870L;
70  
71      /**
72       * The session service.
73       */
74      private SessionService sessionService;
75  
76      /**
77       * Lazy initialization
78       *
79       * @return the sessionService
80       */
81      private SessionService getSessionService()
82      {
83          // don't care about synchronization, lookup is cheap
84          if (sessionService == null)
85          {
86              sessionService = (SessionService)TurbineServices.getInstance().getService(SessionService.SERVICE_NAME);
87          }
88  
89          return sessionService;
90      }
91  
92      /**
93       * Called by the servlet container when a new session is created
94       *
95       * @param event Session creation event.
96       */
97      @Override
98      public void sessionCreated(HttpSessionEvent event)
99      {
100         getSessionService().addSession(event.getSession());
101         event.getSession().setAttribute(getClass().getName(), this);
102     }
103 
104     /**
105      * Called by the servlet container when a session is destroyed
106      *
107      * @param event Session destruction event.
108      */
109     @Override
110     public void sessionDestroyed(HttpSessionEvent event)
111     {
112         getSessionService().removeSession(event.getSession());
113     }
114 
115 
116     // ---- HttpSessionActivationListener implementation -------------------
117 
118     /**
119      * Called by the servlet container when an existing session is
120      * (re-)activated.
121      *
122      * @param event Session activation event.
123      */
124     @Override
125     public void sessionDidActivate(HttpSessionEvent event)
126     {
127         getSessionService().addSession(event.getSession());
128     }
129 
130     /**
131      * Called by the servlet container when a an existing session is
132      * passivated.
133      *
134      * @param event Session passivation event.
135      */
136     @Override
137     public void sessionWillPassivate(HttpSessionEvent event)
138     {
139         getSessionService().removeSession(event.getSession());
140     }
141 }