KickJava   Java API By Example, From Geeks To Geeks.

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


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: SessionUtil.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
29 import com.lutris.appserver.server.session.Session;
30 import com.lutris.appserver.server.session.SessionData;
31 import com.lutris.appserver.server.session.SessionException;
32 import com.lutris.appserver.server.session.SessionManager;
33 import com.lutris.appserver.server.user.User;
34
35 /**
36  * Static methods that can be used by applications to manage sessions.
37  *
38  * @version $Revision: 1.2 $
39  * @author Kyle Clark
40  */

41 public class SessionUtil {
42
43     /**
44      * Prevent instantiation.
45      */

46     private SessionUtil() {
47     }
48
49     /**
50      * Logs a user in.
51      *
52      * @param user the user to log in.
53      * @param session the session to associated with the user.
54      * @param multiple if true the user can have more than one session
55      * associated with him/her. If false, all previous sessions
56      * associated with the user will be removed.
57      * @exception SessionException if an error occurs.
58      */

59     public static void logIn(User user, Session session, boolean multiple)
60         throws SessionException {
61         SessionManager mgr = session.getSessionManager();
62         synchronized(mgr) {
63             if (!multiple) {
64                 Enumeration JavaDoc e = mgr.getSessionKeys(user);
65                 while (e.hasMoreElements()) {
66                     String JavaDoc key = (String JavaDoc)e.nextElement();
67                     mgr.deleteSession(key);
68                 }
69             }
70             session.setUser(user);
71         }
72     }
73
74     /**
75      * Dissasociates the user from the session and all
76      * references to the session are deleted from the
77      * session manager.
78      *
79      * @param session the session associated with the user.
80      * @exception SessionException if an error occurs.
81      */

82     public static void logOut(Session session)
83         throws SessionException {
84     if (session != null) {
85             SessionManager mgr = session.getSessionManager();
86             mgr.deleteSession(session);
87         }
88     }
89
90     /**
91      * If a user attempts to login but has not logged
92      * out from a previous session then this method can be used
93      * to copy data from the previous session into this session
94      * and to log out the previous session.
95      *
96      * @param user the user
97      * @param session the session
98      * @return true if the user was already logged in and the session
99      * has been resumed.
100      * @exception SessionException
101      * if an error occurs.
102      */

103     public static boolean resumeLogIn(User user, Session session)
104         throws SessionException {
105         SessionManager mgr = session.getSessionManager();
106         try {
107             synchronized (mgr) {
108                 Enumeration JavaDoc e = mgr.getSessionKeys(user);
109                 if (e.hasMoreElements()) {
110                     String JavaDoc key = (String JavaDoc)e.nextElement();
111                     if (e.hasMoreElements()) {
112                         // More than one login - we don't deal with it.
113
return false;
114                     }
115                     Session oldSession = mgr.getSession(Thread.currentThread(), key);
116                     SessionData dataSrc = oldSession.getSessionData();
117                     SessionData dataDst = session.getSessionData();
118                     // Copy src to dst
119
String JavaDoc[] k = dataSrc.keys();
120                     for (int i=0; i<k.length; i++) {
121                         dataDst.set(k[i], dataSrc.get(k[i]));
122                     }
123                     SessionUtil.logIn(user, session, false);
124                     return true;
125                 }
126             }
127         } catch (SessionException e) {
128             throw e;
129         } catch (Exception JavaDoc e) {
130             throw new SessionException(e);
131         }
132         return false;
133     }
134 }
135
Popular Tags