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 41 42 final public class ServicePermission extends BasicPermission { 43 static final long serialVersionUID = -7662148639076511574L; 44 47 public final static String GET = "get"; 48 51 public final static String REGISTER = "register"; 52 53 private final static int ACTION_GET = 0x00000001; 54 private final static int ACTION_REGISTER = 0x00000002; 55 private final static int ACTION_ALL = ACTION_GET 56 | ACTION_REGISTER; 57 private final static int ACTION_NONE = 0; 58 61 private transient int action_mask = ACTION_NONE; 62 63 68 private String actions = null; 69 70 99 100 public ServicePermission(String name, String actions) { 101 this(name, getMask(actions)); 102 } 103 104 110 ServicePermission(String name, int mask) { 111 super(name); 112 113 init(mask); 114 } 115 116 121 private void init(int mask) { 122 if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) { 123 throw new IllegalArgumentException ("invalid action string"); 124 } 125 126 action_mask = mask; 127 } 128 129 135 private static int getMask(String actions) { 136 boolean seencomma = false; 137 138 int mask = ACTION_NONE; 139 140 if (actions == null) { 141 return mask; 142 } 143 144 char[] a = actions.toCharArray(); 145 146 int i = a.length - 1; 147 if (i < 0) 148 return mask; 149 150 while (i != -1) { 151 char c; 152 153 while ((i != -1) 155 && ((c = a[i]) == ' ' || c == '\r' || c == '\n' 156 || c == '\f' || c == '\t')) 157 i--; 158 159 int matchlen; 161 162 if (i >= 2 && (a[i - 2] == 'g' || a[i - 2] == 'G') 163 && (a[i - 1] == 'e' || a[i - 1] == 'E') 164 && (a[i] == 't' || a[i] == 'T')) { 165 matchlen = 3; 166 mask |= ACTION_GET; 167 168 } 169 else 170 if (i >= 7 && (a[i - 7] == 'r' || a[i - 7] == 'R') 171 && (a[i - 6] == 'e' || a[i - 6] == 'E') 172 && (a[i - 5] == 'g' || a[i - 5] == 'G') 173 && (a[i - 4] == 'i' || a[i - 4] == 'I') 174 && (a[i - 3] == 's' || a[i - 3] == 'S') 175 && (a[i - 2] == 't' || a[i - 2] == 'T') 176 && (a[i - 1] == 'e' || a[i - 1] == 'E') 177 && (a[i] == 'r' || a[i] == 'R')) { 178 matchlen = 8; 179 mask |= ACTION_REGISTER; 180 181 } 182 else { 183 throw new IllegalArgumentException ("invalid permission: " 185 + actions); 186 } 187 188 seencomma = false; 191 while (i >= matchlen && !seencomma) { 192 switch (a[i - matchlen]) { 193 case ',' : 194 seencomma = true; 195 196 case ' ' : 197 case '\r' : 198 case '\n' : 199 case '\f' : 200 case '\t' : 201 break; 202 default : 203 throw new IllegalArgumentException ( 204 "invalid permission: " + actions); 205 } 206 i--; 207 } 208 209 i -= matchlen; 211 } 212 213 if (seencomma) { 214 throw new IllegalArgumentException ("invalid permission: " + actions); 215 } 216 217 return mask; 218 } 219 220 228 229 public boolean implies(Permission p) { 230 if (p instanceof ServicePermission) { 231 ServicePermission target = (ServicePermission) p; 232 233 return (((action_mask & target.action_mask) == target.action_mask) && super 234 .implies(p)); 235 } 236 237 return (false); 238 } 239 240 247 public String getActions() { 248 if (actions == null) { 249 StringBuffer sb = new StringBuffer (); 250 boolean comma = false; 251 252 if ((action_mask & ACTION_GET) == ACTION_GET) { 253 sb.append(GET); 254 comma = true; 255 } 256 257 if ((action_mask & ACTION_REGISTER) == ACTION_REGISTER) { 258 if (comma) 259 sb.append(','); 260 sb.append(REGISTER); 261 } 262 263 actions = sb.toString(); 264 } 265 266 return (actions); 267 } 268 269 276 public PermissionCollection newPermissionCollection() { 277 return (new ServicePermissionCollection()); 278 } 279 280 292 public boolean equals(Object obj) { 293 if (obj == this) { 294 return (true); 295 } 296 297 if (!(obj instanceof ServicePermission)) { 298 return (false); 299 } 300 301 ServicePermission p = (ServicePermission) obj; 302 303 return ((action_mask == p.action_mask) && getName().equals(p.getName())); 304 } 305 306 311 312 public int hashCode() { 313 return (getName().hashCode() ^ getActions().hashCode()); 314 } 315 316 322 int getMask() { 323 return (action_mask); 324 } 325 326 330 331 private synchronized void writeObject(java.io.ObjectOutputStream s) 332 throws IOException { 333 if (actions == null) 336 getActions(); 337 s.defaultWriteObject(); 338 } 339 340 344 private synchronized void readObject(java.io.ObjectInputStream s) 345 throws IOException , ClassNotFoundException { 346 s.defaultReadObject(); 348 init(getMask(actions)); 349 } 350 } 351 352 359 360 final class ServicePermissionCollection extends PermissionCollection { 361 static final long serialVersionUID = 662615640374640621L; 362 367 private Hashtable permissions; 368 369 374 private boolean all_allowed; 375 376 379 380 public ServicePermissionCollection() { 381 permissions = new Hashtable (); 382 all_allowed = false; 383 } 384 385 397 398 public void add(Permission permission) { 399 if (!(permission instanceof ServicePermission)) 400 throw new IllegalArgumentException ("invalid permission: " 401 + permission); 402 if (isReadOnly()) 403 throw new SecurityException ("attempt to add a Permission to a " 404 + "readonly PermissionCollection"); 405 406 ServicePermission sp = (ServicePermission) permission; 407 String name = sp.getName(); 408 409 ServicePermission existing = (ServicePermission) permissions.get(name); 410 411 if (existing != null) { 412 int oldMask = existing.getMask(); 413 int newMask = sp.getMask(); 414 if (oldMask != newMask) { 415 permissions.put(name, new ServicePermission(name, oldMask 416 | newMask)); 417 } 418 } 419 else { 420 permissions.put(name, permission); 421 } 422 423 if (!all_allowed) { 424 if (name.equals("*")) 425 all_allowed = true; 426 } 427 } 428 429 439 440 public boolean implies(Permission permission) { 441 if (!(permission instanceof ServicePermission)) 442 return (false); 443 444 ServicePermission sp = (ServicePermission) permission; 445 ServicePermission x; 446 447 int desired = sp.getMask(); 448 int effective = 0; 449 450 if (all_allowed) { 452 x = (ServicePermission) permissions.get("*"); 453 if (x != null) { 454 effective |= x.getMask(); 455 if ((effective & desired) == desired) 456 return (true); 457 } 458 } 459 460 464 String name = sp.getName(); 465 466 x = (ServicePermission) permissions.get(name); 467 468 if (x != null) { 469 effective |= x.getMask(); 471 if ((effective & desired) == desired) 472 return (true); 473 } 474 475 int last, offset; 477 478 offset = name.length() - 1; 479 480 while ((last = name.lastIndexOf(".", offset)) != -1) { 481 482 name = name.substring(0, last + 1) + "*"; 483 x = (ServicePermission) permissions.get(name); 484 485 if (x != null) { 486 effective |= x.getMask(); 487 if ((effective & desired) == desired) 488 return (true); 489 } 490 offset = last - 1; 491 } 492 493 return (false); 496 } 497 498 504 505 public Enumeration elements() { 506 return (permissions.elements()); 507 } 508 } 509 | Popular Tags |