KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policyframework > LaunchSessionManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.policyframework;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import com.sslexplorer.security.Constants;
32 import com.sslexplorer.security.SessionInfo;
33 import com.sslexplorer.security.SessionInfoListener;
34 import com.sslexplorer.security.User;
35 import com.sslexplorer.util.TicketGenerator;
36
37
38 /**
39  * Manager for creating and locating {@link LaunchSession} objects.
40  *
41  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
42  */

43 public class LaunchSessionManager {
44         
45
46     // Private instance variables
47

48     private Map JavaDoc<User, List JavaDoc<LaunchSession>> userSessionsList = new HashMap JavaDoc<User, List JavaDoc<LaunchSession>>();
49     private Map JavaDoc<String JavaDoc, LaunchSession> launchSessions = new HashMap JavaDoc<String JavaDoc, LaunchSession>();
50         
51     /**
52      * Get all of the launch sessions for a specified user.
53      *
54      * @param user user
55      * @return launch sessions
56      */

57     public Collection JavaDoc<LaunchSession> getLaunchSessions(User user) {
58         return userSessionsList.get(user);
59     }
60     
61     /**
62      * Get all launch sessions
63      *
64      * @return launch sessions
65      */

66     public Collection JavaDoc<LaunchSession> getLaunchSession() {
67         return launchSessions.values();
68     }
69     
70     /**
71      * Get <i>Launch Session</i> for a given launch ID.
72      * <p>
73      * Note, whereever possible {@link #getLaunchSession(SessionInfo, String)}
74      * should be used as it will help keep the resource sessions secure and
75      * only accessable by their users sessions.
76      *
77      * @param launchId resource sessions ID
78      * @return launch session
79      */

80     public LaunchSession getLaunchSession(String JavaDoc launchId) {
81         return (LaunchSession)launchSessions.get(launchId);
82     }
83
84     /**
85      * Get the <i>Resource Session</i> for a given resource ID from the
86      * session. <code>null</code> will be returned if no such session exists.
87      * <p>
88      * Use of this method is preferable to {@link #getLaunchSession(String)}.
89      *
90      * @param session session
91      * @param launchId launchId
92      * @return resource session
93      */

94     public LaunchSession getLaunchSession(SessionInfo session, String JavaDoc launchId) {
95         SessionsLaunchSessions launchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS);
96         return launchSessions == null ? null : launchSessions.get(launchId);
97     }
98     
99     /**
100      * Remove a launch session from the session it is attached to.
101      *
102      * @param launchSession
103      */

104     public void removeLaunchSession(LaunchSession launchSession) {
105         synchronized (launchSessions) {
106             launchSessions.remove(launchSession.getId());
107             SessionsLaunchSessions sessionLaunchSessions = (SessionsLaunchSessions)launchSession.getSession().getAttribute(Constants.LAUNCH_SESSIONS);
108             if(sessionLaunchSessions != null) {
109                 sessionLaunchSessions.remove(launchSession.getId());
110                 if(sessionLaunchSessions.size() == 0) {
111                     launchSession.getSession().removeAttribute(Constants.LAUNCH_SESSIONS);
112                 }
113             }
114             
115         }
116
117         synchronized(userSessionsList) {
118             List JavaDoc<LaunchSession> us = userSessionsList.get(launchSession.getSession().getUser());
119             if(us != null) {
120                 us.remove(launchSession);
121                 if(us.size() == 0) {
122                     userSessionsList.remove(launchSession.getSession().getUser());
123                 }
124             }
125         }
126     }
127
128     /**
129      * Create a new resource session
130      *
131      * @param session user session
132      * @param resource resource
133      * @param policy policy
134      * @return resource session
135      */

136     public LaunchSession createLaunchSession(SessionInfo session, Resource resource, Policy policy) {
137         
138         // Create the new resource session, making sure there are no ID conflicts
139
String JavaDoc launchId = null;
140         LaunchSession launchSession = null;
141         synchronized(launchSessions) {
142             
143             while(true) {
144                 launchId = TicketGenerator.getInstance().generateUniqueTicket("l", 7);
145                 if(!launchSessions.containsKey(launchId)) {
146                     launchSession = new LaunchSession(launchId, session, resource, policy);
147                     launchSessions.put(launchId, launchSession);
148                     break;
149                 }
150             }
151         }
152
153         // Get the map of resource sessions for this users session
154
SessionsLaunchSessions sessionLaunchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS);
155         if (sessionLaunchSessions == null) {
156             sessionLaunchSessions = new SessionsLaunchSessions(launchSession);
157             session.setAttribute(Constants.LAUNCH_SESSIONS, sessionLaunchSessions);
158         }
159         sessionLaunchSessions.put(launchId, launchSession);
160         
161
162         // Update the user / resource sessions map
163
synchronized(userSessionsList) {
164             List JavaDoc<LaunchSession> us = userSessionsList.get(session.getUser());
165             if(us == null) {
166                 us = new ArrayList JavaDoc<LaunchSession>();
167                 userSessionsList.put(session.getUser(), us);
168             }
169             us.add(launchSession);
170         }
171         
172         return launchSession;
173     }
174
175     /**
176      * Get a collection of <i>Resource Sessions</i> for a given resource type.
177      *
178      * @param session session
179      * @param resourceType resource type
180      * @return resource sessions
181      */

