KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionEnhydra > BasicSessionUserTable


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: BasicSessionUserTable.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25 package com.lutris.appserver.server.sessionEnhydra;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import com.lutris.appserver.server.Enhydra;
31 import com.lutris.appserver.server.user.User;
32 import com.lutris.logging.Logger;
33 import com.lutris.util.Config;
34
35 /**
36  * Table used by StandardSessionManager to cross reference <CODE>User</CODE>
37  * objects and sessions. This table does not hold references to the
38  * user and session objects. Instead it only cross-references the
39  * session keys and user names. An underlying assumption is that the
40  * user name can be used as a unique reference to the user.
41  *
42  * N.B. It is assumed that this class is only used by StandardSessionManager
43  * and that it is responsible for providing high level locks instead of
44  * synchronizing
45  *
46  * @version $Revision: 1.2 $
47  * @author Mark Diekhans
48  * @author Kyle Clark
49  */

50 class BasicSessionUserTable implements StandardSessionUserTable {
51
52     /**
53      * Table indexed by a <CODE>User</CODE> object. Each entry contains
54      * a Hashtable of sessions keys associated with the user name.
55      * The hash table is indexed by the <CODE>Session</CODE> object.
56      */

57     private Hashtable JavaDoc userSessionTable = new Hashtable JavaDoc();
58
59     /**
60      * A empty hash table used to return empty enumerations.
61      */

62     static final private Hashtable JavaDoc emptyTable = new Hashtable JavaDoc();
63
64     /**
65      * Creates an instance of this table. The config options
66      * are ignored.
67      *
68      * @param config
69      * configuration options for this table.
70      */

71     public BasicSessionUserTable(Config config) {
72     }
73
74     /**
75      * Add a session key to the user name of the session xref table.
76      *
77      * @param session
78      * The session key.
79      * @param user
80      * the user to associated with the session key.
81      */

82     public void add(String JavaDoc sessionKey, User user) {
83     Hashtable JavaDoc sessions = (Hashtable JavaDoc)userSessionTable.get(user.getName());
84         if (sessions == null) {
85             sessions = new Hashtable JavaDoc();
86             userSessionTable.put(user.getName(), sessions);
87         }
88     debug(2, "add session: user=" + user.getName()
89               + ": sessionKey=" + sessionKey);
90         sessions.put(sessionKey, sessionKey); // Have to put something!
91
}
92
93     /**
94      * Remove a session from the user to session mapping table.
95      * If the session is not it the table, it is ignored.
96      *
97      * @param sessionKey The session key, with the user already
98      * filled in.
99      * @param user the user associated with the session.
100      */

101     public void remove(String JavaDoc sessionKey, User user) {
102     Hashtable JavaDoc sessions = (Hashtable JavaDoc)userSessionTable.get(user.getName());
103         if (sessions != null) {
104             sessions.remove(sessionKey);
105             if (sessions.size() == 0) {
106                 // Remove the user.
107
userSessionTable.remove(user.getName());
108             }
109         }
110     debug(2, "remove session: user=" + user.getName()
111               + ": sessionKey=" + sessionKey);
112     }
113
114     /**
115      * Removes all references to a session from the user session table.
116      *
117      * @param sessionKey The session key, with the user already
118      * filled in.
119      */

120     public void remove(String JavaDoc sessionKey) {
121         Enumeration JavaDoc users = userSessionTable.keys();
122         while (users.hasMoreElements()) {
123             String JavaDoc userName = (String JavaDoc)users.nextElement();
124             Hashtable JavaDoc sessions = (Hashtable JavaDoc)userSessionTable.get(userName);
125             if (sessions != null) {
126                 sessions.remove(sessionKey);
127                 if (sessions.size() == 0) {
128                     // Remove the user.
129
userSessionTable.remove(userName);
130                 }
131             }
132         }
133     debug(2, "remove sessionKey from user table:" + sessionKey);
134     }
135
136     /**
137      * Get the number of sessions for a user.
138      *
139      * @param user The user object to check for.
140      * @return The count of the number of sessions associated with this
141      * user.
142      */

143     public int numSessions(User user) {
144     Hashtable JavaDoc sessions = (Hashtable JavaDoc)userSessionTable.get(user.getName());
145         if (sessions == null) {
146             return 0;
147         } else {
148             return sessions.size();
149         }
150     }
151
152     /**
153      * Returns the session keys associated with a particular user.
154      *
155      * @param user The user object to check for.
156      * @return An enumeration of the session keys associated
157      * with the user.
158      */

159     public Enumeration JavaDoc getSessionKeys(User user) {
160     Hashtable JavaDoc sessions = (Hashtable JavaDoc)userSessionTable.get(user.getName());
161         if (sessions == null) {
162         debug(2, "getSessionKeys: " + user.getName()
163                   + ": totalKeys=0");
164             return emptyTable.elements();
165         } else {
166         debug(2, "getSessionKeys: " + user.getName()
167                   + ": totalKeys=" + sessions.size());
168             return sessions.elements();
169         }
170     }
171
172     /**
173      * Shutdown this session user table as required. The session
174      * user table should not be used after this method has been called
175      */

176     public void shutdown() {
177         // TODO
178
}
179
180     /**
181      * Prints debug information under Logger.DEBUG.
182      *
183      * @param msg the message to print.
184      */

185     private void debug(String JavaDoc msg) {
186     debug(0, msg);
187     }
188
189     /**
190      * Prints debug information under Logger.DEBUG.
191      *
192      * @param level the debug level.
193      * @param msg the message to print.
194      */

195     protected void debug(int level, String JavaDoc msg) {
196      int dbg = Logger.DEBUG;
197      switch (level) {
198      case 1:
199          dbg = Logger.DEBUG1;
200          break;
201      case 2:
202          dbg = Logger.DEBUG2;
203          break;
204      case 3:
205          dbg = Logger.DEBUG3;
206          break;
207      case 4:
208          dbg = Logger.DEBUG4;
209          break;
210      case 5:
211          dbg = Logger.DEBUG5;
212          break;
213      case 6:
214          dbg = Logger.DEBUG6;
215          break;
216      case 7:
217          dbg = Logger.DEBUG7;
218          break;
219      case 8:
220          dbg = Logger.DEBUG8;
221          break;
222      case 9:
223          dbg = Logger.DEBUG9;
224          break;
225      default:
226          dbg = Logger.DEBUG;
227          break;
228      }
229     Enhydra.getLogChannel().write(dbg, "PersistentSessionHome("
230                                       + Thread.currentThread().getName()
231                                       + "): " + msg);
232     }
233 }
234
Popular Tags