| 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 |