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 48 49 public final class PackagePermission extends BasicPermission { 50 static final long serialVersionUID = -5107705877071099135L; 51 54 public final static String EXPORT = "export"; 55 56 59 public final static String IMPORT = "import"; 60 61 private final static int ACTION_EXPORT = 0x00000001; 62 private final static int ACTION_IMPORT = 0x00000002; 63 private final static int ACTION_ALL = ACTION_EXPORT 64 | ACTION_IMPORT; 65 private final static int ACTION_NONE = 0; 66 69 private transient int action_mask = ACTION_NONE; 70 71 76 private String actions = null; 77 78 105 106 public PackagePermission(String name, String actions) { 107 this(name, getMask(actions)); 108 } 109 110 116 PackagePermission(String name, int mask) { 117 super(name); 118 init(mask); 119 } 120 121 126 private void init(int mask) { 127 if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) { 128 throw new IllegalArgumentException ("invalid action string"); 129 } 130 131 action_mask = mask; 132 } 133 134 140 private static int getMask(String actions) { 141 boolean seencomma = false; 142 143 int mask = ACTION_NONE; 144 145 if (actions == null) { 146 return (mask); 147 } 148 149 char[] a = actions.toCharArray(); 150 151 int i = a.length - 1; 152 if (i < 0) 153 return (mask); 154 155 while (i != -1) { 156 char c; 157 158 while ((i != -1) 160 && ((c = a[i]) == ' ' || c == '\r' || c == '\n' 161 || c == '\f' || c == '\t')) 162 i--; 163 164 int matchlen; 166 167 if (i >= 5 && (a[i - 5] == 'i' || a[i - 5] == 'I') 168 && (a[i - 4] == 'm' || a[i - 4] == 'M') 169 && (a[i - 3] == 'p' || a[i - 3] == 'P') 170 && (a[i - 2] == 'o' || a[i - 2] == 'O') 171 && (a[i - 1] == 'r' || a[i - 1] == 'R') 172 && (a[i] == 't' || a[i] == 'T')) { 173 matchlen = 6; 174 mask |= ACTION_IMPORT; 175 176 } 177 else 178 if (i >= 5 && (a[i - 5] == 'e' || a[i - 5] == 'E') 179 && (a[i - 4] == 'x' || a[i - 4] == 'X') 180 && (a[i - 3] == 'p' || a[i - 3] == 'P') 181 && (a[i - 2] == 'o' || a[i - 2] == 'O') 182 && (a[i - 1] == 'r' || a[i - 1] == 'R') 183 && (a[i] == 't' || a[i] == 'T')) { 184 matchlen = 6; 185 mask |= ACTION_EXPORT | ACTION_IMPORT; 186 187 } 188 else { 189 throw new IllegalArgumentException ("invalid permission: " 191 + actions); 192 } 193 194 seencomma = false; 197 while (i >= matchlen && !seencomma) { 198 switch (a[i - matchlen]) { 199 case ',' : 200 seencomma = true; 201 202 case ' ' : 203 case '\r' : 204 case '\n' : 205 case '\f' : 206 case '\t' : 207 break; 208 default : 209 throw new IllegalArgumentException ( 210 "invalid permission: " + actions); 211 } 212 i--; 213 } 214 215 i -= matchlen; 217 } 218 219 if (seencomma) { 220 throw new IllegalArgumentException ("invalid permission: " + actions); 221 } 222 223 return (mask); 224 } 225 226 250 251 public boolean implies(Permission p) { 252 if (p instanceof PackagePermission) { 253 PackagePermission target = (PackagePermission) p; 254 255 return (((action_mask & target.action_mask) == target.action_mask) && super 256 .implies(p)); 257 } 258 259 return (false); 260 } 261 262 273 274 public String getActions() { 275 if (actions == null) { 276 StringBuffer sb = new StringBuffer (); 277 boolean comma = false; 278 279 if ((action_mask & ACTION_EXPORT) == ACTION_EXPORT) { 280 sb.append(EXPORT); 281 comma = true; 282 } 283 284 if ((action_mask & ACTION_IMPORT) == ACTION_IMPORT) { 285 if (comma) 286 sb.append(','); 287 sb.append(IMPORT); 288 } 289 290 actions = sb.toString(); 291 } 292 293 return (actions); 294 } 295 296 302 public PermissionCollection newPermissionCollection() { 303 return (new PackagePermissionCollection()); 304 } 305 306 320 public boolean equals(Object obj) { 321 if (obj == this) { 322 return (true); 323 } 324 325 if (!(obj instanceof PackagePermission)) { 326 return (false); 327 } 328 329 PackagePermission p = (PackagePermission) obj; 330 331 return ((action_mask == p.action_mask) && getName().equals(p.getName())); 332 } 333 334 339 340 public int hashCode() { 341 return (getName().hashCode() ^ getActions().hashCode()); 342 } 343 344 351 int getMask() { 352 return (action_mask); 353 } 354 355 360 361 private synchronized void writeObject(java.io.ObjectOutputStream s) 362 throws IOException { 363 if (actions == null) 366 getActions(); 367 s.defaultWriteObject(); 368 } 369 370 374 private synchronized void readObject(java.io.ObjectInputStream s) 375 throws IOException , ClassNotFoundException { 376 s.defaultReadObject(); 378 init(getMask(actions)); 379 } 380 } 381 382 389 390 final class PackagePermissionCollection extends PermissionCollection { 391 static final long serialVersionUID = -3350758995234427603L; 392 397 private Hashtable permissions; 398 399 404 private boolean all_allowed; 405 406 409 410 public PackagePermissionCollection() { 411 permissions = new Hashtable (); 412 all_allowed = false; 413 } 414 415 428 429 public void add(Permission permission) { 430 if (!(permission instanceof PackagePermission)) 431 throw new IllegalArgumentException ("invalid permission: " 432 + permission); 433 if (isReadOnly()) 434 throw new SecurityException ("attempt to add a Permission to a " 435 + "readonly PermissionCollection"); 436 437 PackagePermission pp = (PackagePermission) permission; 438 String name = pp.getName(); 439 440 PackagePermission existing = (PackagePermission) permissions.get(name); 441 442 if (existing != null) { 443 int oldMask = existing.getMask(); 444 int newMask = pp.getMask(); 445 if (oldMask != newMask) { 446 permissions.put(name, new PackagePermission(name, oldMask 447 | newMask)); 448 449 } 450 } 451 else { 452 permissions.put(name, permission); 453 } 454 455 if (!all_allowed) { 456 if (name.equals("*")) 457 all_allowed = true; 458 } 459 } 460 461 472 473 public boolean implies(Permission permission) { 474 if (!(permission instanceof PackagePermission)) 475 return (false); 476 477 PackagePermission pp = (PackagePermission) permission; 478 PackagePermission x; 479 480 int desired = pp.getMask(); 481 int effective = 0; 482 483 if (all_allowed) { 485 x = (PackagePermission) permissions.get("*"); 486 if (x != null) { 487 effective |= x.getMask(); 488 if ((effective & desired) == desired) 489 return (true); 490 } 491 } 492 493 497 String name = pp.getName(); 498 499 x = (PackagePermission) permissions.get(name); 500 501 if (x != null) { 502 effective |= x.getMask(); 504 if ((effective & desired) == desired) 505 return (true); 506 } 507 508 int last, offset; 510 511 offset = name.length() - 1; 512 513 while ((last = name.lastIndexOf(".", offset)) != -1) { 514 515 name = name.substring(0, last + 1) + "*"; 516 x = (PackagePermission) permissions.get(name); 517 518 if (x != null) { 519 effective |= x.getMask(); 520 if ((effective & desired) == desired) 521 return (true); 522 } 523 offset = last - 1; 524 } 525 526 return (false); 529 } 530 531 537 538 public Enumeration elements() { 539 return (permissions.elements()); 540 } 541 } 542 | Popular Tags |