KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > security > SessionMap


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Shirley Sasson.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.kernel.security;
48
49 import org.mr.kernel.security.authentication.MaximumNumberOfSessionsPerUserReached;
50
51 import java.util.*;
52
53 /**
54  * Holds a map of all sessions for authenticated clients.
55  * It holds a list of sessions for each authenticated client (since a client may
56  * open more than one sessions as the same time).
57  *
58  * Map key is a UserPrincipal object.
59  * Map value is a list of sessions.
60  *
61  * @version 1.0
62  * @since Mar 19, 2006
63  * @author Shirley Sasson
64  *
65  */

66 public class SessionMap {
67     private Map _userToSessions;
68
69     /**
70      * Constructs a new instance of SessionMap.
71      *
72      */

73     public SessionMap(){
74         _userToSessions = new HashMap();
75     }
76
77     /**
78      * Adds the given session to the list of sessions for a specific UserPrincipal.
79      *
80      * @param user the user for which to add the session.
81      * @param sessionID the session to add
82      * @throws MaximumNumberOfSessionsPerUserReached if the user reached the maximal number of sessions
83      */

84     public void addSessionToUser(UserPrincipal user, SessionID sessionID) throws MaximumNumberOfSessionsPerUserReached {
85         if (_userToSessions.containsKey(user))
86             ((SessionsList) (_userToSessions.get(user))).addSession(sessionID);
87         else {
88             SessionsList list = new SessionsList();
89             list.addSession(sessionID);
90             _userToSessions.put(user, list);
91         }
92     }
93
94     /**
95      * Removes a session from the map. The methos searches for the list that contains this session,
96      * and removes it from the list.
97      *
98      * @param sessionID the session to remove
99      */

100     public void removeSession(SessionID sessionID){
101         Set set = _userToSessions.keySet();
102         Iterator iter = set.iterator();
103         boolean found = false;
104         while (iter.hasNext() && !found){
105             UserPrincipal user = (UserPrincipal) iter.next();
106             SessionsList list = (SessionsList) _userToSessions.get(user);
107             if (list.contains(sessionID)){
108                 list.removeSession(sessionID);
109                 found = true;
110             }
111         }
112     }
113
114     /**
115      * Returns true if this SessionMap contains the specified session ID.
116      *
117      * @param sessionID the session whose presence in this SessionMap is to be tested.
118      * @return true if the specified session is present; false otherwise.
119      */

120     public boolean contains(SessionID sessionID){
121         Set set = _userToSessions.keySet();
122         Iterator iter = set.iterator();
123         while (iter.hasNext()){
124             UserPrincipal user = (UserPrincipal) iter.next();
125             SessionsList list = (SessionsList) _userToSessions.get(user);
126             if (list.contains(sessionID))
127                 return true;
128         }
129         return false;
130     }
131
132     /**
133      * Returns the UserPrincipal associated with the given session.
134      *
135      * @param sessionID the session associated with the user
136      * @return the user associated with this session
137      */

138     public UserPrincipal getPrincipal(SessionID sessionID){
139         Set set = _userToSessions.keySet();
140         Iterator iter = set.iterator();
141         while (iter.hasNext()){
142             UserPrincipal principal = (UserPrincipal) iter.next();
143             SessionsList list = (SessionsList) _userToSessions.get(principal);
144             if (list.contains(sessionID))
145                 return principal;
146         }
147         return null;
148     }
149
150     /**
151      * Retunrs the number of sessions already exist in the map for this user.
152      *
153      * @param user the user to get the number of sessions for
154      * @return the number of sessions already exist in the map for this user
155      */

156     public int getNumOfSessionsPerUser(UserPrincipal user){
157         if (!_userToSessions.containsKey(user))
158             return 0;
159         SessionsList list = (SessionsList) _userToSessions.get(user);
160         return list.size();
161     }
162 }
163
Popular Tags