1 7 8 package java.util; 9 10 import java.io.Serializable ; 11 import java.io.IOException ; 12 import java.security.*; 13 import java.util.Map ; 14 import java.util.HashMap ; 15 import java.util.Enumeration ; 16 import java.util.Hashtable ; 17 import java.util.Collections ; 18 import java.io.ObjectStreamField ; 19 import java.io.ObjectOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.IOException ; 22 import sun.security.util.SecurityConstants; 23 24 74 75 public final class PropertyPermission extends BasicPermission { 76 77 80 private final static int READ = 0x1; 81 82 85 private final static int WRITE = 0x2; 86 89 private final static int ALL = READ|WRITE; 90 93 private final static int NONE = 0x0; 94 95 99 private transient int mask; 100 101 106 private String actions; 109 116 117 private void init(int mask) 118 { 119 120 if ((mask & ALL) != mask) 121 throw new IllegalArgumentException ("invalid actions mask"); 122 123 if (mask == NONE) 124 throw new IllegalArgumentException ("invalid actions mask"); 125 126 if (getName() == null) 127 throw new NullPointerException ("name can't be null"); 128 129 this.mask = mask; 130 } 131 132 142 143 public PropertyPermission(String name, String actions) 144 { 145 super(name,actions); 146 init(getMask(actions)); 147 } 148 149 166 public boolean implies(Permission p) { 167 if (!(p instanceof PropertyPermission )) 168 return false; 169 170 PropertyPermission that = (PropertyPermission ) p; 171 172 175 return ((this.mask & that.mask) == that.mask) && super.implies(that); 176 } 177 178 179 187 public boolean equals(Object obj) { 188 if (obj == this) 189 return true; 190 191 if (! (obj instanceof PropertyPermission )) 192 return false; 193 194 PropertyPermission that = (PropertyPermission ) obj; 195 196 return (this.mask == that.mask) && 197 (this.getName().equals(that.getName())); 198 } 199 200 208 209 public int hashCode() { 210 return this.getName().hashCode(); 211 } 212 213 214 220 private static int getMask(String actions) { 221 222 int mask = NONE; 223 224 if (actions == null) { 225 return mask; 226 } 227 228 if (actions == SecurityConstants.PROPERTY_READ_ACTION) { 230 return READ; 231 } if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) { 232 return WRITE; 233 } else if (actions == SecurityConstants.PROPERTY_RW_ACTION) { 234 return READ|WRITE; 235 } 236 237 char[] a = actions.toCharArray(); 238 239 int i = a.length - 1; 240 if (i < 0) 241 return mask; 242 243 while (i != -1) { 244 char c; 245 246 while ((i!=-1) && ((c = a[i]) == ' ' || 248 c == '\r' || 249 c == '\n' || 250 c == '\f' || 251 c == '\t')) 252 i--; 253 254 int matchlen; 256 257 if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') && 258 (a[i-2] == 'e' || a[i-2] == 'E') && 259 (a[i-1] == 'a' || a[i-1] == 'A') && 260 (a[i] == 'd' || a[i] == 'D')) 261 { 262 matchlen = 4; 263 mask |= READ; 264 265 } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') && 266 (a[i-3] == 'r' || a[i-3] == 'R') && 267 (a[i-2] == 'i' || a[i-2] == 'I') && 268 (a[i-1] == 't' || a[i-1] == 'T') && 269 (a[i] == 'e' || a[i] == 'E')) 270 { 271 matchlen = 5; 272 mask |= WRITE; 273 274 } else { 275 throw new IllegalArgumentException ( 277 "invalid permission: " + actions); 278 } 279 280 boolean seencomma = false; 283 while (i >= matchlen && !seencomma) { 284 switch(a[i-matchlen]) { 285 case ',': 286 seencomma = true; 287 288 case ' ': case '\r': case '\n': 289 case '\f': case '\t': 290 break; 291 default: 292 throw new IllegalArgumentException ( 293 "invalid permission: " + actions); 294 } 295 i--; 296 } 297 298 i -= matchlen; 300 } 301 302 return mask; 303 } 304 305 306 313 static String getActions(int mask) 314 { 315 StringBuilder sb = new StringBuilder (); 316 boolean comma = false; 317 318 if ((mask & READ) == READ) { 319 comma = true; 320 sb.append("read"); 321 } 322 323 if ((mask & WRITE) == WRITE) { 324 if (comma) sb.append(','); 325 else comma = true; 326 sb.append("write"); 327 } 328 return sb.toString(); 329 } 330 331 340 public String getActions() 341 { 342 if (actions == null) 343 actions = getActions(this.mask); 344 345 return actions; 346 } 347 348 354 355 int getMask() { 356 return mask; 357 } 358 359 367 368 public PermissionCollection newPermissionCollection() { 369 return new PropertyPermissionCollection(); 370 } 371 372 373 private static final long serialVersionUID = 885438825399942851L; 374 375 380 private synchronized void writeObject(java.io.ObjectOutputStream s) 381 throws IOException 382 { 383 if (actions == null) 386 getActions(); 387 s.defaultWriteObject(); 388 } 389 390 394 private synchronized void readObject(java.io.ObjectInputStream s) 395 throws IOException , ClassNotFoundException 396 { 397 s.defaultReadObject(); 399 init(getMask(actions)); 400 } 401 } 402 403 417 final class PropertyPermissionCollection extends PermissionCollection 418 implements Serializable 419 { 420 421 425 private transient Map perms; 426 427 432 private boolean all_allowed; 434 435 439 440 public PropertyPermissionCollection() { 441 perms = new HashMap (32); all_allowed = false; 443 } 444 445 457 458 public void add(Permission permission) 459 { 460 if (! (permission instanceof PropertyPermission )) 461 throw new IllegalArgumentException ("invalid permission: "+ 462 permission); 463 if (isReadOnly()) 464 throw new SecurityException ( 465 "attempt to add a Permission to a readonly PermissionCollection"); 466 467 PropertyPermission pp = (PropertyPermission ) permission; 468 String propName = pp.getName(); 469 470 synchronized (this) { 471 PropertyPermission existing = (PropertyPermission ) perms.get(propName); 472 473 if (existing != null) { 474 int oldMask = existing.getMask(); 475 int newMask = pp.getMask(); 476 if (oldMask != newMask) { 477 int effective = oldMask | newMask; 478 String actions = PropertyPermission.getActions(effective); 479 perms.put(propName, new PropertyPermission (propName, actions)); 480 } 481 } else { 482 perms.put(propName, permission); 483 } 484 } 485 486 if (!all_allowed) { 487 if (propName.equals("*")) 488 all_allowed = true; 489 } 490 } 491 492 501 502 public boolean implies(Permission permission) 503 { 504 if (! (permission instanceof PropertyPermission )) 505 return false; 506 507 PropertyPermission pp = (PropertyPermission ) permission; 508 PropertyPermission x; 509 510 int desired = pp.getMask(); 511 int effective = 0; 512 513 if (all_allowed) { 515 synchronized (this) { 516 x = (PropertyPermission ) perms.get("*"); 517 } 518 if (x != null) { 519 effective |= x.getMask(); 520 if ((effective & desired) == desired) 521 return true; 522 } 523 } 524 525 529 String name = pp.getName(); 530 532 synchronized (this) { 533 x = (PropertyPermission ) perms.get(name); 534 } 535 536 if (x != null) { 537 effective |= x.getMask(); 539 if ((effective & desired) == desired) 540 return true; 541 } 542 543 int last, offset; 545 546 offset = name.length()-1; 547 548 while ((last = name.lastIndexOf(".", offset)) != -1) { 549 550 name = name.substring(0, last+1) + "*"; 551 synchronized (this) { 553 x = (PropertyPermission ) perms.get(name); 554 } 555 556 if (x != null) { 557 effective |= x.getMask(); 558 if ((effective & desired) == desired) 559 return true; 560 } 561 offset = last -1; 562 } 563 564 return false; 567 } 568 569 575 576 public Enumeration elements() { 577 synchronized (this) { 579 return Collections.enumeration(perms.values()); 580 } 581 } 582 583 private static final long serialVersionUID = 7015263904581634791L; 584 585 599 private static final ObjectStreamField [] serialPersistentFields = { 600 new ObjectStreamField ("permissions", Hashtable .class), 601 new ObjectStreamField ("all_allowed", Boolean.TYPE), 602 }; 603 604 607 612 private void writeObject(ObjectOutputStream out) throws IOException { 613 615 Hashtable permissions = new Hashtable (perms.size()*2); 617 synchronized (this) { 618 permissions.putAll(perms); 619 } 620 621 ObjectOutputStream.PutField pfields = out.putFields(); 623 pfields.put("all_allowed", all_allowed); 624 pfields.put("permissions", permissions); 625 out.writeFields(); 626 } 627 628 632 private void readObject(ObjectInputStream in) throws IOException , 633 ClassNotFoundException { 634 636 ObjectInputStream.GetField gfields = in.readFields(); 638 639 all_allowed = gfields.get("all_allowed", false); 641 642 Hashtable permissions = (Hashtable )gfields.get("permissions", null); 644 perms = new HashMap (permissions.size()*2); 645 perms.putAll(permissions); 646 } 647 } 648 | Popular Tags |