KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ThreadsPermissionContainer.java
20  *
21  * This object contains all the thread permissions. It is not a singleton
22  * as a result only the object designed to have the rights to elevate or
23  * manipulate a threads premissions directly can access this object.
24  */

25
26 // package definition
27
package com.rift.coad.lib.security;
28
29 // the java imports
30
import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 // log 4 j imports
34
import org.apache.log4j.Logger;
35
36
37 /**
38  * This object contains all the thread permissions. It is not a singleton
39  * as a result only the object designed to have the rights to elevate or
40  * manipulate a threads premissions directly can access this object.
41  *
42  * @author Brett Chaldecott
43  */

44 public class ThreadsPermissionContainer {
45     
46     // log object
47
private Logger log =
48         Logger.getLogger(ThreadsPermissionContainer.class.getName());
49     
50     
51     // the map in which all the information is stored.
52
private Map JavaDoc permissions = null;
53     
54     /**
55      * Creates a new instance of ThreadsPermissionContainer
56      */

57     public ThreadsPermissionContainer() {
58         permissions = new HashMap JavaDoc();
59     }
60     
61     
62     /**
63      * This method placeses the new thread session in the threads list.
64      *
65      * @param threadId The id of the new thread to plase in the map.
66      * @param session The session to add to the list.
67      */

68     public synchronized void putSession(Long JavaDoc threadId,
69             ThreadPermissionSession session) {
70         permissions.put(threadId,session);
71     }
72     
73     
74     /**
75      * This method retrieves the session information from the map. Will return
76      * null if the session is not found.
77      *
78      * @return The list of permissions.
79      * @param threadId The id of the thread.
80      * @exception SecurityException
81      */

82     public synchronized ThreadPermissionSession getSession(Long JavaDoc threadId)
83     throws SecurityException JavaDoc {
84         return (ThreadPermissionSession)permissions.get(threadId);
85     }
86     
87     
88     /**
89      * The synchronized method responsible for removing the session from the
90      * threads list.
91      *
92      * @exception SecurityException
93      */

94     public synchronized void removeSession(Long JavaDoc threadId)
95     throws SecurityException JavaDoc {
96         permissions.remove(threadId);
97     }
98     
99     
100     /**
101      * This method returns the permission information for the current session
102      * identified by the thread making the call.
103      *
104      * @return The thread permission session object.
105      * @exception SecurityException
106      */

107     public ThreadPermissionSession getSession()
108     throws SecurityException JavaDoc {
109         Long JavaDoc threadId = new Long JavaDoc(Thread.currentThread().getId());
110         log.debug("Get session for [" + threadId + "]");
111         ThreadPermissionSession session = getSession(threadId);
112         if (session == null) {
113             throw new SecurityException JavaDoc(
114                     "No session can be found for current thread.");
115         }
116         return session;
117     }
118     
119     
120     /**
121      * This method pushes the role onto the thread session.
122      *
123      * @param roleName The name of the role to push onto the thread session.
124      * @exception SecurityException
125      */

126     public void pushRole(String JavaDoc roleName) throws SecurityException JavaDoc {
127         ThreadPermissionSession session = getSession();
128         Role role = RoleManager.getInstance().getRole(roleName);
129         if (role.canAccessRole(session.getPrincipals()) == false) {
130             throw new SecurityException JavaDoc(
131                     "The session does not have the rights to these permissions");
132         }
133         session.addRole(role);
134     }
135     
136     
137     /**
138      * Removed a role from the session.
139      *
140      * @param roleName The name of the role to remove from the session.
141      * @exception SecurityException
142      */

143     public void popRole(String JavaDoc roleName) throws SecurityException JavaDoc {
144         ThreadPermissionSession session = getSession();
145         Role role = RoleManager.getInstance().getRole(roleName);
146         session.removeRole(role);
147     }
148     
149 }
150
Popular Tags