jdk/src/share/classes/javax/management/modelmbean/DescriptorSupport.java
author dfuchs
Tue, 29 Jul 2008 19:21:59 +0200
changeset 900 55c9c5a88bde
parent 715 f16baef3a20e
child 1510 e747d3193ef2
permissions -rw-r--r--
6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode. Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 526
diff changeset
     2
 * Portions Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author    IBM Corp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Copyright IBM Corp. 1999-2000.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
package javax.management.modelmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import static com.sun.jmx.defaults.JmxProperties.MODELMBEAN_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static com.sun.jmx.mbeanserver.Util.cast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jmx.mbeanserver.GetPropertyAction;
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
    36
import com.sun.jmx.mbeanserver.Util;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.RuntimeOperationsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.reflect.misc.ReflectUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * This class represents the metadata set for a ModelMBean element.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * descriptor is part of the ModelMBeanInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * ModelMBeanNotificationInfo, ModelMBeanAttributeInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * ModelMBeanConstructorInfo, and ModelMBeanParameterInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * A descriptor consists of a collection of fields.  Each field is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * fieldname=fieldvalue format.  Field names are not case sensitive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * case will be preserved on field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * All field names and values are not predefined. New fields can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * defined and added by any program.  Some fields have been predefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * for consistency of implementation and support by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * ModelMBeanInfo, ModelMBeanAttributeInfo, ModelMBeanConstructorInfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * ModelMBeanNotificationInfo, ModelMBeanOperationInfo and ModelMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>The <b>serialVersionUID</b> of this class is <code>-6292969195866300415L</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
@SuppressWarnings("serial")  // serialVersionUID not constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class DescriptorSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         implements javax.management.Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Serialization compatibility stuff:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Two serial forms are supported in this class. The selected form depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // on system property "jmx.serial.form":
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    //  - "1.0" for JMX 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    //  - any other value for JMX 1.1 and higher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Serial version for old serial form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final long oldSerialVersionUID = 8071560848919417985L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // Serial version for new serial form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static final long newSerialVersionUID = -6292969195866300415L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Serializable fields in old serial form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final ObjectStreamField[] oldSerialPersistentFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      new ObjectStreamField("descriptor", HashMap.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      new ObjectStreamField("currClass", String.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Serializable fields in new serial form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final ObjectStreamField[] newSerialPersistentFields =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      new ObjectStreamField("descriptor", HashMap.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Actual serial version and serial form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final long serialVersionUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @serialField descriptor HashMap The collection of fields representing this descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final ObjectStreamField[] serialPersistentFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static final String serialForm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        String form = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        boolean compat = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            form = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            compat = "1.0".equals(form);  // form may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // OK: No compat with 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        serialForm = form;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (compat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            serialPersistentFields = oldSerialPersistentFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            serialVersionUID = oldSerialVersionUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            serialPersistentFields = newSerialPersistentFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            serialVersionUID = newSerialVersionUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // END Serialization compatibility stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /* Spec says that field names are case-insensitive, but that case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
       is preserved.  This means that we need to be able to map from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
       name that may differ in case to the actual name that is used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
       the HashMap.  Thus, descriptorMap is a TreeMap with a Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
       that ignores case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
       Previous versions of this class had a field called "descriptor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
       of type HashMap where the keys were directly Strings.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
       hard to reconcile with the required semantics, so we fabricate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
       that field virtually during serialization and deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
       but keep the real information in descriptorMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private transient SortedMap<String, Object> descriptorMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final String currClass = "DescriptorSupport";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Descriptor default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Default initial descriptor size is 20.  It will grow as needed.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Note that the created empty descriptor is not a valid descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * (the method {@link #isValid isValid} returns <CODE>false</CODE>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public DescriptorSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    "DescriptorSupport()" , "Constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Descriptor constructor.  Takes as parameter the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * capacity of the Map that stores the descriptor fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Capacity will grow as needed.<br> Note that the created empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * descriptor is not a valid descriptor (the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * #isValid isValid} returns <CODE>false</CODE>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param initNumFields The initial capacity of the Map that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * stores the descriptor fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @exception RuntimeOperationsException for illegal value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * initNumFields (&lt;= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception MBeanException Wraps a distributed communication Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public DescriptorSupport(int initNumFields)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throws MBeanException, RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    "Descriptor(initNumFields = " + initNumFields + ")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    "Constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (initNumFields <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        "Descriptor(initNumFields)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        "Illegal arguments: initNumFields <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                "Descriptor field limit invalid: " + initNumFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Descriptor constructor taking a Descriptor as parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Creates a new descriptor initialized to the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * descriptor passed in parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param inDescr the descriptor to be used to initialize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * constructed descriptor. If it is null or contains no descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * fields, an empty Descriptor will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public DescriptorSupport(DescriptorSupport inDescr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    "Descriptor(Descriptor)", "Constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (inDescr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            init(inDescr.descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>Descriptor constructor taking an XML String.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>The format of the XML string is not defined, but an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * implementation must ensure that the string returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * {@link #toXMLString() toXMLString()} on an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * descriptor can be used to instantiate an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * descriptor using this constructor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p>In this implementation, all field values will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * as Strings.  If the field values are not Strings, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * programmer will have to reset or convert these fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * correctly.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param inStr An XML-formatted string used to populate this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Descriptor.  The format is not defined, but any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * implementation must ensure that the string returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * method {@link #toXMLString toXMLString} on an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * descriptor can be used to instantiate an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * descriptor when instantiated using this constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @exception RuntimeOperationsException If the String inStr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * passed in parameter is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception XMLParseException XML parsing problem while parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * the input String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception MBeanException Wraps a distributed communication Exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /* At some stage we should rewrite this code to be cleverer.  Using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
       a StringTokenizer as we do means, first, that we accept a lot of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
       bogus strings without noticing they are bogus, and second, that we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
       split the string being parsed at characters like > even if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
       occur in the middle of a field value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public DescriptorSupport(String inStr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throws MBeanException, RuntimeOperationsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                   XMLParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        /* parse an XML-formatted string and populate internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * structure with it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    "Descriptor(String = '" + inStr + "')", "Constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (inStr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        "Descriptor(String = null)", "Illegal arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            final String msg = "String in parameter is null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        final String lowerInStr = inStr.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (!lowerInStr.startsWith("<descriptor>")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            || !lowerInStr.endsWith("</descriptor>")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throw new XMLParseException("No <descriptor>, </descriptor> pair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // parse xmlstring into structures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // create dummy descriptor: should have same size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // as number of fields in xmlstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // loop through structures and put them in descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        boolean inFld = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        boolean inDesc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        String fieldName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        String fieldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        while (st.hasMoreTokens()) {  // loop through tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            String tok = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (tok.equalsIgnoreCase("FIELD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                inFld = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } else if (tok.equalsIgnoreCase("/FIELD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if ((fieldName != null) && (fieldValue != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    fieldName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        fieldName.substring(fieldName.indexOf('"') + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                            fieldName.lastIndexOf('"'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    final Object fieldValueObject =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                        parseQuotedFieldValue(fieldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    setField(fieldName, fieldValueObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                fieldName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                fieldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                inFld = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } else if (tok.equalsIgnoreCase("DESCRIPTOR")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                inDesc = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else if (tok.equalsIgnoreCase("/DESCRIPTOR")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                inDesc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                fieldName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                fieldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                inFld = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else if (inFld && inDesc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // want kw=value, eg, name="myname" value="myvalue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                int eq_separator = tok.indexOf("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (eq_separator > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    String kwPart = tok.substring(0,eq_separator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    String valPart = tok.substring(eq_separator+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    if (kwPart.equalsIgnoreCase("NAME"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        fieldName = valPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    else if (kwPart.equalsIgnoreCase("VALUE"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        fieldValue = valPart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    else {  // xml parse exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            "Expected `name' or `value', got `" + tok + "'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        throw new XMLParseException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                } else { // xml parse exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        "Expected `keyword=value', got `" + tok + "'";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    throw new XMLParseException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }  // while tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    "Descriptor(XMLString)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Constructor taking field names and field values.  Neither array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param fieldNames String array of field names.  No elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * this array can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param fieldValues Object array of the corresponding field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * values.  Elements of the array can be null. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * <code>fieldValue</code> must be valid for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <code>fieldName</code> (as defined in method {@link #isValid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * isValid})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <p>Note: array sizes of parameters should match. If both arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * are empty, then an empty descriptor is created.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @exception RuntimeOperationsException for illegal value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * field Names or field Values.  The array lengths must be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * If the descriptor construction fails for any reason, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public DescriptorSupport(String[] fieldNames, Object[] fieldValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    "Descriptor(fieldNames,fieldObjects)", "Constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if ((fieldNames == null) || (fieldValues == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            (fieldNames.length != fieldValues.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        "Descriptor(fieldNames,fieldObjects)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        "Illegal arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                "Null or invalid fieldNames or fieldValues";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        /* populate internal structure with fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        for (int i=0; i < fieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // setField will throw an exception if a fieldName is be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // the fieldName and fieldValue will be validated in setField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            setField(fieldNames[i], fieldValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    "Descriptor(fieldNames,fieldObjects)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Constructor taking fields in the <i>fieldName=fieldValue</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param fields String array with each element containing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * field name and value.  If this array is null or empty, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * default constructor will be executed. Null strings or empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * strings will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p>All field values should be Strings.  If the field values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * not Strings, the programmer will have to reset or convert these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * fields correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <p>Note: Each string should be of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <i>fieldName=fieldValue</i>.  The field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * ends at the first {@code =} character; for example if the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * is {@code a=b=c} then the field name is {@code a} and its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * is {@code b=c}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @exception RuntimeOperationsException for illegal value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * field Names or field Values.  The field must contain an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * "=". "=fieldValue", "fieldName", and "fieldValue" are illegal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * FieldName cannot be null.  "fieldName=" will cause the value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * be null.  If the descriptor construction fails for any reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * this exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public DescriptorSupport(String... fields)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    "Descriptor(String... fields)", "Constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (( fields == null ) || ( fields.length == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        for (int i=0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if ((fields[i] == null) || (fields[i].equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            int eq_separator = fields[i].indexOf("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (eq_separator < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                // illegal if no = or is first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            "Descriptor(String... fields)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            "Illegal arguments: field does not have " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            "'=' as a name and value separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                final String msg = "Field in invalid format: no equals sign";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            String fieldName = fields[i].substring(0,eq_separator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            String fieldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (eq_separator < fields[i].length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                // = is not in last character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                fieldValue = fields[i].substring(eq_separator+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            if (fieldName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                            DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                            "Descriptor(String... fields)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            "Illegal arguments: fieldName is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                final String msg = "Field in invalid format: no fieldName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            setField(fieldName,fieldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    "Descriptor(String... fields)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private void init(Map<String, ?> initMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        descriptorMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (initMap != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            descriptorMap.putAll(initMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    // Implementation of the Descriptor interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public synchronized Object getFieldValue(String fieldName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if ((fieldName == null) || (fieldName.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        "getFieldValue(String fieldName)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        "Illegal arguments: null field name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            final String msg = "Fieldname requested is null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Object retValue = descriptorMap.get(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    "getFieldValue(String fieldName = " + fieldName + ")",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    "Returns '" + retValue + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return(retValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public synchronized void setField(String fieldName, Object fieldValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // field name cannot be null or empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if ((fieldName == null) || (fieldName.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        "setField(fieldName,fieldValue)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        "Illegal arguments: null or empty field name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            final String msg = "Field name to be set is null or empty";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (!validateField(fieldName, fieldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        "setField(fieldName,fieldValue)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        "Illegal arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                "Field value invalid: " + fieldName + "=" + fieldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    "setField(fieldName,fieldValue)", "Entry: setting '"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    + fieldName + "' to '" + fieldValue + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // Since we do not remove any existing entry with this name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // the field will preserve whatever case it had, ignoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        // any difference there might be in fieldName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        descriptorMap.put(fieldName, fieldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public synchronized String[] getFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    "getFields()", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        int numberOfEntries = descriptorMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        String[] responseFields = new String[numberOfEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        Set returnedSet = descriptorMap.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    "getFields()", "Returning " + numberOfEntries + " fields");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        for (Iterator iter = returnedSet.iterator(); iter.hasNext(); i++) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   602
            Map.Entry currElement = (Map.Entry) iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (currElement == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                            DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                            "getFields()", "Element is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            } else {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   611
                Object currValue = currElement.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (currValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    responseFields[i] = currElement.getKey() + "=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    if (currValue instanceof java.lang.String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        responseFields[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                            currElement.getKey() + "=" + currValue.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        responseFields[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                            currElement.getKey() + "=(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                            currValue.toString() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    "getFields()", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return responseFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public synchronized String[] getFieldNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    "getFieldNames()", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        int numberOfEntries = descriptorMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        String[] responseFields = new String[numberOfEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        Set returnedSet = descriptorMap.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    "getFieldNames()",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    "Returning " + numberOfEntries + " fields");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        for (Iterator iter = returnedSet.iterator(); iter.hasNext(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            Map.Entry currElement = (Map.Entry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (( currElement == null ) || (currElement.getKey() == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                            DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                            "getFieldNames()", "Field is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                responseFields[i] = currElement.getKey().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    "getFieldNames()", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return responseFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public synchronized Object[] getFieldValues(String... fieldNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    "getFieldValues(String... fieldNames)", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // if fieldNames == null return all values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // if fieldNames is String[0] return no values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        final int numberOfEntries =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            (fieldNames == null) ? descriptorMap.size() : fieldNames.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        final Object[] responseFields = new Object[numberOfEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    "getFieldValues(String... fieldNames)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    "Returning " + numberOfEntries + " fields");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (fieldNames == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            for (Iterator iter = descriptorMap.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                 iter.hasNext(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                responseFields[i] = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            for (i=0; i < fieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                if ((fieldNames[i] == null) || (fieldNames[i].equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    responseFields[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    responseFields[i] = getFieldValue(fieldNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    "getFieldValues(String... fieldNames)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return responseFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public synchronized void setFields(String[] fieldNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                                       Object[] fieldValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    "setFields(fieldNames,fieldValues)", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if ((fieldNames == null) || (fieldValues == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            (fieldNames.length != fieldValues.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        "setFields(fieldNames,fieldValues)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        "Illegal arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            final String msg = "fieldNames and fieldValues are null or invalid";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        for (int i=0; i < fieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if (( fieldNames[i] == null) || (fieldNames[i].equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                            DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                            "setFields(fieldNames,fieldValues)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            "Null field name encountered at element " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                final String msg = "fieldNames is null or invalid";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                throw new RuntimeOperationsException(iae, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            setField(fieldNames[i], fieldValues[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    "setFields(fieldNames,fieldValues)", "Exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Returns a new Descriptor which is a duplicate of the Descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @exception RuntimeOperationsException for illegal value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * field Names or field Values.  If the descriptor construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * fails for any reason, this exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   778
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public synchronized Object clone() throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    "clone()", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        return(new DescriptorSupport(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public synchronized void removeField(String fieldName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if ((fieldName == null) || (fieldName.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        descriptorMap.remove(fieldName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Compares this descriptor to the given object.  The objects are equal if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * the given object is also a Descriptor, and if the two Descriptors have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * the same field names (possibly differing in case) and the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * associated values.  The respective values for a field in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Descriptors are equal if the following conditions hold:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <li>If one value is null then the other must be too.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <li>If one value is a primitive array then the other must be a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * array of the same type with the same elements.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <li>If one value is an object array then the other must be too and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * {@link java.util.Arrays#deepEquals(Object[],Object[]) Arrays.deepEquals}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * must return true.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <li>Otherwise {@link Object#equals(Object)} must return true.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @param o the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @return {@code true} if the objects are the same; {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    // Note: this Javadoc is copied from javax.management.Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    //       due to 6369229.
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   821
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public synchronized boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            return true;
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   825
        if (! (o instanceof Descriptor))
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   826
            return false;
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   827
        if (o instanceof ImmutableDescriptor)
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   828
            return o.equals(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        return new ImmutableDescriptor(descriptorMap).equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <p>Returns the hash code value for this descriptor.  The hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * code is computed as the sum of the hash codes for each field in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * the descriptor.  The hash code of a field with name {@code n}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * and value {@code v} is {@code n.toLowerCase().hashCode() ^ h}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Here {@code h} is the hash code of {@code v}, computed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * <li>If {@code v} is null then {@code h} is 0.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <li>If {@code v} is a primitive array then {@code h} is computed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * the appropriate overloading of {@code java.util.Arrays.hashCode}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <li>If {@code v} is an object array then {@code h} is computed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * {@link java.util.Arrays#deepHashCode(Object[]) Arrays.deepHashCode}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <li>Otherwise {@code h} is {@code v.hashCode()}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @return A hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    // Note: this Javadoc is copied from javax.management.Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    //       due to 6369229.
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   854
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public synchronized int hashCode() {
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   856
        final int size = descriptorMap.size();
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   857
        // descriptorMap is sorted with a comparator that ignores cases.
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   858
        //
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   859
        return Util.hashCode(
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   860
                descriptorMap.keySet().toArray(new String[size]),
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
   861
                descriptorMap.values().toArray(new Object[size]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * Returns true if all of the fields have legal values given their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * This implementation does not support  interoperating with a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * or lookup service. Thus, conforming to the specification, no checking is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * done on the <i>"export"</i> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Otherwise this implementation returns false if:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <LI> name and descriptorType fieldNames are not defined, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * null, or empty, or not String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * <LI> class, role, getMethod, setMethod fieldNames, if defined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * are null or not String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * <LI> persistPeriod, currencyTimeLimit, lastUpdatedTimeStamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * lastReturnedTimeStamp if defined, are null, or not a Numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * String or not a Numeric Value >= -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * <LI> log fieldName, if defined, is null, or not a Boolean or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * not a String with value "t", "f", "true", "false". These String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * values must not be case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <LI> visibility fieldName, if defined, is null, or not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Numeric String or a not Numeric Value >= 1 and <= 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <LI> severity fieldName, if defined, is null, or not a Numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * String or not a Numeric Value >= 0 and <= 6<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * <LI> persistPolicy fieldName, if defined, is null, or not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * the following strings:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *   "OnUpdate", "OnTimer", "NoMoreOftenThan", "OnUnregister", "Always",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *   "Never". These String values must not be case sensitive.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @exception RuntimeOperationsException If the validity checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * fails for any reason, this exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public synchronized boolean isValid() throws RuntimeOperationsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    "isValid()", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        // verify that the descriptor is valid, by iterating over each field...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        Set returnedSet = descriptorMap.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (returnedSet == null) {   // null descriptor, not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        "isValid()", "Returns false (null set)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // must have a name and descriptor type field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        String thisName = (String)(this.getFieldValue("name"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        String thisDescType = (String)(getFieldValue("descriptorType"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if ((thisName == null) || (thisDescType == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            (thisName.equals("")) || (thisDescType.equals(""))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        // According to the descriptor type we validate the fields contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        for (Iterator iter = returnedSet.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            Map.Entry currElement = (Map.Entry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (currElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                if (currElement.getValue() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    // validate the field valued...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    if (validateField((currElement.getKey()).toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                                      (currElement.getValue()).toString())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                                    "isValid()",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                                    "Field " + currElement.getKey() + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                                    currElement.getValue() + " is not valid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        // fell through, all fields OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    "isValid()", "Returns true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    // worker routine for isValid()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    // name is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    // descriptorType is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    // getMethod and setMethod are not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    // persistPeriod is numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    // currencyTimeLimit is numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    // lastUpdatedTimeStamp is numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    // visibility is 1-4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    // severity is 0-6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    // log is T or F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    // role is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    // class is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    // lastReturnedTimeStamp is numeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    private boolean validateField(String fldName, Object fldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        if ((fldName == null) || (fldName.equals("")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        String SfldValue = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        boolean isAString = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        if ((fldValue != null) && (fldValue instanceof java.lang.String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            SfldValue = (String) fldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            isAString = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        boolean nameOrDescriptorType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            (fldName.equalsIgnoreCase("Name") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
             fldName.equalsIgnoreCase("DescriptorType"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if (nameOrDescriptorType ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            fldName.equalsIgnoreCase("SetMethod") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            fldName.equalsIgnoreCase("GetMethod") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            fldName.equalsIgnoreCase("Role") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            fldName.equalsIgnoreCase("Class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (fldValue == null || !isAString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            if (nameOrDescriptorType && SfldValue.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        } else if (fldName.equalsIgnoreCase("visibility")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            long v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            if ((fldValue != null) && (isAString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                v = toNumeric(SfldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            } else if (fldValue instanceof java.lang.Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                v = ((Integer)fldValue).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            } else return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            if (v >= 1 &&  v <= 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        } else if (fldName.equalsIgnoreCase("severity")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            long v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if ((fldValue != null) && (isAString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                v = toNumeric(SfldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            } else if (fldValue instanceof java.lang.Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                v = ((Integer)fldValue).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            } else return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return (v >= 0 && v <= 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        } else if (fldName.equalsIgnoreCase("PersistPolicy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            return (((fldValue != null) && (isAString)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    ( SfldValue.equalsIgnoreCase("OnUpdate") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                      SfldValue.equalsIgnoreCase("OnTimer") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                      SfldValue.equalsIgnoreCase("NoMoreOftenThan") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                      SfldValue.equalsIgnoreCase("Always") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                      SfldValue.equalsIgnoreCase("Never") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                      SfldValue.equalsIgnoreCase("OnUnregister")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        } else if (fldName.equalsIgnoreCase("PersistPeriod") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                   fldName.equalsIgnoreCase("CurrencyTimeLimit") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                   fldName.equalsIgnoreCase("LastUpdatedTimeStamp") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                   fldName.equalsIgnoreCase("LastReturnedTimeStamp")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            long v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            if ((fldValue != null) && (isAString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                v = toNumeric(SfldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            } else if (fldValue instanceof java.lang.Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                v = ((Number)fldValue).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            } else return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return (v >= -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        } else if (fldName.equalsIgnoreCase("log")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return ((fldValue instanceof java.lang.Boolean) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    (isAString &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                     (SfldValue.equalsIgnoreCase("T") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                      SfldValue.equalsIgnoreCase("true") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                      SfldValue.equalsIgnoreCase("F") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                      SfldValue.equalsIgnoreCase("false") )));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        // default to true, it is a field we aren't validating (user etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * <p>Returns an XML String representing the descriptor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <p>The format is not defined, but an implementation must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * ensure that the string returned by this method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * used to build an equivalent descriptor when instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * using the constructor {@link #DescriptorSupport(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * DescriptorSupport(String inStr)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * <p>Fields which are not String objects will have toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * called on them to create the value. The value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * enclosed in parentheses.  It is not guaranteed that you can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * reconstruct these objects unless they have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * specifically set up to support toString() in a meaningful
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * format and have a matching constructor that accepts a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * String in the same format.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * <p>If the descriptor is empty the following String is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * returned: &lt;Descriptor&gt;&lt;/Descriptor&gt;</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @return the XML string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @exception RuntimeOperationsException for illegal value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * field Names or field Values.  If the XML formatted string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * construction fails for any reason, this exception will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    public synchronized String toXMLString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        final StringBuilder buf = new StringBuilder("<Descriptor>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        Set returnedSet = descriptorMap.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        for (Iterator iter = returnedSet.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            final Map.Entry currElement = (Map.Entry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            final String name = currElement.getKey().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            Object value = currElement.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            String valueString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            /* Set valueString to non-null if and only if this is a string that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
               cannot be confused with the encoding of an object.  If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
               could be so confused (surrounded by parentheses) then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
               call makeFieldValue as for any non-String object and end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
               up with an encoding like "(java.lang.String/(thing))".  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            if (value instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                final String svalue = (String) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                if (!svalue.startsWith("(") || !svalue.endsWith(")"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    valueString = quote(svalue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            if (valueString == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                valueString = makeFieldValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            buf.append("<field name=\"").append(name).append("\" value=\"")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                .append(valueString).append("\"></field>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        buf.append("</Descriptor>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    private static final String[] entities = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        " &#32;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        "\"&quot;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        "<&lt;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        ">&gt;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        "&&amp;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        "\r&#13;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        "\t&#9;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        "\n&#10;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        "\f&#12;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    private static final Map<String,Character> entityToCharMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        new HashMap<String,Character>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    private static final String[] charToEntityMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        char maxChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        for (int i = 0; i < entities.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            final char c = entities[i].charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            if (c > maxChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                maxChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        charToEntityMap = new String[maxChar + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        for (int i = 0; i < entities.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            final char c = entities[i].charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            final String entity = entities[i].substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            charToEntityMap[c] = entity;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
  1138
            entityToCharMap.put(entity, c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    private static boolean isMagic(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        return (c < charToEntityMap.length && charToEntityMap[c] != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * Quote the string so that it will be acceptable to the (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * constructor.  Since the parsing code in that constructor is fairly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * stupid, we're obliged to quote apparently innocuous characters like
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * space, <, and >.  In a future version, we should rewrite the parser
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * and only quote " plus either \ or & (depending on the quote syntax).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    private static String quote(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            if (isMagic(s.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (!found)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        final StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            if (isMagic(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                buf.append(charToEntityMap[c]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                buf.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    private static String unquote(String s) throws XMLParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        if (!s.startsWith("\"") || !s.endsWith("\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            throw new XMLParseException("Value must be quoted: <" + s + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        final StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        final int len = s.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        for (int i = 1; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            final char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            final int semi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            final Character quoted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            if (c == '&'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                && (semi = s.indexOf(';', i + 1)) >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                && ((quoted = entityToCharMap.get(s.substring(i, semi+1)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                    != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                buf.append(quoted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                i = semi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                buf.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Make the string that will go inside "..." for a value that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * a plain String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * @throws RuntimeOperationsException if the value cannot be encoded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    private static String makeFieldValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            return "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        Class<?> valueClass = value.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            valueClass.getConstructor(String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                "Class " + valueClass + " does not have a public " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                "constructor with a single string arg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            final RuntimeException iae = new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            throw new RuntimeOperationsException(iae,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                                                 "Cannot make XML descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            // OK: we'll pretend the constructor is there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            // too bad if it's not: we'll find out when we try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            // reconstruct the DescriptorSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        final String quotedValueString = quote(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        return "(" + valueClass.getName() + "/" + quotedValueString + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * Parse a field value from the XML produced by toXMLString().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Given a descriptor XML containing <field name="nnn" value="vvv">,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * the argument to this method will be "vvv" (a string including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * containing quote characters).  If vvv begins and ends with parentheses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * then it may contain:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * - the characters "null", in which case the result is null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * - a value of the form "some.class.name/xxx", in which case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * result is equivalent to `new some.class.name("xxx")';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * - some other string, in which case the result is that string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * without the parentheses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    private static Object parseQuotedFieldValue(String s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            throws XMLParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        s = unquote(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        if (s.equalsIgnoreCase("(null)"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (!s.startsWith("(") || !s.endsWith(")"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        final int slash = s.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        if (slash < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            // compatibility: old code didn't include class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            return s.substring(1, s.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        final String className = s.substring(1, slash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        final Constructor<?> constr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            final ClassLoader contextClassLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            if (contextClassLoader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                ReflectUtil.checkPackageAccess(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            final Class<?> c =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                Class.forName(className, false, contextClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            constr = c.getConstructor(new Class[] {String.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            throw new XMLParseException(e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                                        "Cannot parse value: <" + s + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        final String arg = s.substring(slash + 1, s.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            return constr.newInstance(new Object[] {arg});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                "Cannot construct instance of " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                " with arg: <" + s + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            throw new XMLParseException(e, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * Returns <pv>a human readable string representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * descriptor</pv>.  The string will be in the format of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * "fieldName=fieldValue,fieldName2=fieldValue2,..."<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * If there are no fields in the descriptor, then an empty String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * is returned.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * If a fieldValue is an object then the toString() method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * called on it and its returned value is used as the value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * the field enclosed in parenthesis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @exception RuntimeOperationsException for illegal value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * field Names or field Values.  If the descriptor string fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * for any reason, this exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     */
900
55c9c5a88bde 6402254: Revisit ModelMBean DescriptorSupport implementation of equals and hashCode.
dfuchs
parents: 715
diff changeset
  1291
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    "toString()", "Entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        String respStr = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        String[] fields = getFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        if ((fields == null) || (fields.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                        DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                        "toString()", "Empty Descriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            return respStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    "toString()", "Printing " + fields.length + " fields");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        for (int i=0; i < fields.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            if (i == (fields.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                respStr = respStr.concat(fields[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                respStr = respStr.concat(fields[i] + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        if (MODELMBEAN_LOGGER.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            MODELMBEAN_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                    DescriptorSupport.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    "toString()", "Exit returning " + respStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        return respStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    // utility to convert to int, returns -2 if bogus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    private long toNumeric(String inStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        try {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
  1338
            return java.lang.Long.parseLong(inStr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            return -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * Deserializes a {@link DescriptorSupport} from an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * ObjectInputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        ObjectInputStream.GetField fields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        Map<String, Object> descriptor = cast(fields.get("descriptor", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        init(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        if (descriptor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            descriptorMap.putAll(descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * Serializes a {@link DescriptorSupport} to an {@link ObjectOutputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    /* If you set jmx.serial.form to "1.2.0" or "1.2.1", then we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
       bug-compatible with those versions.  Specifically, field names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
       are forced to lower-case before being written.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
       contradicts the spec, which, though it does not mention
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
       serialization explicitly, does say that the case of field names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
       is preserved.  But in 1.2.0 and 1.2.1, this requirement was not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
       met.  Instead, field names in the descriptor map were forced to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
       lower case.  Those versions expect this to have happened to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
       descriptor they deserialize and e.g. getFieldValue will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
       find a field whose name is spelt with a different case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        ObjectOutputStream.PutField fields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        boolean compat = "1.0".equals(serialForm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        if (compat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            fields.put("currClass", currClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        /* Purge the field "targetObject" from the DescriptorSupport before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
         * serializing since the referenced object is typically not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
         * serializable.  We do this here rather than purging the "descriptor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
         * variable below because that HashMap doesn't do case-insensitivity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
         * See CR 6332962.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        SortedMap<String, Object> startMap = descriptorMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        if (startMap.containsKey("targetObject")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            startMap = new TreeMap<String, Object>(descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            startMap.remove("targetObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        final HashMap<String, Object> descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        if (compat || "1.2.0".equals(serialForm) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                "1.2.1".equals(serialForm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            descriptor = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            for (Map.Entry<String, Object> entry : startMap.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                descriptor.put(entry.getKey().toLowerCase(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            descriptor = new HashMap<String, Object>(startMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        fields.put("descriptor", descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
}