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: SessionUtil.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:
028: import com.lutris.appserver.server.session.Session;
029: import com.lutris.appserver.server.session.SessionData;
030: import com.lutris.appserver.server.session.SessionException;
031: import com.lutris.appserver.server.session.SessionManager;
032: import com.lutris.appserver.server.user.User;
033:
034: /**
035: * Static methods that can be used by applications to manage sessions.
036: *
037: * @version $Revision: 1.2 $
038: * @author Kyle Clark
039: */
040: public class SessionUtil {
041:
042: /**
043: * Prevent instantiation.
044: */
045: private SessionUtil() {
046: }
047:
048: /**
049: * Logs a user in.
050: *
051: * @param user the user to log in.
052: * @param session the session to associated with the user.
053: * @param multiple if true the user can have more than one session
054: * associated with him/her. If false, all previous sessions
055: * associated with the user will be removed.
056: * @exception SessionException if an error occurs.
057: */
058: public static void logIn(User user, Session session,
059: boolean multiple) throws SessionException {
060: SessionManager mgr = session.getSessionManager();
061: synchronized (mgr) {
062: if (!multiple) {
063: Enumeration e = mgr.getSessionKeys(user);
064: while (e.hasMoreElements()) {
065: String key = (String) e.nextElement();
066: mgr.deleteSession(key);
067: }
068: }
069: session.setUser(user);
070: }
071: }
072:
073: /**
074: * Dissasociates the user from the session and all
075: * references to the session are deleted from the
076: * session manager.
077: *
078: * @param session the session associated with the user.
079: * @exception SessionException if an error occurs.
080: */
081: public static void logOut(Session session) throws SessionException {
082: if (session != null) {
083: SessionManager mgr = session.getSessionManager();
084: mgr.deleteSession(session);
085: }
086: }
087:
088: /**
089: * If a user attempts to login but has not logged
090: * out from a previous session then this method can be used
091: * to copy data from the previous session into this session
092: * and to log out the previous session.
093: *
094: * @param user the user
095: * @param session the session
096: * @return true if the user was already logged in and the session
097: * has been resumed.
098: * @exception SessionException
099: * if an error occurs.
100: */
101: public static boolean resumeLogIn(User user, Session session)
102: throws SessionException {
103: SessionManager mgr = session.getSessionManager();
104: try {
105: synchronized (mgr) {
106: Enumeration e = mgr.getSessionKeys(user);
107: if (e.hasMoreElements()) {
108: String key = (String) e.nextElement();
109: if (e.hasMoreElements()) {
110: // More than one login - we don't deal with it.
111: return false;
112: }
113: Session oldSession = mgr.getSession(Thread
114: .currentThread(), key);
115: SessionData dataSrc = oldSession.getSessionData();
116: SessionData dataDst = session.getSessionData();
117: // Copy src to dst
118: String[] k = dataSrc.keys();
119: for (int i = 0; i < k.length; i++) {
120: dataDst.set(k[i], dataSrc.get(k[i]));
121: }
122: SessionUtil.logIn(user, session, false);
123: return true;
124: }
125: }
126: } catch (SessionException e) {
127: throw e;
128: } catch (Exception e) {
129: throw new SessionException(e);
130: }
131: return false;
132: }
133: }
|