1. /*
  2. * @(#)file DescriptorSupport.java
  3. * @(#)author IBM Corp.
  4. * @(#)version 1.48
  5. * @(#)lastedit 04/03/26
  6. */
  7. /*
  8. * Copyright IBM Corp. 1999-2000. All rights reserved.
  9. *
  10. * The program is provided "as is" without any warranty express or implied,
  11. * including the warranty of non-infringement and the implied warranties of
  12. * merchantibility and fitness for a particular purpose. IBM will not be
  13. * liable for any damages suffered by you or any third party claim against
  14. * you regarding the Program.
  15. *
  16. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  17. * This software is the proprietary information of Sun Microsystems, Inc.
  18. * Use is subject to license terms.
  19. *
  20. * Copyright 2004 Sun Microsystems, Inc. Tous droits reserves.
  21. * Ce logiciel est propriete de Sun Microsystems, Inc.
  22. * Distribue par des licences qui en restreignent l'utilisation.
  23. *
  24. */
  25. package javax.management.modelmbean;
  26. import java.io.IOException;
  27. import java.io.ObjectInputStream;
  28. import java.io.ObjectOutputStream;
  29. import java.io.ObjectStreamField;
  30. import java.lang.reflect.Constructor;
  31. import java.security.AccessController;
  32. import java.security.PrivilegedAction;
  33. import java.util.BitSet;
  34. import java.util.HashMap;
  35. import java.util.Iterator;
  36. import java.util.Map;
  37. import java.util.Set;
  38. import java.util.StringTokenizer;
  39. import javax.management.RuntimeOperationsException;
  40. import javax.management.MBeanException;
  41. import javax.management.Descriptor;
  42. import com.sun.jmx.mbeanserver.GetPropertyAction;
  43. import com.sun.jmx.trace.Trace;
  44. /**
  45. * This class represents the metadata set for a ModelMBean element. A
  46. * descriptor is part of the ModelMBeanInfo,
  47. * ModelMBeanNotificationInfo, ModelMBeanAttributeInfo,
  48. * ModelMBeanConstructorInfo, and ModelMBeanParameterInfo.
  49. * <P>
  50. * A descriptor consists of a collection of fields. Each field is in
  51. * fieldname=fieldvalue format. Field names are not case sensitive,
  52. * case will be preserved on field values.
  53. * <P>
  54. * All field names and values are not predefined. New fields can be
  55. * defined and added by any program. Some fields have been predefined
  56. * for consistency of implementation and support by the
  57. * ModelMBeanInfo, ModelMBeanAttributeInfo, ModelMBeanConstructorInfo,
  58. * ModelMBeanNotificationInfo, ModelMBeanOperationInfo and ModelMBean
  59. * classes.
  60. *
  61. * @since 1.5
  62. */
  63. public class DescriptorSupport
  64. implements javax.management.Descriptor
  65. {
  66. // Serialization compatibility stuff:
  67. // Two serial forms are supported in this class. The selected form depends
  68. // on system property "jmx.serial.form":
  69. // - "1.0" for JMX 1.0
  70. // - any other value for JMX 1.1 and higher
  71. //
  72. // Serial version for old serial form
  73. private static final long oldSerialVersionUID = 8071560848919417985L;
  74. //
  75. // Serial version for new serial form
  76. private static final long newSerialVersionUID = -6292969195866300415L;
  77. //
  78. // Serializable fields in old serial form
  79. private static final ObjectStreamField[] oldSerialPersistentFields =
  80. {
  81. new ObjectStreamField("descriptor", HashMap.class),
  82. new ObjectStreamField("currClass", String.class)
  83. };
  84. //
  85. // Serializable fields in new serial form
  86. private static final ObjectStreamField[] newSerialPersistentFields =
  87. {
  88. new ObjectStreamField("descriptor", HashMap.class)
  89. };
  90. //
  91. // Actual serial version and serial form
  92. private static final long serialVersionUID;
  93. /**
  94. * @serialField descriptor HashMap The collection of fields representing this descriptor
  95. */
  96. private static final ObjectStreamField[] serialPersistentFields;
  97. private static final String serialForm;
  98. static {
  99. String form = null;
  100. boolean compat = false;
  101. try {
  102. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  103. form = (String) AccessController.doPrivileged(act);
  104. compat = "1.0".equals(form); // form may be null
  105. } catch (Exception e) {
  106. // OK: No compat with 1.0
  107. }
  108. serialForm = form;
  109. if (compat) {
  110. serialPersistentFields = oldSerialPersistentFields;
  111. serialVersionUID = oldSerialVersionUID;
  112. } else {
  113. serialPersistentFields = newSerialPersistentFields;
  114. serialVersionUID = newSerialVersionUID;
  115. }
  116. }
  117. //
  118. // END Serialization compatibility stuff
  119. /* Spec says that field names are case-insensitive, but that case
  120. is preserved. This means that we need to be able to map from a
  121. name that may differ in case to the actual name that is used in
  122. the HashMap. Thus, descriptorMap is indexed by CaseIgnoreString
  123. rather than plain String.
  124. Previous versions of this class had a field called "descriptor"
  125. of type HashMap where the keys were directly Strings. This is
  126. hard to reconcile with the required semantics, so we fabricate
  127. that field virtually during serialization and deserialization
  128. but keep the real information in descriptorMap.
  129. */
  130. private transient Map/*<CaseIgnoreString,Object>*/ descriptorMap;
  131. private static final int DEFAULT_SIZE = 20;
  132. private static final String currClass = "DescriptorSupport";
  133. /**
  134. * Descriptor default constructor.
  135. * Default initial descriptor size is 20. It will grow as needed.<br>
  136. * Note that the created empty descriptor is not a valid descriptor
  137. * (the method {@link #isValid isValid} returns <CODE>false</CODE>)
  138. */
  139. public DescriptorSupport()
  140. {
  141. if (tracing())
  142. {
  143. trace("Descriptor()","Constructor");
  144. }
  145. descriptorMap = new HashMap(DEFAULT_SIZE);
  146. }
  147. /**
  148. * Descriptor constructor. Takes as parameter the initial
  149. * capacity of the Map that stores the descriptor fields.
  150. * Capacity will grow as needed.<br> Note that the created empty
  151. * descriptor is not a valid descriptor (the method {@link
  152. * #isValid isValid} returns <CODE>false</CODE>).
  153. *
  154. * @param initNumFields The initial capacity of the Map that
  155. * stores the descriptor fields.
  156. *
  157. * @exception RuntimeOperationsException for illegal value for
  158. * initNumFields (<= 0)
  159. * @exception MBeanException Wraps a distributed communication Exception.
  160. */
  161. public DescriptorSupport(int initNumFields)
  162. throws MBeanException, RuntimeOperationsException {
  163. if (tracing()) {
  164. trace("Descriptor(maxNumFields = " + initNumFields + ")",
  165. "Constructor");
  166. }
  167. if (initNumFields <= 0) {
  168. if (tracing()) {
  169. trace("Descriptor(maxNumFields)",
  170. "Illegal arguments: initNumFields <= 0");
  171. }
  172. final String msg =
  173. "Descriptor field limit invalid: " + initNumFields;
  174. final RuntimeException iae = new IllegalArgumentException(msg);
  175. throw new RuntimeOperationsException(iae, msg);
  176. }
  177. descriptorMap = new HashMap(initNumFields);
  178. }
  179. /**
  180. * Descriptor constructor taking a Descriptor as parameter.
  181. * Creates a new descriptor initialized to the values of the
  182. * descriptor passed in parameter.
  183. *
  184. * @param inDescr the descriptor to be used to initialize the
  185. * constructed descriptor. If it is null or contains no descriptor
  186. * fields, an empty Descriptor will be created.
  187. */
  188. public DescriptorSupport(DescriptorSupport inDescr) {
  189. if (tracing()) {
  190. trace("Descriptor(Descriptor)","Constructor");
  191. }
  192. if (inDescr == null) {
  193. descriptorMap = new HashMap(DEFAULT_SIZE);
  194. } else {
  195. descriptorMap = new HashMap(inDescr.descriptorMap);
  196. }
  197. }
  198. /**
  199. * <p>Descriptor constructor taking an XML String.</p>
  200. *
  201. * <p>The format of the XML string is not defined, but an
  202. * implementation must ensure that the string returned by
  203. * {@link #toXMLString() toXMLString()} on an existing
  204. * descriptor can be used to instantiate an equivalent
  205. * descriptor using this constructor.</p>
  206. *
  207. * <p>In this implementation, all field values will be created
  208. * as Strings. If the field values are not Strings, the
  209. * programmer will have to reset or convert these fields
  210. * correctly.</p>
  211. *
  212. * @param inStr An XML-formatted string used to populate this
  213. * Descriptor. The format is not defined, but any
  214. * implementation must ensure that the string returned by
  215. * method {@link #toXMLString toXMLString} on an existing
  216. * descriptor can be used to instantiate an equivalent
  217. * descriptor when instantiated using this constructor.
  218. *
  219. * @exception RuntimeOperationsException If the String inStr
  220. * passed in parameter is null
  221. * @exception XMLParseException XML parsing problem while parsing
  222. * the input String
  223. * @exception MBeanException Wraps a distributed communication Exception.
  224. */
  225. /* At some stage we should rewrite this code to be cleverer. Using
  226. a StringTokenizer as we do means, first, that we accept a lot of
  227. bogus strings without noticing they are bogus, and second, that we
  228. split the string being parsed at characters like > even if they
  229. occur in the middle of a field value. */
  230. public DescriptorSupport(String inStr)
  231. throws MBeanException, RuntimeOperationsException,
  232. XMLParseException {
  233. /* parse an XML-formatted string and populate internal
  234. * structure with it */
  235. if (tracing()) {
  236. trace("Descriptor(String ='" + inStr + "')","Constructor");
  237. }
  238. if (inStr == null) {
  239. if (tracing()) {
  240. trace("Descriptor(String = null)","Illegal arguments");
  241. }
  242. final String msg = "String in parameter is null";
  243. final RuntimeException iae = new IllegalArgumentException(msg);
  244. throw new RuntimeOperationsException(iae, msg);
  245. }
  246. final String lowerInStr = inStr.toLowerCase();
  247. if (!lowerInStr.startsWith("<descriptor>")
  248. || !lowerInStr.endsWith("</descriptor>")) {
  249. throw new XMLParseException("No <descriptor>, </descriptor> pair");
  250. }
  251. // parse xmlstring into structures
  252. descriptorMap = new HashMap(DEFAULT_SIZE);
  253. // create dummy descriptor: should have same size
  254. // as number of fields in xmlstring
  255. // loop through structures and put them in descriptor
  256. StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f");
  257. boolean inFld = false;
  258. boolean inDesc = false;
  259. String fieldName = null;
  260. String fieldValue = null;
  261. while (st.hasMoreTokens()) { // loop through tokens
  262. String tok = st.nextToken();
  263. if (tok.equalsIgnoreCase("FIELD")) {
  264. inFld = true;
  265. } else if (tok.equalsIgnoreCase("/FIELD")) {
  266. if ((fieldName != null) && (fieldValue != null)) {
  267. fieldName =
  268. fieldName.substring(fieldName.indexOf('"') + 1,
  269. fieldName.lastIndexOf('"'));
  270. final Object fieldValueObject =
  271. parseQuotedFieldValue(fieldValue);
  272. setField(fieldName, fieldValueObject);
  273. }
  274. fieldName = null;
  275. fieldValue = null;
  276. inFld = false;
  277. } else if (tok.equalsIgnoreCase("DESCRIPTOR")) {
  278. inDesc = true;
  279. } else if (tok.equalsIgnoreCase("/DESCRIPTOR")) {
  280. inDesc = false;
  281. fieldName = null;
  282. fieldValue = null;
  283. inFld = false;
  284. } else if (inFld && inDesc) {
  285. // want kw=value, eg, name="myname" value="myvalue"
  286. int eq_separator = tok.indexOf("=");
  287. if (eq_separator > 0) {
  288. String kwPart = tok.substring(0,eq_separator);
  289. String valPart = tok.substring(eq_separator+1);
  290. if (kwPart.equalsIgnoreCase("NAME"))
  291. fieldName = valPart;
  292. else if (kwPart.equalsIgnoreCase("VALUE"))
  293. fieldValue = valPart;
  294. else { // xml parse exception
  295. final String msg =
  296. "Expected `name' or `value', got `" + tok + "'";
  297. throw new XMLParseException(msg);
  298. }
  299. } else { // xml parse exception
  300. final String msg =
  301. "Expected `keyword=value', got `" + tok + "'";
  302. throw new XMLParseException(msg);
  303. }
  304. }
  305. } // while tokens
  306. if (tracing()) {
  307. trace("Descriptor(XMLString)","Exit");
  308. }
  309. }
  310. /**
  311. * Constructor taking field names and field values. The array and
  312. * array elements cannot be null.
  313. *
  314. * @param fieldNames String array of field names. No elements of
  315. * this array can be null.
  316. * @param fieldValues Object array of the corresponding field
  317. * values. Elements of the array can be null. The
  318. * <code>fieldValue</code> must be valid for the
  319. * <code>fieldName</code> (as defined in method {@link #isValid
  320. * isValid})
  321. *
  322. * <p>Note: array sizes of parameters should match. If both arrays
  323. * are null or empty, then an empty descriptor is created.</p>
  324. *
  325. * @exception RuntimeOperationsException for illegal value for
  326. * field Names or field Values. The array lengths must be equal.
  327. * If the descriptor construction fails for any reason, this
  328. * exception will be thrown.
  329. *
  330. */
  331. public DescriptorSupport(String[] fieldNames, Object[] fieldValues)
  332. throws RuntimeOperationsException {
  333. if (tracing()) {
  334. trace("Descriptor(fieldNames, fieldObjects)","Constructor");
  335. }
  336. if ((fieldNames == null) || (fieldValues == null) ||
  337. (fieldNames.length != fieldValues.length)) {
  338. if (tracing()) {
  339. trace("Descriptor(String[],Object[])","Illegal arguments");
  340. }
  341. final String msg =
  342. "Null or invalid fieldNames or fieldValues";
  343. final RuntimeException iae = new IllegalArgumentException(msg);
  344. throw new RuntimeOperationsException(iae, msg);
  345. }
  346. /* populate internal structure with fields */
  347. descriptorMap = new HashMap(fieldNames.length);
  348. // a field value can be "null"
  349. for (int i=0; i < fieldNames.length; i++) {
  350. // setField will throw an exception if a fieldName is be null.
  351. // the fieldName and fieldValue will be validated in setField.
  352. setField(fieldNames[i], fieldValues[i]);
  353. }
  354. if (tracing()) {
  355. trace("Descriptor(fieldNames, fieldObjects)","Exit");
  356. }
  357. }
  358. /**
  359. * Constructor taking fields in the <i>fieldName=fieldValue</i>
  360. * format.
  361. *
  362. * @param fields String array with each element containing a
  363. * field name and value. If this array is null or empty, then the
  364. * default constructor will be executed. Null strings or empty
  365. * strings will be ignored.
  366. *
  367. * <p>All field values should be Strings. If the field values are
  368. * not Strings, the programmer will have to reset or convert these
  369. * fields correctly.
  370. *
  371. * <p>Note: Each string should be of the form
  372. * <i>fieldName=fieldValue</i>.
  373. *
  374. * @exception RuntimeOperationsException for illegal value for
  375. * field Names or field Values. The field must contain an
  376. * "=". "=fieldValue", "fieldName", and "fieldValue" are illegal.
  377. * FieldName cannot be null. "fieldName=" will cause the value to
  378. * be null. If the descriptor construction fails for any reason,
  379. * this exception will be thrown.
  380. *
  381. */
  382. public DescriptorSupport(String[] fields)
  383. {
  384. if (tracing()) {
  385. trace("Descriptor(fields)","Constructor");
  386. }
  387. if (( fields == null ) || ( fields.length == 0)) {
  388. descriptorMap = new HashMap(DEFAULT_SIZE);
  389. return;
  390. }
  391. descriptorMap = new HashMap(fields.length);
  392. for (int i=0; i < fields.length; i++) {
  393. if ((fields[i] == null) || (fields[i].equals(""))) {
  394. continue;
  395. }
  396. int eq_separator = fields[i].indexOf("=");
  397. if (eq_separator < 0) {
  398. // illegal if no = or is first character
  399. if (tracing()) {
  400. trace("Descriptor(String[])",
  401. "Illegal arguments: field does not have '=' " +
  402. "as a name and value separator");
  403. }
  404. final String msg = "Field in invalid format: no equals sign";
  405. final RuntimeException iae = new IllegalArgumentException(msg);
  406. throw new RuntimeOperationsException(iae, msg);
  407. }
  408. String fieldName = fields[i].substring(0,eq_separator);
  409. String fieldValue = null;
  410. if (eq_separator < fields[i].length()) {
  411. // = is not in last character
  412. fieldValue = fields[i].substring(eq_separator+1);
  413. }
  414. if (fieldName.equals("")) {
  415. if (tracing()) {
  416. trace("Descriptor(String[])",
  417. "Illegal arguments: fieldName is empty");
  418. }
  419. final String msg = "Field in invalid format: no fieldName";
  420. final RuntimeException iae = new IllegalArgumentException(msg);
  421. throw new RuntimeOperationsException(iae, msg);
  422. }
  423. setField(fieldName,fieldValue);
  424. }
  425. if (tracing()) {
  426. trace("Descriptor(fields)","Exit");
  427. }
  428. }
  429. // Implementation of the Descriptor interface
  430. /**
  431. * Returns the value for a specific fieldname.
  432. *
  433. * @param inFieldName The field name in question; if not found,
  434. * null is returned.
  435. *
  436. * @return An Object representing the field value
  437. *
  438. * @exception RuntimeOperationsException for illegal value (null
  439. * or empty string) for field Names.
  440. */
  441. public synchronized Object getFieldValue(String inFieldName)
  442. throws RuntimeOperationsException {
  443. if ((inFieldName == null) || (inFieldName.equals(""))) {
  444. if (tracing()) {
  445. trace("getField()","Illegal arguments: null field name.");
  446. }
  447. final String msg = "Fieldname requested is null";
  448. final RuntimeException iae = new IllegalArgumentException(msg);
  449. throw new RuntimeOperationsException(iae, msg);
  450. }
  451. Object retValue = descriptorMap.get(new CaseIgnoreString(inFieldName));
  452. if (tracing()) {
  453. trace("getField(" + inFieldName + ")",
  454. "Returns '" + retValue + "'");
  455. }
  456. return(retValue);
  457. }
  458. /**
  459. * Sets the string value for a specific fieldname. The value
  460. * must be valid for the field (as defined in method {@link
  461. * #isValid isValid}). If the field does not exist, it is
  462. * added at the end of the Descriptor. If it does exist, the
  463. * value is replaced.
  464. *
  465. * @param inFieldName The field name to be set. Must
  466. * not be null or empty string.
  467. * @param fieldValue The field value to be set for the field
  468. * name. Can be null or empty string.
  469. *
  470. * @exception RuntimeOperationsException for illegal value for
  471. * field Names.
  472. *
  473. */
  474. public synchronized void setField(String inFieldName, Object fieldValue)
  475. throws RuntimeOperationsException {
  476. // field name cannot be null or empty
  477. if ((inFieldName == null) || (inFieldName.equals(""))) {
  478. if (tracing()) {
  479. trace("setField(String,String)",
  480. "Illegal arguments: null or empty field name");
  481. }
  482. final String msg = "Fieldname to be set is null or empty";
  483. final RuntimeException iae = new IllegalArgumentException(msg);
  484. throw new RuntimeOperationsException(iae, msg);
  485. }
  486. if (!validateField(inFieldName, fieldValue)) {
  487. if (tracing()) {
  488. trace("setField(fieldName,FieldValue)","Illegal arguments");
  489. }
  490. final String msg =
  491. "Field value invalid: " + inFieldName + "=" + fieldValue;
  492. final RuntimeException iae = new IllegalArgumentException(msg);
  493. throw new RuntimeOperationsException(iae, msg);
  494. }
  495. if (tracing()) {
  496. if (fieldValue != null) {
  497. trace("setField(fieldName, fieldValue)",
  498. "Entry: setting '" + inFieldName + "' to '" +
  499. fieldValue + "'.");
  500. }
  501. }
  502. descriptorMap.put(new CaseIgnoreString(inFieldName), fieldValue);
  503. }
  504. /**
  505. * Returns all the fields in the descriptor. The order is not the
  506. * order in which the fields were set.
  507. *
  508. * @return String array of fields in the format
  509. * <i>fieldName=fieldValue</i>. If there are no fields in the
  510. * descriptor, then an empty String array is returned. If a
  511. * fieldValue is not a String then the toString() method is called
  512. * on it and its returned value is used as the value for the field
  513. * enclosed in parenthesis.
  514. *
  515. * @see #setFields
  516. */
  517. public synchronized String[] getFields() {
  518. if (tracing()) {
  519. trace("getFields()","Entry");
  520. }
  521. int numberOfEntries = descriptorMap.size();
  522. String[] responseFields = new String[numberOfEntries];
  523. Set returnedSet = descriptorMap.entrySet();
  524. int i = 0;
  525. Object currValue = null;
  526. Map.Entry currElement = null;
  527. if (tracing()) {
  528. trace("getFields()","Returning " + numberOfEntries + " fields");
  529. }
  530. for (Iterator iter = returnedSet.iterator(); iter.hasNext(); i++) {
  531. currElement = (Map.Entry) iter.next();
  532. if (currElement == null) {
  533. if (tracing()) {
  534. trace("getFields()","Element is null");
  535. }
  536. } else {
  537. currValue = currElement.getValue();
  538. if (currValue == null) {
  539. responseFields[i] = currElement.getKey() + "=";
  540. } else {
  541. if (currValue instanceof java.lang.String) {
  542. responseFields[i] =
  543. currElement.getKey() + "=" + currValue.toString();
  544. } else {
  545. responseFields[i] =
  546. currElement.getKey() + "=(" +
  547. currValue.toString() + ")";
  548. }
  549. }
  550. }
  551. }
  552. if (tracing()) {
  553. trace("getFields()","Exit");
  554. }
  555. return responseFields;
  556. }
  557. /**
  558. * Returns all the fields names in the descriptor. The order is
  559. * not the order in which the fields were set.
  560. *
  561. * @return String array of fields names. If the descriptor is
  562. * empty, you will get an empty array.
  563. *
  564. */
  565. public synchronized String[] getFieldNames() {
  566. if (tracing()) {
  567. trace("getFieldNames()","Entry");
  568. }
  569. int numberOfEntries = descriptorMap.size();
  570. String[] responseFields = new String[numberOfEntries];
  571. Set returnedSet = descriptorMap.entrySet();
  572. int i = 0;
  573. if (tracing()) {
  574. trace("getFieldNames()","Returning " + numberOfEntries + " fields");
  575. }
  576. for (Iterator iter = returnedSet.iterator(); iter.hasNext(); i++) {
  577. Map.Entry currElement = (Map.Entry) iter.next();
  578. if (( currElement == null ) || (currElement.getKey() == null)) {
  579. if (tracing()) {
  580. trace("getFieldNames()","Field is null");
  581. }
  582. } else {
  583. responseFields[i] = currElement.getKey().toString();
  584. }
  585. }
  586. if (tracing()) {
  587. trace("getFieldNames()","Exit");
  588. }
  589. return responseFields;
  590. }
  591. /**
  592. * Returns all the field values in the descriptor as an array of
  593. * Objects. The returned values are in the same order as the
  594. * fieldNames String array parameter.
  595. *
  596. * @param fieldNames String array of the names of the fields that
  597. * the values should be returned for.<br>
  598. * If the array is empty then an empty array will be returned.<br>
  599. * If the array is 'null' then all values will be returned. The
  600. * order is not the order in which the fields were set.<br>
  601. * If a field name in the array does not exist, then null is
  602. * returned for the matching array element being returned.
  603. *
  604. * @return Object array of field values. If the descriptor is
  605. * empty, you will get an empty array.
  606. */
  607. public synchronized Object[] getFieldValues(String[] fieldNames) {
  608. if (tracing()) {
  609. trace("getFieldValues(fieldNames)","Entry");
  610. }
  611. // if fieldNames == null return all values
  612. // if fieldNames is String[0] return no values
  613. int numberOfEntries = descriptorMap.size();
  614. /* Following test is somewhat inconsistent but is called for
  615. by the @return clause above. */
  616. if (numberOfEntries == 0)
  617. return new Object[0];
  618. Object[] responseFields;
  619. if (fieldNames != null) {
  620. responseFields = new Object[fieldNames.length];
  621. // room for selected
  622. } else {
  623. responseFields = new Object[numberOfEntries];
  624. // room for all
  625. }
  626. int i = 0;
  627. if (tracing()) {
  628. trace("getFieldValues()",
  629. "Returning " + numberOfEntries + " fields");
  630. }
  631. if (fieldNames == null) {
  632. for (Iterator iter = descriptorMap.values().iterator();
  633. iter.hasNext(); i++)
  634. responseFields[i] = iter.next();
  635. } else {
  636. for (i=0; i < fieldNames.length; i++) {
  637. if ((fieldNames[i] == null) || (fieldNames[i].equals(""))) {
  638. responseFields[i] = null;
  639. } else {
  640. responseFields[i] = getFieldValue(fieldNames[i]);
  641. }
  642. }
  643. }
  644. if (tracing()) {
  645. trace("getFieldValues()","Exit");
  646. }
  647. return responseFields;
  648. }
  649. /**
  650. * Sets all Fields in the list to the new value with the same
  651. * index in the fieldValue array. Array sizes must match. The
  652. * field value will be validated before it is set (by calling the
  653. * method {@link #isValid isValid}). If it is not valid, then an
  654. * exception will be thrown. If the arrays are empty, then no
  655. * change will take effect.
  656. *
  657. * @param fieldNames String array of field names. The array and
  658. * array elements cannot be null.
  659. * @param fieldValues Object array of the corresponding field
  660. * values. The array cannot be null. Elements of the array can
  661. * be null.
  662. *
  663. * @exception RuntimeOperationsException for illegal value for
  664. * field Names or field Values. Neither can be null. The array
  665. * lengths must be equal.
  666. *
  667. * @see #getFields
  668. */
  669. public synchronized void setFields(String[] fieldNames,
  670. Object[] fieldValues)
  671. throws RuntimeOperationsException {
  672. if (tracing()) {
  673. trace("setFields(fieldNames, ObjectValues)","Entry");
  674. }
  675. if ((fieldNames == null) || (fieldValues == null) ||
  676. (fieldNames.length != fieldValues.length)) {
  677. if (tracing()) {
  678. trace("Descriptor.setFields(String[],Object[])",
  679. "Illegal arguments");
  680. }
  681. final String msg = "FieldNames and FieldValues are null or invalid";
  682. final RuntimeException iae = new IllegalArgumentException(msg);
  683. throw new RuntimeOperationsException(iae, msg);
  684. }
  685. for (int i=0; i < fieldNames.length; i++) {
  686. if (( fieldNames[i] == null) || (fieldNames[i].equals(""))) {
  687. if (tracing()) {
  688. trace("Descriptor.setFields(String[],Object[])",
  689. "Null field name encountered at " + i + " element");
  690. }
  691. final String msg = "FieldNames is null or invalid";
  692. final RuntimeException iae = new IllegalArgumentException(msg);
  693. throw new RuntimeOperationsException(iae, msg);
  694. }
  695. setField(fieldNames[i], fieldValues[i]);
  696. }
  697. if (tracing()) {
  698. trace("Descriptor.setFields(fieldNames, fieldObjects)","Exit");
  699. }
  700. }
  701. /**
  702. * Returns a new Descriptor which is a duplicate of the Descriptor.
  703. *
  704. * @exception RuntimeOperationsException for illegal value for
  705. * field Names or field Values. If the descriptor construction
  706. * fails for any reason, this exception will be thrown.
  707. */
  708. public synchronized Object clone() throws RuntimeOperationsException {
  709. if (tracing()) {
  710. trace("Descriptor.clone()","Executed");
  711. }
  712. return(new DescriptorSupport(this));
  713. }
  714. /**
  715. * Removes a field from the descriptor.
  716. *
  717. * @param fieldName String name of the field to be removed.
  718. * If the field is not found no exception is thrown.
  719. */
  720. public synchronized void removeField(String fieldName) {
  721. if ((fieldName == null) || (fieldName.equals(""))) {
  722. return;
  723. }
  724. descriptorMap.remove(new CaseIgnoreString(fieldName));
  725. }
  726. /**
  727. * Returns true if all of the fields have legal values given their
  728. * names.
  729. * <P>
  730. * This implementation does not support interoperating with a directory
  731. * or lookup service. Thus, conforming to the specification, no checking is
  732. * done on the <i>"export"</i> field.
  733. * <P>
  734. * Otherwise this implementation returns false if:
  735. * <P>
  736. * <UL>
  737. * <LI> name and descriptorType fieldNames are not defined, or
  738. * null, or empty, or not String
  739. * <LI> class, role, getMethod, setMethod fieldNames, if defined,
  740. * are null or not String
  741. * <LI> persistPeriod, currencyTimeLimit, lastUpdatedTimeStamp,
  742. * lastReturnedTimeStamp if defined, are null, or not a Numeric
  743. * String or not a Numeric Value >= -1
  744. * <LI> log fieldName, if defined, is null, or not a Boolean or
  745. * not a String with value "t", "f", "true", "false". These String
  746. * values must not be case sensitive.
  747. * <LI> visibility fieldName, if defined, is null, or not a
  748. * Numeric String or a not Numeric Value >= 1 and <= 4
  749. * <LI> severity fieldName, if defined, is null, or not a Numeric
  750. * String or not a Numeric Value >= 0 and <= 6<br>
  751. * <LI> persistPolicy fieldName, if defined, is null, or not a
  752. * following String :<br>
  753. * "OnUpdate", "OnTimer", "NoMoreOftenThan", "Always",
  754. * "Never". These String values must not be case sensitive.<br>
  755. * </UL>
  756. *
  757. * @exception RuntimeOperationsException If the validity checking
  758. * fails for any reason, this exception will be thrown.
  759. */
  760. public synchronized boolean isValid() throws RuntimeOperationsException {
  761. if (tracing()) {
  762. trace("Descriptor.isValid()","Executed");
  763. }
  764. // verify that the descriptor is valid, by iterating over each field...
  765. Set returnedSet = descriptorMap.entrySet();
  766. if (returnedSet == null) { // null descriptor, not valid
  767. if (tracing()) {
  768. trace("Descriptor.isValid()","returns false (null set)");
  769. }
  770. return false;
  771. }
  772. // must have a name and descriptor type field
  773. String thisName = (String)(this.getFieldValue("name"));
  774. String thisDescType = (String)(getFieldValue("descriptorType"));
  775. if ((thisName == null) || (thisDescType == null) ||
  776. (thisName.equals("")) || (thisDescType.equals(""))) {
  777. return false;
  778. }
  779. // According to the descriptor type we validate the fields contained
  780. for (Iterator iter = returnedSet.iterator(); iter.hasNext();) {
  781. Map.Entry currElement = (Map.Entry) iter.next();
  782. if (currElement != null) {
  783. if (currElement.getValue() != null) {
  784. // validate the field valued...
  785. if (validateField((currElement.getKey()).toString(),
  786. (currElement.getValue()).toString())) {
  787. continue;
  788. } else {
  789. if (tracing()) {
  790. trace("isValid()",
  791. "Field " + currElement.getKey() + "=" +
  792. currElement.getValue() + " is not valid");
  793. }
  794. return false;
  795. }
  796. }
  797. }
  798. }
  799. // fell through, all fields OK
  800. if (tracing()) {
  801. trace("Descriptor.isValid()","returns true");
  802. }
  803. return true;
  804. }
  805. // worker routine for isValid()
  806. // name is not null
  807. // descriptorType is not null
  808. // getMethod and setMethod are not null
  809. // persistPeriod is numeric
  810. // currencyTimeLimit is numeric
  811. // lastUpdatedTimeStamp is numeric
  812. // visibility is 1-4
  813. // severity is 0-6
  814. // log is T or F
  815. // role is not null
  816. // class is not null
  817. // lastReturnedTimeStamp is numeric
  818. private boolean validateField(String fldName, Object fldValue) {
  819. if ((fldName == null) || (fldName.equals("")))
  820. return false;
  821. String SfldValue = "";
  822. boolean isAString = false;
  823. if ((fldValue != null) && (fldValue instanceof java.lang.String)) {
  824. SfldValue = (String) fldValue;
  825. isAString = true;
  826. }
  827. boolean nameOrDescriptorType =
  828. (fldName.equalsIgnoreCase("Name") ||
  829. fldName.equalsIgnoreCase("DescriptorType"));
  830. if (nameOrDescriptorType ||
  831. fldName.equalsIgnoreCase("SetMethod") ||
  832. fldName.equalsIgnoreCase("GetMethod") ||
  833. fldName.equalsIgnoreCase("Role") ||
  834. fldName.equalsIgnoreCase("Class")) {
  835. if (fldValue == null || !isAString)
  836. return false;
  837. if (nameOrDescriptorType && SfldValue.equals(""))
  838. return false;
  839. return true;
  840. } else if (fldName.equalsIgnoreCase("visibility")) {
  841. long v;
  842. if ((fldValue != null) && (isAString)) {
  843. v = toNumeric(SfldValue);
  844. } else if (fldValue instanceof java.lang.Integer) {
  845. v = ((Integer)fldValue).intValue();
  846. } else return false;
  847. if (v >= 1 && v <= 4)
  848. return true;
  849. else
  850. return false;
  851. } else if (fldName.equalsIgnoreCase("severity")) {
  852. long v;
  853. if ((fldValue != null) && (isAString)) {
  854. v = toNumeric(SfldValue);
  855. } else if (fldValue instanceof java.lang.Integer) {
  856. v = ((Integer)fldValue).intValue();
  857. } else return false;
  858. return (v >= 0 && v <= 6);
  859. } else if (fldName.equalsIgnoreCase("PersistPolicy")) {
  860. return (((fldValue != null) && (isAString)) &&
  861. ( SfldValue.equalsIgnoreCase("OnUpdate") ||
  862. SfldValue.equalsIgnoreCase("OnTimer") ||
  863. SfldValue.equalsIgnoreCase("NoMoreOftenThan") ||
  864. SfldValue.equalsIgnoreCase("Always") ||
  865. SfldValue.equalsIgnoreCase("Never") ));
  866. } else if (fldName.equalsIgnoreCase("PersistPeriod") ||
  867. fldName.equalsIgnoreCase("CurrencyTimeLimit") ||
  868. fldName.equalsIgnoreCase("LastUpdatedTimeStamp") ||
  869. fldName.equalsIgnoreCase("LastReturnedTimeStamp")) {
  870. long v;
  871. if ((fldValue != null) && (isAString)) {
  872. v = toNumeric(SfldValue);
  873. } else if (fldValue instanceof java.lang.Number) {
  874. v = ((Number)fldValue).longValue();
  875. } else return false;
  876. return (v >= -1);
  877. } else if (fldName.equalsIgnoreCase("log")) {
  878. return ((fldValue instanceof java.lang.Boolean) ||
  879. (isAString &&
  880. (SfldValue.equalsIgnoreCase("T") ||
  881. SfldValue.equalsIgnoreCase("true") ||
  882. SfldValue.equalsIgnoreCase("F") ||
  883. SfldValue.equalsIgnoreCase("false") )));
  884. }
  885. // default to true, it is a field we aren't validating (user etc.)
  886. return true;
  887. }
  888. /**
  889. * <p>Returns an XML String representing the descriptor.</p>
  890. *
  891. * <p>The format is not defined, but an implementation must
  892. * ensure that the string returned by this method can be
  893. * used to build an equivalent descriptor when instantiated
  894. * using the constructor {@link #DescriptorSupport(String)
  895. * DescriptorSupport(String inStr)}.</p>
  896. *
  897. * <p>Fields which are not String objects will have toString()
  898. * called on them to create the value. The value will be
  899. * enclosed in parentheses. It is not guaranteed that you can
  900. * reconstruct these objects unless they have been
  901. * specifically set up to support toString() in a meaningful
  902. * format and have a matching constructor that accepts a
  903. * String in the same format.</p>
  904. *
  905. * <p>If the descriptor is empty the following String is
  906. * returned: <Descriptor></Descriptor></p>
  907. *
  908. * @return the XML string.
  909. *
  910. * @exception RuntimeOperationsException for illegal value for
  911. * field Names or field Values. If the XML formated string
  912. * construction fails for any reason, this exception will be
  913. * thrown.
  914. */
  915. public synchronized String toXMLString() {
  916. StringBuffer buf = new StringBuffer("<Descriptor>");
  917. Set returnedSet = descriptorMap.entrySet();
  918. for (Iterator iter = returnedSet.iterator(); iter.hasNext(); ) {
  919. final Map.Entry currElement = (Map.Entry) iter.next();
  920. final String name = currElement.getKey().toString();
  921. Object value = currElement.getValue();
  922. String valueString = null;
  923. /* Set valueString to non-null iff this is a string that
  924. cannot be confused with the encoding of an object. If it
  925. could be so confused (surrounded by parentheses) then we
  926. call makeFieldValue as for any non-String object and end
  927. up with an encoding like "(java.lang.String/(thing))". */
  928. if (value instanceof String) {
  929. final String svalue = (String) value;
  930. if (!svalue.startsWith("(") || !svalue.endsWith(")"))
  931. valueString = quote(svalue);
  932. }
  933. if (valueString == null)
  934. valueString = makeFieldValue(value);
  935. buf.append("<field name=\"").append(name).append("\" value=\"")
  936. .append(valueString).append("\"></field>");
  937. }
  938. buf.append("</Descriptor>");
  939. return buf.toString();
  940. }
  941. private static final String[] entities = {
  942. " ",
  943. "\""",
  944. "<<",
  945. ">>",
  946. "&&",
  947. "\r ",
  948. "\t ",
  949. "\n ",
  950. "\f ",
  951. };
  952. private static final Map entityToCharMap = new HashMap();
  953. private static final String[] charToEntityMap;
  954. static {
  955. char maxChar = 0;
  956. for (int i = 0; i < entities.length; i++) {
  957. final char c = entities[i].charAt(0);
  958. if (c > maxChar)
  959. maxChar = c;
  960. }
  961. charToEntityMap = new String[maxChar + 1];
  962. for (int i = 0; i < entities.length; i++) {
  963. final char c = entities[i].charAt(0);
  964. final String entity = entities[i].substring(1);
  965. charToEntityMap[c] = entity;
  966. entityToCharMap.put(entity, new Character(c));
  967. }
  968. }
  969. private static boolean isMagic(char c) {
  970. return (c < charToEntityMap.length && charToEntityMap[c] != null);
  971. }
  972. /*
  973. * Quote the string so that it will be acceptable to the (String)
  974. * constructor. Since the parsing code in that constructor is fairly
  975. * stupid, we're obliged to quote apparently innocuous characters like
  976. * space, <, and >. In a future version, we should rewrite the parser
  977. * and only quote " plus either \ or & (depending on the quote syntax).
  978. */
  979. private static String quote(String s) {
  980. boolean found = false;
  981. for (int i = 0; i < s.length(); i++) {
  982. if (isMagic(s.charAt(i))) {
  983. found = true;
  984. break;
  985. }
  986. }
  987. if (!found)
  988. return s;
  989. StringBuffer buf = new StringBuffer();
  990. for (int i = 0; i < s.length(); i++) {
  991. char c = s.charAt(i);
  992. if (isMagic(c))
  993. buf.append(charToEntityMap[c]);
  994. else
  995. buf.append(c);
  996. }
  997. return buf.toString();
  998. }
  999. private static String unquote(String s) throws XMLParseException {
  1000. if (!s.startsWith("\"") || !s.endsWith("\""))
  1001. throw new XMLParseException("Value must be quoted: <" + s + ">");
  1002. StringBuffer buf = new StringBuffer();
  1003. final int len = s.length() - 1;
  1004. for (int i = 1; i < len; i++) {
  1005. final char c = s.charAt(i);
  1006. final int semi;
  1007. final Character quoted;
  1008. if (c == '&'
  1009. && (semi = s.indexOf(';', i + 1)) >= 0
  1010. && ((quoted =
  1011. (Character) entityToCharMap.get(s.substring(i, semi+1)))
  1012. != null)) {
  1013. buf.append(quoted);
  1014. i = semi;
  1015. } else
  1016. buf.append(c);
  1017. }
  1018. return buf.toString();
  1019. }
  1020. /**
  1021. * Make the string that will go inside "..." for a value that is not
  1022. * a plain String.
  1023. * @throws RuntimeOperationsException if the value cannot be encoded.
  1024. */
  1025. private static String makeFieldValue(Object value) {
  1026. if (value == null)
  1027. return "(null)";
  1028. Class valueClass = value.getClass();
  1029. try {
  1030. valueClass.getConstructor(new Class[] {String.class});
  1031. } catch (NoSuchMethodException e) {
  1032. final String msg =
  1033. "Class " + valueClass + " does not have a public " +
  1034. "constructor with a single string arg";
  1035. final RuntimeException iae = new IllegalArgumentException(msg);
  1036. throw new RuntimeOperationsException(iae,
  1037. "Cannot make XML descriptor");
  1038. } catch (SecurityException e) {
  1039. // OK: we'll pretend the constructor is there
  1040. // too bad if it's not: we'll find out when we try to
  1041. // reconstruct the DescriptorSupport
  1042. }
  1043. final String quotedValueString = quote(value.toString());
  1044. return "(" + valueClass.getName() + "/" + quotedValueString + ")";
  1045. }
  1046. /*
  1047. * Parse a field value from the XML produced by toXMLString().
  1048. * Given a descriptor XML containing <field name="nnn" value="vvv">,
  1049. * the argument to this method will be "vvv" (a string including the
  1050. * containing quote characters). If vvv begins and ends with parentheses,
  1051. * then it may contain:
  1052. * - the characters "null", in which case the result is null;
  1053. * - a value of the form "some.class.name/xxx", in which case the
  1054. * result is equivalent to `new some.class.name("xxx")';
  1055. * - some other string, in which case the result is that string,
  1056. * without the parentheses.
  1057. */
  1058. private static Object parseQuotedFieldValue(String s)
  1059. throws XMLParseException {
  1060. s = unquote(s);
  1061. if (s.equalsIgnoreCase("(null)"))
  1062. return null;
  1063. if (!s.startsWith("(") || !s.endsWith(")"))
  1064. return s;
  1065. final int slash = s.indexOf('/');
  1066. if (slash < 0) {
  1067. // compatibility: old code didn't include class name
  1068. return s.substring(1, s.length() - 1);
  1069. }
  1070. final String className = s.substring(1, slash);
  1071. final Constructor constr;
  1072. try {
  1073. final ClassLoader contextClassLoader =
  1074. Thread.currentThread().getContextClassLoader();
  1075. final Class c =
  1076. Class.forName(className, false, contextClassLoader);
  1077. constr = c.getConstructor(new Class[] {String.class});
  1078. } catch (Exception e) {
  1079. throw new XMLParseException(e,
  1080. "Cannot parse value: <" + s + ">");
  1081. }
  1082. final String arg = s.substring(slash + 1, s.length() - 1);
  1083. try {
  1084. return constr.newInstance(new Object[] {arg});
  1085. } catch (Exception e) {
  1086. final String msg =
  1087. "Cannot construct instance of " + className +
  1088. " with arg: <" + s + ">";
  1089. throw new XMLParseException(e, msg);
  1090. }
  1091. }
  1092. /**
  1093. * Returns <pv>a human readable string representing the
  1094. * descriptor</pv>. The string will be in the format of
  1095. * "fieldName=fieldValue,fieldName2=fieldValue2,..."<br>
  1096. *
  1097. * If there are no fields in the descriptor, then an empty String
  1098. * is returned.<br>
  1099. *
  1100. * If a fieldValue is an object then the toString() method is
  1101. * called on it and its returned value is used as the value for
  1102. * the field enclosed in parenthesis.
  1103. *
  1104. * @exception RuntimeOperationsException for illegal value for
  1105. * field Names or field Values. If the descriptor string fails
  1106. * for any reason, this exception will be thrown.
  1107. */
  1108. public synchronized String toString() {
  1109. if (tracing()) {
  1110. trace("Descriptor.toString()","Entry");
  1111. }
  1112. String respStr = "";
  1113. String[] fields = getFields();
  1114. if (tracing()) {
  1115. trace("Descriptor.toString()",
  1116. "Printing " + fields.length + " fields");
  1117. }
  1118. if ((fields == null) || (fields.length == 0)) {
  1119. if (tracing()) {
  1120. trace("Descriptor.toString()","Empty Descriptor");
  1121. }
  1122. return respStr;
  1123. }
  1124. for (int i=0; i < fields.length; i++) {
  1125. if (i == (fields.length - 1)) {
  1126. respStr = respStr.concat(fields[i]);
  1127. } else {
  1128. respStr = respStr.concat(fields[i] + ", ");
  1129. }
  1130. }
  1131. if (tracing()) {
  1132. trace("Descriptor.toString()","Exit returning " + respStr);
  1133. }
  1134. return respStr;
  1135. }
  1136. // utility to convert to int, returns -2 if bogus.
  1137. private long toNumeric(String inStr) {
  1138. long result = -2;
  1139. try {
  1140. result = java.lang.Long.parseLong(inStr);
  1141. } catch (Exception e) {
  1142. return -2;
  1143. }
  1144. return result;
  1145. }
  1146. /* Wrapper for String that can be used as a case-insensitive key
  1147. in a Map. Currently, we make a new instance of this class
  1148. every time we need a key. Since both this class and String
  1149. are immutable, we could reasonably have a WeakHashMap to
  1150. generate canonical instances of this class. Thus, every time
  1151. you put a "displayName" field in your descriptor, we'd use
  1152. the same CaseIgnoreString instance to represent it. The
  1153. semantics would be that an entry would remain in the
  1154. WeakHashMap so long as there is at least one extant
  1155. DescriptorSupport using it; this could be achieved by wrapping
  1156. the CaseIgnoreString value in the WeakHashMap inside a
  1157. WeakReference. Thus, if there is a DescriptorSupport then it
  1158. strongly-references the CaseIgnoreString via descriptorMap,
  1159. and the CaseIgnoreString strong-references the String
  1160. instance, preventing the WeakHashMap entry from disappearing.
  1161. It's not clear that this would really be worth the effort,
  1162. though.
  1163. */
  1164. private static class CaseIgnoreString {
  1165. private String string;
  1166. CaseIgnoreString(String string) {
  1167. this.string = string;
  1168. }
  1169. public String toString() {
  1170. return string;
  1171. }
  1172. public boolean equals(Object o) {
  1173. return (o instanceof CaseIgnoreString &&
  1174. ((CaseIgnoreString) o).string.equalsIgnoreCase(string));
  1175. }
  1176. /* We don't bother caching hashCode() values even though they are
  1177. a bit expensive to compute, because they will typically only be
  1178. computed once for any given instance, when that instance is
  1179. used as a key in a method of descriptorMap. If at some later
  1180. stage we keep e.g. a WeakHashMap of canonical CaseIgnoreString
  1181. instances for each String, then it would be worth caching the
  1182. hashCode(). */
  1183. public int hashCode() {
  1184. return string.toLowerCase().hashCode();
  1185. }
  1186. }
  1187. // Trace and debug functions
  1188. private boolean tracing() {
  1189. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN);
  1190. }
  1191. private void trace(String inClass, String inMethod, String inText) {
  1192. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass,
  1193. inMethod,
  1194. Integer.toHexString(this.hashCode()) + " " + inText);
  1195. }
  1196. private void trace(String inMethod, String inText) {
  1197. trace(currClass, inMethod, inText);
  1198. }
  1199. /**
  1200. * Deserializes a {@link DescriptorSupport} from an {@link
  1201. * ObjectInputStream}.
  1202. */
  1203. private void readObject(ObjectInputStream in)
  1204. throws IOException, ClassNotFoundException {
  1205. ObjectInputStream.GetField fields = in.readFields();
  1206. Map descriptor = (Map) fields.get("descriptor", null);
  1207. descriptorMap = new HashMap();
  1208. for (Iterator it = descriptor.entrySet().iterator(); it.hasNext(); ) {
  1209. Map.Entry entry = (Map.Entry) it.next();
  1210. setField((String) entry.getKey(), entry.getValue());
  1211. }
  1212. }
  1213. /**
  1214. * Serializes a {@link DescriptorSupport} to an {@link ObjectOutputStream}.
  1215. */
  1216. /* If you set jmx.serial.form to "1.2.0" or "1.2.1", then we are
  1217. bug-compatible with those versions. Specifically, field names
  1218. are forced to lower-case before being written. This
  1219. contradicts the spec, which, though it does not mention
  1220. serialization explicitly, does say that the case of field names
  1221. is preserved. But in 1.2.0 and 1.2.1, this requirement was not
  1222. met. Instead, field names in the descriptor map were forced to
  1223. lower case. Those versions expect this to have happened to a
  1224. descriptor they deserialize and e.g. getFieldValue will not
  1225. find a field whose name is spelt with a different case.
  1226. */
  1227. private void writeObject(ObjectOutputStream out) throws IOException {
  1228. ObjectOutputStream.PutField fields = out.putFields();
  1229. boolean compat = "1.0".equals(serialForm);
  1230. if (compat)
  1231. fields.put("currClass", currClass);
  1232. boolean forceLowerCase = (compat || "1.2.0".equals(serialForm) ||
  1233. "1.2.1".equals(serialForm));
  1234. Map descriptor = new HashMap();
  1235. for (Iterator it = descriptorMap.entrySet().iterator();
  1236. it.hasNext(); ) {
  1237. Map.Entry entry = (Map.Entry) it.next();
  1238. String fieldName = entry.getKey().toString();
  1239. if (forceLowerCase)
  1240. fieldName = fieldName.toLowerCase();
  1241. descriptor.put(fieldName, entry.getValue());
  1242. }
  1243. fields.put("descriptor", descriptor);
  1244. out.writeFields();
  1245. }
  1246. }