1 18 package org.osgi.service.wireadmin; 19 20 import java.io.IOException ; 21 import java.security.*; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 25 40 final public class WirePermission extends BasicPermission { 41 static final long serialVersionUID = -5583709391516569321L; 42 45 public static final String PRODUCE = "produce"; 46 49 public static final String CONSUME = "consume"; 50 private final static int ACTION_PRODUCE = 0x00000001; 51 private final static int ACTION_CONSUME = 0x00000002; 52 private final static int ACTION_ALL = ACTION_PRODUCE 53 | ACTION_CONSUME; 54 private final static int ACTION_NONE = 0; 55 58 private transient int action_mask = ACTION_NONE; 59 64 private String actions = null; 65 66 73 public WirePermission(String name, String actions) { 74 this(name, getMask(actions)); 75 } 76 77 83 WirePermission(String name, int mask) { 84 super(name); 85 init(mask); 86 } 87 88 93 private void init(int mask) { 94 if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) { 95 throw new IllegalArgumentException ("invalid action string"); 96 } 97 action_mask = mask; 98 } 99 100 106 private static int getMask(String actions) { 107 boolean seencomma = false; 108 int mask = ACTION_NONE; 109 if (actions == null) { 110 return mask; 111 } 112 char[] a = actions.toCharArray(); 113 int i = a.length - 1; 114 if (i < 0) 115 return mask; 116 while (i != -1) { 117 char c; 118 while ((i != -1) 120 && ((c = a[i]) == ' ' || c == '\r' || c == '\n' 121 || c == '\f' || c == '\t')) 122 i--; 123 int matchlen; 125 if (i >= 6 && (a[i - 6] == 'p' || a[i - 6] == 'P') 126 && (a[i - 5] == 'r' || a[i - 5] == 'R') 127 && (a[i - 4] == 'o' || a[i - 4] == 'O') 128 && (a[i - 3] == 'd' || a[i - 3] == 'D') 129 && (a[i - 2] == 'u' || a[i - 2] == 'U') 130 && (a[i - 1] == 'c' || a[i - 1] == 'C') 131 && (a[i] == 'e' || a[i] == 'E')) { 132 matchlen = 7; 133 mask |= ACTION_PRODUCE; 134 } 135 else 136 if (i >= 6 && (a[i - 6] == 'c' || a[i - 6] == 'C') 137 && (a[i - 5] == 'o' || a[i - 5] == 'O') 138 && (a[i - 4] == 'n' || a[i - 4] == 'N') 139 && (a[i - 3] == 's' || a[i - 3] == 'S') 140 && (a[i - 2] == 'u' || a[i - 2] == 'U') 141 && (a[i - 1] == 'm' || a[i - 1] == 'M') 142 && (a[i] == 'e' || a[i] == 'E')) { 143 matchlen = 7; 144 mask |= ACTION_CONSUME; 145 } 146 else { 147 throw new IllegalArgumentException ("invalid permission: " 149 + actions); 150 } 151 seencomma = false; 154 while (i >= matchlen && !seencomma) { 155 switch (a[i - matchlen]) { 156 case ',' : 157 seencomma = true; 158 159 case ' ' : 160 case '\r' : 161 case '\n' : 162 case '\f' : 163 case '\t' : 164 break; 165 default : 166 throw new IllegalArgumentException ( 167 "invalid permission: " + actions); 168 } 169 i--; 170 } 171 i -= matchlen; 173 } 174 if (seencomma) { 175 throw new IllegalArgumentException ("invalid permission: " + actions); 176 } 177 return mask; 178 } 179 180 199 public boolean implies(Permission p) { 200 if (p instanceof WirePermission) { 201 WirePermission target = (WirePermission) p; 202 return ((action_mask & target.action_mask) == target.action_mask) 203 && super.implies(p); 204 } 205 return false; 206 } 207 208 215 public String getActions() { 216 if (actions == null) { 217 StringBuffer sb = new StringBuffer (); 218 boolean comma = false; 219 if ((action_mask & ACTION_PRODUCE) == ACTION_PRODUCE) { 220 sb.append(PRODUCE); 221 comma = true; 222 } 223 if ((action_mask & ACTION_CONSUME) == ACTION_CONSUME) { 224 if (comma) 225 sb.append(','); 226 sb.append(CONSUME); 227 } 228 actions = sb.toString(); 229 } 230 return actions; 231 } 232 233 240 public PermissionCollection newPermissionCollection() { 241 return new WirePermissionCollection(); 242 } 243 244 255 public boolean equals(Object obj) { 256 if (obj == this) { 257 return true; 258 } 259 if (!(obj instanceof WirePermission)) { 260 return false; 261 } 262 WirePermission p = (WirePermission) obj; 263 return (action_mask == p.action_mask) && getName().equals(p.getName()); 264 } 265 266 271 public int hashCode() { 272 return getName().hashCode() ^ getActions().hashCode(); 273 } 274 275 281 int getMask() { 282 return action_mask; 283 } 284 285 294 public String toString() { 295 StringBuffer sb = new StringBuffer (); 296 sb.append('('); 297 sb.append(getClass().getName()); 298 sb.append(" \""); 299 sb.append(getName()); 300 sb.append("\" \""); 301 sb.append(getActions()); 302 sb.append("\")"); 303 return sb.toString(); 304 } 305 306 311 private synchronized void writeObject(java.io.ObjectOutputStream s) 312 throws IOException { 313 if (actions == null) 316 getActions(); 317 s.defaultWriteObject(); 318 } 319 320 324 private synchronized void readObject(java.io.ObjectInputStream s) 325 throws IOException , ClassNotFoundException { 326 s.defaultReadObject(); 328 init(getMask(actions)); 329 } 330 } 331 335 336 final class WirePermissionCollection extends PermissionCollection { 337 static final long serialVersionUID = 2617521094909826016L; 338 343 private Hashtable permissions; 344 349 private boolean all_allowed; 350 351 355 public WirePermissionCollection() { 356 permissions = new Hashtable (); 357 all_allowed = false; 358 } 359 360 371 public void add(Permission permission) { 372 if (!(permission instanceof WirePermission)) 373 throw new IllegalArgumentException ("invalid permission: " 374 + permission); 375 if (isReadOnly()) 376 throw new SecurityException ("attempt to add a Permission to a " 377 + "readonly PermissionCollection"); 378 WirePermission p = (WirePermission) permission; 379 String name = p.getName(); 380 WirePermission existing = (WirePermission) permissions.get(name); 381 if (existing != null) { 382 int oldMask = existing.getMask(); 383 int newMask = p.getMask(); 384 if (oldMask != newMask) { 385 permissions.put(name, new WirePermission(name, oldMask 386 | newMask)); 387 } 388 } 389 else { 390 permissions.put(name, permission); 391 } 392 if (!all_allowed) { 393 if (name.equals("*")) 394 all_allowed = true; 395 } 396 } 397 398 407 public boolean implies(Permission permission) { 408 if (!(permission instanceof WirePermission)) 409 return false; 410 WirePermission p = (WirePermission) permission; 411 WirePermission x; 412 int desired = p.getMask(); 413 int effective = 0; 414 if (all_allowed) { 416 x = (WirePermission) permissions.get("*"); 417 if (x != null) { 418 effective |= x.getMask(); 419 if ((effective & desired) == desired) 420 return true; 421 } 422 } 423 String name = p.getName(); 427 x = (WirePermission) permissions.get(name); 428 if (x != null) { 429 effective |= x.getMask(); 431 if ((effective & desired) == desired) 432 return true; 433 } 434 int last, offset; 436 offset = name.length() - 1; 437 while ((last = name.lastIndexOf(".", offset)) != -1) { 438 name = name.substring(0, last + 1) + "*"; 439 x = (WirePermission) permissions.get(name); 440 if (x != null) { 441 effective |= x.getMask(); 442 if ((effective & desired) == desired) 443 return (true); 444 } 445 offset = last - 1; 446 } 447 return false; 450 } 451 452 457 public Enumeration elements() { 458 return permissions.elements(); 459 } 460 } 461 | Popular Tags |