182     public Collection JavaDoc<LaunchSession> getLaunchSessionsForType(SessionInfo session, ResourceType resourceType) {
183         SessionsLaunchSessions launchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS);
184         List JavaDoc<LaunchSession> l = new ArrayList JavaDoc<LaunchSession>();
185         if (launchSessions != null) {
186             for (LaunchSession launchSession : launchSessions.values()) {
187                 if (launchSession.getResource() != null && launchSession.getResource().getResourceType() == resourceType) {
188                     l.add(launchSession);
189                 }
190             }
191         }
192         return l;
193     }
194
195
196     /**
197      * Get the first <i>Launch Session</i> for a given resource
198      *
199      * @param session session
200      * @param resource resource
201      * @return resource
202      */

203     public LaunchSession getFirstLaunchSessionForResource(SessionInfo session, Resource resource) {
204         LaunchSession launchSession = null;
205         Iterator JavaDoc<LaunchSession> i = getLaunchSessionsForResource(session, resource).iterator();
206         if(i.hasNext()) {
207             launchSession = i.next();
208         }
209         return launchSession;
210     }
211
212
213     /**
214      * Get a collection of <i>Resource Sessions</i> for a given resource.
215      *
216      * @param session session
217      * @param resource resource
218      * @return resource sessions
219      */

220     public Collection JavaDoc<LaunchSession> getLaunchSessionsForResource(SessionInfo session, Resource resource) {
221         SessionsLaunchSessions launchSessions = (SessionsLaunchSessions)session.getAttribute(Constants.LAUNCH_SESSIONS);
222         List JavaDoc<LaunchSession> l = new ArrayList JavaDoc<LaunchSession>();
223         if (launchSessions != null) {
224             for (LaunchSession launchSession : launchSessions.values()) {
225                 if (launchSession.getResource() != null && launchSession.getResource().equals(resource)) {
226                     l.add(launchSession);
227                 }
228             }
229         }
230         return l;
231     }
232
233     /**
234      * Get a policy for launching a resource given a request. The request is
235      * examined for a <i>policy</i> parameter. If no policy paramter is found,
236      * it is not a number or if the policy doesn't exist an {@link IllegalArgumentException}
237      * will be thrown. If -1 is supplied, the first granting policy will be used. If no
238      * policy grants access, then the first policy attached to the resource is
239      * found.
240      *
241      * @param request request to retrieve policy parameter from
242      * @param session session
243      * @param resource resource
244      * @return policy policy
245      * @throws IllegalArgumentException if policy id missing or invalid
246      * @throws Exception on any other error
247      */

248     public static Policy getLaunchRequestPolicy(HttpServletRequest JavaDoc request, SessionInfo session, Resource resource)
249                     throws IllegalArgumentException JavaDoc, Exception JavaDoc {
250         try {
251             // LDP - Allow the request to be null
252

253             /* BPS - If you are going to make changes like this please please please please please update the javadoc to reflect it
254              * Or at the very least remove the javadoc completely. Its far better to have no javadoc than javadoc that is
255              * misleading or downright wrong </rant>
256              */

257             int policyId = request==null ? -1 : Integer.parseInt(String.valueOf(request.getParameter("policy")));
258             Policy policy = policyId == -1 ? PolicyDatabaseFactory.getInstance().getGrantingPolicyForUser(session.getUser(), resource)
259                             : PolicyDatabaseFactory.getInstance().getPolicy(policyId);
260             if (policy == null) {
261                 if(policyId == -1) {
262                     List JavaDoc l = PolicyDatabaseFactory.getInstance().getPoliciesAttachedToResource(resource,
263                                     session.getUser().getRealm());
264                     if(l.size() == 0)
265                         throw new Exception JavaDoc("Resource is not attached to any policies.");
266                     return (Policy)l.get(0);
267                 }
268                 throw new IllegalArgumentException JavaDoc("No policy with ID of " + policy);
269             }
270             return policy;
271         } catch (NumberFormatException JavaDoc nfe) {
272             throw new IllegalArgumentException JavaDoc("No policy ID provided.");
273         }
274     }
275     
276     class SessionsLaunchSessions extends HashMap JavaDoc<String JavaDoc, LaunchSession> implements SessionInfoListener {
277         
278         private LaunchSession launchSession;
279         
280         SessionsLaunchSessions(LaunchSession launchSession) {
281             this.launchSession = launchSession;
282         }
283          
284         public void invalidated() {
285             synchronized(userSessionsList) {
286                 List JavaDoc<LaunchSession> l = userSessionsList.get(launchSession.getSession().getUser());
287                 for(LaunchSession r : values()) {
288                     l.remove(r);
289                     launchSessions.remove(r.getId());
290                 }
291                 if(l.size() == 0) {
292                     userSessionsList.remove(launchSession.getSession().getUser());
293                 }
294             }
295         }
296     }
297 }
298
Popular Tags