1 7 package org.jboss.security; 8 9 import java.io.IOException ; 10 import java.security.CodeSource ; 11 import java.security.Permission ; 12 import java.security.PermissionCollection ; 13 import java.security.Permissions ; 14 import java.security.Principal ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Set ; 19 import javax.security.auth.AuthPermission ; 20 import javax.security.auth.Subject ; 21 22 27 public class AuthorizationInfo 28 { 29 private static Set emptySet = new HashSet (); 30 private ArrayList policyMap = new ArrayList (); 31 private PolicyEntry[] policyEntries; 32 33 37 static class PolicyEntry 38 { 39 private CodeSource cs; 40 private Principal [] principals; 41 private ArrayList permissions; 42 43 PolicyEntry(CodeSource cs, Principal [] principals, ArrayList permissions) 44 { 45 this.cs = cs; 46 this.principals = principals; 47 this.permissions = permissions; 48 } 49 50 public void getPermissions(PermissionCollection perms) 51 { 52 int length = permissions == null ? 0 : permissions.size(); 53 for(int n = 0; n < length; n ++) 54 { 55 Permission permission = (Permission ) permissions.get(n); 56 perms.add(permission); 57 } 58 } 59 60 public boolean implies(CodeSource codesrc, Set subjectPrincipals) 61 { 62 boolean implies = false; 63 if( this.cs == codesrc ) 65 { implies = true; 67 } 68 else if( this.cs != null && codesrc != null && this.cs.implies(codesrc) ) 69 { 70 implies = true; 71 } 72 73 if( implies == true ) 75 { 76 if( this.principals != null ) 77 { 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 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 { 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 toString() 134 { 135 StringBuffer buffer = new StringBuffer (); 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 getPermissions(Subject subject, CodeSource codesource) 152 { 153 PermissionCollection perms = new Permissions (); 154 Set 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 toString() 167 { 168 StringBuffer buffer = new StringBuffer ("permissions:"); 169 return buffer.toString(); 170 } 171 172 public void grant(CodeSource cs, ArrayList permissions) 173 { 174 grant(cs, permissions, null); 175 } 176 public void grant(CodeSource cs, ArrayList permissions, Principal [] principals) 177 { 178 PolicyEntry entry = new PolicyEntry(cs, principals, permissions); 179 policyMap.add(entry); 180 } 181 } 182 | Popular Tags |