1 23 24 package javax.security.jacc; 25 26 import java.security.*; 27 import java.lang.reflect.*; 28 29 import java.util.HashMap ; 30 import java.io.IOException ; 31 import java.io.ObjectStreamField ; 32 33 34 53 54 public final class EJBMethodPermission extends Permission 55 implements java.io.Serializable 56 { 57 58 private static final String interfaceKeys[] = 59 { "Local", "LocalHome", "Remote", "Home", "ServiceEndpoint" }; 60 61 private static HashMap interfaceHash = new HashMap (); 62 static { 63 for (int i=0; i<interfaceKeys.length; i++) 64 interfaceHash.put(interfaceKeys[i], new Integer (i)); 65 }; 66 67 private transient int methodInterface; 68 69 private transient String otherMethodInterface = null; 70 71 private transient String methodName; 72 73 private transient String methodParams; 74 75 private transient String actions; 76 77 private transient int hashCodeValue = 0; 78 79 private static final long serialVersionUID = 1L; 80 81 88 private static final ObjectStreamField [] serialPersistentFields = { 89 new ObjectStreamField ("actions", java.lang.String .class) 90 }; 91 92 154 155 public EJBMethodPermission(String name, String actions) 156 { 157 super(name); 158 setMethodSpec(actions); 159 } 160 161 195 196 public EJBMethodPermission(String EJBName, String methodName, 197 String methodInterface, String [] methodParams) 198 { 199 super(EJBName); 200 setMethodSpec(methodName,methodInterface,methodParams); 201 } 202 203 227 228 public EJBMethodPermission(String EJBName, String methodInterface, 229 Method method) 230 { 231 super(EJBName); 232 setMethodSpec(methodInterface,method); 233 } 234 235 249 250 public boolean equals(Object o) 251 { 252 if (o == null || ! (o instanceof EJBMethodPermission )) return false; 253 254 EJBMethodPermission that = (EJBMethodPermission ) o; 255 256 if (!this.getName().equals(that.getName())) return false; 257 258 if (this.methodName != null) { 259 if (that.methodName == null || 260 !this.methodName.equals(that.methodName)) return false; 261 } 262 else if (that.methodName != null) return false; 263 264 if (this.methodInterface != that.methodInterface) return false; 265 266 if (this.methodInterface == -2 && 267 !this.otherMethodInterface.equals(that.otherMethodInterface)) 268 return false; 269 270 if (this.methodParams != null) { 271 if (that.methodParams == null || 272 !this.methodParams.equals(that.methodParams)) return false; 273 } 274 else if (that.methodParams != null) return false; 275 276 return true; 277 } 278 279 321 322 public String getActions() 323 { 324 if (this.actions == null) { 325 326 String iSpec = (this.methodInterface == -1 ? null : 327 (this.methodInterface < 0 ? 328 this.otherMethodInterface : 329 interfaceKeys[this.methodInterface])); 330 331 if (this.methodName == null) { 332 if (iSpec == null) { 333 if (this.methodParams != null) 334 this.actions = "," + this.methodParams; 335 } 336 else if (this.methodParams == null) 337 this.actions = "," + iSpec; 338 else this.actions = "," + iSpec + this.methodParams; 339 } 340 else if (iSpec == null) { 341 if (this.methodParams == null) this.actions = this.methodName; 342 else this.actions = this.methodName + "," + this.methodParams; 343 } 344 else if (this.methodParams == null) { 345 this.actions = this.methodName + "," + iSpec; 346 } 347 else this.actions = this.methodName + "," + iSpec + 348 this.methodParams; 349 } 350 351 return this.actions; 352 } 353 354 371 372 public int hashCode() 373 { 374 if (hashCodeValue == 0) { 375 376 String hashInput; 377 String actions = this.getActions(); 378 379 if (actions == null) hashInput = this.getName(); 380 else hashInput = new String (this.getName() + " " + actions); 381 382 hashCodeValue = hashInput.hashCode(); 383 } 384 return this.hashCodeValue; 385 } 386 387 420 public boolean implies(Permission permission) 421 { 422 if (permission == null || 423 ! (permission instanceof EJBMethodPermission )) return false; 424 425 EJBMethodPermission that = (EJBMethodPermission ) permission; 426 427 if (!this.getName().equals(that.getName())) return false; 428 429 if (this.methodName != null && 430 (that.methodName == null || 431 !this.methodName.equals(that.methodName))) return false; 432 433 if (this.methodInterface != -1 && 434 (that.methodInterface == -1 || 435 this.methodInterface != that.methodInterface)) return false; 436 437 if (this.methodInterface == -2 && 438 !this.otherMethodInterface.equals(that.otherMethodInterface)) 439 return false; 440 441 if (this.methodParams != null && 442 (that.methodParams == null || 443 !this.methodParams.equals(that.methodParams))) return false; 444 445 return true; 446 } 447 448 450 457 private synchronized void readObject(java.io.ObjectInputStream s) 458 throws IOException ,ClassNotFoundException 459 { 460 setMethodSpec((String ) s.readFields().get("actions",null)); 461 } 462 463 471 private synchronized void writeObject(java.io.ObjectOutputStream s) 472 throws IOException 473 { 474 s.putFields().put("actions",this.getActions()); 475 s.writeFields(); 476 } 477 478 private void setMethodSpec (String actions) 479 { 480 481 String mInterface = null; 482 483 this.methodName = null; 484 this.methodParams = null; 485 486 if (actions != null) { 487 488 if (actions.length() > 0) { 489 490 int i = actions.indexOf(','); 491 if (i < 0) this.methodName = actions; 492 else if (i >= 0) { 493 494 if (i != 0) this.methodName = actions.substring(0,i); 495 496 if (actions.length() == i+1) 497 throw new 498 IllegalArgumentException ("illegal actions spec"); 499 500 int j = actions.substring(i+1).indexOf(','); 501 if (j < 0) mInterface = actions.substring(i+1); 502 503 else { 504 if (j > 0) mInterface = actions.substring(i+1,i+j+1); 505 this.methodParams = actions.substring(i+j+1); 506 507 if (this.methodParams.length() > 1 && 508 this.methodParams.endsWith(",")) 509 throw new 510 IllegalArgumentException ("illegal methodParam"); 511 } 512 } 513 } else { 514 actions = null; 516 } 517 } 518 519 this.methodInterface = validateInterface(mInterface); 520 521 if (this.methodInterface < -1) 522 this.otherMethodInterface = mInterface; 523 524 this.actions = actions; 525 } 526 527 private void setMethodSpec(String methodName,String mInterface, 528 String [] methodParams) 529 { 530 if (methodName != null && methodName.indexOf(',') >= 0) 531 throw new IllegalArgumentException ("illegal methodName"); 532 533 this.methodInterface = validateInterface(mInterface); 534 535 if (this.methodInterface < -1) 536 this.otherMethodInterface = mInterface; 537 538 if (methodParams != null) { 539 540 StringBuffer mParams = new StringBuffer (","); 541 542 for (int i=0; i<methodParams.length; i++) { 543 if (methodParams[i] == null || 544 methodParams[i].indexOf(',') >= 0) 545 throw new IllegalArgumentException ("illegal methodParam"); 546 if (i == 0) mParams.append(methodParams[i]); 547 else mParams.append("," + methodParams[i]); 548 } 549 this.methodParams = mParams.toString(); 550 } 551 else this.methodParams = null; 552 553 this.methodName = methodName; 554 } 555 556 private void setMethodSpec(String mInterface, Method method) 557 { 558 this.methodInterface = validateInterface(mInterface); 559 560 if (this.methodInterface < -1) 561 this.otherMethodInterface = mInterface; 562 563 this.methodName = method.getName(); 564 565 Class [] params = method.getParameterTypes(); 566 567 StringBuffer mParams = new StringBuffer (","); 568 569 for (int i=0; i<params.length; i++) { 570 571 String pname = params[i].getName(); 572 Class compType = params[i].getComponentType(); 573 574 if (compType != null) { 576 String brackets = "[]"; 577 while (compType.getComponentType() != null) { 578 compType = compType.getComponentType(); 579 brackets = brackets + "[]"; 580 } 581 pname = compType.getName() + brackets; 582 } 583 584 if (i == 0) mParams.append(pname); 585 else mParams.append("," + pname); 586 } 587 588 this.methodParams = mParams.toString(); 589 } 590 591 private static int validateInterface (String methodInterface) 592 { 593 int result = -1; 594 if (methodInterface != null && methodInterface.length() > 0) { 595 Integer i = (Integer ) interfaceHash.get(methodInterface); 596 if (i != null) result = i.intValue(); 597 else result = -2; 598 } 599 return result; 600 } 601 602 } 603 | Popular Tags |