KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > session > TurbineSessionService


1 package org.apache.turbine.services.session;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
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,v 1.8.2.2 2004/05/20 03:06:50 seade Exp $
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 JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc getActiveUsers()
119     {
120         Collection JavaDoc users;
121         synchronized (activeSessions)
122         {
123             // Pre-allocate a list which won't need expansion more
124
// than once.
125
users = new ArrayList JavaDoc((int) (activeSessions.size() * 0.7));
126             for (Iterator JavaDoc i = activeSessions.values().iterator(); i.hasNext();)
127             {
128                 User u = getUserFromSession((HttpSession JavaDoc) 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 JavaDoc 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 JavaDoc getSession(String JavaDoc sessionId)
157     {
158         return (HttpSession JavaDoc) 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 JavaDoc getSessionsForUser(User user)
169     {
170         Collection JavaDoc sessions = new ArrayList JavaDoc();
171         synchronized (activeSessions)
172         {
173             for (Iterator JavaDoc i = activeSessions.values().iterator(); i.hasNext();)
174             {
175                 HttpSession JavaDoc session = (HttpSession JavaDoc) 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 JavaDoc();
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 }
211
Popular Tags