1 18 19 package org.osgi.framework; 20 21 import java.io.IOException ; 22 import java.security.*; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 26 49 50 public final class BundlePermission extends BasicPermission { 51 52 private static final long serialVersionUID = 3257846601685873716L; 53 54 57 public final static String PROVIDE = "provide"; 58 59 62 public final static String REQUIRE = "require"; 63 64 67 public final static String HOST = "host"; 68 69 72 public final static String FRAGMENT = "fragment"; 73 74 private final static int ACTION_PROVIDE = 0x00000001; 75 private final static int ACTION_REQUIRE = 0x00000002; 76 private final static int ACTION_HOST = 0x00000004; 77 private final static int ACTION_FRAGMENT = 0x00000008; 78 private final static int ACTION_ALL = ACTION_PROVIDE 79 | ACTION_REQUIRE 80 | ACTION_HOST 81 | ACTION_FRAGMENT; 82 private final static int ACTION_NONE = 0; 83 86 private transient int action_mask = ACTION_NONE; 87 88 93 private String actions = null; 94 95 111 112 public BundlePermission(String symbolicName, String actions) { 113 this(symbolicName, getMask(actions)); 114 } 115 116 122 BundlePermission(String symbolicName, int mask) { 123 super(symbolicName); 124 init(mask); 125 } 126 127 132 private void init(int mask) { 133 if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) { 134 throw new IllegalArgumentException ("invalid action string"); 135 } 136 137 action_mask = mask; 138 } 139 140 146 private static int getMask(String actions) { 147 boolean seencomma = false; 148 149 int mask = ACTION_NONE; 150 151 if (actions == null) { 152 return (mask); 153 } 154 155 char[] a = actions.toCharArray(); 156 157 int i = a.length - 1; 158 if (i < 0) 159 return (mask); 160 161 while (i != -1) { 162 char c; 163 164 while ((i != -1) 166 && ((c = a[i]) == ' ' || c == '\r' || c == '\n' 167 || c == '\f' || c == '\t')) 168 i--; 169 170 int matchlen; 172 173 if (i >= 6 && (a[i - 6] == 'p' || a[i - 6] == 'P') 174 && (a[i - 5] == 'r' || a[i - 5] == 'R') 175 && (a[i - 4] == 'o' || a[i - 4] == 'O') 176 && (a[i - 3] == 'v' || a[i - 3] == 'V') 177 && (a[i - 2] == 'i' || a[i - 2] == 'I') 178 && (a[i - 1] == 'd' || a[i - 1] == 'D') 179 && (a[i] == 'e' || a[i] == 'E')) { 180 matchlen = 7; 181 mask |= ACTION_PROVIDE | ACTION_REQUIRE; 182 } 183 else 184 if (i >= 6 && (a[i - 6] == 'r' || a[i - 6] == 'R') 185 && (a[i - 5] == 'e' || a[i - 5] == 'E') 186 && (a[i - 4] == 'q' || a[i - 4] == 'Q') 187 && (a[i - 3] == 'u' || a[i - 3] == 'U') 188 && (a[i - 2] == 'i' || a[i - 2] == 'I') 189 && (a[i - 1] == 'r' || a[i - 1] == 'R') 190 && (a[i] == 'e' || a[i] == 'E')) { 191 matchlen = 7; 192 mask |= ACTION_REQUIRE; 193 } 194 else 195 if (i >= 3 && (a[i - 3] == 'h' || a[i - 3] == 'H') 196 && (a[i - 2] == 'o' || a[i - 2] == 'O') 197 && (a[i - 1] == 's' || a[i - 1] == 'S') 198 && (a[i] == 't' || a[i] == 'T')) { 199 matchlen = 4; 200 mask |= ACTION_HOST; 201 } 202 else 203 if (i >= 7 && (a[i - 7] == 'f' || a[i - 7] == 'F') 204 && (a[i - 6] == 'r' || a[i - 6] == 'R') 205 && (a[i - 5] == 'a' || a[i - 5] == 'A') 206 && (a[i - 4] == 'g' || a[i - 4] == 'G') 207 && (a[i - 3] == 'm' || a[i - 3] == 'M') 208 && (a[i - 2] == 'e' || a[i - 2] == 'E') 209 && (a[i - 1] == 'n' || a[i - 1] == 'N') 210 && (a[i] == 't' || a[i] == 'T')) { 211 matchlen = 8; 212 mask |= ACTION_FRAGMENT; 213 } 214 else { 215 throw new IllegalArgumentException ( 217 "invalid permission: " + actions); 218 } 219 220 seencomma = false; 223 while (i >= matchlen && !seencomma) { 224 switch (a[i - matchlen]) { 225 case ',' : 226 seencomma = true; 227 228 case ' ' : 229 case '\r' : 230 case '\n' : 231 case '\f' : 232 case '\t' : 233 break; 234 default : 235 throw new IllegalArgumentException ( 236 "invalid permission: " + actions); 237 } 238 i--; 239 } 240 241 i -= matchlen; 243 } 244 245 if (seencomma) { 246 throw new IllegalArgumentException ("invalid permission: " + actions); 247 } 248 249 return (mask); 250 } 251 252 276 277 public boolean implies(Permission p) { 278 if (p instanceof BundlePermission) { 279 BundlePermission target = (BundlePermission) p; 280 281 return (((action_mask & target.action_mask) == target.action_mask) && super 282 .implies(p)); 283 } 284 285 return (false); 286 } 287 288 298 299 public String getActions() { 300 if (actions == null) { 301 StringBuffer sb = new StringBuffer (); 302 boolean comma = false; 303 304 if ((action_mask & ACTION_PROVIDE) == ACTION_PROVIDE) { 305 sb.append(PROVIDE); 306 comma = true; 307 } 308 309 if ((action_mask & ACTION_REQUIRE) == ACTION_REQUIRE) { 310 if (comma) 311 sb.append(','); 312 sb.append(REQUIRE); 313 comma = true; 314 } 315 316 if ((action_mask & ACTION_HOST) == ACTION_HOST) { 317 if (comma) 318 sb.append(','); 319 sb.append(HOST); 320 comma = true; 321 } 322 323 if ((action_mask & ACTION_FRAGMENT) == ACTION_FRAGMENT) { 324 if (comma) 325 sb.append(','); 326 sb.append(FRAGMENT); 327 } 328 329 actions = sb.toString(); 330 } 331 332 return (actions); 333 } 334 335 341 public PermissionCollection newPermissionCollection() { 342 return (new BundlePermissionCollection()); 343 } 344 345 359 public boolean equals(Object obj) { 360 if (obj == this) { 361 return (true); 362 } 363 364 if (!(obj instanceof BundlePermission)) { 365 return (false); 366 } 367 368 BundlePermission p = (BundlePermission) obj; 369 370 return ((action_mask == p.action_mask) && getName().equals(p.getName())); 371 } 372 373 378 379 public int hashCode() { 380 return (getName().hashCode() ^ getActions().hashCode()); 381 } 382 383 390 int getMask() { 391 return (action_mask); 392 } 393 394 399 400 private synchronized void writeObject(java.io.ObjectOutputStream s) 401 throws IOException { 402 if (actions == null) 405 getActions(); 406 s.defaultWriteObject(); 407 } 408 409 413 private synchronized void readObject(java.io.ObjectInputStream s) 414 throws IOException , ClassNotFoundException { 415 s.defaultReadObject(); 417 init(getMask(actions)); 418 } 419 } 420 421 428 429 final class BundlePermissionCollection extends PermissionCollection { 430 431 434 private static final long serialVersionUID = 3258407326846433079L; 435 436 441 private Hashtable permissions; 442 443 448 private boolean all_allowed; 449 450 454 455 public BundlePermissionCollection() { 456 permissions = new Hashtable (); 457 all_allowed = false; 458 } 459 460 471 472 public void add(Permission permission) { 473 if (!(permission instanceof BundlePermission)) 474 throw new IllegalArgumentException ("invalid permission: " 475 + permission); 476 if (isReadOnly()) 477 throw new SecurityException ("attempt to add a Permission to a " 478 + "readonly PermissionCollection"); 479 480 BundlePermission bp = (BundlePermission) permission; 481 String name = bp.getName(); 482 483 BundlePermission existing = (BundlePermission) permissions.get(name); 484 485 if (existing != null) { 486 int oldMask = existing.getMask(); 487 int newMask = bp.getMask(); 488 if (oldMask != newMask) { 489 permissions.put(name, new BundlePermission(name, oldMask 490 | newMask)); 491 492 } 493 } 494 else { 495 permissions.put(name, permission); 496 } 497 498 if (!all_allowed) { 499 if (name.equals("*")) 500 all_allowed = true; 501 } 502 } 503 504 515 516 public boolean implies(Permission permission) { 517 if (!(permission instanceof BundlePermission)) 518 return (false); 519 520 BundlePermission bp = (BundlePermission) permission; 521 BundlePermission x; 522 523 int desired = bp.getMask(); 524 int effective = 0; 525 526 if (all_allowed) { 528 x = (BundlePermission) permissions.get("*"); 529 if (x != null) { 530 effective |= x.getMask(); 531 if ((effective & desired) == desired) 532 return (true); 533 } 534 } 535 536 540 String name = bp.getName(); 541 542 x = (BundlePermission) permissions.get(name); 543 544 if (x != null) { 545 effective |= x.getMask(); 547 if ((effective & desired) == desired) 548 return (true); 549 } 550 551 int last, offset; 553 554 offset = name.length() - 1; 555 556 while ((last = name.lastIndexOf(".", offset)) != -1) { 557 558 name = name.substring(0, last + 1) + "*"; 559 x = (BundlePermission) permissions.get(name); 560 561 if (x != null) { 562 effective |= x.getMask(); 563 if ((effective & desired) == desired) 564 return (true); 565 } 566 offset = last - 1; 567 } 568 569 return (false); 572 } 573 574 580 581 public Enumeration elements() { 582 return (permissions.elements()); 583 } 584 } 585 | Popular Tags |