001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: BasicSessionUserTable.java,v 1.2 2006-06-15 13:44:07 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.sessionEnhydra;
025:
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028:
029: import com.lutris.appserver.server.Enhydra;
030: import com.lutris.appserver.server.user.User;
031: import com.lutris.logging.Logger;
032: import com.lutris.util.Config;
033:
034: /**
035: * Table used by StandardSessionManager to cross reference <CODE>User</CODE>
036: * objects and sessions. This table does not hold references to the
037: * user and session objects. Instead it only cross-references the
038: * session keys and user names. An underlying assumption is that the
039: * user name can be used as a unique reference to the user.
040: *
041: * N.B. It is assumed that this class is only used by StandardSessionManager
042: * and that it is responsible for providing high level locks instead of
043: * synchronizing
044: *
045: * @version $Revision: 1.2 $
046: * @author Mark Diekhans
047: * @author Kyle Clark
048: */
049: class BasicSessionUserTable implements StandardSessionUserTable {
050:
051: /**
052: * Table indexed by a <CODE>User</CODE> object. Each entry contains
053: * a Hashtable of sessions keys associated with the user name.
054: * The hash table is indexed by the <CODE>Session</CODE> object.
055: */
056: private Hashtable userSessionTable = new Hashtable();
057:
058: /**
059: * A empty hash table used to return empty enumerations.
060: */
061: static final private Hashtable emptyTable = new Hashtable();
062:
063: /**
064: * Creates an instance of this table. The config options
065: * are ignored.
066: *
067: * @param config
068: * configuration options for this table.
069: */
070: public BasicSessionUserTable(Config config) {
071: }
072:
073: /**
074: * Add a session key to the user name of the session xref table.
075: *
076: * @param session
077: * The session key.
078: * @param user
079: * the user to associated with the session key.
080: */
081: public void add(String sessionKey, User user) {
082: Hashtable sessions = (Hashtable) userSessionTable.get(user
083: .getName());
084: if (sessions == null) {
085: sessions = new Hashtable();
086: userSessionTable.put(user.getName(), sessions);
087: }
088: debug(2, "add session: user=" + user.getName()
089: + ": sessionKey=" + sessionKey);
090: sessions.put(sessionKey, sessionKey); // Have to put something!
091: }
092:
093: /**
094: * Remove a session from the user to session mapping table.
095: * If the session is not it the table, it is ignored.
096: *
097: * @param sessionKey The session key, with the user already
098: * filled in.
099: * @param user the user associated with the session.
100: */
101: public void remove(String sessionKey, User user) {
102: Hashtable sessions = (Hashtable) userSessionTable.get(user
103: .getName());
104: if (sessions != null) {
105: sessions.remove(sessionKey);
106: if (sessions.size() == 0) {
107: // Remove the user.
108: userSessionTable.remove(user.getName());
109: }
110: }
111: debug(2, "remove session: user=" + user.getName()
112: + ": sessionKey=" + sessionKey);
113: }
114:
115: /**
116: * Removes all references to a session from the user session table.
117: *
118: * @param sessionKey The session key, with the user already
119: * filled in.
120: */
121: public void remove(String sessionKey) {
122: Enumeration users = userSessionTable.keys();
123: while (users.hasMoreElements()) {
124: String userName = (String) users.nextElement();
125: Hashtable sessions = (Hashtable) userSessionTable
126: .get(userName);
127: if (sessions != null) {
128: sessions.remove(sessionKey);
129: if (sessions.size() == 0) {
130: // Remove the user.
131: userSessionTable.remove(userName);
132: }
133: }
134: }
135: debug(2, "remove sessionKey from user table:" + sessionKey);
136: }
137:
138: /**
139: * Get the number of sessions for a user.
140: *
141: * @param user The user object to check for.
142: * @return The count of the number of sessions associated with this
143: * user.
144: */
145: public int numSessions(User user) {
146: Hashtable sessions = (Hashtable) userSessionTable.get(user
147: .getName());
148: if (sessions == null) {
149: return 0;
150: } else {
151: return sessions.size();
152: }
153: }
154:
155: /**
156: * Returns the session keys associated with a particular user.
157: *
158: * @param user The user object to check for.
159: * @return An enumeration of the session keys associated
160: * with the user.
161: */
162: public Enumeration getSessionKeys(User user) {
163: Hashtable sessions = (Hashtable) userSessionTable.get(user
164: .getName());
165: if (sessions == null) {
166: debug(2, "getSessionKeys: " + user.getName()
167: + ": totalKeys=0");
168: return emptyTable.elements();
169: } else {
170: debug(2, "getSessionKeys: " + user.getName()
171: + ": totalKeys=" + sessions.size());
172: return sessions.elements();
173: }
174: }
175:
176: /**
177: * Shutdown this session user table as required. The session
178: * user table should not be used after this method has been called
179: */
180: public void shutdown() {
181: // TODO
182: }
183:
184: /**
185: * Prints debug information under Logger.DEBUG.
186: *
187: * @param msg the message to print.
188: */
189: private void debug(String msg) {
190: debug(0, msg);
191: }
192:
193: /**
194: * Prints debug information under Logger.DEBUG.
195: *
196: * @param level the debug level.
197: * @param msg the message to print.
198: */
199: protected void debug(int level, String msg) {
200: int dbg = Logger.DEBUG;
201: switch (level) {
202: case 1:
203: dbg = Logger.DEBUG1;
204: break;
205: case 2:
206: dbg = Logger.DEBUG2;
207: break;
208: case 3:
209: dbg = Logger.DEBUG3;
210: break;
211: case 4:
212: dbg = Logger.DEBUG4;
213: break;
214: case 5:
215: dbg = Logger.DEBUG5;
216: break;
217: case 6:
218: dbg = Logger.DEBUG6;
219: break;
220: case 7:
221: dbg = Logger.DEBUG7;
222: break;
223: case 8:
224: dbg = Logger.DEBUG8;
225: break;
226: case 9:
227: dbg = Logger.DEBUG9;
228: break;
229: default:
230: dbg = Logger.DEBUG;
231: break;
232: }
233: Enhydra.getLogChannel().write(
234: dbg,
235: "PersistentSessionHome("
236: + Thread.currentThread().getName() + "): "
237: + msg);
238: }
239: }
|