1 7 8 package java.security; 9 10 import java.security.*; 11 import java.util.Enumeration ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.HashMap ; 15 import java.util.Hashtable ; 16 import java.util.Collections ; 17 import java.util.StringTokenizer ; 18 import java.io.ObjectStreamField ; 19 import java.io.ObjectOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.IOException ; 22 23 61 62 public abstract class BasicPermission extends Permission 63 implements java.io.Serializable 64 { 65 66 private static final long serialVersionUID = 6279438298436773498L; 67 68 private transient boolean wildcard; 70 71 private transient String path; 73 74 78 79 private void init(String name) 80 { 81 if (name == null) 82 throw new NullPointerException ("name can't be null"); 83 84 int len = name.length(); 85 86 if (len == 0) { 87 throw new IllegalArgumentException ("name can't be empty"); 88 } 89 90 char last = name.charAt(len - 1); 91 92 if (last == '*' && (len == 1 || name.charAt(len - 2) == '.')) { 94 wildcard = true; 95 if (len == 1) { 96 path = ""; 97 } else { 98 path = name.substring(0, len - 1); 99 } 100 } else { 101 path = name; 102 } 103 } 104 105 116 117 public BasicPermission(String name) 118 { 119 super(name); 120 init(name); 121 } 122 123 124 135 public BasicPermission(String name, String actions) 136 { 137 super(name); 138 init(name); 139 } 140 141 158 public boolean implies(Permission p) { 159 if ((p == null) || (p.getClass() != getClass())) 160 return false; 161 162 BasicPermission that = (BasicPermission ) p; 163 164 if (this.wildcard) { 165 if (that.wildcard) 166 return that.path.startsWith(path); 168 else 169 return (that.path.length() > this.path.length()) && 171 that.path.startsWith(this.path); 172 } else { 173 if (that.wildcard) { 174 return false; 176 } 177 else { 178 return this.path.equals(that.path); 179 } 180 } 181 } 182 183 192 public boolean equals(Object obj) { 193 if (obj == this) 194 return true; 195 196 if ((obj == null) || (obj.getClass() != getClass())) 197 return false; 198 199 BasicPermission bp = (BasicPermission ) obj; 200 201 return getName().equals(bp.getName()); 202 } 203 204 205 213 214 public int hashCode() { 215 return this.getName().hashCode(); 216 } 217 218 225 public String getActions() 226 { 227 return ""; 228 } 229 230 245 246 public PermissionCollection newPermissionCollection() { 247 return new BasicPermissionCollection (); 248 } 249 250 254 private void readObject(ObjectInputStream s) 255 throws IOException , ClassNotFoundException 256 { 257 s.defaultReadObject(); 258 init(getName()); 260 } 261 } 262 263 283 284 final class BasicPermissionCollection 285 extends PermissionCollection 286 implements java.io.Serializable 287 { 288 289 private static final long serialVersionUID = 739301742472979399L; 290 291 296 private transient Map perms; 297 298 304 private boolean all_allowed; 305 306 312 private Class permClass; 313 314 318 319 public BasicPermissionCollection() { 320 perms = new HashMap (11); 321 all_allowed = false; 322 } 323 324 339 340 public void add(Permission permission) 341 { 342 if (! (permission instanceof BasicPermission )) 343 throw new IllegalArgumentException ("invalid permission: "+ 344 permission); 345 if (isReadOnly()) 346 throw new SecurityException ("attempt to add a Permission to a readonly PermissionCollection"); 347 348 BasicPermission bp = (BasicPermission ) permission; 349 350 if (perms.size() == 0) { 351 permClass = bp.getClass(); 353 } else { 354 if (bp.getClass() != permClass) 356 throw new IllegalArgumentException ("invalid permission: " + 357 permission); 358 } 359 360 synchronized (this) { 361 perms.put(bp.getName(), permission); 362 } 363 364 if (!all_allowed) { 366 if (bp.getName().equals("*")) 367 all_allowed = true; 368 } 369 } 370 371 380 381 public boolean implies(Permission permission) 382 { 383 if (! (permission instanceof BasicPermission )) 384 return false; 385 386 BasicPermission bp = (BasicPermission ) permission; 387 388 if (bp.getClass() != permClass) 390 return false; 391 392 if (all_allowed) 394 return true; 395 396 400 String path = bp.getName(); 401 403 Permission x; 404 405 synchronized (this) { 406 x = (Permission ) perms.get(path); 407 } 408 409 if (x != null) { 410 return x.implies(permission); 412 } 413 414 int last, offset; 416 417 offset = path.length()-1; 418 419 while ((last = path.lastIndexOf(".", offset)) != -1) { 420 421 path = path.substring(0, last+1) + "*"; 422 424 synchronized (this) { 425 x = (Permission ) perms.get(path); 426 } 427 428 if (x != null) { 429 return x.implies(permission); 430 } 431 offset = last -1; 432 } 433 434 return false; 437 } 438 439 445 446 public Enumeration elements() { 447 synchronized (this) { 449 return Collections.enumeration(perms.values()); 450 } 451 } 452 453 472 private static final ObjectStreamField [] serialPersistentFields = { 473 new ObjectStreamField ("permissions", Hashtable .class), 474 new ObjectStreamField ("all_allowed", Boolean.TYPE), 475 new ObjectStreamField ("permClass", Class .class), 476 }; 477 478 481 486 private void writeObject(ObjectOutputStream out) throws IOException { 487 489 Hashtable permissions = new Hashtable (perms.size()*2); 491 492 synchronized (this) { 493 permissions.putAll(perms); 494 } 495 496 ObjectOutputStream.PutField pfields = out.putFields(); 498 pfields.put("all_allowed", all_allowed); 499 pfields.put("permissions", permissions); 500 pfields.put("permClass", permClass); 501 out.writeFields(); 502 } 503 504 508 private void readObject(java.io.ObjectInputStream in) 509 throws IOException , ClassNotFoundException 510 { 511 513 ObjectInputStream.GetField gfields = in.readFields(); 515 516 Hashtable permissions = (Hashtable )gfields.get("permissions", null); 518 perms = new HashMap (permissions.size()*2); 519 perms.putAll(permissions); 520 521 all_allowed = gfields.get("all_allowed", false); 523 524 permClass = (Class ) gfields.get("permClass", null); 526 527 if (permClass == null) { 528 Enumeration e = permissions.elements(); 530 if (e.hasMoreElements()) { 531 Permission p = (Permission )e.nextElement(); 532 permClass = p.getClass(); 533 } 534 } 535 } 536 } 537 | Popular Tags |