1 7 8 package java.security; 9 10 import java.util.Enumeration ; 11 import java.util.List ; 12 import java.util.ArrayList ; 13 import sun.security.util.Debug; 14 import sun.security.util.SecurityConstants; 15 16 36 37 public class ProtectionDomain { 38 39 40 private CodeSource codesource ; 41 42 43 private ClassLoader classloader; 44 45 46 private Principal [] principals; 47 48 49 private PermissionCollection permissions; 50 51 53 private boolean staticPermissions; 54 55 private static final Debug debug = Debug.getInstance("domain"); 56 57 67 public ProtectionDomain(CodeSource codesource, 68 PermissionCollection permissions) { 69 this.codesource = codesource; 70 if (permissions != null) { 71 this.permissions = permissions; 72 this.permissions.setReadOnly(); 73 } 74 this.classloader = null; 75 this.principals = new Principal [0]; 76 staticPermissions = true; 77 } 78 79 108 public ProtectionDomain(CodeSource codesource, 109 PermissionCollection permissions, 110 ClassLoader classloader, 111 Principal [] principals) { 112 this.codesource = codesource; 113 if (permissions != null) { 114 this.permissions = permissions; 115 this.permissions.setReadOnly(); 116 } 117 this.classloader = classloader; 118 this.principals = (principals != null ? 119 (Principal [])principals.clone(): 120 new Principal [0]); 121 staticPermissions = false; 122 } 123 124 129 public final CodeSource getCodeSource() { 130 return this.codesource; 131 } 132 133 134 140 public final ClassLoader getClassLoader() { 141 return this.classloader; 142 } 143 144 145 152 public final Principal [] getPrincipals() { 153 return (Principal [])this.principals.clone(); 154 } 155 156 163 public final PermissionCollection getPermissions() { 164 return permissions; 165 } 166 167 194 public boolean implies(Permission permission) { 195 if (!staticPermissions && 196 Policy.getPolicyNoCheck().implies(this, permission)) 197 return true; 198 if (permissions != null) 199 return permissions.implies(permission); 200 201 return false; 202 } 203 204 207 public String toString() { 208 String pals = "<no principals>"; 209 if (principals != null && principals.length > 0) { 210 StringBuilder palBuf = new StringBuilder ("(principals "); 211 212 for (int i = 0; i < principals.length; i++) { 213 palBuf.append(principals[i].getClass().getName() + 214 " \"" + principals[i].getName() + 215 "\""); 216 if (i < principals.length-1) 217 palBuf.append(",\n"); 218 else 219 palBuf.append(")\n"); 220 } 221 pals = palBuf.toString(); 222 } 223 224 PermissionCollection pc = Policy.isSet() && seeAllp() ? 227 mergePermissions(): 228 getPermissions(); 229 230 return "ProtectionDomain "+ 231 " "+codesource+"\n"+ 232 " "+classloader+"\n"+ 233 " "+pals+"\n"+ 234 " "+pc+"\n"; 235 } 236 237 252 private static boolean seeAllp() { 253 SecurityManager sm = System.getSecurityManager(); 254 255 if (sm == null) { 256 return true; 257 } else { 258 if (debug != null) { 259 if (sm.getClass().getClassLoader() == null && 260 Policy.getPolicyNoCheck().getClass().getClassLoader() 261 == null) { 262 return true; 263 } 264 } else { 265 try { 266 sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION); 267 return true; 268 } catch (SecurityException se) { 269 } 271 } 272 } 273 274 return false; 275 } 276 277 private PermissionCollection mergePermissions() { 278 if (staticPermissions) 279 return permissions; 280 281 PermissionCollection perms = (PermissionCollection ) 282 java.security.AccessController.doPrivileged 283 (new java.security.PrivilegedAction () { 284 public Object run() { 285 Policy p = Policy.getPolicyNoCheck(); 286 return p.getPermissions(ProtectionDomain.this); 287 } 288 }); 289 290 Permissions mergedPerms = new Permissions (); 291 int swag = 32; 292 int vcap = 8; 293 Enumeration e; 294 List pdVector = new ArrayList (vcap); 295 List plVector = new ArrayList (swag); 296 297 if (permissions != null) { 300 synchronized (permissions) { 301 e = permissions.elements(); 302 while (e.hasMoreElements()) { 303 Permission p = (Permission )e.nextElement(); 304 pdVector.add(p); 305 } 306 } 307 } 308 309 if (perms != null) { 312 synchronized (perms) { 313 e = perms.elements(); 314 while (e.hasMoreElements()) { 315 plVector.add(e.nextElement()); 316 vcap++; 317 } 318 } 319 } 320 321 if (perms != null && permissions != null) { 322 synchronized (permissions) { 327 e = permissions.elements(); while (e.hasMoreElements()) { 329 Permission pdp = (Permission )e.nextElement(); 330 Class pdpClass = pdp.getClass(); 331 String pdpActions = pdp.getActions(); 332 String pdpName = pdp.getName(); 333 for (int i = 0; i < plVector.size(); i++) { 334 Permission pp = (Permission ) plVector.get(i); 335 if (pdpClass.isInstance(pp)) { 336 if (pdpName.equals(pp.getName()) && 340 pdpActions.equals(pp.getActions())) { 341 plVector.remove(i); 342 break; 343 } 344 } 345 } 346 } 347 } 348 } 349 350 if (perms !=null) { 351 354 for (int i = plVector.size()-1; i >= 0; i--) { 355 mergedPerms.add((Permission )plVector.get(i)); 356 } 357 } 358 if (permissions != null) { 359 for (int i = pdVector.size()-1; i >= 0; i--) { 360 mergedPerms.add((Permission )pdVector.get(i)); 361 } 362 } 363 364 return mergedPerms; 365 } 366 } 367 | Popular Tags |