1 7 25 26 package javax.management.modelmbean; 27 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.ObjectStreamField ; 32 33 import java.lang.reflect.Constructor ; 34 35 import java.security.AccessController ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import java.util.StringTokenizer ; 41 42 import javax.management.RuntimeOperationsException ; 43 import javax.management.MBeanException ; 44 45 import com.sun.jmx.mbeanserver.GetPropertyAction; 46 47 import com.sun.jmx.trace.Trace; 48 import java.util.Collections ; 49 import java.util.SortedMap ; 50 import java.util.TreeMap ; 51 52 import sun.reflect.misc.ReflectUtil; 53 54 73 74 public class DescriptorSupport 75 implements javax.management.Descriptor 76 { 77 78 private static final long oldSerialVersionUID = 8071560848919417985L; 86 private static final long newSerialVersionUID = -6292969195866300415L; 89 private static final ObjectStreamField [] oldSerialPersistentFields = 92 { 93 new ObjectStreamField ("descriptor", HashMap .class), 94 new ObjectStreamField ("currClass", String .class) 95 }; 96 private static final ObjectStreamField [] newSerialPersistentFields = 99 { 100 new ObjectStreamField ("descriptor", HashMap .class) 101 }; 102 private static final long serialVersionUID; 105 108 private static final ObjectStreamField [] serialPersistentFields; 109 private static final String serialForm; 110 static { 111 String form = null; 112 boolean compat = false; 113 try { 114 GetPropertyAction act = new GetPropertyAction("jmx.serial.form"); 115 form = (String ) AccessController.doPrivileged(act); 116 compat = "1.0".equals(form); } catch (Exception e) { 118 } 120 serialForm = form; 121 if (compat) { 122 serialPersistentFields = oldSerialPersistentFields; 123 serialVersionUID = oldSerialVersionUID; 124 } else { 125 serialPersistentFields = newSerialPersistentFields; 126 serialVersionUID = newSerialVersionUID; 127 } 128 } 129 132 144 private transient SortedMap <String , Object > descriptorMap; 145 146 private static final int DEFAULT_SIZE = 20; 147 private static final String currClass = "DescriptorSupport"; 148 149 150 156 public DescriptorSupport() { 157 if (tracing()) 158 trace("DescriptorSupport()", "Constructor"); 159 init(null); 160 } 161 162 176 public DescriptorSupport(int initNumFields) 177 throws MBeanException , RuntimeOperationsException { 178 if (tracing()) { 179 trace("Descriptor(initNumFields=" + initNumFields + ")", 180 "Constructor"); 181 } 182 if (initNumFields <= 0) { 183 if (tracing()) { 184 trace("Descriptor(maxNumFields)", 185 "Illegal arguments: initNumFields <= 0"); 186 } 187 final String msg = 188 "Descriptor field limit invalid: " + initNumFields; 189 final RuntimeException iae = new IllegalArgumentException (msg); 190 throw new RuntimeOperationsException (iae, msg); 191 } 192 init(null); 193 } 194 195 204 public DescriptorSupport(DescriptorSupport inDescr) { 205 if (tracing()) { 206 trace("Descriptor(Descriptor)","Constructor"); 207 } 208 if (inDescr == null) 209 init(null); 210 else 211 init(inDescr.descriptorMap); 212 } 213 214 215 242 247 public DescriptorSupport(String inStr) 248 throws MBeanException , RuntimeOperationsException , 249 XMLParseException { 250 252 if (tracing()) { 253 trace("Descriptor(String ='" + inStr + "')","Constructor"); 254 } 255 if (inStr == null) { 256 if (tracing()) { 257 trace("Descriptor(String = null)","Illegal arguments"); 258 } 259 final String msg = "String in parameter is null"; 260 final RuntimeException iae = new IllegalArgumentException (msg); 261 throw new RuntimeOperationsException (iae, msg); 262 } 263 264 final String lowerInStr = inStr.toLowerCase(); 265 if (!lowerInStr.startsWith("<descriptor>") 266 || !lowerInStr.endsWith("</descriptor>")) { 267 throw new XMLParseException ("No <descriptor>, </descriptor> pair"); 268 } 269 270 init(null); 272 276 StringTokenizer st = new StringTokenizer (inStr, "<> \t\n\r\f"); 277 278 boolean inFld = false; 279 boolean inDesc = false; 280 String fieldName = null; 281 String fieldValue = null; 282 283 284 while (st.hasMoreTokens()) { String tok = st.nextToken(); 286 287 if (tok.equalsIgnoreCase("FIELD")) { 288 inFld = true; 289 } else if (tok.equalsIgnoreCase("/FIELD")) { 290 if ((fieldName != null) && (fieldValue != null)) { 291 fieldName = 292 fieldName.substring(fieldName.indexOf('"') + 1, 293 fieldName.lastIndexOf('"')); 294 final Object fieldValueObject = 295 parseQuotedFieldValue(fieldValue); 296 setField(fieldName, fieldValueObject); 297 } 298 fieldName = null; 299 fieldValue = null; 300 inFld = false; 301 } else if (tok.equalsIgnoreCase("DESCRIPTOR")) { 302 inDesc = true; 303 } else if (tok.equalsIgnoreCase("/DESCRIPTOR")) { 304 inDesc = false; 305 fieldName = null; 306 fieldValue = null; 307 inFld = false; 308 } else if (inFld && inDesc) { 309 int eq_separator = tok.indexOf("="); 311 if (eq_separator > 0) { 312 String kwPart = tok.substring(0,eq_separator); 313 String valPart = tok.substring(eq_separator+1); 314 if (kwPart.equalsIgnoreCase("NAME")) 315 fieldName = valPart; 316 else if (kwPart.equalsIgnoreCase("VALUE")) 317 fieldValue = valPart; 318 else { final String msg = 320 "Expected `name' or `value', got `" + tok + "'"; 321 throw new XMLParseException (msg); 322 } 323 } else { final String msg = 325 "Expected `keyword=value', got `" + tok + "'"; 326 throw new XMLParseException (msg); 327 } 328 } 329 } 331 if (tracing()) { 332 trace("Descriptor(XMLString)","Exit"); 333 } 334 } 335 336 357 public DescriptorSupport(String [] fieldNames, Object [] fieldValues) 358 throws RuntimeOperationsException { 359 if (tracing()) { 360 trace("Descriptor(fieldNames, fieldObjects)","Constructor"); 361 } 362 363 if ((fieldNames == null) || (fieldValues == null) || 364 (fieldNames.length != fieldValues.length)) { 365 if (tracing()) { 366 trace("Descriptor(String[],Object[])","Illegal arguments"); 367 } 368 369 final String msg = 370 "Null or invalid fieldNames or fieldValues"; 371 final RuntimeException iae = new IllegalArgumentException (msg); 372 throw new RuntimeOperationsException (iae, msg); 373 } 374 375 376 init(null); 377 for (int i=0; i < fieldNames.length; i++) { 378 setField(fieldNames[i], fieldValues[i]); 381 } 382 if (tracing()) { 383 trace("Descriptor(fieldNames, fieldObjects)","Exit"); 384 } 385 } 386 387 411 public DescriptorSupport(String [] fields) 412 { 413 if (tracing()) { 414 trace("Descriptor(fields)","Constructor"); 415 } 416 init(null); 417 if (( fields == null ) || ( fields.length == 0)) 418 return; 419 420 init(null); 421 422 for (int i=0; i < fields.length; i++) { 423 if ((fields[i] == null) || (fields[i].equals(""))) { 424 continue; 425 } 426 int eq_separator = fields[i].indexOf("="); 427 if (eq_separator < 0) { 428 if (tracing()) { 430 trace("Descriptor(String[])", 431 "Illegal arguments: field does not have '=' " + 432 "as a name and value separator"); 433 } 434 final String msg = "Field in invalid format: no equals sign"; 435 final RuntimeException iae = new IllegalArgumentException (msg); 436 throw new RuntimeOperationsException (iae, msg); 437 } 438 439 String fieldName = fields[i].substring(0,eq_separator); 440 String fieldValue = null; 441 if (eq_separator < fields[i].length()) { 442 fieldValue = fields[i].substring(eq_separator+1); 444 } 445 446 if (fieldName.equals("")) { 447 if (tracing()) { 448 trace("Descriptor(String[])", 449 "Illegal arguments: fieldName is empty"); 450 } 451 452 final String msg = "Field in invalid format: no fieldName"; 453 final RuntimeException iae = new IllegalArgumentException (msg); 454 throw new RuntimeOperationsException (iae, msg); 455 } 456 457 setField(fieldName,fieldValue); 458 } 459 if (tracing()) { 460 trace("Descriptor(fields)","Exit"); 461 } 462 } 463 464 private void init(Map <String , ?> initMap) { 465 descriptorMap = 466 new TreeMap <String , Object >(String.CASE_INSENSITIVE_ORDER); 467 if (initMap != null) 468 descriptorMap.putAll(initMap); 469 } 470 471 473 474 485 public synchronized Object getFieldValue(String inFieldName) 486 throws RuntimeOperationsException { 487 488 if ((inFieldName == null) || (inFieldName.equals(""))) { 489 if (tracing()) { 490 trace("getField()","Illegal arguments: null field name."); 491 } 492 final String msg = "Fieldname requested is null"; 493 final RuntimeException iae = new IllegalArgumentException (msg); 494 throw new RuntimeOperationsException (iae, msg); 495 } 496 Object retValue = descriptorMap.get(inFieldName); 497 if (tracing()) { 498 trace("getField(" + inFieldName + ")", 499 "Returns '" + retValue + "'"); 500 } 501 return(retValue); 502 } 503 504 520 public synchronized void setField(String inFieldName, Object fieldValue) 521 throws RuntimeOperationsException { 522 523 if ((inFieldName == null) || (inFieldName.equals(""))) { 525 if (tracing()) { 526 trace("setField(String,String)", 527 "Illegal arguments: null or empty field name"); 528 } 529 530 final String msg = "Fieldname to be set is null or empty"; 531 final RuntimeException iae = new IllegalArgumentException (msg); 532 throw new RuntimeOperationsException (iae, msg); 533 } 534 535 if (!validateField(inFieldName, fieldValue)) { 536 if (tracing()) { 537 trace("setField(fieldName,FieldValue)","Illegal arguments"); 538 } 539 540 final String msg = 541 "Field value invalid: " + inFieldName + "=" + fieldValue; 542 final RuntimeException iae = new IllegalArgumentException (msg); 543 throw new RuntimeOperationsException (iae, msg); 544 } 545 546 if (tracing()) { 547 if (fieldValue != null) { 548 trace("setField(fieldName, fieldValue)", 549 "Entry: setting '" + inFieldName + "' to '" + 550 fieldValue + "'."); 551 } 552 } 553 554 descriptorMap.put(inFieldName, fieldValue); 558 } 559 560 573 public synchronized String [] getFields() { 574 if (tracing()) { 575 trace("getFields()","Entry"); 576 } 577 int numberOfEntries = descriptorMap.size(); 578 579 String [] responseFields = new String [numberOfEntries]; 580 Set returnedSet = descriptorMap.entrySet(); 581 582 int i = 0; 583 Object currValue = null; 584 Map.Entry currElement = null; 585 586 if (tracing()) { 587 trace("getFields()","Returning " + numberOfEntries + " fields"); 588 } 589 for (Iterator iter = returnedSet.iterator(); iter.hasNext(); i++) { 590 currElement = (Map.Entry ) iter.next(); 591 592 if (currElement == null) { 593 if (tracing()) { 594 trace("getFields()","Element is null"); 595 } 596 } else { 597 currValue = currElement.getValue(); 598 if (currValue == null) { 599 responseFields[i] = currElement.getKey() + "="; 600 } else { 601 if (currValue instanceof java.lang.String ) { 602 responseFields[i] = 603 currElement.getKey() + "=" + currValue.toString(); 604 } else { 605 responseFields[i] = 606 currElement.getKey() + "=(" + 607 currValue.toString() + ")"; 608 } 609 } 610 } 611 } 612 613 if (tracing()) { 614 trace("getFields()","Exit"); 615 } 616 617 return responseFields; 618 } 619 620 628 public synchronized String [] getFieldNames() { 629 if (tracing()) { 630 trace("getFieldNames()","Entry"); 631 } 632 int numberOfEntries = descriptorMap.size(); 633 634 String [] responseFields = new String [numberOfEntries]; 635 Set returnedSet = descriptorMap.entrySet(); 636 637 int i = 0; 638 639 if (tracing()) { 640 trace("getFieldNames()","Returning " + numberOfEntries + " fields"); 641 } 642 643 for (Iterator iter = returnedSet.iterator(); iter.hasNext(); i++) { 644 Map.Entry currElement = (Map.Entry ) iter.next(); 645 646 if (( currElement == null ) || (currElement.getKey() == null)) { 647 if (tracing()) { 648 trace("getFieldNames()","Field is null"); 649 } 650 } else { 651 responseFields[i] = currElement.getKey().toString(); 652 } 653 } 654 655 if (tracing()) { 656 trace("getFieldNames()","Exit"); 657 } 658 659 return responseFields; 660 } 661 662 663 679 public synchronized Object [] getFieldValues(String [] fieldNames) { 680 if (tracing()) { 681 trace("getFieldValues(fieldNames)","Entry"); 682 } 683 686 int numberOfEntries = descriptorMap.size(); 687 688 690 if (numberOfEntries == 0) 691 return new Object [0]; 692 693 Object [] responseFields; 694 if (fieldNames != null) { 695 responseFields = new Object [fieldNames.length]; 696 } else { 698 responseFields = new Object [numberOfEntries]; 699 } 701 702 int i = 0; 703 704 if (tracing()) { 705 trace("getFieldValues()", 706 "Returning " + numberOfEntries + " fields"); 707 } 708 709 if (fieldNames == null) { 710 for (Iterator iter = descriptorMap.values().iterator(); 711 iter.hasNext(); i++) 712 responseFields[i] = iter.next(); 713 } else { 714 for (i=0; i < fieldNames.length; i++) { 715 if ((fieldNames[i] == null) || (fieldNames[i].equals(""))) { 716 responseFields[i] = null; 717 } else { 718 responseFields[i] = getFieldValue(fieldNames[i]); 719 } 720 } 721 } 722 723 724 if (tracing()) { 725 trace("getFieldValues()","Exit"); 726 } 727 728 return responseFields; 729 } 730 731 751 public synchronized void setFields(String [] fieldNames, 752 Object [] fieldValues) 753 throws RuntimeOperationsException { 754 755 if (tracing()) { 756 trace("setFields(fieldNames, ObjectValues)","Entry"); 757 } 758 759 760 if ((fieldNames == null) || (fieldValues == null) || 761 (fieldNames.length != fieldValues.length)) { 762 if (tracing()) { 763 trace("Descriptor.setFields(String[],Object[])", 764 "Illegal arguments"); 765 } 766 767 final String msg = "FieldNames and FieldValues are null or invalid"; 768 final RuntimeException iae = new IllegalArgumentException (msg); 769 throw new RuntimeOperationsException (iae, msg); 770 } 771 772 for (int i=0; i < fieldNames.length; i++) { 773 if (( fieldNames[i] == null) || (fieldNames[i].equals(""))) { 774 if (tracing()) { 775 trace("Descriptor.setFields(String[],Object[])", 776 "Null field name encountered at " + i + " element"); 777 } 778 779 final String msg = "FieldNames is null or invalid"; 780 final RuntimeException iae = new IllegalArgumentException (msg); 781 throw new RuntimeOperationsException (iae, msg); 782 } 783 setField(fieldNames[i], fieldValues[i]); 784 } 785 if (tracing()) { 786 trace("Descriptor.setFields(fieldNames, fieldObjects)","Exit"); 787 } 788 } 789 790 797 798 public synchronized Object clone() throws RuntimeOperationsException { 799 if (tracing()) { 800 trace("Descriptor.clone()","Executed"); 801 } 802 return(new DescriptorSupport (this)); 803 } 804 805 811 public synchronized void removeField(String fieldName) { 812 if ((fieldName == null) || (fieldName.equals(""))) { 813 return; 814 } 815 816 descriptorMap.remove(fieldName); 817 } 818 819 820 854 855 public synchronized boolean isValid() throws RuntimeOperationsException { 856 if (tracing()) { 857 trace("Descriptor.isValid()","Executed"); 858 } 859 861 Set returnedSet = descriptorMap.entrySet(); 862 863 if (returnedSet == null) { if (tracing()) { 865 trace("Descriptor.isValid()","returns false (null set)"); 866 } 867 return false; 868 } 869 String thisName = (String )(this.getFieldValue("name")); 871 String thisDescType = (String )(getFieldValue("descriptorType")); 872 873 if ((thisName == null) || (thisDescType == null) || 874 (thisName.equals("")) || (thisDescType.equals(""))) { 875 return false; 876 } 877 878 880 for (Iterator iter = returnedSet.iterator(); iter.hasNext();) { 881 Map.Entry currElement = (Map.Entry ) iter.next(); 882 883 if (currElement != null) { 884 if (currElement.getValue() != null) { 885 if (validateField((currElement.getKey()).toString(), 887 (currElement.getValue()).toString())) { 888 continue; 889 } else { 890 if (tracing()) { 891 trace("isValid()", 892 "Field " + currElement.getKey() + "=" + 893 currElement.getValue() + " is not valid"); 894 } 895 return false; 896 } 897 } 898 } 899 } 900 901 903 if (tracing()) { 904 trace("Descriptor.isValid()","returns true"); 905 } 906 907 return true; 908 } 909 910 911 925 926 private boolean validateField(String fldName, Object fldValue) { 927 if ((fldName == null) || (fldName.equals(""))) 928 return false; 929 String SfldValue = ""; 930 boolean isAString = false; 931 if ((fldValue != null) && (fldValue instanceof java.lang.String )) { 932 SfldValue = (String ) fldValue; 933 isAString = true; 934 } 935 936 boolean nameOrDescriptorType = 937 (fldName.equalsIgnoreCase("Name") || 938 fldName.equalsIgnoreCase("DescriptorType")); 939 if (nameOrDescriptorType || 940 fldName.equalsIgnoreCase("SetMethod") || 941 fldName.equalsIgnoreCase("GetMethod") || 942 fldName.equalsIgnoreCase("Role") || 943 fldName.equalsIgnoreCase("Class")) { 944 if (fldValue == null || !isAString) 945 return false; 946 if (nameOrDescriptorType && SfldValue.equals("")) 947 return false; 948 return true; 949 } else if (fldName.equalsIgnoreCase("visibility")) { 950 long v; 951 if ((fldValue != null) && (isAString)) { 952 v = toNumeric(SfldValue); 953 } else if (fldValue instanceof java.lang.Integer ) { 954 v = ((Integer )fldValue).intValue(); 955 } else return false; 956 957 if (v >= 1 && v <= 4) 958 return true; 959 else 960 return false; 961 } else if (fldName.equalsIgnoreCase("severity")) { 962 963 long v; 964 if ((fldValue != null) && (isAString)) { 965 v = toNumeric(SfldValue); 966 } else if (fldValue instanceof java.lang.Integer ) { 967 v = ((Integer )fldValue).intValue(); 968 } else return false; 969 970 return (v >= 0 && v <= 6); 971 } else if (fldName.equalsIgnoreCase("PersistPolicy")) { 972 return (((fldValue != null) && (isAString)) && 973 ( SfldValue.equalsIgnoreCase("OnUpdate") || 974 SfldValue.equalsIgnoreCase("OnTimer") || 975 SfldValue.equalsIgnoreCase("NoMoreOftenThan") || 976 SfldValue.equalsIgnoreCase("Always") || 977 SfldValue.equalsIgnoreCase("Never") )); 978 } else if (fldName.equalsIgnoreCase("PersistPeriod") || 979 fldName.equalsIgnoreCase("CurrencyTimeLimit") || 980 fldName.equalsIgnoreCase("LastUpdatedTimeStamp") || 981 fldName.equalsIgnoreCase("LastReturnedTimeStamp")) { 982 983 long v; 984 if ((fldValue != null) && (isAString)) { 985 v = toNumeric(SfldValue); 986 } else if (fldValue instanceof java.lang.Number ) { 987 v = ((Number )fldValue).longValue(); 988 } else return false; 989 990 return (v >= -1); 991 } else if (fldName.equalsIgnoreCase("log")) { 992 return ((fldValue instanceof java.lang.Boolean ) || 993 (isAString && 994 (SfldValue.equalsIgnoreCase("T") || 995 SfldValue.equalsIgnoreCase("true") || 996 SfldValue.equalsIgnoreCase("F") || 997 SfldValue.equalsIgnoreCase("false") ))); 998 } 999 1000 return true; 1002 } 1003 1004 1005 1006 1033 public synchronized String toXMLString() { 1034 StringBuffer buf = new StringBuffer ("<Descriptor>"); 1035 Set returnedSet = descriptorMap.entrySet(); 1036 for (Iterator iter = returnedSet.iterator(); iter.hasNext(); ) { 1037 final Map.Entry currElement = (Map.Entry ) iter.next(); 1038 final String name = currElement.getKey().toString(); 1039 Object value = currElement.getValue(); 1040 String valueString = null; 1041 1046 if (value instanceof String ) { 1047 final String svalue = (String ) value; 1048 if (!svalue.startsWith("(") || !svalue.endsWith(")")) 1049 valueString = quote(svalue); 1050 } 1051 if (valueString == null) 1052 valueString = makeFieldValue(value); 1053 buf.append("<field name=\"").append(name).append("\" value=\"") 1054 .append(valueString).append("\"></field>"); 1055 } 1056 buf.append("</Descriptor>"); 1057 return buf.toString(); 1058 } 1059 1060 private static final String [] entities = { 1061 "  ", 1062 "\""", 1063 "<<", 1064 ">>", 1065 "&&", 1066 "\r ", 1067 "\t	", 1068 "\n ", 1069 "\f", 1070 }; 1071 private static final Map <String ,Character > entityToCharMap = 1072 new HashMap <String ,Character >(); 1073 private static final String [] charToEntityMap; 1074 1075 static { 1076 char maxChar = 0; 1077 for (int i = 0; i < entities.length; i++) { 1078 final char c = entities[i].charAt(0); 1079 if (c > maxChar) 1080 maxChar = c; 1081 } 1082 charToEntityMap = new String [maxChar + 1]; 1083 for (int i = 0; i < entities.length; i++) { 1084 final char c = entities[i].charAt(0); 1085 final String entity = entities[i].substring(1); 1086 charToEntityMap[c] = entity; 1087 entityToCharMap.put(entity, new Character (c)); 1088 } 1089 } 1090 1091 private static boolean isMagic(char c) { 1092 return (c < charToEntityMap.length && charToEntityMap[c] != null); 1093 } 1094 1095 1102 private static String quote(String s) { 1103 boolean found = false; 1104 for (int i = 0; i < s.length(); i++) { 1105 if (isMagic(s.charAt(i))) { 1106 found = true; 1107 break; 1108 } 1109 } 1110 if (!found) 1111 return s; 1112 StringBuffer buf = new StringBuffer (); 1113 for (int i = 0; i < s.length(); i++) { 1114 char c = s.charAt(i); 1115 if (isMagic(c)) 1116 buf.append(charToEntityMap[c]); 1117 else 1118 buf.append(c); 1119 } 1120 return buf.toString(); 1121 } 1122 1123 private static String unquote(String s) throws XMLParseException { 1124 if (!s.startsWith("\"") || !s.endsWith("\"")) 1125 throw new XMLParseException ("Value must be quoted: <" + s + ">"); 1126 StringBuffer buf = new StringBuffer (); 1127 final int len = s.length() - 1; 1128 for (int i = 1; i < len; i++) { 1129 final char c = s.charAt(i); 1130 final int semi; 1131 final Character quoted; 1132 if (c == '&' 1133 && (semi = s.indexOf(';', i + 1)) >= 0 1134 && ((quoted = 1135 (Character ) entityToCharMap.get(s.substring(i, semi+1))) 1136 != null)) { 1137 buf.append(quoted); 1138 i = semi; 1139 } else 1140 buf.append(c); 1141 } 1142 return buf.toString(); 1143 } 1144 1145 1150 private static String makeFieldValue(Object value) { 1151 if (value == null) 1152 return "(null)"; 1153 1154 Class valueClass = value.getClass(); 1155 try { 1156 valueClass.getConstructor(new Class [] {String .class}); 1157 } catch (NoSuchMethodException e) { 1158 final String msg = 1159 "Class " + valueClass + " does not have a public " + 1160 "constructor with a single string arg"; 1161 final RuntimeException iae = new IllegalArgumentException (msg); 1162 throw new RuntimeOperationsException (iae, 1163 "Cannot make XML descriptor"); 1164 } catch (SecurityException e) { 1165 } 1169 1170 final String quotedValueString = quote(value.toString()); 1171 1172 return "(" + valueClass.getName() + "/" + quotedValueString + ")"; 1173 } 1174 1175 1187 private static Object parseQuotedFieldValue(String s) 1188 throws XMLParseException { 1189 s = unquote(s); 1190 if (s.equalsIgnoreCase("(null)")) 1191 return null; 1192 if (!s.startsWith("(") || !s.endsWith(")")) 1193 return s; 1194 final int slash = s.indexOf('/'); 1195 if (slash < 0) { 1196 return s.substring(1, s.length() - 1); 1198 } 1199 final String className = s.substring(1, slash); 1200 final Constructor constr; 1201 try { 1202 final ClassLoader contextClassLoader = 1203 Thread.currentThread().getContextClassLoader(); 1204 if (contextClassLoader == null) 1205 ReflectUtil.checkPackageAccess(className); 1206 final Class c = 1207 Class.forName(className, false, contextClassLoader); 1208 constr = c.getConstructor(new Class [] {String .class}); 1209 } catch (Exception e) { 1210 throw new XMLParseException (e, 1211 "Cannot parse value: <" + s + ">"); 1212 } 1213 final String arg = s.substring(slash + 1, s.length() - 1); 1214 try { 1215 return constr.newInstance(new Object [] {arg}); 1216 } catch (Exception e) { 1217 final String msg = 1218 "Cannot construct instance of " + className + 1219 " with arg: <" + s + ">"; 1220 throw new XMLParseException (e, msg); 1221 } 1222 } 1223 1224 1240 public synchronized String toString() { 1241 if (tracing()) { 1242 trace("Descriptor.toString()","Entry"); 1243 } 1244 1245 String respStr = ""; 1246 String [] fields = getFields(); 1247 1248 if (tracing()) { 1249 trace("Descriptor.toString()", 1250 "Printing " + fields.length + " fields"); 1251 } 1252 1253 if ((fields == null) || (fields.length == 0)) { 1254 if (tracing()) { 1255 trace("Descriptor.toString()","Empty Descriptor"); 1256 } 1257 return respStr; 1258 } 1259 1260 for (int i=0; i < fields.length; i++) { 1261 if (i == (fields.length - 1)) { 1262 respStr = respStr.concat(fields[i]); 1263 } else { 1264 respStr = respStr.concat(fields[i] + ", "); 1265 } 1266 } 1267 1268 if (tracing()) { 1269 trace("Descriptor.toString()","Exit returning " + respStr); 1270 } 1271 1272 return respStr; 1273 } 1274 1275 1277 private long toNumeric(String inStr) { 1278 long result = -2; 1279 try { 1280 result = java.lang.Long.parseLong(inStr); 1281 } catch (Exception e) { 1282 return -2; 1283 } 1284 return result; 1285 } 1286 1287 1288 1290 private boolean tracing() { 1291 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN); 1292 } 1293 1294 private void trace(String inClass, String inMethod, String inText) { 1295 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass, 1296 inMethod, 1297 Integer.toHexString(this.hashCode()) + " " + inText); 1298 } 1299 1300 private void trace(String inMethod, String inText) { 1301 trace(currClass, inMethod, inText); 1302 } 1303 1304 1308 private void readObject(ObjectInputStream in) 1309 throws IOException , ClassNotFoundException { 1310 ObjectInputStream.GetField fields = in.readFields(); 1311 Map descriptor = (Map ) fields.get("descriptor", null); 1312 init(null); 1313 descriptorMap.putAll(descriptor); 1314 } 1315 1316 1317 1320 1331 private void writeObject(ObjectOutputStream out) throws IOException { 1332 ObjectOutputStream.PutField fields = out.putFields(); 1333 boolean compat = "1.0".equals(serialForm); 1334 if (compat) 1335 fields.put("currClass", currClass); 1336 1337 1343 SortedMap <String , Object > startMap = descriptorMap; 1344 if (startMap.containsKey("targetObject")) { 1345 startMap = new TreeMap <String , Object >(descriptorMap); 1346 startMap.remove("targetObject"); 1347 } 1348 1349 final HashMap <String , Object > descriptor; 1350 if (compat || "1.2.0".equals(serialForm) || 1351 "1.2.1".equals(serialForm)) { 1352 descriptor = new HashMap <String , Object >(); 1353 for (Map.Entry <String , Object > entry : startMap.entrySet()) 1354 descriptor.put(entry.getKey().toLowerCase(), entry.getValue()); 1355 } else 1356 descriptor = new HashMap <String , Object >(startMap); 1357 1358 fields.put("descriptor", descriptor); 1359 out.writeFields(); 1360 } 1361} 1362 | Popular Tags |