KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > principals > RolePrincipal


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name$
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.core.principals;
29
30 import java.security.Permission JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import net.sf.jguard.core.authorization.permissions.JGPermissionCollection;
36
37
38 /**
39  * This Principal represents the notion of a role.
40  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
41  * @author <a HREF="mailto:vinipitta@users.sourceforge.net">Vinícius Pitta Lima de Araújo</a>
42  * @author <a HREF="mailto:tandilero@users.sourceforge.net">Maximiliano Batelli</a>
43  */

44 public class RolePrincipal implements BasePrincipal{
45
46
47     /**
48      * serial version id.
49      */

50     private static final long serialVersionUID = 3761412993065431095L;
51
52     private String JavaDoc localName="";
53
54     //indicate to which application this role apply
55
private String JavaDoc applicationName = DEFAULT_APPLICATION_NAME;
56     //permissions bound to a domain owned by this Principal
57
private Set JavaDoc permissionsFromDomains;
58     //all the permissions (bound or not bound to a domain owned by this Principal)
59
private Set JavaDoc permissions;
60     //permisisons not bound to a Domain owned by this Principal.
61
private Set JavaDoc orphanedPermissions;
62     //domains owned to this principal
63
private Set JavaDoc domains;
64     private boolean active = true;
65     private String JavaDoc definition = "true";
66
67     /**
68      * All principals that this role inherites from. This property is use to implement the General Hierarchy
69      * proposed by the NIST.
70      */

71     private Set JavaDoc descendants;
72
73     /**
74      * empty constructor.
75      */

76     public RolePrincipal(){
77         permissions = new HashSet JavaDoc();
78         domains = new HashSet JavaDoc();
79         orphanedPermissions = new HashSet JavaDoc();
80         permissionsFromDomains = new HashSet JavaDoc();
81         descendants = new HashSet JavaDoc();
82     }
83
84     /**
85      * constructor.
86      * @param name
87      */

88     public RolePrincipal(String JavaDoc name){
89         permissions = new HashSet JavaDoc();
90         this.setName(name);
91         domains = new HashSet JavaDoc();
92         orphanedPermissions = new HashSet JavaDoc();
93         permissionsFromDomains = new HashSet JavaDoc();
94         descendants = new HashSet JavaDoc();
95     }
96
97
98     /**
99      * constructor.
100      * @param localName
101      * @param applicationName
102      */

103     public RolePrincipal(String JavaDoc localName,String JavaDoc applicationName){
104         permissions = new HashSet JavaDoc();
105         this.localName = localName;
106         this.applicationName = applicationName;
107         domains = new HashSet JavaDoc();
108         orphanedPermissions = new HashSet JavaDoc();
109         permissionsFromDomains = new HashSet JavaDoc();
110         descendants = new HashSet JavaDoc();
111     }
112     /**
113      * override the java.lang.Object 's <i>clone</i> method.
114      * @return new JGuardPricipal with the same internal references (permissions and Domains).
115      */

116     public Object JavaDoc clone(){
117        RolePrincipal clone = new RolePrincipal(this.localName,this.applicationName);
118        clone.setDomains(new HashSet JavaDoc(this.domains));
119        clone.setPermissions(new HashSet JavaDoc(this.permissions));
120        clone.setDescendants(new HashSet JavaDoc(this.descendants));
121         return clone;
122
123     }
124     /**
125      * @see java.security.Principal#getName()
126      */

127     public String JavaDoc getName() {
128         return getName(localName, applicationName);
129     }
130     
131     
132     /**
133      * @see java.security.Principal#getName()
134      */

135     public static String JavaDoc getName(String JavaDoc localName) {
136         return getName(localName, "*");
137     }
138     /**
139      * @see java.security.Principal#getName()
140      */

141     public static String JavaDoc getName(String JavaDoc localName,String JavaDoc applicationName) {
142         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(20);
143         sb.append(applicationName);
144         sb.append("#");
145         sb.append(localName);
146         return sb.toString();
147     }
148
149     /**
150      * compare an object to this RolePrincipal.
151      * override the Object's <i>equals</i> method.
152      * @param another object to compare
153      * @return true if another is equals to this RolePrincipal
154      */

155     public boolean equals(Object JavaDoc another){
156
157         if(another instanceof RolePrincipal){
158             RolePrincipal principal = (RolePrincipal)another;
159             if(principal.getName().equals(this.getName())){
160                 return true;
161             }
162         }
163
164         return false;
165     }
166
167     /**
168      * override the Object's <i>toString</i> method.
169      * return String representation
170      */

171     public String JavaDoc toString(){
172         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
173         sb.append(" principal class name =");
174         sb.append(getClass().getName());
175         sb.append("\n");
176         sb.append(" principal localName =");
177         sb.append(localName);
178         sb.append("\n");
179         sb.append(" principal application name =");
180         sb.append(applicationName);
181         sb.append("\n");
182         sb.append(" principal domains =");
183         sb.append(domains);
184         sb.append("\n");
185         sb.append(" principal permissions =");
186         sb.append(permissions);
187         sb.append("\n");
188         sb.append(" principal descendants =");
189         sb.append(descendants);
190         sb.append("\n");
191         return sb.toString();
192     }
193
194     /**
195      * override Object's <i>hashcode</i> method.
196      */

197     public int hashCode(){
198         return localName.hashCode()+applicationName.hashCode();
199     }
200     /**
201      * @param string
202      */

203     public void setName(String JavaDoc string) {
204         String JavaDoc[] tokens = string.split("#");
205         if(tokens.length==1){
206             applicationName = "*";
207             localName = tokens[0];
208         }else if(tokens.length==2){
209             applicationName = tokens[0];
210             localName = tokens[1];
211             
212         }else{
213             throw new IllegalArgumentException JavaDoc(" name is composed of applicationName#localName");
214         }
215         
216         
217         
218         
219     }
220
221     /**
222      * return all permissions owned by this Principal plus
223      * permissions inherited from descendants.
224      * @return permissions
225      */

226     public Set JavaDoc getAllPermissions() {
227         //all permissions owned by this principal
228
Set JavaDoc allPermissions = new HashSet JavaDoc();
229         allPermissions.addAll(permissions);
230
231         //get inherited permissions
232
for (Iterator JavaDoc it = descendants.iterator(); it.hasNext(); ) {
233             allPermissions.addAll(((RolePrincipal) it.next()).getAllPermissions());
234         }
235
236         return allPermissions;
237     }
238
239     /**
240      * return the permissions bounded to a domain plus orphanedPermisions.
241      * @return
242      */

243     public Set JavaDoc getPermissions(){
244         return permissions;
245     }
246
247     /**
248      * return the permissions not bound to a domain owned by this RolePrincipal.
249      * @return
250      */

251     public Set JavaDoc getOrphanedPermissions(){
252         return orphanedPermissions;
253     }
254
255     /**
256      * @param perms
257      */

258     public void setPermissions(Set JavaDoc perms) {
259         Iterator JavaDoc itPermissionsSet = perms.iterator();
260         while(itPermissionsSet.hasNext()){
261           Permission JavaDoc perm = (Permission JavaDoc)itPermissionsSet.next();
262           addPermission(perm);
263         }
264     }
265     /**
266      * add a permission to the RolePrincipal.
267      * @param permission
268      */

269     public void addPermission(Permission JavaDoc permission){
270         permissions.add(permission);
271         Iterator JavaDoc itDomains = domains.iterator();
272         boolean orphan = true;
273         while(itDomains.hasNext()){
274             JGPermissionCollection temp = (JGPermissionCollection)itDomains.next();
275             if(temp.containsPermission(permission)){
276                 orphan = false;
277                 break;
278             }
279         }
280         if(orphan == true){
281             orphanedPermissions.add(permission);
282         }else{
283             permissionsFromDomains.add(permission);
284         }
285     }
286
287     /**
288      * add an Domain to the RolePrincipal.
289      * @param domain
290      */

291     public void addDomain(JGPermissionCollection domain){
292         domains.add(domain);
293         Set JavaDoc permissionsDomain = domain.getPermissions();
294         //we remove from orphanedPermissions the permissions bound to this Domain
295
orphanedPermissions.removeAll(permissionsDomain);
296         //we add the permissions owned by domain to permissionsFromDomains and permissions
297
permissionsFromDomains.addAll(permissionsDomain);
298         permissions.addAll(permissionsDomain);
299     }
300
301
302     /**
303      * @return application name
304      */

305     public String JavaDoc getApplicationName() {
306         return applicationName;
307     }
308
309     /**
310      * define application name.
311      * @param string
312      */

313     public void setApplicationName(String JavaDoc string) {
314         applicationName = string;
315     }
316
317     /**
318      * @return Returns the domains.
319      */

320     public Set JavaDoc getDomains() {
321         return domains;
322     }
323
324
325     /**
326      * @return Returns the permissionsFromDomains.
327      */

328     public Set JavaDoc getPermissionsFromDomains() {
329         return permissionsFromDomains;
330     }
331
332     /**
333      * @param doms The domains to set.
334      */

335     public void setDomains(Set JavaDoc doms) {
336         Iterator JavaDoc it = doms.iterator();
337         while(it.hasNext()){
338             JGPermissionCollection dom = (JGPermissionCollection)it.next();
339             this.addDomain(dom);
340         }
341
342     }
343
344
345
346     /**
347      * method used to compare two objects.
348      * this method is used in Collection to <strong>order</strong> items, and MUST be
349      * consistent with the <i>equals</i> method (eache method should return 0/true in the same cases).
350      * @param o object to compare
351      * @see java.lang.Comparable#compareTo(java.lang.Object)
352      * @return 0 if both objects are equals,
353      * &lt;0 if 0 is lesser than the RolePrincipal,
354      * &gt;0 if 0 is greater than the RolePrincipal
355      * @see java.lang.Comparable#compareTo(java.lang.Object)
356      */

357     public int compareTo(Object JavaDoc o) {
358         RolePrincipal principal = (RolePrincipal)o;
359         if (this.equals(o)){
360             return 0;
361         }
362
363         return this.getName().compareTo(principal.getName());
364     }
365
366     public void removeDomain(JGPermissionCollection domain){
367         permissionsFromDomains.removeAll(domain.getPermissions());
368         permissions.removeAll(domain.getPermissions());
369         domains.remove(domain);
370
371     }
372
373     public Set JavaDoc getDescendants() {
374         return descendants;
375     }
376
377     public void setDescendants(Set JavaDoc descendants) {
378         this.descendants = descendants;
379     }
380
381     public boolean isActive() {
382         return active;
383     }
384
385     public void setActive(boolean active) {
386         this.active = active;
387     }
388
389     /**
390      * @return Returns definition.
391      */

392     public String JavaDoc getDefinition() {
393         return definition;
394     }
395
396     /**
397      * @param definition The definition to set.
398      */

399     public void setDefinition(String JavaDoc definition) {
400         this.definition = definition;
401     }
402
403     public String JavaDoc getLocalName() {
404         return localName;
405     }
406
407     public void setLocalName(String JavaDoc localName) {
408         this.localName = localName;
409     }
410 }
411
Popular Tags