KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > ThreadPermissionSession


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ThreadPermissionSession.java
20  *
21  * This object stores the thread permissions for the current session.
22  */

23
24 // package definition
25
package com.rift.coad.lib.security;
26
27 // the java imports
28
import java.util.Vector JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31
32 /**
33  * This object stores the thread permissions for the current session.
34  *
35  * @author Brett Chaldecott
36  */

37 public class ThreadPermissionSession {
38     
39     // The list of containers for which the principals are being stored.
40
private Long JavaDoc threadId = null;
41     private UserSession user = null;
42     private Vector JavaDoc containerList = null;
43     
44     /**
45      * Creates a new instance of ThreadSecuritySession
46      *
47      * @param threadId The id of the thread.
48      * @param user The reference to the user object.
49      */

50     public ThreadPermissionSession(Long JavaDoc threadId, UserSession user) {
51         this.threadId = threadId;
52         this.user = user;
53         containerList = new Vector JavaDoc();
54         containerList.add(user);
55     }
56     
57     
58     /**
59      * The getter method for the thread id information.
60      *
61      * @return The long object containing the thread id information.
62      * @exception SecurityException
63      */

64     public Long JavaDoc getThreadId() throws SecurityException JavaDoc {
65         try {
66             user.touch();
67         } catch (Exception JavaDoc ex) {
68             throw new SecurityException JavaDoc("Failed to touch the users session : " +
69                     ex.getMessage(),ex);
70         }
71         return threadId;
72     }
73     
74     
75     /**
76      * This method retrieves the use information.
77      *
78      * @return The object containing the user information associated with this
79      * thread.
80      * @exception SecurityException
81      */

82     public UserSession getUser() throws SecurityException JavaDoc {
83         try {
84             user.touch();
85         } catch (Exception JavaDoc ex) {
86             throw new SecurityException JavaDoc("Failed to touch the users session : " +
87                     ex.getMessage(),ex);
88         }
89         return user;
90     }
91     
92     
93     /**
94      * This method returns the list of principals.
95      *
96      * @return The set containing the list of principals.
97      * @exception SecurityException
98      */

99     public Set JavaDoc getPrincipals() throws SecurityException JavaDoc {
100         Set JavaDoc principals = new HashSet JavaDoc();
101         for (int i = 0; i < containerList.size(); i++) {
102             principals.addAll(((PrincipalContainer)containerList.get(i)).
103                     getPrincipals());
104         }
105         try {
106             user.touch();
107         } catch (Exception JavaDoc ex) {
108             throw new SecurityException JavaDoc("Failed to touch the users session : " +
109                     ex.getMessage(),ex);
110         }
111         return principals;
112     }
113     
114     
115     /**
116      * This method adds the specified role to the list principal containers.
117      *
118      * @param role The object containining the role information.
119      * @exception SecurityException
120      */

121     public void addRole(Role role) throws SecurityException JavaDoc {
122         try {
123             user.touch();
124         } catch (Exception JavaDoc ex) {
125             throw new SecurityException JavaDoc("Failed to touch the users session : " +
126                     ex.getMessage(),ex);
127         }
128         containerList.add(role);
129     }
130     
131     
132     /**
133      * This method removes the role from the container list.
134      *
135      * @param role The object containing the role information.
136      */

137     public void removeRole(Role role) {
138         for (int i = 0; i < containerList.size(); i++) {
139             if (((PrincipalContainer)containerList.get(containerList.size() -
140                     (i + 1))).getName().equals(role.getName())) {
141                 containerList.remove(containerList.size() - (i + 1));
142                 break;
143             }
144         }
145     }
146 }
147
Popular Tags