1 package org.apache.turbine.services.session;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * <listener>
41 * <listener-class>
42 * org.apache.turbine.session.SessionListener
43 * </listener-class>
44 * </listener>
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
69
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
127
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
151
152
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
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 }