1 7 8 package javax.management; 9 10 import java.security.Permission ; 11 import java.io.IOException ; 12 import java.io.ObjectInputStream ; 13 14 146 public class MBeanPermission extends Permission { 147 148 private static final long serialVersionUID = -2416928705275160661L; 149 150 153 private static final int AddNotificationListener = 0x00001; 154 private static final int GetAttribute = 0x00002; 155 private static final int GetClassLoader = 0x00004; 156 private static final int GetClassLoaderFor = 0x00008; 157 private static final int GetClassLoaderRepository = 0x00010; 158 private static final int GetDomains = 0x00020; 159 private static final int GetMBeanInfo = 0x00040; 160 private static final int GetObjectInstance = 0x00080; 161 private static final int Instantiate = 0x00100; 162 private static final int Invoke = 0x00200; 163 private static final int IsInstanceOf = 0x00400; 164 private static final int QueryMBeans = 0x00800; 165 private static final int QueryNames = 0x01000; 166 private static final int RegisterMBean = 0x02000; 167 private static final int RemoveNotificationListener = 0x04000; 168 private static final int SetAttribute = 0x08000; 169 private static final int UnregisterMBean = 0x10000; 170 171 174 private static final int NONE = 0x00000; 175 176 179 private static final int ALL = 180 AddNotificationListener | 181 GetAttribute | 182 GetClassLoader | 183 GetClassLoaderFor | 184 GetClassLoaderRepository | 185 GetDomains | 186 GetMBeanInfo | 187 GetObjectInstance | 188 Instantiate | 189 Invoke | 190 IsInstanceOf | 191 QueryMBeans | 192 QueryNames | 193 RegisterMBean | 194 RemoveNotificationListener | 195 SetAttribute | 196 UnregisterMBean; 197 198 201 private static final ObjectName allObjectNames; 202 static { 203 try { 204 allObjectNames = new ObjectName ("*:*"); 205 } catch (MalformedObjectNameException e) { 206 throw new IllegalArgumentException ("can't happen"); 207 } 208 } 209 210 213 private String actions; 214 215 218 private transient int mask; 219 220 224 private transient String classNamePrefix; 225 226 230 private transient boolean classNameExactMatch; 231 232 236 private transient String member; 237 238 242 private transient ObjectName objectName; 243 244 247 private void parseActions() { 248 249 int mask; 250 251 if (actions == null) 252 throw new IllegalArgumentException ("MBeanPermission: " + 253 "actions can't be null"); 254 if (actions.equals("")) 255 throw new IllegalArgumentException ("MBeanPermission: " + 256 "actions can't be empty"); 257 258 mask = getMask(actions); 259 260 if ((mask & ALL) != mask) 261 throw new IllegalArgumentException ("Invalid actions mask"); 262 if (mask == NONE) 263 throw new IllegalArgumentException ("Invalid actions mask"); 264 this.mask = mask; 265 } 266 267 270 private void parseName() { 271 String name = getName(); 272 273 if (name.equals("")) 274 throw new IllegalArgumentException ("MBeanPermission name " + 275 "cannot be empty"); 276 277 281 282 284 int openingBracket = name.indexOf("["); 285 if (openingBracket == -1) { 286 objectName = allObjectNames; 289 } else { 290 if (!name.endsWith("]")) { 291 throw new IllegalArgumentException ("MBeanPermission: " + 292 "The ObjectName in the " + 293 "target name must be " + 294 "included in square " + 295 "brackets"); 296 } else { 297 try { 300 String on = name.substring(openingBracket + 1, 303 name.length() - 1); 304 if (on.equals("")) 305 objectName = allObjectNames; 306 else if (on.equals("-")) 307 objectName = null; 308 else 309 objectName = new ObjectName (on); 310 } catch (MalformedObjectNameException e) { 311 throw new IllegalArgumentException ("MBeanPermission: " + 312 "The target name does " + 313 "not specify a valid " + 314 "ObjectName"); 315 } 316 } 317 318 name = name.substring(0, openingBracket); 319 } 320 321 323 int poundSign = name.indexOf("#"); 324 325 if (poundSign == -1) 326 setMember("*"); 327 else { 328 String memberName = name.substring(poundSign + 1); 329 setMember(memberName); 330 name = name.substring(0, poundSign); 331 } 332 333 335 setClassName(name); 336 } 337 338 342 private void initName(String className, String member, 343 ObjectName objectName) { 344 setClassName(className); 345 setMember(member); 346 this.objectName = objectName; 347 } 348 349 private void setClassName(String className) { 350 if (className == null || className.equals("-")) { 351 classNamePrefix = null; 352 classNameExactMatch = false; 353 } else if (className.equals("") || className.equals("*")) { 354 classNamePrefix = ""; 355 classNameExactMatch = false; 356 } else if (className.endsWith(".*")) { 357 classNamePrefix = className.substring(0, className.length() - 1); 359 classNameExactMatch = false; 360 } else { 361 classNamePrefix = className; 362 classNameExactMatch = true; 363 } 364 } 365 366 private void setMember(String member) { 367 if (member == null || member.equals("-")) 368 this.member = null; 369 else if (member.equals("")) 370 this.member = "*"; 371 else 372 this.member = member; 373 } 374 375 393 public MBeanPermission(String name, String actions) { 394 super(name); 395 396 parseName(); 397 398 this.actions = actions; 399 parseActions(); 400 } 401 402 429 public MBeanPermission(String className, 430 String member, 431 ObjectName objectName, 432 String actions) { 433 434 super(makeName(className, member, objectName)); 435 initName(className, member, objectName); 436 437 this.actions = actions; 438 parseActions(); 439 } 440 441 private static String makeName(String className, String member, 442 ObjectName objectName) { 443 StringBuffer name = new StringBuffer (); 444 if (className == null) 445 className = "-"; 446 name.append(className); 447 if (member == null) 448 member = "-"; 449 name.append("#" + member); 450 if (objectName == null) 451 name.append("[-]"); 452 else 453 name.append("[").append(objectName.getCanonicalName()).append("]"); 454 455 457 if (name.length() == 0) 458 return "*"; 459 else 460 return name.toString(); 461 } 462 463 469 public String getActions() { 470 471 if (actions == null) 472 actions = getActions(this.mask); 473 474 return actions; 475 } 476 477 481 private static String getActions(int mask) { 482 StringBuffer sb = new StringBuffer (); 483 boolean comma = false; 484 485 if ((mask & AddNotificationListener) == AddNotificationListener) { 486 comma = true; 487 sb.append("addNotificationListener"); 488 } 489 490 if ((mask & GetAttribute) == GetAttribute) { 491 if (comma) sb.append(','); 492 else comma = true; 493 sb.append("getAttribute"); 494 } 495 496 if ((mask & GetClassLoader) == GetClassLoader) { 497 if (comma) sb.append(','); 498 else comma = true; 499 sb.append("getClassLoader"); 500 } 501 502 if ((mask & GetClassLoaderFor) == GetClassLoaderFor) { 503 if (comma) sb.append(','); 504 else comma = true; 505 sb.append("getClassLoaderFor"); 506 } 507 508 if ((mask & GetClassLoaderRepository) == GetClassLoaderRepository) { 509 if (comma) sb.append(','); 510 else comma = true; 511 sb.append("getClassLoaderRepository"); 512 } 513 514 if ((mask & GetDomains) == GetDomains) { 515 if (comma) sb.append(','); 516 else comma = true; 517 sb.append("getDomains"); 518 } 519 520 if ((mask & GetMBeanInfo) == GetMBeanInfo) { 521 if (comma) sb.append(','); 522 else comma = true; 523 sb.append("getMBeanInfo"); 524 } 525 526 if ((mask & GetObjectInstance) == GetObjectInstance) { 527 if (comma) sb.append(','); 528 else comma = true; 529 sb.append("getObjectInstance"); 530 } 531 532 if ((mask & Instantiate) == Instantiate) { 533 if (comma) sb.append(','); 534 else comma = true; 535 sb.append("instantiate"); 536 } 537 538 if ((mask & Invoke) == Invoke) { 539 if (comma) sb.append(','); 540 else comma = true; 541 sb.append("invoke"); 542 } 543 544 if ((mask & IsInstanceOf) == IsInstanceOf) { 545 if (comma) sb.append(','); 546 else comma = true; 547 sb.append("isInstanceOf"); 548 } 549 550 if ((mask & QueryMBeans) == QueryMBeans) { 551 if (comma) sb.append(','); 552 else comma = true; 553 sb.append("queryMBeans"); 554 } 555 556 if ((mask & QueryNames) == QueryNames) { 557 if (comma) sb.append(','); 558 else comma = true; 559 sb.append("queryNames"); 560 } 561 562 if ((mask & RegisterMBean) == RegisterMBean) { 563 if (comma) sb.append(','); 564 else comma = true; 565 sb.append("registerMBean"); 566 } 567 568 if ((mask & RemoveNotificationListener) == RemoveNotificationListener) { 569 if (comma) sb.append(','); 570 else comma = true; 571 sb.append("removeNotificationListener"); 572 } 573 574 if ((mask & SetAttribute) == SetAttribute) { 575 if (comma) sb.append(','); 576 else comma = true; 577 sb.append("setAttribute"); 578 } 579 580 if ((mask & UnregisterMBean) == UnregisterMBean) { 581 if (comma) sb.append(','); 582 else comma = true; 583 sb.append("unregisterMBean"); 584 } 585 586 return sb.toString(); 587 } 588 589 594 public int hashCode() { 595 return this.getName().hashCode() + this.getActions().hashCode(); 596 } 597 598 604 private static int getMask(String action) { 605 606 617 618 int mask = NONE; 619 620 if (action == null) { 621 return mask; 622 } 623 624 if (action.equals("*")) { 625 return ALL; 626 } 627 628 char[] a = action.toCharArray(); 629 630 int i = a.length - 1; 631 if (i < 0) 632 return mask; 633 634 while (i != -1) { 635 char c; 636 637 while ((i!=-1) && ((c = a[i]) == ' ' || 639 c == '\r' || 640 c == '\n' || 641 c == '\f' || 642 c == '\t')) 643 i--; 644 645 int matchlen; 647 648 if (i >= 25 && 649 (a[i-25] == 'r') && 650 (a[i-24] == 'e') && 651 (a[i-23] == 'm') && 652 (a[i-22] == 'o') && 653 (a[i-21] == 'v') && 654 (a[i-20] == 'e') && 655 (a[i-19] == 'N') && 656 (a[i-18] == 'o') && 657 (a[i-17] == 't') && 658 (a[i-16] == 'i') && 659 (a[i-15] == 'f') && 660 (a[i-14] == 'i') && 661 (a[i-13] == 'c') && 662 (a[i-12] == 'a') && 663 (a[i-11] == 't') && 664 (a[i-10] == 'i') && 665 (a[i-9] == 'o') && 666 (a[i-8] == 'n') && 667 (a[i-7] == 'L') && 668 (a[i-6] == 'i') && 669 (a[i-5] == 's') && 670 (a[i-4] == 't') && 671 (a[i-3] == 'e') && 672 (a[i-2] == 'n') && 673 (a[i-1] == 'e') && 674 (a[i] == 'r')) { 675 matchlen = 26; 676 mask |= RemoveNotificationListener; 677 } else if (i >= 23 && 678 (a[i-23] == 'g') && 679 (a[i-22] == 'e') && 680 (a[i-21] == 't') && 681 (a[i-20] == 'C') && 682 (a[i-19] == 'l') && 683 (a[i-18] == 'a') && 684 (a[i-17] == 's') && 685 (a[i-16] == 's') && 686 (a[i-15] == 'L') && 687 (a[i-14] == 'o') && 688 (a[i-13] == 'a') && 689 (a[i-12] == 'd') && 690 (a[i-11] == 'e') && 691 (a[i-10] == 'r') && 692 (a[i-9] == 'R') && 693 (a[i-8] == 'e') && 694 (a[i-7] == 'p') && 695 (a[i-6] == 'o') && 696 (a[i-5] == 's') && 697 (a[i-4] == 'i') && 698 (a[i-3] == 't') && 699 (a[i-2] == 'o') && 700 (a[i-1] == 'r') && 701 (a[i] == 'y')) { 702 matchlen = 24; 703 mask |= GetClassLoaderRepository; 704 } else if (i >= 22 && 705 (a[i-22] == 'a') && 706 (a[i-21] == 'd') && 707 (a[i-20] == 'd') && 708 (a[i-19] == 'N') && 709 (a[i-18] == 'o') && 710 (a[i-17] == 't') && 711 (a[i-16] == 'i') && 712 (a[i-15] == 'f') && 713 (a[i-14] == 'i') && 714 (a[i-13] == 'c') && 715 (a[i-12] == 'a') && 716 (a[i-11] == 't') && 717 (a[i-10] == 'i') && 718 (a[i-9] == 'o') && 719 (a[i-8] == 'n') && 720 (a[i-7] == 'L') && 721 (a[i-6] == 'i') && 722 (a[i-5] == 's') && 723 (a[i-4] == 't') && 724 (a[i-3] == 'e') && 725 (a[i-2] == 'n') && 726 (a[i-1] == 'e') && 727 (a[i] == 'r')) { 728 matchlen = 23; 729 mask |= AddNotificationListener; 730 } else if (i >= 16 && 731 (a[i-16] == 'g') && 732 (a[i-15] == 'e') && 733 (a[i-14] == 't') && 734 (a[i-13] == 'C') && 735 (a[i-12] == 'l') && 736 (a[i-11] == 'a') && 737 (a[i-10] == 's') && 738 (a[i-9] == 's') && 739 (a[i-8] == 'L') && 740 (a[i-7] == 'o') && 741 (a[i-6] == 'a') && 742 (a[i-5] == 'd') && 743 (a[i-4] == 'e') && 744 (a[i-3] == 'r') && 745 (a[i-2] == 'F') && 746 (a[i-1] == 'o') && 747 (a[i] == 'r')) { 748 matchlen = 17; 749 mask |= GetClassLoaderFor; 750 } else if (i >= 16 && 751 (a[i-16] == 'g') && 752 (a[i-15] == 'e') && 753 (a[i-14] == 't') && 754 (a[i-13] == 'O') && 755 (a[i-12] == 'b') && 756 (a[i-11] == 'j') && 757 (a[i-10] == 'e') && 758 (a[i-9] == 'c') && 759 (a[i-8] == 't') && 760 (a[i-7] == 'I') && 761 (a[i-6] == 'n') && 762 (a[i-5] == 's') && 763 (a[i-4] == 't') && 764 (a[i-3] == 'a') && 765 (a[i-2] == 'n') && 766 (a[i-1] == 'c') && 767 (a[i] == 'e')) { 768 matchlen = 17; 769 mask |= GetObjectInstance; 770 } else if (i >= 14 && 771 (a[i-14] == 'u') && 772 (a[i-13] == 'n') && 773 (a[i-12] == 'r') && 774 (a[i-11] == 'e') && 775 (a[i-10] == 'g') && 776 (a[i-9] == 'i') && 777 (a[i-8] == 's') && 778 (a[i-7] == 't') && 779 (a[i-6] == 'e') && 780 (a[i-5] == 'r') && 781 (a[i-4] == 'M') && 782 (a[i-3] == 'B') && 783 (a[i-2] == 'e') && 784 (a[i-1] == 'a') && 785 (a[i] == 'n')) { 786 matchlen = 15; 787 mask |= UnregisterMBean; 788 } else if (i >= 13 && 789 (a[i-13] == 'g') && 790 (a[i-12] == 'e') && 791 (a[i-11] == 't') && 792 (a[i-10] == 'C') && 793 (a[i-9] == 'l') && 794 (a[i-8] == 'a') && 795 (a[i-7] == 's') && 796 (a[i-6] == 's') && 797 (a[i-5] == 'L') && 798 (a[i-4] == 'o') && 799 (a[i-3] == 'a') && 800 (a[i-2] == 'd') && 801 (a[i-1] == 'e') && 802 (a[i] == 'r')) { 803 matchlen = 14; 804 mask |= GetClassLoader; 805 } else if (i >= 12 && 806 (a[i-12] == 'r') && 807 (a[i-11] == 'e') && 808 (a[i-10] == 'g') && 809 (a[i-9] == 'i') && 810 (a[i-8] == 's') && 811 (a[i-7] == 't') && 812 (a[i-6] == 'e') && 813 (a[i-5] == 'r') && 814 (a[i-4] == 'M') && 815 (a[i-3] == 'B') && 816 (a[i-2] == 'e') && 817 (a[i-1] == 'a') && 818 (a[i] == 'n')) { 819 matchlen = 13; 820 mask |= RegisterMBean; 821 } else if (i >= 11 && 822 (a[i-11] == 'g') && 823 (a[i-10] == 'e') && 824 (a[i-9] == 't') && 825 (a[i-8] == 'A') && 826 (a[i-7] == 't') && 827 (a[i-6] == 't') && 828 (a[i-5] == 'r') && 829 (a[i-4] == 'i') && 830 (a[i-3] == 'b') && 831 (a[i-2] == 'u') && 832 (a[i-1] == 't') && 833 (a[i] == 'e')) { 834 matchlen = 12; 835 mask |= GetAttribute; 836 } else if (i >= 11 && 837 (a[i-11] == 'g') && 838 (a[i-10] == 'e') && 839 (a[i-9] == 't') && 840 (a[i-8] == 'M') && 841 (a[i-7] == 'B') && 842 (a[i-6] == 'e') && 843 (a[i-5] == 'a') && 844 (a[i-4] == 'n') && 845 (a[i-3] == 'I') && 846 (a[i-2] == 'n') && 847 (a[i-1] == 'f') && 848 (a[i] == 'o')) { 849 matchlen = 12; 850 mask |= GetMBeanInfo; 851 } else if (i >= 11 && 852 (a[i-11] == 'i') && 853 (a[i-10] == 's') && 854 (a[i-9] == 'I') && 855 (a[i-8] == 'n') && 856 (a[i-7] == 's') && 857 (a[i-6] == 't') && 858 (a[i-5] == 'a') && 859 (a[i-4] == 'n') && 860 (a[i-3] == 'c') && 861 (a[i-2] == 'e') && 862 (a[i-1] == 'O') && 863 (a[i] == 'f')) { 864 matchlen = 12; 865 mask |= IsInstanceOf; 866 } else if (i >= 11 && 867 (a[i-11] == 's') && 868 (a[i-10] == 'e') && 869 (a[i-9] == 't') && 870 (a[i-8] == 'A') && 871 (a[i-7] == 't') && 872 (a[i-6] == 't') && 873 (a[i-5] == 'r') && 874 (a[i-4] == 'i') && 875 (a[i-3] == 'b') && 876 (a[i-2] == 'u') && 877 (a[i-1] == 't') && 878 (a[i] == 'e')) { 879 matchlen = 12; 880 mask |= SetAttribute; 881 } else if (i >= 10 && 882 (a[i-10] == 'i') && 883 (a[i-9] == 'n') && 884 (a[i-8] == 's') && 885 (a[i-7] == 't') && 886 (a[i-6] == 'a') && 887 (a[i-5] == 'n') && 888 (a[i-4] == 't') && 889 (a[i-3] == 'i') && 890 (a[i-2] == 'a') && 891 (a[i-1] == 't') && 892 (a[i] == 'e')) { 893 matchlen = 11; 894 mask |= Instantiate; 895 } else if (i >= 10 && 896 (a[i-10] == 'q') && 897 (a[i-9] == 'u') && 898 (a[i-8] == 'e') && 899 (a[i-7] == 'r') && 900 (a[i-6] == 'y') && 901 (a[i-5] == 'M') && 902 (a[i-4] == 'B') && 903 (a[i-3] == 'e') && 904 (a[i-2] == 'a') && 905 (a[i-1] == 'n') && 906 (a[i] == 's')) { 907 matchlen = 11; 908 mask |= QueryMBeans; 909 } else if (i >= 9 && 910 (a[i-9] == 'g') && 911 (a[i-8] == 'e') && 912 (a[i-7] == 't') && 913 (a[i-6] == 'D') && 914 (a[i-5] == 'o') && 915 (a[i-4] == 'm') && 916 (a[i-3] == 'a') && 917 (a[i-2] == 'i') && 918 (a[i-1] == 'n') && 919 (a[i] == 's')) { 920 matchlen = 10; 921 mask |= GetDomains; 922 } else if (i >= 9 && 923 (a[i-9] == 'q') && 924 (a[i-8] == 'u') && 925 (a[i-7] == 'e') && 926 (a[i-6] == 'r') && 927 (a[i-5] == 'y') && 928 (a[i-4] == 'N') && 929 (a[i-3] == 'a') && 930 (a[i-2] == 'm') && 931 (a[i-1] == 'e') && 932 (a[i] == 's')) { 933 matchlen = 10; 934 mask |= QueryNames; 935 } else if (i >= 5 && 936 (a[i-5] == 'i') && 937 (a[i-4] == 'n') && 938 (a[i-3] == 'v') && 939 (a[i-2] == 'o') && 940 (a[i-1] == 'k') && 941 (a[i] == 'e')) { 942 matchlen = 6; 943 mask |= Invoke; 944 } else { 945 throw new IllegalArgumentException ("Invalid permission: " + 947 action); 948 } 949 950 boolean seencomma = false; 953 while (i >= matchlen && !seencomma) { 954 switch(a[i-matchlen]) { 955 case ',': 956 seencomma = true; 957 958 case ' ': case '\r': case '\n': 959 case '\f': case '\t': 960 break; 961 default: 962 throw new IllegalArgumentException ("Invalid permission: " + 963 action); 964 } 965 i--; 966 } 967 968 i -= matchlen; 970 } 971 972 return mask; 973 } 974 975 1017 public boolean implies(Permission p) { 1018 if (!(p instanceof MBeanPermission )) 1019 return false; 1020 1021 MBeanPermission that = (MBeanPermission ) p; 1022 1023 1029 1030 if ((this.mask & QueryMBeans) == QueryMBeans) { 1031 if (((this.mask | QueryNames) & that.mask) != that.mask) { 1032 return false; 1034 } 1035 } else { 1036 if ((this.mask & that.mask) != that.mask) { 1037 return false; 1039 } 1040 } 1041 1042 1063 1069 1070 if (that.classNamePrefix == null) { 1071 } else if (this.classNamePrefix == null) { 1073 return false; 1075 } else if (this.classNameExactMatch) { 1076 if (!that.classNameExactMatch) 1077 return false; if (!that.classNamePrefix.equals(this.classNamePrefix)) 1079 return false; } else { 1081 if (!that.classNamePrefix.startsWith(this.classNamePrefix)) 1084 return false; 1085 } 1086 1087 1088 1089 if (that.member == null) { 1090 } else if (this.member == null) { 1092 return false; 1094 } else if (this.member.equals("*")) { 1095 } else if (!this.member.equals(that.member)) { 1097 return false; 1098 } 1099 1100 1101 1102 if (that.objectName == null) { 1103 } else if (this.objectName == null) { 1105 return false; 1107 } else if (!this.objectName.apply(that.objectName)) { 1108 1112 if (!this.objectName.equals(that.objectName)) 1113 return false; 1114 } 1115 1116 return true; 1117 } 1118 1119 1128 public boolean equals(Object obj) { 1129 if (obj == this) 1130 return true; 1131 1132 if (! (obj instanceof MBeanPermission )) 1133 return false; 1134 1135 MBeanPermission that = (MBeanPermission ) obj; 1136 1137 return (this.mask == that.mask) && 1138 (this.getName().equals(that.getName())); 1139 } 1140 1141 1144 private void readObject(ObjectInputStream in) 1145 throws IOException , ClassNotFoundException { 1146 in.defaultReadObject(); 1147 parseName(); 1148 parseActions(); 1149 } 1150} 1151 | Popular Tags |