001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: SessionManager.java$
021: * $FileID: 4505$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:20 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: */
028:
029: package org.sourcejammer.server.security;
030:
031: import java.util.Hashtable;
032: import java.util.Enumeration;
033: import java.util.Random;
034:
035: /**
036: * Title: $FileName: SessionManager.java$
037: * @version $VerNum: 3$
038: * @author $AuthorName: Rob MacGrogan$<br><br>
039: *
040: * $Description: $<br>
041: * $KeyWordsOff: $<br><br>
042: *
043: * Manages asynchronous user sessions by associating a session ID with a
044: * User object.
045: */
046: public class SessionManager {
047:
048: private static SessionManager moInstance = new SessionManager();
049: private Hashtable mhshSessions;
050: private Random idGenerator = new Random();
051:
052: private SessionManager() {
053: mhshSessions = new Hashtable();
054: }
055:
056: public static SessionManager getInstance() {
057: return moInstance;
058: }
059:
060: /**
061: * Returns session ID.
062: */
063: public synchronized long startSession(User oUser) {
064: Long lngSession = getUniqueSessionID();
065: Session session = new Session();
066: session.setSessionID(lngSession.longValue());
067: session.setUser(oUser);
068: mhshSessions.put(lngSession, session);
069: System.out.println(":: started session "
070: + lngSession.longValue() + " for "
071: + oUser.getUserName());
072: return lngSession.longValue();
073: }
074:
075: private synchronized Long getUniqueSessionID() {
076: Long lngSession = null;
077: boolean bGotID = false;
078: while (!bGotID) {
079: long lSession = idGenerator.nextLong();
080: lSession = Math.abs(lSession);
081: lngSession = new Long(lSession);
082: if (mhshSessions.get(lngSession) == null) {
083: bGotID = true;
084: } else {
085: bGotID = false;
086: }
087: }//end while looking for unique ID
088: return lngSession;
089: }
090:
091: public User getUserForSession(long lSessionID)
092: throws NoSuchSessionException {
093: Long lngSession = new Long(lSessionID);
094: Session session = (Session) mhshSessions.get(lngSession);
095: if (session == null) {
096: throw new NoSuchSessionException(
097: "There is no current session with id " + lSessionID
098: + ".");
099: }
100: User oUser = session.getUser();
101: session.update();
102: return oUser;
103: }
104:
105: public void endSession(long lSessionID)
106: throws NoSuchSessionException {
107: Long lngSession = new Long(lSessionID);
108: Session session = (Session) mhshSessions.get(lngSession);
109: if (session == null) {
110: throw new NoSuchSessionException(
111: "There is no current session with id " + lSessionID
112: + ".");
113: }
114: User oUser = session.getUser();
115: mhshSessions.remove(lngSession);
116: System.out.println(":: ended session " + lSessionID + " for "
117: + oUser.getUserName());
118: }
119:
120: public void removeOldSessions(long olderThanTime)
121: throws NoSuchSessionException {
122: //Maybe there's a more efficient way to do this, but I think it's OK for now.
123: Enumeration enm = mhshSessions.elements();
124: while (enm.hasMoreElements()) {
125: Session session = (Session) enm.nextElement();
126: if (session.getLastUpdateTime() < olderThanTime) {
127: endSession(session.getSessionID());
128: System.out.println("Killed expired session -- id:"
129: + session.getSessionID() + ", user:"
130: + session.getUser().getUserName());
131: }
132: }//end while
133: }
134:
135: }
|