KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > AuthorizationInfo


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.security;
8
9 import java.io.IOException JavaDoc;
10 import java.security.CodeSource JavaDoc;
11 import java.security.Permission JavaDoc;
12 import java.security.PermissionCollection JavaDoc;
13 import java.security.Permissions JavaDoc;
14 import java.security.Principal JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19 import javax.security.auth.AuthPermission JavaDoc;
20 import javax.security.auth.Subject JavaDoc;
21
22 /**
23
24 @author Scott.Stark@jboss.org
25 @version $Revision: 1.5 $
26 */

27 public class AuthorizationInfo
28 {
29     private static Set JavaDoc emptySet = new HashSet JavaDoc();
30     private ArrayList JavaDoc policyMap = new ArrayList JavaDoc();
31     private PolicyEntry[] policyEntries;
32
33     /** An inner class that represents a grant entry in policyMap. It is composed
34      *of a CodeSource and an array of Prinicpals along with the granted
35      *permissions.
36      */

37     static class PolicyEntry
38     {
39         private CodeSource JavaDoc cs;
40         private Principal JavaDoc[] principals;
41         private ArrayList JavaDoc permissions;
42
43         PolicyEntry(CodeSource JavaDoc cs, Principal JavaDoc[] principals, ArrayList JavaDoc permissions)
44         {
45             this.cs = cs;
46             this.principals = principals;
47             this.permissions = permissions;
48         }
49
50         public void getPermissions(PermissionCollection JavaDoc perms)
51         {
52             int length = permissions == null ? 0 : permissions.size();
53             for(int n = 0; n < length; n ++)
54             {
55                 Permission JavaDoc permission = (Permission JavaDoc) permissions.get(n);
56                 perms.add(permission);
57             }
58         }
59
60         public boolean implies(CodeSource JavaDoc codesrc, Set JavaDoc subjectPrincipals)
61         {
62             boolean implies = false;
63             // Check codesources
64
if( this.cs == codesrc )
65             { // Both null or the same object
66
implies = true;
67             }
68             else if( this.cs != null && codesrc != null && this.cs.implies(codesrc) )
69             {
70                 implies = true;
71             }
72
73             // Check Principals
74
if( implies == true )
75             {
76                 if( this.principals != null )
77                 { // Every one of our principals must be in subjectPrincipals
78
for(int p = 0; p < this.principals.length; p ++)
79                     {
80                         if( subjectPrincipals.contains(this.principals[p]) == false )
81                         {
82                             implies = false;
83                             break;
84                         }
85                     }
86                 }
87             }
88
89             return implies;
90         }
91         public boolean equals(Object JavaDoc obj)
92         {
93             PolicyEntry key = (PolicyEntry) obj;
94             boolean equals = this.cs == key.cs;
95             if( equals == false )
96             {
97                 if( this.cs != null && key.cs != null )
98                     equals = this.cs.equals(key.cs);
99                 if( equals == true )
100                 { // Every principal in this must equal
101
if( this.principals != null && key.principals != null && this.principals.length == key.principals.length )
102                     {
103                         for(int p = 0; p < this.principals.length; p ++)
104                         {
105                             if( this.principals[p].equals(key.principals[p]) == false )
106                             {
107                                 equals = false;
108                                 break;
109                             }
110                         }
111                     }
112                     else if( this.principals != null || key.principals != null )
113                     {
114                         equals = false;
115                     }
116                 }
117             }
118             return equals;
119         }
120         public int hashCode()
121         {
122             int hashCode = 0;
123             if( cs != null )
124                 hashCode = cs.hashCode();
125             int length = (this.principals == null ? 0 : this.principals.length);
126             for(int p = 0; p < length; p ++)
127             {
128                 hashCode += this.principals[p].hashCode();
129             }
130             return hashCode;
131         }
132
133         public String JavaDoc toString()
134         {
135             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
136             buffer.append("cs=");
137             buffer.append(cs);
138             buffer.append("; principals=");
139             for(int p = 0; principals != null && p < principals.length; p ++)
140                 buffer.append(principals[p]);
141             buffer.append("; permissions=");
142             buffer.append(permissions);
143             return buffer.toString();
144         }
145     }
146
147     public AuthorizationInfo()
148     {
149     }
150
151     public PermissionCollection JavaDoc getPermissions(Subject JavaDoc subject, CodeSource JavaDoc codesource)
152     {
153         PermissionCollection JavaDoc perms = new Permissions JavaDoc();
154         Set JavaDoc subjectPrincipals = emptySet;
155         if( subject != null )
156             subjectPrincipals = subject.getPrincipals();
157         for(int n = 0; n < policyMap.size(); n ++)
158         {
159             PolicyEntry entry = (PolicyEntry) policyMap.get(n);
160             if( entry.implies(codesource, subjectPrincipals) == true )
161                 entry.getPermissions(perms);
162         }
163         return perms;
164     }
165
166     public String JavaDoc toString()
167     {
168         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("permissions:");
169         return buffer.toString();
170     }
171
172     public void grant(CodeSource JavaDoc cs, ArrayList JavaDoc permissions)
173     {
174         grant(cs, permissions, null);
175     }
176     public void grant(CodeSource JavaDoc cs, ArrayList JavaDoc permissions, Principal JavaDoc[] principals)
177     {
178         PolicyEntry entry = new PolicyEntry(cs, principals, permissions);
179         policyMap.add(entry);
180     }
181 }
182
Popular Tags