1 7 8 package javax.management; 9 10 11 import java.io.InvalidObjectException ; 13 import java.io.IOException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.io.ObjectStreamField ; 17 import java.io.Serializable ; 18 import java.security.AccessController ; 19 import java.security.PrivilegedAction ; 20 import java.util.Arrays ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Hashtable ; 24 25 import com.sun.jmx.mbeanserver.GetPropertyAction; 26 27 158 public class ObjectName implements QueryExp , Serializable { 159 160 164 private final static class Property { 165 166 int _key_index; 167 int _key_length; 168 int _value_length; 169 170 173 Property(int key_index, int key_length, int value_length) { 174 _key_index = key_index; 175 _key_length = key_length; 176 _value_length = value_length; 177 } 178 179 182 void setKeyIndex(int key_index) { 183 _key_index = key_index; 184 } 185 186 189 String getKeyString(String name) { 190 return name.substring(_key_index, _key_index + _key_length); 191 } 192 193 196 String getValueString(String name) { 197 int in_begin = _key_index + _key_length + 1; 198 int out_end = in_begin + _value_length; 199 return name.substring(in_begin, out_end); 200 } 201 } 202 204 205 206 208 209 211 private static final long oldSerialVersionUID = -5467795090068647408L; 218 private static final long newSerialVersionUID = 1081892073854801359L; 221 private static final ObjectStreamField [] oldSerialPersistentFields = 224 { 225 new ObjectStreamField ("domain", String .class), 226 new ObjectStreamField ("propertyList", Hashtable .class), 227 new ObjectStreamField ("propertyListString", String .class), 228 new ObjectStreamField ("canonicalName", String .class), 229 new ObjectStreamField ("pattern", Boolean.TYPE), 230 new ObjectStreamField ("propertyPattern", Boolean.TYPE) 231 }; 232 private static final ObjectStreamField [] newSerialPersistentFields = { }; 235 private static final long serialVersionUID; 238 private static final ObjectStreamField [] serialPersistentFields; 239 private static boolean compat = false; 240 static { 241 try { 242 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 243 String form = (String ) AccessController.doPrivileged(act); 244 compat = (form != null && form.equals("1.0")); 245 } catch (Exception e) { 246 } 248 if (compat) { 249 serialPersistentFields = oldSerialPersistentFields; 250 serialVersionUID = oldSerialVersionUID; 251 } else { 252 serialPersistentFields = newSerialPersistentFields; 253 serialVersionUID = newSerialVersionUID; 254 } 255 } 256 257 260 262 265 static final private Property[] _Empty_property_array = new Property[0]; 266 267 270 static final private Hashtable _EmptyPropertyList = new Hashtable (1); 271 272 273 275 277 280 private transient String _canonicalName; 281 282 283 286 private transient Property[] _kp_array; 287 288 291 private transient Property[] _ca_array; 292 293 294 297 private transient int _domain_length = 0; 298 299 300 304 private transient Hashtable _propertyList; 305 306 309 private transient boolean _domain_pattern = false; 310 311 315 private transient boolean _property_pattern = false; 316 317 319 321 322 324 326 337 private void construct(String name) 338 throws MalformedObjectNameException , NullPointerException { 339 340 if (name == null) 342 throw new NullPointerException ("name cannot be null"); 343 344 if (name.length() == 0) { 346 _canonicalName = "*:*"; 348 _kp_array = _Empty_property_array; 349 _ca_array = _Empty_property_array; 350 _domain_length = 1; 351 _propertyList = null; 352 _domain_pattern = true; 353 _property_pattern = true; 354 return; 355 } 356 357 char[] name_chars = name.toCharArray(); 359 int len = name_chars.length; 360 char[] canonical_chars = new char[len]; int cname_index = 0; 363 int index = 0; 364 char c, c1; 365 366 domain_parsing: 368 while (index < len) { 369 switch (c = name_chars[index]) { 370 case ':' : 371 _domain_length = index++; 372 break domain_parsing; 373 case '=' : 374 int i = ++index; 375 while ((i < len) && (name_chars[i++] != ':')) 376 if (i == len) 377 throw new MalformedObjectNameException ( 378 "Domain part must be specified"); 379 break; 380 case '\n' : 381 throw new MalformedObjectNameException ( 382 "Invalid character '\\n' in domain name"); 383 case '*' : 384 case '?' : 385 _domain_pattern = true; 386 default : 387 index++; 388 } 389 } 390 391 if (index == len) 393 throw new MalformedObjectNameException ( 394 "Key properties cannot be empty"); 395 396 System.arraycopy(name_chars, 0, canonical_chars, 0, _domain_length); 398 canonical_chars[_domain_length] = ':'; 399 cname_index = _domain_length + 1; 400 401 Property prop; 403 HashMap keys_map = new HashMap (); 404 String [] keys; 405 String key_name; 406 boolean quoted_value; 407 int property_index = 0; 408 int in_index; 409 int key_index, key_length, value_index, value_length; 410 411 keys = new String [10]; 412 _kp_array = new Property[10]; 413 _property_pattern = false; 414 415 while (index < len) { 416 c = name_chars[index]; 417 418 if (c == '*') { 420 if (_property_pattern) 421 throw new MalformedObjectNameException ( 422 "Cannot have several '*' characters in pattern " + 423 "properties"); 424 else { 425 _property_pattern = true; 426 if ((++index < len ) && (name_chars[index] != ',')) 427 throw new MalformedObjectNameException ( 428 "Invalid character found after '*': end of " + 429 "name or ',' expected"); 430 else if (index == len) { 431 if (property_index == 0) { 432 _kp_array = _Empty_property_array; 434 _ca_array = _Empty_property_array; 435 _propertyList = _EmptyPropertyList; 436 } 437 break; 438 } 439 else { 440 index++; 442 continue; 443 } 444 } 445 } 446 447 in_index = index; 449 key_index = in_index; 450 while ((in_index < len) && ((c1 = name_chars[in_index++]) != '=')) 451 switch (c1) { 452 case '*' : 454 case '?' : 455 case ',' : 456 case ':' : 457 case '\n' : 458 final String ichar = ((c1=='\n')?"\\n":""+c1); 459 throw new MalformedObjectNameException ( 460 "Invalid character '" + ichar + 461 "' in key part of property"); 462 default: ; 463 } 464 if (in_index == len) 465 throw new MalformedObjectNameException ( 466 "Unterminated key property part"); 467 if (in_index == index) 468 throw new MalformedObjectNameException ("Invalid key (empty)"); 469 value_index = in_index; key_length = value_index - key_index - 1; 472 if (name_chars[in_index] == '\"') { 474 quoted_value = true; 475 quoted_value_parsing: 477 while ((++in_index < len) && 478 ((c1 = name_chars[in_index]) != '\"')) { 479 if (c1 == '\\') { 481 if (++in_index == len) 482 throw new MalformedObjectNameException ( 483 "Unterminated quoted value"); 484 switch (c1 = name_chars[in_index]) { 485 case '\\' : 486 case '\"' : 487 case '?' : 488 case '*' : 489 case 'n' : 490 break; default : 492 throw new MalformedObjectNameException ( 493 "Invalid escape sequence '\\" + 494 c1 + "' in quoted value"); 495 } 496 } else if (c1 == '\n') { 497 throw new MalformedObjectNameException ( 498 "Newline in quoted value"); 499 } else { 500 switch (c1) { 501 case '?' : 502 case '*' : 503 throw new MalformedObjectNameException ( 504 "Invalid unescaped reserved character '" + 505 c1 + "' in quoted value"); 506 default: 507 break; 508 } 509 } 510 } 511 if (in_index == len) 512 throw new MalformedObjectNameException ( 513 "Unterminated quoted value"); 514 else value_length = ++in_index - value_index; 515 } 516 else { 517 quoted_value = false; 519 while ((in_index < len) && ((c1 = name_chars[in_index]) != ',')) 520 switch (c1) { 521 case '*' : 523 case '?' : 524 case '=' : 525 case ':' : 526 case '"' : 527 case '\n' : 528 final String ichar = ((c1=='\n')?"\\n":""+c1); 529 throw new MalformedObjectNameException ( 530 "Invalid character '" + c1 + 531 "' in value part of property"); 532 default : in_index++; 533 } 534 value_length = in_index - value_index; 535 } 536 537 if (in_index == len - 1) { 539 if (quoted_value) 540 throw new MalformedObjectNameException ( 541 "Invalid ending character `" + 542 name_chars[in_index] + "'"); 543 else throw new MalformedObjectNameException ( 544 "Invalid ending comma"); 545 } 546 else in_index++; 547 548 prop = new Property(key_index, key_length, value_length); 550 key_name = name.substring(key_index, key_index + key_length); 551 552 if (property_index == keys.length) { 553 String [] tmp_string_array = new String [property_index + 10]; 554 System.arraycopy(keys, 0, tmp_string_array, 0, property_index); 555 keys = tmp_string_array; 556 } 557 keys[property_index] = key_name; 558 559 addProperty(prop, property_index, keys_map, key_name); 560 property_index++; 561 index = in_index; 562 } 563 564 setCanonicalName(name_chars, canonical_chars, keys, 566 keys_map, cname_index, property_index); 567 } 568 569 581 private void construct(String domain, Hashtable props) 582 throws MalformedObjectNameException , NullPointerException { 583 584 if (domain == null) 586 throw new NullPointerException ("domain cannot be null"); 587 588 if (props == null) 590 throw new NullPointerException ("key property list cannot be null"); 591 592 if (props.isEmpty()) 594 throw new MalformedObjectNameException ( 595 "key property list cannot be empty"); 596 597 if (!isDomain(domain)) 599 throw new MalformedObjectNameException ("Invalid domain: " + domain); 600 601 final StringBuffer sb = new StringBuffer (); 603 sb.append(domain).append(':'); 604 _domain_length = domain.length(); 605 606 int nb_props = props.size(); 608 _kp_array = new Property[nb_props]; 609 610 String [] keys = new String [nb_props]; 611 final Enumeration e = props.keys(); 612 final HashMap keys_map = new HashMap (); 613 Property prop; 614 int key_index; 615 for (int i = 0; e.hasMoreElements(); i++ ) { 616 if (i > 0) sb.append(","); 617 String key = ""; 618 try { 619 key = (String )e.nextElement(); 620 } catch (Exception x) { 621 throw new MalformedObjectNameException ("Invalid key `" + 622 key + "'"); 623 } 624 String value = ""; 625 try { 626 value = (String )props.get(key); 627 } catch (Exception x) { 628 throw new MalformedObjectNameException ("Invalid value `" + 629 value + "'"); 630 } 631 key_index = sb.length(); 632 checkKey(key); 633 sb.append(key); 634 keys[i] = key; 635 sb.append("="); 636 checkValue(value); 637 sb.append(value); 638 prop = new Property(key_index, key.length(), value.length()); 639 addProperty(prop, i, keys_map, key); 640 } 641 642 int len = sb.length(); 644 char[] initial_chars = new char[len]; 645 sb.getChars(0, len, initial_chars, 0); 646 char[] canonical_chars = new char[len]; 647 System.arraycopy(initial_chars, 0, canonical_chars, 0, 648 _domain_length + 1); 649 setCanonicalName(initial_chars, canonical_chars, keys, keys_map, 650 _domain_length + 1, _kp_array.length); 651 } 652 654 656 660 private void addProperty(Property prop, int index, 661 HashMap keys_map, String key_name) 662 throws MalformedObjectNameException { 663 664 if (keys_map.containsKey(key_name)) throw new 665 MalformedObjectNameException ("key `" + 666 key_name +"' already defined"); 667 668 if (index == _kp_array.length) { 670 Property[] tmp_prop_array = new Property[index + 10]; 671 System.arraycopy(_kp_array, 0, tmp_prop_array, 0, index); 672 _kp_array = tmp_prop_array; 673 } 674 _kp_array[index] = prop; 675 keys_map.put(key_name, prop); 676 } 677 678 683 private void setCanonicalName(char[] specified_chars, 684 char[] canonical_chars, 685 String [] keys, HashMap keys_map, 686 int prop_index, int nb_props) { 687 688 if (_kp_array != _Empty_property_array) { 690 String [] tmp_keys = new String [nb_props]; 691 Property[] tmp_props = new Property[nb_props]; 692 693 System.arraycopy(keys, 0, tmp_keys, 0, nb_props); 694 Arrays.sort(tmp_keys); 695 keys = tmp_keys; 696 System.arraycopy(_kp_array, 0, tmp_props, 0 , nb_props); 697 _kp_array = tmp_props; 698 _ca_array = new Property[nb_props]; 699 700 for (int i = 0; i < nb_props; i++) 703 _ca_array[i] = (Property) keys_map.get(keys[i]); 704 705 int last_index = nb_props - 1; 708 int prop_len; 709 Property prop; 710 for (int i = 0; i <= last_index; i++) { 711 prop = _ca_array[i]; 712 prop_len = prop._key_length + prop._value_length + 1; 714 System.arraycopy(specified_chars, prop._key_index, 715 canonical_chars, prop_index, prop_len); 716 prop.setKeyIndex(prop_index); 717 prop_index += prop_len; 718 if (i != last_index) { 719 canonical_chars[prop_index] = ','; 720 prop_index++; 721 } 722 } 723 } 724 725 if (_property_pattern) { 727 if (_kp_array != _Empty_property_array) 728 canonical_chars[prop_index++] = ','; 729 canonical_chars[prop_index++] = '*'; 730 } 731 732 _canonicalName = (new String (canonical_chars, 0, prop_index)).intern(); 734 } 735 736 746 private final static int parseKey(final char[] s, final int startKey) 747 throws MalformedObjectNameException { 748 int next = startKey; 749 int endKey = startKey; 750 final int len = s.length; 751 while (next < len) { 752 final char k = s[next++]; 753 switch (k) { 754 case '*': 755 case '?': 756 case ',': 757 case ':': 758 case '\n': 759 final String ichar = ((k=='\n')?"\\n":""+k); 760 throw new 761 MalformedObjectNameException ("Invalid character in key: `" 762 + ichar + "'"); 763 case '=': 764 endKey = next-1; 766 break; 767 default: 768 if (next < len) continue; 769 else endKey=next; 770 } 771 break; 772 } 773 return endKey; 774 } 775 776 786 private final static int parseValue(final char[] s, final int startValue) 787 throws MalformedObjectNameException { 788 789 int next = startValue; 790 int endValue = startValue; 791 792 final int len = s.length; 793 final char q=s[startValue]; 794 795 if (q == '"') { 796 if (++next == len) throw new 798 MalformedObjectNameException ("Invalid quote"); 799 while (next < len) { 800 char last = s[next]; 801 if (last == '\\') { 802 if (++next == len) throw new 803 MalformedObjectNameException ( 804 "Invalid unterminated quoted character sequence"); 805 last = s[next]; 806 switch (last) { 807 case '\\' : 808 case '?' : 809 case '*' : 810 case 'n' : 811 break; 812 case '\"' : 813 if (next+1 == len) throw new 818 MalformedObjectNameException ( 819 "Missing termination quote"); 820 break; 821 default: 822 throw new 823 MalformedObjectNameException ( 824 "Invalid quoted character sequence '\\" + 825 last + "'"); 826 } 827 } else if (last == '\n') { 828 throw new MalformedObjectNameException ( 829 "Newline in quoted value"); 830 } else if (last == '\"') { 831 next++; 832 break; 833 } else { 834 switch (last) { 835 case '?' : 836 case '*' : 837 throw new MalformedObjectNameException ( 838 "Invalid unescaped reserved character '" + 839 last + "' in quoted value"); 840 default: 841 break; 842 } 843 } 844 next++; 845 846 if ((next >= len) && (last != '\"')) throw new 851 MalformedObjectNameException ("Missing termination quote"); 852 } 853 endValue = next; 854 if (next < len) { 855 if (s[next++] != ',') throw new 856 MalformedObjectNameException ("Invalid quote"); 857 } 858 } 859 else { 860 while (next < len) { 862 final char v=s[next++]; 863 switch(v) { 864 case '*': 865 case '?': 866 case '=': 867 case ':': 868 case '\n' : 869 final String ichar = ((v=='\n')?"\\n":""+v); 870 throw new 871 MalformedObjectNameException ("Invalid character `" + 872 ichar + "' in value"); 873 case ',': 874 endValue = next-1; 875 break; 876 default: 877 if (next < len) continue; 878 else endValue=next; 879 } 880 break; 881 } 882 } 883 return endValue; 884 } 885 886 890 private String checkValue(String val) 891 throws MalformedObjectNameException { 892 893 if (val == null) throw new 894 MalformedObjectNameException ("Invalid value (null)"); 895 896 final int len = val.length(); 897 if (len == 0) throw new 898 MalformedObjectNameException ("Invalid value (empty)"); 899 900 final char[] s = val.toCharArray(); 901 final int endValue = parseValue(s,0); 902 if (endValue < len) throw new 903 MalformedObjectNameException ("Invalid character in value: `" + 904 s[endValue] + "'"); 905 return val; 906 } 907 908 912 private String checkKey(String key) 913 throws MalformedObjectNameException { 914 915 if (key == null) throw new 916 MalformedObjectNameException ("Invalid key (null)"); 917 918 final int len = key.length(); 919 if (len == 0) throw new 920 MalformedObjectNameException ("Invalid key (empty)"); 921 final char[] k=key.toCharArray(); 922 final int endKey = parseKey(k,0); 923 if (endKey < len) throw new 924 MalformedObjectNameException ("Invalid character in value: `" + 925 k[endKey] + "'"); 926 return key; 927 } 928 929 935 private static boolean wildmatch(char[] s, char[] p, int si, int pi) { 936 char c; 937 final int slen = s.length; 938 final int plen = p.length; 939 940 while (pi < plen) { c = p[pi++]; 942 if (c == '?') { 943 if (++si > slen) return false; 944 } else if (c == '*') { if (pi >= plen) return true; 946 do { 947 if (wildmatch(s,p,si,pi)) return true; 948 } while (++si < slen); 949 return false; 950 } else { 951 if (si >= slen || c != s[si++]) return false; 952 } 953 } 954 return (si == slen); 955 } 956 957 959 961 964 private boolean isDomain(String domain) { 965 if (domain == null) return true; 966 final char[] d=domain.toCharArray(); 967 final int len = d.length; 968 int next = 0; 969 while (next < len) { 970 final char c = d[next++]; 971 switch (c) { 972 case ':' : 973 case '\n' : 974 return false; 975 case '*' : 976 case '?' : 977 _domain_pattern = true; 978 default: 979 continue; 980 } 981 } 982 return true; 983 } 984 985 987 989 1047 private void readObject(ObjectInputStream in) 1048 throws IOException , ClassNotFoundException { 1049 1050 String cn; 1051 if (compat) { 1052 final ObjectInputStream.GetField fields = in.readFields(); 1056 String propListString = 1057 (String )fields.get("propertyListString", ""); 1058 1059 final boolean propPattern = 1061 fields.get("propertyPattern" , false); 1062 if (propPattern) { 1063 propListString = 1064 (propListString.length()==0?"*":(propListString+",*")); 1065 } 1066 1067 cn = (String )fields.get("domain", "default")+ 1068 ":"+ propListString; 1069 } else { 1070 in.defaultReadObject(); 1073 cn = (String )in.readObject(); 1074 } 1075 1076 try { 1077 construct(cn); 1078 } catch (NullPointerException e) { 1079 throw new InvalidObjectException (e.toString()); 1080 } catch (MalformedObjectNameException e) { 1081 throw new InvalidObjectException (e.toString()); 1082 } 1083 } 1084 1085 1086 1144 private void writeObject(ObjectOutputStream out) 1145 throws IOException { 1146 if (compat) 1147 { 1148 ObjectOutputStream.PutField fields = out.putFields(); 1151 fields.put("domain", _canonicalName.substring(0, _domain_length)); 1152 fields.put("propertyList", getKeyPropertyList()); 1153 fields.put("propertyListString", getKeyPropertyListString()); 1154 fields.put("canonicalName", _canonicalName); 1155 fields.put("pattern", (_domain_pattern || _property_pattern)); 1156 fields.put("propertyPattern", _property_pattern); 1157 out.writeFields(); 1158 } 1159 else 1160 { 1161 out.defaultWriteObject(); 1164 out.writeObject(getSerializedNameString()); 1165 } 1166 } 1167 1168 1170 1172 1174 1176 1195 public static ObjectName getInstance(String name) 1196 throws MalformedObjectNameException , NullPointerException { 1197 return new ObjectName (name); 1198 } 1199 1200 1223 public static ObjectName getInstance(String domain, String key, 1224 String value) 1225 throws MalformedObjectNameException , NullPointerException { 1226 return new ObjectName (domain, key, value); 1227 } 1228 1229 1255 public static ObjectName getInstance(String domain, Hashtable table) 1256 throws MalformedObjectNameException , NullPointerException { 1257 return new ObjectName (domain, table); 1258 } 1259 1260 1290 public static ObjectName getInstance(ObjectName name) 1291 throws NullPointerException { 1292 if (name.getClass().equals(ObjectName .class)) 1293 return name; 1294 try { 1295 return new ObjectName (name.getSerializedNameString()); 1296 } catch (MalformedObjectNameException e) { 1297 throw new IllegalArgumentException ("Unexpected: " + e); 1298 } 1300 } 1301 1302 1312 public ObjectName(String name) 1313 throws MalformedObjectNameException , NullPointerException { 1314 construct(name); 1315 } 1316 1317 1330 public ObjectName(String domain, String key, String value) 1331 throws MalformedObjectNameException , NullPointerException { 1332 Hashtable table = new Hashtable (1); 1336 table.put(key, value); 1337 construct(domain, table); 1338 } 1339 1340 1356 public ObjectName(String domain, Hashtable table) 1357 throws MalformedObjectNameException , NullPointerException { 1358 construct(domain, table); 1359 } 1360 1361 1363 1364 1366 1373 public boolean isPattern() { 1374 return (_domain_pattern || _property_pattern); 1375 } 1376 1377 1384 public boolean isDomainPattern() { 1385 return _domain_pattern; 1386 } 1387 1388 1393 public boolean isPropertyPattern() { 1394 return _property_pattern; 1395 } 1396 1397 1423 public String getCanonicalName() { 1424 return _canonicalName; 1425 } 1426 1427 1432 public String getDomain() { 1433 return _canonicalName.substring(0, _domain_length); 1434 } 1435 1436 1446 public String getKeyProperty(String property) throws NullPointerException { 1447 return (String ) _getKeyPropertyList().get(property); 1448 } 1449 1450 1460 private final Hashtable _getKeyPropertyList() { 1461 synchronized (this) { 1462 if (_propertyList == null) { 1463 _propertyList = new Hashtable (); 1466 int len = _ca_array.length; 1467 Property prop; 1468 for (int i = len - 1; i >= 0; i--) { 1469 prop = _ca_array[i]; 1470 _propertyList.put(prop.getKeyString(_canonicalName), 1471 prop.getValueString(_canonicalName)); 1472 } 1473 } 1474 } 1475 return _propertyList; 1476 } 1477 1478 1489 public Hashtable getKeyPropertyList() { 1490 return (Hashtable )_getKeyPropertyList().clone(); 1491 } 1492 1493 1503 public String getKeyPropertyListString() { 1504 if (_kp_array.length == 0) return ""; 1506 1507 final int total_size = _canonicalName.length() - _domain_length - 1 1510 - (_property_pattern?2:0); 1511 1512 final char[] dest_chars = new char[total_size]; 1513 final char[] value = _canonicalName.toCharArray(); 1514 writeKeyPropertyListString(value,dest_chars,0); 1515 return new String (dest_chars); 1516 } 1517 1518 1528 private String getSerializedNameString() { 1529 1530 final int total_size = _canonicalName.length(); 1532 final char[] dest_chars = new char[total_size]; 1533 final char[] value = _canonicalName.toCharArray(); 1534 final int offset = _domain_length+1; 1535 1536 System.arraycopy(value, 0, dest_chars, 0, offset); 1539 1540 final int end = writeKeyPropertyListString(value,dest_chars,offset); 1542 1543 if (_property_pattern) { 1545 if (end == offset) { 1546 dest_chars[end] = '*'; 1548 } else { 1549 dest_chars[end] = ','; 1551 dest_chars[end+1] = '*'; 1552 } 1553 } 1554 1555 return new String (dest_chars); 1556 } 1557 1558 1568 private int writeKeyPropertyListString(char[] canonicalChars, 1569 char[] data, int offset) { 1570 if (_kp_array.length == 0) return offset; 1571 1572 final char[] dest_chars = data; 1573 final char[] value = _canonicalName.toCharArray(); 1574 1575 int index = offset; 1576 final int len = _kp_array.length; 1577 final int last = len - 1; 1578 for (int i = 0; i < len; i++) { 1579 final Property prop = _kp_array[i]; 1580 final int prop_len = prop._key_length + prop._value_length + 1; 1581 System.arraycopy(value, prop._key_index, dest_chars, index, 1582 prop_len); 1583 index += prop_len; 1584 if (i < last ) dest_chars[index++] = ','; 1585 } 1586 return index; 1587 } 1588 1589 1590 1591 1602 public String getCanonicalKeyPropertyListString() { 1603 if (_ca_array.length == 0) return ""; 1604 1605 int len = _canonicalName.length(); 1606 if (_property_pattern) len -= 2; 1607 return _canonicalName.substring(_domain_length +1, len); 1608 } 1609 1611 1613 1621 public String toString() { 1622 return getSerializedNameString(); 1623 } 1624 1625 1637 public boolean equals(Object object) { 1638 1639 if (this == object) return true; 1641 1642 if (!(object instanceof ObjectName )) return false; 1644 1645 ObjectName on = (ObjectName ) object; 1648 String on_string = on._canonicalName; 1649 if (_canonicalName == on_string) return true; 1650 1651 return false; 1654 } 1655 1656 1660 public int hashCode() { 1661 return _canonicalName.hashCode(); 1662 } 1663 1664 1694 public static String quote(String s) 1695 throws NullPointerException { 1696 final StringBuffer buf = new StringBuffer ("\""); 1697 final int len = s.length(); 1698 for (int i = 0; i < len; i++) { 1699 char c = s.charAt(i); 1700 switch (c) { 1701 case '\n': 1702 c = 'n'; 1703 case '\\': 1705 case '\"': 1706 case '*': 1707 case '?': 1708 buf.append('\\'); 1709 break; 1710 } 1711 buf.append(c); 1712 } 1713 buf.append('"'); 1714 return buf.toString(); 1715 } 1716 1717 1739 public static String unquote(String q) 1740 throws IllegalArgumentException , NullPointerException { 1741 final StringBuffer buf = new StringBuffer (); 1742 final int len = q.length(); 1743 if (len < 2 || q.charAt(0) != '"' || q.charAt(len - 1) != '"') 1744 throw new IllegalArgumentException ("Argument not quoted"); 1745 for (int i = 1; i < len - 1; i++) { 1746 char c = q.charAt(i); 1747 if (c == '\\') { 1748 if (i == len - 2) 1749 throw new IllegalArgumentException ("Trailing backslash"); 1750 c = q.charAt(++i); 1751 switch (c) { 1752 case 'n': 1753 c = '\n'; 1754 break; 1755 case '\\': 1756 case '\"': 1757 case '*': 1758 case '?': 1759 break; 1760 default: 1761 throw new IllegalArgumentException ( 1762 "Bad character '" + c + "' after backslash"); 1763 } 1764 } 1765 else { 1766 switch (c) { 1767 case '*' : 1768 case '?' : 1769 case '\"': 1770 case '\n': 1771 throw new IllegalArgumentException ( 1772 "Invalid unescaped character '" + c + 1773 "' in the string to unquote"); 1774 default : ; 1775 } 1776 } 1777 buf.append(c); 1778 } 1779 return buf.toString(); 1780 } 1781 1782 1784 1786 1787 1805 public boolean apply(ObjectName name) throws NullPointerException { 1806 1807 if (name == null) throw new NullPointerException (); 1808 1809 if (name._domain_pattern || name._property_pattern) 1810 return false; 1811 1812 if (!_domain_pattern && !_property_pattern) 1814 return _canonicalName.equals(name._canonicalName); 1815 1816 return matchDomains(name) && matchKeys(name); 1817 } 1818 1819 private final boolean matchDomains(ObjectName name) { 1820 if (_domain_pattern) { 1821 final char[] dom_pattern = getDomain().toCharArray(); 1823 final char[] dom_string = name.getDomain().toCharArray(); 1824 return wildmatch(dom_string,dom_pattern,0,0); 1825 } 1826 return getDomain().equals(name.getDomain()); 1827 } 1828 1829 private final boolean matchKeys(ObjectName name) { 1830 if (_property_pattern) { 1831 final Hashtable nameProps = name._getKeyPropertyList(); 1833 final Property[] props=_ca_array; 1834 final String cn=_canonicalName; 1835 for (int i= props.length -1; i >= 0 ; i--) { 1836 1837 1840 final Property p = props[i]; 1841 final String k = p.getKeyString(cn); 1842 final String v = (String )nameProps.get(k); 1843 1844 if (v == null) return false; 1846 1847 if (v.equals(p.getValueString(cn))) continue; 1850 return false; 1851 } 1852 return true; 1853 } 1854 final String p1 = name.getCanonicalKeyPropertyListString(); 1855 final String p2 = getCanonicalKeyPropertyListString(); 1856 return (p1.equals(p2)); 1857 } 1858 1859 1863 public void setMBeanServer(MBeanServer mbs) { } 1864 1865 1867 1869} 1870 | Popular Tags |