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.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Hashtable;
25  import java.util.Iterator;
26  import java.util.Map;
27  import javax.servlet.http.HttpSession;
28  
29  import org.apache.turbine.om.security.User;
30  import org.apache.turbine.services.TurbineBaseService;
31  
32  /***
33   * The SessionService allows thread-safe access to the current
34   * sessions of the current context.  The session objects that are
35   * cached by this service are obtained through a listener, which must
36   * be configured via your web application's <code>web.xml</code>
37   * deployement descriptor as follows:
38   *
39   * <blockquote><code><pre>
40   * &lt;listener&gt;
41   *   &lt;listener-class&gt;
42   *     org.apache.turbine.session.SessionListener
43   *   &lt;/listener-class&gt;
44   * &lt;/listener&gt;
45   * </pre></code></blockquote>
46   *
47   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
48   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
49   * @since 2.3
50   * @version $Id: TurbineSessionService.java 534527 2007-05-02 16:10:59Z tv $
51   * @see org.apache.turbine.services.session.TurbineSession
52   * @see org.apache.turbine.services.session.SessionListener
53   */
54  public class TurbineSessionService
55          extends TurbineBaseService
56          implements SessionService
57  {
58      /*** Map of active sessions */
59      private Map activeSessions;
60  
61      /***
62       * Gets a list of the active sessions.
63       *
64       * @return A copy of the list of <code>HttpSession</code> objects.
65       */
66      public Collection getActiveSessions()
67      {
68          // Sync externally to allow ArrayList's ctor to iterate
69          // activeSessions' values in a thread-safe fashion.
70          synchronized (activeSessions)
71          {
72              return new ArrayList(activeSessions.values());
73          }
74      }
75  
76      /***
77       * Adds a session to the current list.  This method should only be
78       * called by the listener.
79       *
80       * @param session Session to add
81       */
82      public void addSession(HttpSession session)
83      {
84          activeSessions.put(session.getId(), session);
85      }
86  
87      /***
88       * Removes a session from the current list.  This method should only be
89       * called by the listener.
90       *
91       * @param session Session to remove
92       */
93      public void removeSession(HttpSession session)
94      {
95          activeSessions.remove(session.getId());
96      }
97  
98      /***
99       * Determines if a given user is currently logged in.  The actual
100      * implementation of the User object must implement the equals()
101      * method.  By default, Torque based objects (liek TurbineUser)
102      * have an implementation of equals() that will compare the
103      * result of getPrimaryKey().
104      *
105      * @param user User to check for
106      * @return true if the user is logged in on one of the
107      * active sessions.
108      */
109     public boolean isUserLoggedIn(User user)
110     {
111         return getActiveUsers().contains(user);
112     }
113 
114     /***
115      * Gets a collection of all user objects representing the users currently
116      * logged in.  This will exclude any instances of anonymous user that
117      * Turbine will use before the user actually logs on.
118      *
119      * @return A set of {@link org.apache.turbine.om.security.User} objects.
120      */
121     public Collection getActiveUsers()
122     {
123         Collection users;
124         synchronized (activeSessions)
125         {
126             // Pre-allocate a list which won't need expansion more
127             // than once.
128             users = new ArrayList((int) (activeSessions.size() * 0.7));
129             for (Iterator i = activeSessions.values().iterator(); i.hasNext();)
130             {
131                 User u = getUserFromSession((HttpSession) i.next());
132                 if (u != null && u.hasLoggedIn())
133                 {
134                     users.add(u);
135                 }
136             }
137         }
138 
139         return users;
140     }
141 
142     /***
143      * Gets the User object of the the specified HttpSession.
144      *
145      * @param session The session from which to extract a user.
146      * @return The Turbine User object.
147      */
148     public User getUserFromSession(HttpSession session)
149     {
150         // Not sure of other containers, but Tomcat 5.0.28 sometimes returns
151         // invalid sessions which will result in IllegalStateException when
152         // session.getAttribute() is invoked below.
153         try
154         {
155             return (User) session.getAttribute(User.SESSION_KEY);
156         }
157         catch (IllegalStateException e)
158         {
159             return null;
160         }
161     }
162 
163     /***
164      * Gets the HttpSession by the session identifier
165      *
166      * @param sessionId The unique session identifier.
167      * @return The session keyed by the specified identifier.
168      */
169     public HttpSession getSession(String sessionId)
170     {
171         return (HttpSession) this.activeSessions.get(sessionId);
172     }
173 
174     /***
175      * Get a collection of all session on which the given user
176      * is logged in.
177      *
178      * @param user the user
179      * @return Collection of HtttSession objects
180      */
181     public Collection getSessionsForUser(User user)
182     {
183         Collection sessions = new ArrayList();
184         synchronized (activeSessions)
185         {
186             for (Iterator i = activeSessions.values().iterator(); i.hasNext();)
187             {
188                 HttpSession session = (HttpSession) i.next();
189                 User u = this.getUserFromSession(session);
190                 if (user.equals(u))
191                 {
192                     sessions.add(session);
193                 }
194             }
195         }
196 
197         return sessions;
198     }
199 
200 
201     // ---- Service initilization ------------------------------------------
202 
203     /***
204      * Initializes the service
205      */
206     public void init()
207     {
208         this.activeSessions = new Hashtable();
209 
210         setInit(true);
211     }
212 
213     /***
214      * Returns to uninitialized state.
215      */
216     public void shutdown()
217     {
218         this.activeSessions = null;
219 
220         setInit(false);
221     }
222 
223 }