Source code: com/opencms/core/CmsCoreSession.java
1 /*
2 * File : $Source: /usr/local/cvs/opencms/src/com/opencms/core/CmsCoreSession.java,v $
3 * Date : $Date: 2003/01/20 17:57:49 $
4 * Version: $Revision: 1.10 $
5 *
6 * This library is part of OpenCms -
7 * the Open Source Content Mananagement System
8 *
9 * Copyright (C) 2001 The OpenCms Group
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * For further information about OpenCms, please see the
22 * OpenCms Website: http://www.opencms.org
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 package com.opencms.core;
30
31 import java.util.Enumeration;
32 import java.util.Hashtable;
33 import java.util.Vector;
34
35 /**
36 * This class implements a session storage which is mainly used to count the
37 * currently logged in OpenCms users.<p>
38 *
39 * It is required for user authentification of OpenCms.
40 * For each active user, its name and other additional information
41 * (like the current user group) are stored in a hashtable, useing the session
42 * Id as key to them.<p>
43 *
44 * When the session gets destroyed, the user will removed from the storage.<p>
45 *
46 * One of the main purposes of this stored user session list is the
47 * <code>sendBroadcastMessage()</code> method.
48 *
49 * @author Michael Emmerich
50 *
51 * @version $Revision: 1.10 $ $Date: 2003/01/20 17:57:49 $
52 *
53 * @see #sendBroadcastMessage(String message)
54 */
55 public class CmsCoreSession implements I_CmsConstants {
56
57 /**
58 * Hashtable storage to store all active users.
59 */
60 private Hashtable m_sessions;
61
62 /**
63 * Constructor, creates a new CmsCoreSession object.
64 */
65 public CmsCoreSession() {
66 m_sessions = new Hashtable();
67 }
68
69 /**
70 * Removes a user from the session storage.
71 * This is done when the session of the User is destroyed.
72 *
73 * @param sessionID The actual session Id.
74 */
75 public void deleteUser(String sessionId) {
76 m_sessions.remove(sessionId);
77 }
78
79 /**
80 * Gets the current usergroup of a user from the session storage.
81 *
82 * @param sessionID The actual session Id.
83 * @return The name of the current group of the user or the default guest group;
84 */
85 public String getCurrentGroup(String sessionId) {
86 Hashtable userinfo = null;
87 String currentGroup = C_GROUP_GUEST;
88 userinfo = getUser(sessionId);
89
90 // this user does exist, so get his current Group.
91 if(userinfo != null) {
92 currentGroup = (String)userinfo.get(C_SESSION_CURRENTGROUP);
93 if(currentGroup == null) {
94 currentGroup = C_GROUP_GUEST;
95 }
96 }
97 return currentGroup;
98 }
99
100 /**
101 * Gets the current project of a user from the session storage.
102 *
103 * @param sessionID The actual session Id.
104 * @return The name of the project of the user or the default project;
105 */
106 public Integer getCurrentProject(String sessionId) {
107 Hashtable userinfo = null;
108 Integer currentProject = new Integer(C_PROJECT_ONLINE_ID);
109 userinfo = getUser(sessionId);
110
111 // this user does exist, so get his current Project.
112 if(userinfo != null) {
113 currentProject = (Integer)userinfo.get(C_SESSION_PROJECT);
114 if(currentProject == null) {
115 currentProject = new Integer(C_PROJECT_ONLINE_ID);
116 }
117 }
118 return currentProject;
119 }
120
121 /**
122 * Gets the complete user information of a user from the session storage.
123 *
124 * @param sessionId A currently valid session id.
125 * @return table with user information or null
126 */
127 public Hashtable getUser(String sessionId) {
128 Hashtable userinfo = null;
129 userinfo = (Hashtable)m_sessions.get(sessionId);
130 return userinfo;
131 }
132
133 /**
134 * Gets the username of a user from the session storage.
135 *
136 * @param sessionId A currently valid session id.
137 * @return The name of the requested user or null.
138 */
139 public String getUserName(String sessionId) {
140 Hashtable userinfo = null;
141 String username = null;
142 userinfo = getUser(sessionId);
143
144 // this user does exist, so get his name.
145 if(userinfo != null) {
146 username = (String)userinfo.get(C_SESSION_USERNAME);
147 }
148 return username;
149 }
150
151 /**
152 * Puts a new user into the sesstion storage.<p>
153 *
154 * A user is stored with its current
155 * session id after a positive authentification.
156 *
157 * @param sessionId A currently valid session id.
158 * @param username The name of the user to be stored.
159 */
160 public void putUser(String sessionId, String username) {
161 Hashtable userinfo = new Hashtable();
162 userinfo.put(C_SESSION_USERNAME, username);
163 putUser(sessionId, userinfo);
164 }
165
166 /**
167 * Puts a new user into the sesstion storage.<p>
168 *
169 * A user is stored with its current
170 * session id after a positive authentification.
171 *
172 * @param sessionId A currently valid session id.
173 * @param username The name of the user to be stored.
174 * @param group The name of the users current group.
175 * @param project The id of the users current project.
176 */
177 public void putUser(String sessionId, String username, String group, Integer project) {
178 Hashtable userinfo = new Hashtable();
179 userinfo.put(C_SESSION_USERNAME, username);
180 userinfo.put(C_SESSION_CURRENTGROUP, group);
181 userinfo.put(C_SESSION_PROJECT, project);
182 putUser(sessionId, userinfo);
183 }
184
185 /**
186 * Puts a new user into the sesstion storage,
187 * this method also stores a complete hashtable with additional
188 * user information.<p>
189 *
190 * A user is stored with its current
191 * session id after a positive authentification.
192 *
193 * @param session A currently valid session id.
194 * @param userinfo A Hashtable containing information (including the name) about the user.
195 */
196 public void putUser(String sessionId, Hashtable userinfo) {
197 m_sessions.put(sessionId, userinfo);
198 }
199
200 /**
201 * Returns the number of current sessions in the system.
202 *
203 * @return the number of current sessions in the system
204 */
205 public int size() {
206 return m_sessions.size();
207 }
208
209 /**
210 * Returns a string-representation for this object which
211 * can be used for debugging.
212 *
213 * @return string-representation for this object.
214 */
215 public String toString() {
216 StringBuffer output = new StringBuffer();
217 String key;
218 Hashtable value;
219 String name;
220 Enumeration enu = m_sessions.keys();
221 output.append("[CmsCoreSessions]:\n");
222 while(enu.hasMoreElements()) {
223 key = (String)enu.nextElement();
224 output.append(key + " : ");
225 value = (Hashtable)m_sessions.get(key);
226 name = (String)value.get(C_SESSION_USERNAME);
227 output.append(name + "\n");
228 }
229 return output.toString();
230 }
231
232 /**
233 * Returns a Vector with all currently logged in users.<p>
234 *
235 * The Vector elements are <code>Hashtables</code> with the users name,
236 * the current project, the current group a Boolean if current messages
237 * are pending.
238 *
239 * @return a Vector with all currently logged in users
240 */
241 public Vector getLoggedInUsers() {
242 Vector output = new Vector();
243 String key;
244 Hashtable value;
245
246 // Hastable to return in the vector for one user-entry
247 Hashtable userentry;
248
249 Enumeration enu = m_sessions.keys();
250 while(enu.hasMoreElements()) {
251 userentry = new Hashtable(4);
252 key = (String)enu.nextElement();
253 value = (Hashtable)m_sessions.get(key);
254 userentry.put(C_SESSION_USERNAME, value.get(C_SESSION_USERNAME));
255 userentry.put(C_SESSION_PROJECT, value.get(C_SESSION_PROJECT));
256 userentry.put(C_SESSION_CURRENTGROUP, value.get(C_SESSION_CURRENTGROUP));
257 userentry.put(C_SESSION_MESSAGEPENDING, new Boolean( ((Hashtable)value.get(C_SESSION_DATA)).containsKey(C_SESSION_BROADCASTMESSAGE) ));
258
259 output.addElement(userentry);
260 }
261 return output;
262 }
263
264 /**
265 * Sends a broadcast message to all logged in users.
266 *
267 * @param message the message to send to all users.
268 */
269 public void sendBroadcastMessage(String message) {
270 String key;
271 Hashtable value;
272
273 Hashtable session_data;
274 String session_message;
275
276 Enumeration enu = m_sessions.keys();
277 while(enu.hasMoreElements()) {
278 key = (String)enu.nextElement();
279 value = (Hashtable)m_sessions.get(key);
280 session_data = (Hashtable)value.get(C_SESSION_DATA);
281 session_message = (String)session_data.get(C_SESSION_BROADCASTMESSAGE);
282 if(session_message == null) {
283 session_message = "";
284 }
285 session_message += message;
286 session_data.put(C_SESSION_BROADCASTMESSAGE, session_message);
287 }
288 }
289 }