View Javadoc

1   package org.apache.turbine.services.session;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Hashtable;
22  import java.util.Iterator;
23  import java.util.Map;
24  import javax.servlet.http.HttpSession;
25  
26  import org.apache.turbine.om.security.User;
27  import org.apache.turbine.services.TurbineBaseService;
28  
29  /***
30   * The SessionService allows thread-safe access to the current
31   * sessions of the current context.  The session objects that are
32   * cached by this service are obtained through a listener, which must
33   * be configured via your web application's <code>web.xml</code>
34   * deployement descriptor as follows:
35   *
36   * <blockquote><code><pre>
37   * &lt;listener&gt;
38   *   &lt;listener-class&gt;
39   *     org.apache.turbine.session.SessionListener
40   *   &lt;/listener-class&gt;
41   * &lt;/listener&gt;
42   * </pre></code></blockquote>
43   *
44   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
45   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
46   * @since 2.3
47   * @version $Id: TurbineSessionService.java 264148 2005-08-29 14:21:04Z henning $
48   * @see org.apache.turbine.services.session.TurbineSession
49   * @see org.apache.turbine.services.session.SessionListener
50   */
51  public class TurbineSessionService
52          extends TurbineBaseService
53          implements SessionService
54  {
55      /*** Map of active sessions */
56      private Map activeSessions;
57  
58      /***
59       * Gets a list of the active sessions.
60       *
61       * @return A copy of the list of <code>HttpSession</code> objects.
62       */
63      public Collection getActiveSessions()
64      {
65          // Sync externally to allow ArrayList's ctor to iterate
66          // activeSessions' values in a thread-safe fashion.
67          synchronized (activeSessions)
68          {
69              return new ArrayList(activeSessions.values());
70          }
71      }
72  
73      /***
74       * Adds a session to the current list.  This method should only be
75       * called by the listener.
76       *
77       * @param session Session to add
78       */
79      public void addSession(HttpSession session)
80      {
81          activeSessions.put(session.getId(), session);
82      }
83  
84      /***
85       * Removes a session from the current list.  This method should only be
86       * called by the listener.
87       *
88       * @param session Session to remove
89       */
90      public void removeSession(HttpSession session)
91      {
92          activeSessions.remove(session.getId());
93      }
94  
95      /***
96       * Determines if a given user is currently logged in.  The actual
97       * implementation of the User object must implement the equals()
98       * method.  By default, Torque based objects (liek TurbineUser)
99       * have an implementation of equals() that will compare the
100      * result of getPrimaryKey().
101      *
102      * @param user User to check for
103      * @return true if the user is logged in on one of the
104      * active sessions.
105      */
106     public boolean isUserLoggedIn(User user)
107     {
108         return getActiveUsers().contains(user);
109     }
110 
111     /***
112      * Gets a collection of all user objects representing the users currently
113      * logged in.  This will exclude any instances of anonymous user that
114      * Turbine will use before the user actually logs on.
115      *
116      * @return A set of {@link org.apache.turbine.om.security.User} objects.
117      */
118     public Collection getActiveUsers()
119     {
120         Collection users;
121         synchronized (activeSessions)
122         {
123             // Pre-allocate a list which won't need expansion more
124             // than once.
125             users = new ArrayList((int) (activeSessions.size() * 0.7));
126             for (Iterator i = activeSessions.values().iterator(); i.hasNext();)
127             {
128                 User u = getUserFromSession((HttpSession) i.next());
129                 if (u != null && u.hasLoggedIn())
130                 {
131                     users.add(u);
132                 }
133             }
134         }
135 
136         return users;
137     }
138 
139     /***
140      * Gets the User object of the the specified HttpSession.
141      *
142      * @param session The session from which to extract a user.
143      * @return The Turbine User object.
144      */
145     public User getUserFromSession(HttpSession session)
146     {
147         return (User) session.getAttribute(User.SESSION_KEY);
148     }
149 
150     /***
151      * Gets the HttpSession by the session identifier
152      *
153      * @param sessionId The unique session identifier.
154      * @return The session keyed by the specified identifier.
155      */
156     public HttpSession getSession(String sessionId)
157     {
158         return (HttpSession) this.activeSessions.get(sessionId);
159     }
160 
161     /***
162      * Get a collection of all session on which the given user
163      * is logged in.
164      *
165      * @param user the user
166      * @return Collection of HtttSession objects
167      */
168     public Collection getSessionsForUser(User user)
169     {
170         Collection sessions = new ArrayList();
171         synchronized (activeSessions)
172         {
173             for (Iterator i = activeSessions.values().iterator(); i.hasNext();)
174             {
175                 HttpSession session = (HttpSession) i.next();
176                 User u = this.getUserFromSession(session);
177                 if (user.equals(u))
178                 {
179                     sessions.add(session);
180                 }
181             }
182         }
183 
184         return sessions;
185     }
186 
187 
188     // ---- Service initilization ------------------------------------------
189 
190     /***
191      * Initializes the service
192      */
193     public void init()
194     {
195         this.activeSessions = new Hashtable();
196 
197         setInit(true);
198     }
199 
200     /***
201      * Returns to uninitialized state.
202      */
203     public void shutdown()
204     {
205         this.activeSessions = null;
206 
207         setInit(false);
208     }
209 
210 }