1 7 8 package java.security; 9 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.NoSuchElementException ; 13 import java.util.Map ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.util.Collections ; 19 import java.io.Serializable ; 20 import java.io.ObjectStreamField ; 21 import java.io.ObjectOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.IOException ; 24 25 26 64 65 public final class Permissions extends PermissionCollection 66 implements Serializable 67 { 68 72 private transient Map <Class <?>, PermissionCollection > permsMap; 73 74 private transient boolean hasUnresolved = false; 77 78 private PermissionCollection allPermission; 80 81 84 public Permissions() { 85 permsMap = new HashMap (11); 86 allPermission = null; 87 } 88 89 106 107 public void add(Permission permission) { 108 if (isReadOnly()) 109 throw new SecurityException ( 110 "attempt to add a Permission to a readonly Permissions object"); 111 112 PermissionCollection pc; 113 114 synchronized (this) { 115 pc = getPermissionCollection(permission, true); 116 pc.add(permission); 117 } 118 119 if (permission instanceof AllPermission ) { 121 allPermission = pc; 122 } 123 if (permission instanceof UnresolvedPermission ) { 124 hasUnresolved = true; 125 } 126 } 127 128 155 156 public boolean implies(Permission permission) { 157 if (allPermission != null) { 159 return true; } else { 161 synchronized (this) { 162 PermissionCollection pc = getPermissionCollection(permission, 163 false); 164 if (pc != null) { 165 return pc.implies(permission); 166 } else { 167 return false; 169 } 170 } 171 } 172 } 173 174 180 181 public Enumeration <Permission > elements() { 182 185 synchronized (this) { 186 return new PermissionsEnumerator(permsMap.values().iterator()); 187 } 188 } 189 190 221 private PermissionCollection getPermissionCollection(Permission p, 222 boolean createEmpty) { 223 Class c = p.getClass(); 224 225 PermissionCollection pc = (PermissionCollection ) permsMap.get(c); 226 227 if (!hasUnresolved && !createEmpty) { 228 return pc; 229 } else if (pc == null) { 230 231 pc = (hasUnresolved ? getUnresolvedPermissions(p) : null); 233 234 if (pc == null && createEmpty) { 236 237 pc = p.newPermissionCollection(); 238 239 if (pc == null) 242 pc = new PermissionsHash(); 243 } 244 245 if (pc != null) { 246 permsMap.put(c, pc); 247 } 248 } 249 return pc; 250 } 251 252 261 private PermissionCollection getUnresolvedPermissions(Permission p) 262 { 263 265 UnresolvedPermissionCollection uc = 266 (UnresolvedPermissionCollection ) permsMap.get(UnresolvedPermission .class); 267 268 if (uc == null) 270 return null; 271 272 List unresolvedPerms = uc.getUnresolvedPermissions(p); 273 274 if (unresolvedPerms == null) 276 return null; 277 278 java.security.cert.Certificate certs[] = null; 279 280 Object signers[] = p.getClass().getSigners(); 281 282 int n = 0; 283 if (signers != null) { 284 for (int j=0; j < signers.length; j++) { 285 if (signers[j] instanceof java.security.cert.Certificate ) { 286 n++; 287 } 288 } 289 certs = new java.security.cert.Certificate [n]; 290 n = 0; 291 for (int j=0; j < signers.length; j++) { 292 if (signers[j] instanceof java.security.cert.Certificate ) { 293 certs[n++] = (java.security.cert.Certificate )signers[j]; 294 } 295 } 296 } 297 298 PermissionCollection pc = null; 299 synchronized (unresolvedPerms) { 300 int len = unresolvedPerms.size(); 301 for (int i = 0; i < len; i++) { 302 UnresolvedPermission up = 303 (UnresolvedPermission )unresolvedPerms.get(i); 304 Permission perm = up.resolve(p, certs); 305 if (perm != null) { 306 if (pc == null) { 307 pc = p.newPermissionCollection(); 308 if (pc == null) 309 pc = new PermissionsHash(); 310 } 311 pc.add(perm); 312 } 313 } 314 } 315 return pc; 316 } 317 318 private static final long serialVersionUID = 4858622370623524688L; 319 320 324 329 private static final ObjectStreamField [] serialPersistentFields = { 330 new ObjectStreamField ("perms", Hashtable .class), 331 new ObjectStreamField ("allPermission", PermissionCollection .class), 332 }; 333 334 337 342 private void writeObject(ObjectOutputStream out) throws IOException { 343 345 Hashtable perms = new Hashtable (permsMap.size()*2); synchronized (this) { 348 perms.putAll(permsMap); 349 } 350 351 ObjectOutputStream.PutField pfields = out.putFields(); 353 354 pfields.put("allPermission", allPermission); pfields.put("perms", perms); 356 out.writeFields(); 357 } 358 359 363 private void readObject(ObjectInputStream in) throws IOException , 364 ClassNotFoundException { 365 367 ObjectInputStream.GetField gfields = in.readFields(); 369 370 allPermission = (PermissionCollection ) gfields.get("allPermission", null); 372 373 Hashtable perms = (Hashtable )gfields.get("perms", null); 375 permsMap = new HashMap (perms.size()*2); 376 permsMap.putAll(perms); 377 378 UnresolvedPermissionCollection uc = 380 (UnresolvedPermissionCollection ) permsMap.get(UnresolvedPermission .class); 381 hasUnresolved = (uc != null && uc.elements().hasMoreElements()); 382 } 383 } 384 385 final class PermissionsEnumerator implements Enumeration <Permission > { 386 387 private Iterator <PermissionCollection > perms; 389 private Enumeration <Permission > permset; 391 392 PermissionsEnumerator(Iterator <PermissionCollection > e) { 393 perms = e; 394 permset = getNextEnumWithMore(); 395 } 396 397 public boolean hasMoreElements() { 399 402 if (permset == null) 403 return false; 404 405 407 if (permset.hasMoreElements()) 408 return true; 409 410 permset = getNextEnumWithMore(); 412 413 return (permset != null); 415 } 416 417 public Permission nextElement() { 419 420 423 if (hasMoreElements()) { 424 return permset.nextElement(); 425 } else { 426 throw new NoSuchElementException ("PermissionsEnumerator"); 427 } 428 429 } 430 431 private Enumeration <Permission > getNextEnumWithMore() { 432 while (perms.hasNext()) { 433 PermissionCollection pc = (PermissionCollection )perms.next(); 434 Enumeration <Permission > next =pc.elements(); 435 if (next.hasMoreElements()) 436 return next; 437 } 438 return null; 439 440 } 441 } 442 443 455 456 final class PermissionsHash extends PermissionCollection 457 implements Serializable 458 { 459 463 private transient Map permsMap; 464 465 468 469 PermissionsHash() { 470 permsMap = new HashMap (11); 471 } 472 473 478 479 public void add(Permission permission) { 480 synchronized (this) { 481 permsMap.put(permission, permission); 482 } 483 } 484 485 494 495 public boolean implies(Permission permission) { 496 synchronized (this) { 499 Permission p = (Permission ) permsMap.get(permission); 500 501 if (p == null) { 503 Iterator enum_ = permsMap.values().iterator(); 504 while (enum_.hasNext()) { 505 p = (Permission ) enum_.next(); 506 if (p.implies(permission)) 507 return true; 508 } 509 return false; 510 } else { 511 return true; 512 } 513 } 514 } 515 516 521 522 public Enumeration <Permission > elements() { 523 synchronized (this) { 525 return Collections.enumeration(permsMap.values()); 526 } 527 } 528 529 private static final long serialVersionUID = -8491988220802933440L; 530 537 private static final ObjectStreamField [] serialPersistentFields = { 538 new ObjectStreamField ("perms", Hashtable .class), 539 }; 540 541 544 548 private void writeObject(ObjectOutputStream out) throws IOException { 549 551 Hashtable perms = new Hashtable (permsMap.size()*2); 553 synchronized (this) { 554 perms.putAll(permsMap); 555 } 556 557 ObjectOutputStream.PutField pfields = out.putFields(); 559 pfields.put("perms", perms); 560 out.writeFields(); 561 } 562 563 567 private void readObject(ObjectInputStream in) throws IOException , 568 ClassNotFoundException { 569 571 ObjectInputStream.GetField gfields = in.readFields(); 573 574 Hashtable perms = (Hashtable )gfields.get("perms", null); 576 permsMap = new HashMap (perms.size()*2); 577 permsMap.putAll(perms); 578 } 579 } 580 | Popular Tags |