0N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management;
0N/A
0N/Aimport com.sun.jmx.mbeanserver.GetPropertyAction;
147N/Aimport com.sun.jmx.mbeanserver.Util;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InvalidObjectException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectStreamField;
0N/Aimport java.security.AccessController;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Map;
0N/A
0N/A/**
0N/A * <p>Represents the object name of an MBean, or a pattern that can
0N/A * match the names of several MBeans. Instances of this class are
0N/A * immutable.</p>
0N/A *
0N/A * <p>An instance of this class can be used to represent:</p>
0N/A * <ul>
0N/A * <li>An object name</li>
0N/A * <li>An object name pattern, within the context of a query</li>
0N/A * </ul>
0N/A *
0N/A * <p>An object name consists of two parts, the domain and the key
0N/A * properties.</p>
0N/A *
0N/A * <p>The <em>domain</em> is a string of characters not including
1790N/A * the character colon (<code>:</code>). It is recommended that the domain
1790N/A * should not contain the string "{@code //}", which is reserved for future use.
0N/A *
0N/A * <p>If the domain includes at least one occurrence of the wildcard
0N/A * characters asterisk (<code>*</code>) or question mark
0N/A * (<code>?</code>), then the object name is a pattern. The asterisk
0N/A * matches any sequence of zero or more characters, while the question
1790N/A * mark matches any single character.</p>
0N/A *
0N/A * <p>If the domain is empty, it will be replaced in certain contexts
0N/A * by the <em>default domain</em> of the MBean server in which the
0N/A * ObjectName is used.</p>
0N/A *
0N/A * <p>The <em>key properties</em> are an unordered set of keys and
0N/A * associated values.</p>
0N/A *
0N/A * <p>Each <em>key</em> is a nonempty string of characters which may
0N/A * not contain any of the characters comma (<code>,</code>), equals
0N/A * (<code>=</code>), colon, asterisk, or question mark. The same key
0N/A * may not occur twice in a given ObjectName.</p>
0N/A *
0N/A * <p>Each <em>value</em> associated with a key is a string of
0N/A * characters that is either unquoted or quoted.</p>
0N/A *
0N/A * <p>An <em>unquoted value</em> is a possibly empty string of
0N/A * characters which may not contain any of the characters comma,
0N/A * equals, colon, or quote.</p>
0N/A *
0N/A * <p>If the <em>unquoted value</em> contains at least one occurrence
0N/A * of the wildcard characters asterisk or question mark, then the object
0N/A * name is a <em>property value pattern</em>. The asterisk matches any
0N/A * sequence of zero or more characters, while the question mark matches
0N/A * any single character.</p>
0N/A *
0N/A * <p>A <em>quoted value</em> consists of a quote (<code>"</code>),
0N/A * followed by a possibly empty string of characters, followed by
0N/A * another quote. Within the string of characters, the backslash
0N/A * (<code>\</code>) has a special meaning. It must be followed by
0N/A * one of the following characters:</p>
0N/A *
0N/A * <ul>
0N/A * <li>Another backslash. The second backslash has no special
0N/A * meaning and the two characters represent a single backslash.</li>
0N/A *
0N/A * <li>The character 'n'. The two characters represent a newline
0N/A * ('\n' in Java).</li>
0N/A *
0N/A * <li>A quote. The two characters represent a quote, and that quote
0N/A * is not considered to terminate the quoted value. An ending closing
0N/A * quote must be present for the quoted value to be valid.</li>
0N/A *
0N/A * <li>A question mark (?) or asterisk (*). The two characters represent
0N/A * a question mark or asterisk respectively.</li>
0N/A * </ul>
0N/A *
0N/A * <p>A quote may not appear inside a quoted value except immediately
0N/A * after an odd number of consecutive backslashes.</p>
0N/A *
0N/A * <p>The quotes surrounding a quoted value, and any backslashes
0N/A * within that value, are considered to be part of the value.</p>
0N/A *
0N/A * <p>If the <em>quoted value</em> contains at least one occurrence of
0N/A * the characters asterisk or question mark and they are not preceded
0N/A * by a backslash, then they are considered as wildcard characters and
0N/A * the object name is a <em>property value pattern</em>. The asterisk
0N/A * matches any sequence of zero or more characters, while the question
0N/A * mark matches any single character.</p>
0N/A *
0N/A * <p>An ObjectName may be a <em>property list pattern</em>. In this
0N/A * case it may have zero or more keys and associated values. It matches
0N/A * a nonpattern ObjectName whose domain matches and that contains the
0N/A * same keys and associated values, as well as possibly other keys and
0N/A * values.</p>
0N/A *
0N/A * <p>An ObjectName is a <em>property value pattern</em> when at least
0N/A * one of its <em>quoted</em> or <em>unquoted</em> key property values
0N/A * contains the wildcard characters asterisk or question mark as described
0N/A * above. In this case it has one or more keys and associated values, with
0N/A * at least one of the values containing wildcard characters. It matches a
0N/A * nonpattern ObjectName whose domain matches and that contains the same
0N/A * keys whose values match; if the property value pattern is also a
0N/A * property list pattern then the nonpattern ObjectName can contain
0N/A * other keys and values.</p>
0N/A *
0N/A * <p>An ObjectName is a <em>property pattern</em> if it is either a
0N/A * <em>property list pattern</em> or a <em>property value pattern</em>
0N/A * or both.</p>
0N/A *
0N/A * <p>An ObjectName is a pattern if its domain contains a wildcard or
0N/A * if the ObjectName is a property pattern.</p>
0N/A *
0N/A * <p>If an ObjectName is not a pattern, it must contain at least one
0N/A * key with its associated value.</p>
0N/A *
0N/A * <p>Examples of ObjectName patterns are:</p>
0N/A *
0N/A * <ul>
0N/A * <li>{@code *:type=Foo,name=Bar} to match names in any domain whose
0N/A * exact set of keys is {@code type=Foo,name=Bar}.</li>
0N/A * <li>{@code d:type=Foo,name=Bar,*} to match names in the domain
0N/A * {@code d} that have the keys {@code type=Foo,name=Bar} plus
0N/A * zero or more other keys.</li>
0N/A * <li>{@code *:type=Foo,name=Bar,*} to match names in any domain
0N/A * that has the keys {@code type=Foo,name=Bar} plus zero or
0N/A * more other keys.</li>
0N/A * <li>{@code d:type=F?o,name=Bar} will match e.g.
0N/A * {@code d:type=Foo,name=Bar} and {@code d:type=Fro,name=Bar}.</li>
0N/A * <li>{@code d:type=F*o,name=Bar} will match e.g.
0N/A * {@code d:type=Fo,name=Bar} and {@code d:type=Frodo,name=Bar}.</li>
0N/A * <li>{@code d:type=Foo,name="B*"} will match e.g.
0N/A * {@code d:type=Foo,name="Bling"}. Wildcards are recognized even
0N/A * inside quotes, and like other special characters can be escaped
0N/A * with {@code \}.</li>
0N/A * </ul>
0N/A *
0N/A * <p>An ObjectName can be written as a String with the following
0N/A * elements in order:</p>
0N/A *
0N/A * <ul>
0N/A * <li>The domain.
0N/A * <li>A colon (<code>:</code>).
0N/A * <li>A key property list as defined below.
0N/A * </ul>
0N/A *
0N/A * <p>A key property list written as a String is a comma-separated
0N/A * list of elements. Each element is either an asterisk or a key
0N/A * property. A key property consists of a key, an equals
0N/A * (<code>=</code>), and the associated value.</p>
0N/A *
0N/A * <p>At most one element of a key property list may be an asterisk.
0N/A * If the key property list contains an asterisk element, the
0N/A * ObjectName is a property list pattern.</p>
0N/A *
0N/A * <p>Spaces have no special significance in a String representing an
0N/A * ObjectName. For example, the String:
0N/A * <pre>
0N/A * domain: key1 = value1 , key2 = value2
0N/A * </pre>
0N/A * represents an ObjectName with two keys. The name of each key
0N/A * contains six characters, of which the first and last are spaces.
0N/A * The value associated with the key <code>"&nbsp;key1&nbsp;"</code>
0N/A * also begins and ends with a space.</p>
0N/A *
0N/A * <p>In addition to the restrictions on characters spelt out above,
0N/A * no part of an ObjectName may contain a newline character
0N/A * (<code>'\n'</code>), whether the domain, a key, or a value, whether
0N/A * quoted or unquoted. The newline character can be represented in a
0N/A * quoted value with the sequence <code>\n</code>.
0N/A *
0N/A * <p>The rules on special characters and quoting apply regardless of
0N/A * which constructor is used to make an ObjectName.</p>
0N/A *
0N/A * <p>To avoid collisions between MBeans supplied by different
0N/A * vendors, a useful convention is to begin the domain name with the
0N/A * reverse DNS name of the organization that specifies the MBeans,
0N/A * followed by a period and a string whose interpretation is
0N/A * determined by that organization. For example, MBeans specified by
2997N/A * <code>example.com</code> would have
2997N/A * domains such as <code>com.example.MyDomain</code>. This is essentially
0N/A * the same convention as for Java-language package names.</p>
0N/A *
0N/A * <p>The <b>serialVersionUID</b> of this class is <code>1081892073854801359L</code>.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A@SuppressWarnings("serial") // don't complain serialVersionUID not constant
248N/Apublic class ObjectName implements Comparable<ObjectName>, QueryExp {
0N/A
0N/A /**
0N/A * A structure recording property structure and
0N/A * proposing minimal services
0N/A */
0N/A private static class Property {
0N/A
0N/A int _key_index;
0N/A int _key_length;
0N/A int _value_length;
0N/A
0N/A /**
0N/A * Constructor.
0N/A */
0N/A Property(int key_index, int key_length, int value_length) {
0N/A _key_index = key_index;
0N/A _key_length = key_length;
0N/A _value_length = value_length;
0N/A }
0N/A
0N/A /**
0N/A * Assigns the key index of property
0N/A */
0N/A void setKeyIndex(int key_index) {
0N/A _key_index = key_index;
0N/A }
0N/A
0N/A /**
0N/A * Returns a key string for receiver key
0N/A */
1790N/A String getKeyString(String name) {
1790N/A return name.substring(_key_index, _key_index + _key_length);
0N/A }
0N/A
0N/A /**
0N/A * Returns a value string for receiver key
0N/A */
1790N/A String getValueString(String name) {
1790N/A int in_begin = _key_index + _key_length + 1;
1790N/A int out_end = in_begin + _value_length;
0N/A return name.substring(in_begin, out_end);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Marker class for value pattern property.
0N/A */
0N/A private static class PatternProperty extends Property {
0N/A /**
0N/A * Constructor.
0N/A */
0N/A PatternProperty(int key_index, int key_length, int value_length) {
0N/A super(key_index, key_length, value_length);
0N/A }
0N/A }
0N/A
0N/A // Inner classes <========================================
0N/A
0N/A
0N/A
0N/A // Private fields ---------------------------------------->
0N/A
0N/A
0N/A // Serialization compatibility stuff -------------------->
0N/A
0N/A // Two serial forms are supported in this class. The selected form depends
0N/A // on system property "jmx.serial.form":
0N/A // - "1.0" for JMX 1.0
0N/A // - any other value for JMX 1.1 and higher
0N/A //
0N/A // Serial version for old serial form
0N/A private static final long oldSerialVersionUID = -5467795090068647408L;
0N/A //
0N/A // Serial version for new serial form
0N/A private static final long newSerialVersionUID = 1081892073854801359L;
0N/A //
0N/A // Serializable fields in old serial form
0N/A private static final ObjectStreamField[] oldSerialPersistentFields =
0N/A {
0N/A new ObjectStreamField("domain", String.class),
0N/A new ObjectStreamField("propertyList", Hashtable.class),
0N/A new ObjectStreamField("propertyListString", String.class),
0N/A new ObjectStreamField("canonicalName", String.class),
0N/A new ObjectStreamField("pattern", Boolean.TYPE),
0N/A new ObjectStreamField("propertyPattern", Boolean.TYPE)
0N/A };
0N/A //
0N/A // Serializable fields in new serial form
0N/A private static final ObjectStreamField[] newSerialPersistentFields = { };
0N/A //
0N/A // Actual serial version and serial form
0N/A private static final long serialVersionUID;
0N/A private static final ObjectStreamField[] serialPersistentFields;
0N/A private static boolean compat = false;
0N/A static {
0N/A try {
0N/A GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
0N/A String form = AccessController.doPrivileged(act);
0N/A compat = (form != null && form.equals("1.0"));
0N/A } catch (Exception e) {
0N/A // OK: exception means no compat with 1.0, too bad
0N/A }
0N/A if (compat) {
0N/A serialPersistentFields = oldSerialPersistentFields;
0N/A serialVersionUID = oldSerialVersionUID;
0N/A } else {
0N/A serialPersistentFields = newSerialPersistentFields;
0N/A serialVersionUID = newSerialVersionUID;
0N/A }
0N/A }
0N/A
0N/A //
0N/A // Serialization compatibility stuff <==============================
0N/A
0N/A // Class private fields ----------------------------------->
0N/A
0N/A /**
0N/A * a shared empty array for empty property lists
0N/A */
0N/A static final private Property[] _Empty_property_array = new Property[0];
0N/A
0N/A
0N/A // Class private fields <==============================
0N/A
0N/A // Instance private fields ----------------------------------->
0N/A
0N/A /**
0N/A * a String containing the canonical name
0N/A */
0N/A private transient String _canonicalName;
0N/A
0N/A
0N/A /**
0N/A * An array of properties in the same seq order as time creation
0N/A */
0N/A private transient Property[] _kp_array;
0N/A
0N/A /**
0N/A * An array of properties in the same seq order as canonical order
0N/A */
0N/A private transient Property[] _ca_array;
0N/A
0N/A
0N/A /**
0N/A * The length of the domain part of built objectname
0N/A */
0N/A private transient int _domain_length = 0;
0N/A
0N/A
0N/A /**
0N/A * The propertyList of built object name. Initialized lazily.
0N/A * Table that contains all the pairs (key,value) for this ObjectName.
0N/A */
0N/A private transient Map<String,String> _propertyList;
0N/A
0N/A /**
0N/A * boolean that declares if this ObjectName domain part is a pattern
0N/A */
0N/A private transient boolean _domain_pattern = false;
0N/A
0N/A /**
0N/A * boolean that declares if this ObjectName contains a pattern on the
0N/A * key property list
0N/A */
0N/A private transient boolean _property_list_pattern = false;
0N/A
0N/A /**
0N/A * boolean that declares if this ObjectName contains a pattern on the
0N/A * value of at least one key property
0N/A */
0N/A private transient boolean _property_value_pattern = false;
0N/A
0N/A // Instance private fields <=======================================
0N/A
0N/A // Private fields <========================================
0N/A
0N/A
0N/A // Private methods ---------------------------------------->
0N/A
0N/A // Category : Instance construction ------------------------->
0N/A
0N/A /**
0N/A * Initializes this {@link ObjectName} from the given string
0N/A * representation.
0N/A *
0N/A * @param name A string representation of the {@link ObjectName}
0N/A *
0N/A * @exception MalformedObjectNameException The string passed as a
0N/A * parameter does not have the right format.
0N/A * @exception NullPointerException The <code>name</code> parameter
0N/A * is null.
0N/A */
0N/A private void construct(String name)
572N/A throws MalformedObjectNameException {
0N/A
0N/A // The name cannot be null
0N/A if (name == null)
0N/A throw new NullPointerException("name cannot be null");
0N/A
0N/A // Test if the name is empty
0N/A if (name.length() == 0) {
0N/A // this is equivalent to the whole word query object name.
0N/A _canonicalName = "*:*";
0N/A _kp_array = _Empty_property_array;
0N/A _ca_array = _Empty_property_array;
0N/A _domain_length = 1;
0N/A _propertyList = null;
0N/A _domain_pattern = true;
0N/A _property_list_pattern = true;
0N/A _property_value_pattern = false;
0N/A return;
0N/A }
0N/A
0N/A // initialize parsing of the string
528N/A final char[] name_chars = name.toCharArray();
528N/A final int len = name_chars.length;
528N/A final char[] canonical_chars = new char[len]; // canonical form will
528N/A // be same length at most
0N/A int cname_index = 0;
0N/A int index = 0;
0N/A char c, c1;
0N/A
0N/A // parses domain part
0N/A domain_parsing:
0N/A while (index < len) {
277N/A switch (name_chars[index]) {
0N/A case ':' :
0N/A _domain_length = index++;
0N/A break domain_parsing;
0N/A case '=' :
0N/A // ":" omission check.
0N/A //
0N/A // Although "=" is a valid character in the domain part
0N/A // it is true that it is rarely used in the real world.
0N/A // So check straight away if the ":" has been omitted
0N/A // from the ObjectName. This allows us to provide a more
0N/A // accurate exception message.
0N/A int i = ++index;
0N/A while ((i < len) && (name_chars[i++] != ':'))
0N/A if (i == len)
0N/A throw new MalformedObjectNameException(
0N/A "Domain part must be specified");
0N/A break;
0N/A case '\n' :
0N/A throw new MalformedObjectNameException(
0N/A "Invalid character '\\n' in domain name");
0N/A case '*' :
0N/A case '?' :
0N/A _domain_pattern = true;
0N/A index++;
0N/A break;
0N/A default :
0N/A index++;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A // check for non-empty properties
0N/A if (index == len)
0N/A throw new MalformedObjectNameException(
0N/A "Key properties cannot be empty");
0N/A
0N/A // we have got the domain part, begins building of _canonicalName
0N/A System.arraycopy(name_chars, 0, canonical_chars, 0, _domain_length);
0N/A canonical_chars[_domain_length] = ':';
0N/A cname_index = _domain_length + 1;
0N/A
0N/A // parses property list
0N/A Property prop;
0N/A Map<String,Property> keys_map = new HashMap<String,Property>();
0N/A String[] keys;
0N/A String key_name;
0N/A boolean quoted_value;
0N/A int property_index = 0;
0N/A int in_index;
0N/A int key_index, key_length, value_index, value_length;
0N/A
0N/A keys = new String[10];
0N/A _kp_array = new Property[10];
0N/A _property_list_pattern = false;
0N/A _property_value_pattern = false;
0N/A
0N/A while (index < len) {
0N/A c = name_chars[index];
0N/A
0N/A // case of pattern properties
0N/A if (c == '*') {
0N/A if (_property_list_pattern)
0N/A throw new MalformedObjectNameException(
0N/A "Cannot have several '*' characters in pattern " +
0N/A "property list");
0N/A else {
0N/A _property_list_pattern = true;
0N/A if ((++index < len ) && (name_chars[index] != ','))
0N/A throw new MalformedObjectNameException(
0N/A "Invalid character found after '*': end of " +
0N/A "name or ',' expected");
0N/A else if (index == len) {
0N/A if (property_index == 0) {
0N/A // empty properties case
0N/A _kp_array = _Empty_property_array;
0N/A _ca_array = _Empty_property_array;
0N/A _propertyList = Collections.emptyMap();
0N/A }
0N/A break;
0N/A } else {
0N/A // correct pattern spec in props, continue
0N/A index++;
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // standard property case, key part
0N/A in_index = index;
0N/A key_index = in_index;
0N/A if (name_chars[in_index] == '=')
0N/A throw new MalformedObjectNameException("Invalid key (empty)");
0N/A while ((in_index < len) && ((c1 = name_chars[in_index++]) != '='))
0N/A switch (c1) {
0N/A // '=' considered to introduce value part
0N/A case '*' :
0N/A case '?' :
0N/A case ',' :
0N/A case ':' :
0N/A case '\n' :
0N/A final String ichar = ((c1=='\n')?"\\n":""+c1);
0N/A throw new MalformedObjectNameException(
0N/A "Invalid character '" + ichar +
0N/A "' in key part of property");
0N/A }
0N/A if (name_chars[in_index - 1] != '=')
0N/A throw new MalformedObjectNameException(
0N/A "Unterminated key property part");
0N/A value_index = in_index; // in_index pointing after '=' char
0N/A key_length = value_index - key_index - 1; // found end of key
0N/A
0N/A // standard property case, value part
0N/A boolean value_pattern = false;
0N/A if (in_index < len && name_chars[in_index] == '\"') {
0N/A quoted_value = true;
0N/A // the case of quoted value part
0N/A quoted_value_parsing:
0N/A while ((++in_index < len) &&
0N/A ((c1 = name_chars[in_index]) != '\"')) {
0N/A // the case of an escaped character
0N/A if (c1 == '\\') {
0N/A if (++in_index == len)
0N/A throw new MalformedObjectNameException(
0N/A "Unterminated quoted value");
0N/A switch (c1 = name_chars[in_index]) {
0N/A case '\\' :
0N/A case '\"' :
0N/A case '?' :
0N/A case '*' :
0N/A case 'n' :
0N/A break; // valid character
0N/A default :
0N/A throw new MalformedObjectNameException(
0N/A "Invalid escape sequence '\\" +
0N/A c1 + "' in quoted value");
0N/A }
0N/A } else if (c1 == '\n') {
0N/A throw new MalformedObjectNameException(
0N/A "Newline in quoted value");
0N/A } else {
0N/A switch (c1) {
0N/A case '?' :
0N/A case '*' :
0N/A value_pattern = true;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A if (in_index == len)
0N/A throw new MalformedObjectNameException(
0N/A "Unterminated quoted value");
0N/A else value_length = ++in_index - value_index;
0N/A } else {
0N/A // the case of standard value part
0N/A quoted_value = false;
0N/A while ((in_index < len) && ((c1 = name_chars[in_index]) != ','))
0N/A switch (c1) {
0N/A // ',' considered to be the value separator
0N/A case '*' :
0N/A case '?' :
0N/A value_pattern = true;
0N/A in_index++;
0N/A break;
0N/A case '=' :
0N/A case ':' :
0N/A case '"' :
0N/A case '\n' :
0N/A final String ichar = ((c1=='\n')?"\\n":""+c1);
0N/A throw new MalformedObjectNameException(
277N/A "Invalid character '" + ichar +
0N/A "' in value part of property");
0N/A default :
0N/A in_index++;
0N/A break;
0N/A }
0N/A value_length = in_index - value_index;
0N/A }
0N/A
0N/A // Parsed property, checks the end of name
0N/A if (in_index == len - 1) {
0N/A if (quoted_value)
0N/A throw new MalformedObjectNameException(
0N/A "Invalid ending character `" +
0N/A name_chars[in_index] + "'");
0N/A else throw new MalformedObjectNameException(
0N/A "Invalid ending comma");
0N/A } else in_index++;
0N/A
0N/A // we got the key and value part, prepare a property for this
0N/A if (!value_pattern) {
1790N/A prop = new Property(key_index, key_length, value_length);
0N/A } else {
0N/A _property_value_pattern = true;
1790N/A prop = new PatternProperty(key_index, key_length, value_length);
0N/A }
0N/A key_name = name.substring(key_index, key_index + key_length);
0N/A
0N/A if (property_index == keys.length) {
0N/A String[] tmp_string_array = new String[property_index + 10];
0N/A System.arraycopy(keys, 0, tmp_string_array, 0, property_index);
0N/A keys = tmp_string_array;
0N/A }
0N/A keys[property_index] = key_name;
0N/A
0N/A addProperty(prop, property_index, keys_map, key_name);
0N/A property_index++;
0N/A index = in_index;
0N/A }
0N/A
0N/A // computes and set canonical name
0N/A setCanonicalName(name_chars, canonical_chars, keys,
0N/A keys_map, cname_index, property_index);
0N/A }
0N/A
0N/A /**
0N/A * Construct an ObjectName from a domain and a Hashtable.
0N/A *
0N/A * @param domain Domain of the ObjectName.
0N/A * @param props Map containing couples <i>key</i> -> <i>value</i>.
0N/A *
0N/A * @exception MalformedObjectNameException The <code>domain</code>
0N/A * contains an illegal character, or one of the keys or values in
0N/A * <code>table</code> contains an illegal character, or one of the
0N/A * values in <code>table</code> does not follow the rules for quoting.
0N/A * @exception NullPointerException One of the parameters is null.
0N/A */
0N/A private void construct(String domain, Map<String,String> props)
572N/A throws MalformedObjectNameException {
0N/A
0N/A // The domain cannot be null
0N/A if (domain == null)
0N/A throw new NullPointerException("domain cannot be null");
0N/A
0N/A // The key property list cannot be null
0N/A if (props == null)
0N/A throw new NullPointerException("key property list cannot be null");
0N/A
0N/A // The key property list cannot be empty
0N/A if (props.isEmpty())
0N/A throw new MalformedObjectNameException(
0N/A "key property list cannot be empty");
0N/A
0N/A // checks domain validity
0N/A if (!isDomain(domain))
0N/A throw new MalformedObjectNameException("Invalid domain: " + domain);
0N/A
0N/A // init canonicalname
0N/A final StringBuilder sb = new StringBuilder();
0N/A sb.append(domain).append(':');
0N/A _domain_length = domain.length();
0N/A
0N/A // allocates the property array
0N/A int nb_props = props.size();
0N/A _kp_array = new Property[nb_props];
0N/A
0N/A String[] keys = new String[nb_props];
0N/A final Map<String,Property> keys_map = new HashMap<String,Property>();
0N/A Property prop;
0N/A int key_index;
0N/A int i = 0;
0N/A for (Map.Entry<String,String> entry : props.entrySet()) {
0N/A if (sb.length() > 0)
0N/A sb.append(",");
0N/A String key = entry.getKey();
0N/A String value;
0N/A try {
0N/A value = entry.getValue();
0N/A } catch (ClassCastException e) {
0N/A throw new MalformedObjectNameException(e.getMessage());
0N/A }
0N/A key_index = sb.length();
0N/A checkKey(key);
0N/A sb.append(key);
0N/A keys[i] = key;
0N/A sb.append("=");
0N/A boolean value_pattern = checkValue(value);
0N/A sb.append(value);
0N/A if (!value_pattern) {
1790N/A prop = new Property(key_index,
0N/A key.length(),
0N/A value.length());
0N/A } else {
0N/A _property_value_pattern = true;
1790N/A prop = new PatternProperty(key_index,
0N/A key.length(),
0N/A value.length());
0N/A }
0N/A addProperty(prop, i, keys_map, key);
0N/A i++;
0N/A }
0N/A
0N/A // initialize canonical name and data structure
0N/A int len = sb.length();
0N/A char[] initial_chars = new char[len];
0N/A sb.getChars(0, len, initial_chars, 0);
0N/A char[] canonical_chars = new char[len];
0N/A System.arraycopy(initial_chars, 0, canonical_chars, 0,
0N/A _domain_length + 1);
0N/A setCanonicalName(initial_chars, canonical_chars, keys, keys_map,
0N/A _domain_length + 1, _kp_array.length);
0N/A }
0N/A // Category : Instance construction <==============================
0N/A
0N/A // Category : Internal utilities ------------------------------>
0N/A
0N/A /**
0N/A * Add passed property to the list at the given index
0N/A * for the passed key name
0N/A */
0N/A private void addProperty(Property prop, int index,
0N/A Map<String,Property> keys_map, String key_name)
0N/A throws MalformedObjectNameException {
0N/A
0N/A if (keys_map.containsKey(key_name)) throw new
0N/A MalformedObjectNameException("key `" +
0N/A key_name +"' already defined");
0N/A
0N/A // if no more space for property arrays, have to increase it
0N/A if (index == _kp_array.length) {
0N/A Property[] tmp_prop_array = new Property[index + 10];
0N/A System.arraycopy(_kp_array, 0, tmp_prop_array, 0, index);
0N/A _kp_array = tmp_prop_array;
0N/A }
0N/A _kp_array[index] = prop;
0N/A keys_map.put(key_name, prop);
0N/A }
0N/A
0N/A /**
0N/A * Sets the canonical name of receiver from input 'specified_chars'
0N/A * array, by filling 'canonical_chars' array with found 'nb-props'
0N/A * properties starting at position 'prop_index'.
0N/A */
0N/A private void setCanonicalName(char[] specified_chars,
0N/A char[] canonical_chars,
0N/A String[] keys, Map<String,Property> keys_map,
0N/A int prop_index, int nb_props) {
0N/A
0N/A // Sort the list of found properties
0N/A if (_kp_array != _Empty_property_array) {
0N/A String[] tmp_keys = new String[nb_props];
0N/A Property[] tmp_props = new Property[nb_props];
0N/A
0N/A System.arraycopy(keys, 0, tmp_keys, 0, nb_props);
0N/A Arrays.sort(tmp_keys);
0N/A keys = tmp_keys;
0N/A System.arraycopy(_kp_array, 0, tmp_props, 0 , nb_props);
0N/A _kp_array = tmp_props;
0N/A _ca_array = new Property[nb_props];
0N/A
0N/A // now assigns _ca_array to the sorted list of keys
0N/A // (there cannot be two identical keys in an objectname.
0N/A for (int i = 0; i < nb_props; i++)
0N/A _ca_array[i] = keys_map.get(keys[i]);
0N/A
0N/A // now we build the canonical name and set begin indexes of
0N/A // properties to reflect canonical form
0N/A int last_index = nb_props - 1;
0N/A int prop_len;
0N/A Property prop;
0N/A for (int i = 0; i <= last_index; i++) {
0N/A prop = _ca_array[i];
0N/A // length of prop including '=' char
0N/A prop_len = prop._key_length + prop._value_length + 1;
1790N/A System.arraycopy(specified_chars, prop._key_index,
0N/A canonical_chars, prop_index, prop_len);
1790N/A prop.setKeyIndex(prop_index);
0N/A prop_index += prop_len;
0N/A if (i != last_index) {
0N/A canonical_chars[prop_index] = ',';
0N/A prop_index++;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // terminate canonicalname with '*' in case of pattern
0N/A if (_property_list_pattern) {
0N/A if (_kp_array != _Empty_property_array)
0N/A canonical_chars[prop_index++] = ',';
0N/A canonical_chars[prop_index++] = '*';
0N/A }
0N/A
0N/A // we now build the canonicalname string
0N/A _canonicalName = (new String(canonical_chars, 0, prop_index)).intern();
0N/A }
0N/A
0N/A /**
0N/A * Parse a key.
0N/A * <pre>final int endKey=parseKey(s,startKey);</pre>
0N/A * <p>key starts at startKey (included), and ends at endKey (excluded).
0N/A * If (startKey == endKey), then the key is empty.
0N/A *
0N/A * @param s The char array of the original string.
0N/A * @param startKey index at which to begin parsing.
0N/A * @return The index following the last character of the key.
0N/A **/
0N/A private static int parseKey(final char[] s, final int startKey)
0N/A throws MalformedObjectNameException {
0N/A int next = startKey;
0N/A int endKey = startKey;
0N/A final int len = s.length;
0N/A while (next < len) {
0N/A final char k = s[next++];
0N/A switch (k) {
0N/A case '*':
0N/A case '?':
0N/A case ',':
0N/A case ':':
0N/A case '\n':
0N/A final String ichar = ((k=='\n')?"\\n":""+k);
0N/A throw new
0N/A MalformedObjectNameException("Invalid character in key: `"
0N/A + ichar + "'");
0N/A case '=':
0N/A // we got the key.
0N/A endKey = next-1;
0N/A break;
0N/A default:
0N/A if (next < len) continue;
0N/A else endKey=next;
0N/A }
0N/A break;
0N/A }
0N/A return endKey;
0N/A }
0N/A
0N/A /**
0N/A * Parse a value.
0N/A * <pre>final int endVal=parseValue(s,startVal);</pre>
0N/A * <p>value starts at startVal (included), and ends at endVal (excluded).
0N/A * If (startVal == endVal), then the key is empty.
0N/A *
0N/A * @param s The char array of the original string.
0N/A * @param startValue index at which to begin parsing.
0N/A * @return The first element of the int array indicates the index
0N/A * following the last character of the value. The second
0N/A * element of the int array indicates that the value is
0N/A * a pattern when its value equals 1.
0N/A **/
0N/A private static int[] parseValue(final char[] s, final int startValue)
0N/A throws MalformedObjectNameException {
0N/A
0N/A boolean value_pattern = false;
0N/A
0N/A int next = startValue;
0N/A int endValue = startValue;
0N/A
0N/A final int len = s.length;
0N/A final char q=s[startValue];
0N/A
0N/A if (q == '"') {
0N/A // quoted value
0N/A if (++next == len) throw new
0N/A MalformedObjectNameException("Invalid quote");
0N/A while (next < len) {
0N/A char last = s[next];
0N/A if (last == '\\') {
0N/A if (++next == len) throw new
0N/A MalformedObjectNameException(
0N/A "Invalid unterminated quoted character sequence");
0N/A last = s[next];
0N/A switch (last) {
0N/A case '\\' :
0N/A case '?' :
0N/A case '*' :
0N/A case 'n' :
0N/A break;
0N/A case '\"' :
0N/A // We have an escaped quote. If this escaped
0N/A // quote is the last character, it does not
0N/A // qualify as a valid termination quote.
0N/A //
0N/A if (next+1 == len) throw new
0N/A MalformedObjectNameException(
0N/A "Missing termination quote");
0N/A break;
0N/A default:
0N/A throw new
0N/A MalformedObjectNameException(
0N/A "Invalid quoted character sequence '\\" +
0N/A last + "'");
0N/A }
0N/A } else if (last == '\n') {
0N/A throw new MalformedObjectNameException(
0N/A "Newline in quoted value");
0N/A } else if (last == '\"') {
0N/A next++;
0N/A break;
0N/A } else {
0N/A switch (last) {
0N/A case '?' :
0N/A case '*' :
0N/A value_pattern = true;
0N/A break;
0N/A }
0N/A }
0N/A next++;
0N/A
0N/A // Check that last character is a termination quote.
0N/A // We have already handled the case were the last
0N/A // character is an escaped quote earlier.
0N/A //
0N/A if ((next >= len) && (last != '\"')) throw new
0N/A MalformedObjectNameException("Missing termination quote");
0N/A }
0N/A endValue = next;
0N/A if (next < len) {
0N/A if (s[next++] != ',') throw new
0N/A MalformedObjectNameException("Invalid quote");
0N/A }
0N/A } else {
0N/A // Non quoted value.
0N/A while (next < len) {
0N/A final char v=s[next++];
0N/A switch(v) {
0N/A case '*':
0N/A case '?':
0N/A value_pattern = true;
0N/A if (next < len) continue;
0N/A else endValue=next;
0N/A break;
0N/A case '=':
0N/A case ':':
0N/A case '\n' :
0N/A final String ichar = ((v=='\n')?"\\n":""+v);
0N/A throw new
0N/A MalformedObjectNameException("Invalid character `" +
0N/A ichar + "' in value");
0N/A case ',':
0N/A endValue = next-1;
0N/A break;
0N/A default:
0N/A if (next < len) continue;
0N/A else endValue=next;
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A return new int[] { endValue, value_pattern ? 1 : 0 };
0N/A }
0N/A
0N/A /**
0N/A * Check if the supplied value is a valid value.
0N/A *
0N/A * @return true if the value is a pattern, otherwise false.
0N/A */
0N/A private static boolean checkValue(String val)
0N/A throws MalformedObjectNameException {
0N/A
0N/A if (val == null) throw new
0N/A NullPointerException("Invalid value (null)");
0N/A
0N/A final int len = val.length();
0N/A if (len == 0)
0N/A return false;
0N/A
0N/A final char[] s = val.toCharArray();
0N/A final int[] result = parseValue(s,0);
0N/A final int endValue = result[0];
0N/A final boolean value_pattern = result[1] == 1;
0N/A if (endValue < len) throw new
0N/A MalformedObjectNameException("Invalid character in value: `" +
0N/A s[endValue] + "'");
0N/A return value_pattern;
0N/A }
0N/A
0N/A /**
0N/A * Check if the supplied key is a valid key.
0N/A */
0N/A private static void checkKey(String key)
572N/A throws MalformedObjectNameException {
0N/A
0N/A if (key == null) throw new
0N/A NullPointerException("Invalid key (null)");
0N/A
0N/A final int len = key.length();
0N/A if (len == 0) throw new
0N/A MalformedObjectNameException("Invalid key (empty)");
0N/A final char[] k=key.toCharArray();
0N/A final int endKey = parseKey(k,0);
0N/A if (endKey < len) throw new
0N/A MalformedObjectNameException("Invalid character in value: `" +
0N/A k[endKey] + "'");
0N/A }
0N/A
0N/A
0N/A // Category : Internal utilities <==============================
0N/A
0N/A // Category : Internal accessors ------------------------------>
0N/A
0N/A /**
0N/A * Check if domain is a valid domain. Set _domain_pattern if appropriate.
0N/A */
0N/A private boolean isDomain(String domain) {
0N/A if (domain == null) return true;
745N/A final int len = domain.length();
0N/A int next = 0;
0N/A while (next < len) {
745N/A final char c = domain.charAt(next++);
0N/A switch (c) {
0N/A case ':' :
0N/A case '\n' :
0N/A return false;
0N/A case '*' :
0N/A case '?' :
0N/A _domain_pattern = true;
0N/A break;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A // Category : Internal accessors <==============================
0N/A
0N/A // Category : Serialization ----------------------------------->
0N/A
0N/A /**
0N/A * Deserializes an {@link ObjectName} from an {@link ObjectInputStream}.
0N/A * @serialData <ul>
0N/A * <li>In the current serial form (value of property
0N/A * <code>jmx.serial.form</code> differs from
0N/A * <code>1.0</code>): the string
0N/A * &quot;&lt;domain&gt;:&lt;properties&gt;&lt;wild&gt;&quot;,
0N/A * where: <ul>
0N/A * <li>&lt;domain&gt; represents the domain part
0N/A * of the {@link ObjectName}</li>
0N/A * <li>&lt;properties&gt; represents the list of
0N/A * properties, as returned by
0N/A * {@link #getKeyPropertyListString}
0N/A * <li>&lt;wild&gt; is empty if not
0N/A * <code>isPropertyPattern</code>, or
0N/A * is the character "<code>*</code>" if
0N/A * <code>isPropertyPattern</code>
0N/A * and &lt;properties&gt; is empty, or
0N/A * is "<code>,*</code>" if
0N/A * <code>isPropertyPattern</code> and
0N/A * &lt;properties&gt; is not empty.
0N/A * </li>
0N/A * </ul>
0N/A * The intent is that this string could be supplied
0N/A * to the {@link #ObjectName(String)} constructor to
0N/A * produce an equivalent {@link ObjectName}.
0N/A * </li>
0N/A * <li>In the old serial form (value of property
0N/A * <code>jmx.serial.form</code> is
0N/A * <code>1.0</code>): &lt;domain&gt; &lt;propertyList&gt;
0N/A * &lt;propertyListString&gt; &lt;canonicalName&gt;
0N/A * &lt;pattern&gt; &lt;propertyPattern&gt;,
0N/A * where: <ul>
0N/A * <li>&lt;domain&gt; represents the domain part
0N/A * of the {@link ObjectName}</li>
0N/A * <li>&lt;propertyList&gt; is the
0N/A * {@link Hashtable} that contains all the
0N/A * pairs (key,value) for this
0N/A * {@link ObjectName}</li>
0N/A * <li>&lt;propertyListString&gt; is the
0N/A * {@link String} representation of the
0N/A * list of properties in any order (not
0N/A * mandatorily a canonical representation)
0N/A * </li>
0N/A * <li>&lt;canonicalName&gt; is the
0N/A * {@link String} containing this
0N/A * {@link ObjectName}'s canonical name</li>
0N/A * <li>&lt;pattern&gt; is a boolean which is
0N/A * <code>true</code> if this
0N/A * {@link ObjectName} contains a pattern</li>
0N/A * <li>&lt;propertyPattern&gt; is a boolean which
0N/A * is <code>true</code> if this
0N/A * {@link ObjectName} contains a pattern in
0N/A * the list of properties</li>
0N/A * </ul>
0N/A * </li>
0N/A * </ul>
0N/A */
0N/A private void readObject(ObjectInputStream in)
0N/A throws IOException, ClassNotFoundException {
0N/A
0N/A String cn;
0N/A if (compat) {
0N/A // Read an object serialized in the old serial form
0N/A //
0N/A //in.defaultReadObject();
0N/A final ObjectInputStream.GetField fields = in.readFields();
470N/A String propListString =
470N/A (String)fields.get("propertyListString", "");
470N/A
470N/A // 6616825: take care of property patterns
470N/A final boolean propPattern =
470N/A fields.get("propertyPattern" , false);
470N/A if (propPattern) {
470N/A propListString =
470N/A (propListString.length()==0?"*":(propListString+",*"));
470N/A }
470N/A
0N/A cn = (String)fields.get("domain", "default")+
470N/A ":"+ propListString;
0N/A } else {
0N/A // Read an object serialized in the new serial form
0N/A //
0N/A in.defaultReadObject();
0N/A cn = (String)in.readObject();
0N/A }
0N/A
0N/A try {
1790N/A construct(cn);
0N/A } catch (NullPointerException e) {
0N/A throw new InvalidObjectException(e.toString());
0N/A } catch (MalformedObjectNameException e) {
0N/A throw new InvalidObjectException(e.toString());
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Serializes an {@link ObjectName} to an {@link ObjectOutputStream}.
0N/A * @serialData <ul>
0N/A * <li>In the current serial form (value of property
0N/A * <code>jmx.serial.form</code> differs from
0N/A * <code>1.0</code>): the string
0N/A * &quot;&lt;domain&gt;:&lt;properties&gt;&lt;wild&gt;&quot;,
0N/A * where: <ul>
0N/A * <li>&lt;domain&gt; represents the domain part
0N/A * of the {@link ObjectName}</li>
0N/A * <li>&lt;properties&gt; represents the list of
0N/A * properties, as returned by
0N/A * {@link #getKeyPropertyListString}
0N/A * <li>&lt;wild&gt; is empty if not
0N/A * <code>isPropertyPattern</code>, or
0N/A * is the character "<code>*</code>" if
0N/A * this <code>isPropertyPattern</code>
0N/A * and &lt;properties&gt; is empty, or
0N/A * is "<code>,*</code>" if
0N/A * <code>isPropertyPattern</code> and
0N/A * &lt;properties&gt; is not empty.
0N/A * </li>
0N/A * </ul>
0N/A * The intent is that this string could be supplied
0N/A * to the {@link #ObjectName(String)} constructor to
0N/A * produce an equivalent {@link ObjectName}.
0N/A * </li>
0N/A * <li>In the old serial form (value of property
0N/A * <code>jmx.serial.form</code> is
0N/A * <code>1.0</code>): &lt;domain&gt; &lt;propertyList&gt;
0N/A * &lt;propertyListString&gt; &lt;canonicalName&gt;
0N/A * &lt;pattern&gt; &lt;propertyPattern&gt;,
0N/A * where: <ul>
0N/A * <li>&lt;domain&gt; represents the domain part
0N/A * of the {@link ObjectName}</li>
0N/A * <li>&lt;propertyList&gt; is the
0N/A * {@link Hashtable} that contains all the
0N/A * pairs (key,value) for this
0N/A * {@link ObjectName}</li>
0N/A * <li>&lt;propertyListString&gt; is the
0N/A * {@link String} representation of the
0N/A * list of properties in any order (not
0N/A * mandatorily a canonical representation)
0N/A * </li>
0N/A * <li>&lt;canonicalName&gt; is the
0N/A * {@link String} containing this
0N/A * {@link ObjectName}'s canonical name</li>
0N/A * <li>&lt;pattern&gt; is a boolean which is
0N/A * <code>true</code> if this
0N/A * {@link ObjectName} contains a pattern</li>
0N/A * <li>&lt;propertyPattern&gt; is a boolean which
0N/A * is <code>true</code> if this
0N/A * {@link ObjectName} contains a pattern in
0N/A * the list of properties</li>
0N/A * </ul>
0N/A * </li>
0N/A * </ul>
0N/A */
0N/A private void writeObject(ObjectOutputStream out)
0N/A throws IOException {
0N/A
0N/A if (compat)
0N/A {
0N/A // Serializes this instance in the old serial form
0N/A // Read CR 6441274 before making any changes to this code
0N/A ObjectOutputStream.PutField fields = out.putFields();
1790N/A fields.put("domain", _canonicalName.substring(0, _domain_length));
0N/A fields.put("propertyList", getKeyPropertyList());
0N/A fields.put("propertyListString", getKeyPropertyListString());
1790N/A fields.put("canonicalName", _canonicalName);
0N/A fields.put("pattern", (_domain_pattern || _property_list_pattern));
0N/A fields.put("propertyPattern", _property_list_pattern);
0N/A out.writeFields();
0N/A }
0N/A else
0N/A {
0N/A // Serializes this instance in the new serial form
0N/A //
0N/A out.defaultWriteObject();
1790N/A out.writeObject(getSerializedNameString());
0N/A }
0N/A }
0N/A
0N/A // Category : Serialization <===================================
0N/A
0N/A // Private methods <========================================
0N/A
0N/A // Public methods ---------------------------------------->
0N/A
0N/A // Category : ObjectName Construction ------------------------------>
0N/A
0N/A /**
0N/A * <p>Return an instance of ObjectName that can be used anywhere
0N/A * an object obtained with {@link #ObjectName(String) new
0N/A * ObjectName(name)} can be used. The returned object may be of
0N/A * a subclass of ObjectName. Calling this method twice with the
0N/A * same parameters may return the same object or two equal but
0N/A * not identical objects.</p>
0N/A *
0N/A * @param name A string representation of the object name.
0N/A *
0N/A * @return an ObjectName corresponding to the given String.
0N/A *
0N/A * @exception MalformedObjectNameException The string passed as a
0N/A * parameter does not have the right format.
0N/A * @exception NullPointerException The <code>name</code> parameter
0N/A * is null.
0N/A *
0N/A */
0N/A public static ObjectName getInstance(String name)
1790N/A throws MalformedObjectNameException, NullPointerException {
0N/A return new ObjectName(name);
0N/A }
0N/A
0N/A /**
0N/A * <p>Return an instance of ObjectName that can be used anywhere
0N/A * an object obtained with {@link #ObjectName(String, String,
0N/A * String) new ObjectName(domain, key, value)} can be used. The
0N/A * returned object may be of a subclass of ObjectName. Calling
0N/A * this method twice with the same parameters may return the same
0N/A * object or two equal but not identical objects.</p>
0N/A *
0N/A * @param domain The domain part of the object name.
0N/A * @param key The attribute in the key property of the object name.
0N/A * @param value The value in the key property of the object name.
0N/A *
0N/A * @return an ObjectName corresponding to the given domain,
0N/A * key, and value.
0N/A *
0N/A * @exception MalformedObjectNameException The
0N/A * <code>domain</code>, <code>key</code>, or <code>value</code>
0N/A * contains an illegal character, or <code>value</code> does not
0N/A * follow the rules for quoting.
0N/A * @exception NullPointerException One of the parameters is null.
0N/A *
0N/A */
0N/A public static ObjectName getInstance(String domain, String key,
0N/A String value)
572N/A throws MalformedObjectNameException {
0N/A return new ObjectName(domain, key, value);
0N/A }
0N/A
0N/A /**
0N/A * <p>Return an instance of ObjectName that can be used anywhere
0N/A * an object obtained with {@link #ObjectName(String, Hashtable)
0N/A * new ObjectName(domain, table)} can be used. The returned
0N/A * object may be of a subclass of ObjectName. Calling this method
0N/A * twice with the same parameters may return the same object or
0N/A * two equal but not identical objects.</p>
0N/A *
0N/A * @param domain The domain part of the object name.
0N/A * @param table A hash table containing one or more key
0N/A * properties. The key of each entry in the table is the key of a
0N/A * key property in the object name. The associated value in the
0N/A * table is the associated value in the object name.
0N/A *
0N/A * @return an ObjectName corresponding to the given domain and
0N/A * key mappings.
0N/A *
0N/A * @exception MalformedObjectNameException The <code>domain</code>
0N/A * contains an illegal character, or one of the keys or values in
0N/A * <code>table</code> contains an illegal character, or one of the
0N/A * values in <code>table</code> does not follow the rules for
0N/A * quoting.
0N/A * @exception NullPointerException One of the parameters is null.
0N/A *
0N/A */
0N/A public static ObjectName getInstance(String domain,
0N/A Hashtable<String,String> table)
572N/A throws MalformedObjectNameException {
0N/A return new ObjectName(domain, table);
0N/A }
0N/A
0N/A /**
0N/A * <p>Return an instance of ObjectName that can be used anywhere
0N/A * the given object can be used. The returned object may be of a
0N/A * subclass of ObjectName. If <code>name</code> is of a subclass
0N/A * of ObjectName, it is not guaranteed that the returned object
0N/A * will be of the same class.</p>
0N/A *
0N/A * <p>The returned value may or may not be identical to
0N/A * <code>name</code>. Calling this method twice with the same
0N/A * parameters may return the same object or two equal but not
0N/A * identical objects.</p>
0N/A *
0N/A * <p>Since ObjectName is immutable, it is not usually useful to
0N/A * make a copy of an ObjectName. The principal use of this method
0N/A * is to guard against a malicious caller who might pass an
0N/A * instance of a subclass with surprising behavior to sensitive
0N/A * code. Such code can call this method to obtain an ObjectName
0N/A * that is known not to have surprising behavior.</p>
0N/A *
0N/A * @param name an instance of the ObjectName class or of a subclass
0N/A *
0N/A * @return an instance of ObjectName or a subclass that is known to
0N/A * have the same semantics. If <code>name</code> respects the
0N/A * semantics of ObjectName, then the returned object is equal
0N/A * (though not necessarily identical) to <code>name</code>.
0N/A *
0N/A * @exception NullPointerException The <code>name</code> is null.
0N/A *
0N/A */
572N/A public static ObjectName getInstance(ObjectName name) {
0N/A if (name.getClass().equals(ObjectName.class))
0N/A return name;
1790N/A return Util.newObjectName(name.getSerializedNameString());
528N/A }
528N/A
528N/A /**
0N/A * Construct an object name from the given string.
0N/A *
0N/A * @param name A string representation of the object name.
0N/A *
0N/A * @exception MalformedObjectNameException The string passed as a
0N/A * parameter does not have the right format.
0N/A * @exception NullPointerException The <code>name</code> parameter
0N/A * is null.
0N/A */
0N/A public ObjectName(String name)
572N/A throws MalformedObjectNameException {
0N/A construct(name);
0N/A }
0N/A
0N/A /**
0N/A * Construct an object name with exactly one key property.
0N/A *
0N/A * @param domain The domain part of the object name.
0N/A * @param key The attribute in the key property of the object name.
0N/A * @param value The value in the key property of the object name.
0N/A *
0N/A * @exception MalformedObjectNameException The
0N/A * <code>domain</code>, <code>key</code>, or <code>value</code>
0N/A * contains an illegal character, or <code>value</code> does not
0N/A * follow the rules for quoting.
0N/A * @exception NullPointerException One of the parameters is null.
0N/A */
0N/A public ObjectName(String domain, String key, String value)
572N/A throws MalformedObjectNameException {
0N/A // If key or value are null a NullPointerException
0N/A // will be thrown by the put method in Hashtable.
0N/A //
0N/A Map<String,String> table = Collections.singletonMap(key, value);
0N/A construct(domain, table);
0N/A }
0N/A
0N/A /**
0N/A * Construct an object name with several key properties from a Hashtable.
0N/A *
0N/A * @param domain The domain part of the object name.
0N/A * @param table A hash table containing one or more key
0N/A * properties. The key of each entry in the table is the key of a
0N/A * key property in the object name. The associated value in the
0N/A * table is the associated value in the object name.
0N/A *
0N/A * @exception MalformedObjectNameException The <code>domain</code>
0N/A * contains an illegal character, or one of the keys or values in
0N/A * <code>table</code> contains an illegal character, or one of the
0N/A * values in <code>table</code> does not follow the rules for
0N/A * quoting.
0N/A * @exception NullPointerException One of the parameters is null.
0N/A */
0N/A public ObjectName(String domain, Hashtable<String,String> table)
572N/A throws MalformedObjectNameException {
0N/A construct(domain, table);
0N/A /* The exception for when a key or value in the table is not a
0N/A String is now ClassCastException rather than
0N/A MalformedObjectNameException. This was not previously
0N/A specified. */
0N/A }
0N/A
0N/A // Category : ObjectName Construction <==============================
0N/A
0N/A
0N/A // Category : Getter methods ------------------------------>
0N/A
0N/A /**
0N/A * Checks whether the object name is a pattern.
0N/A * <p>
0N/A * An object name is a pattern if its domain contains a
0N/A * wildcard or if the object name is a property pattern.
0N/A *
0N/A * @return True if the name is a pattern, otherwise false.
0N/A */
0N/A public boolean isPattern() {
0N/A return (_domain_pattern ||
0N/A _property_list_pattern ||
0N/A _property_value_pattern);
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the object name is a pattern on the domain part.
0N/A *
0N/A * @return True if the name is a domain pattern, otherwise false.
0N/A *
0N/A */
0N/A public boolean isDomainPattern() {
0N/A return _domain_pattern;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the object name is a pattern on the key properties.
0N/A * <p>
0N/A * An object name is a pattern on the key properties if it is a
0N/A * pattern on the key property list (e.g. "d:k=v,*") or on the
0N/A * property values (e.g. "d:k=*") or on both (e.g. "d:k=*,*").
0N/A *
0N/A * @return True if the name is a property pattern, otherwise false.
0N/A */
0N/A public boolean isPropertyPattern() {
0N/A return _property_list_pattern || _property_value_pattern;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the object name is a pattern on the key property list.
0N/A * <p>
0N/A * For example, "d:k=v,*" and "d:k=*,*" are key property list patterns
0N/A * whereas "d:k=*" is not.
0N/A *
0N/A * @return True if the name is a property list pattern, otherwise false.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public boolean isPropertyListPattern() {
0N/A return _property_list_pattern;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the object name is a pattern on the value part
0N/A * of at least one of the key properties.
0N/A * <p>
0N/A * For example, "d:k=*" and "d:k=*,*" are property value patterns
0N/A * whereas "d:k=v,*" is not.
0N/A *
0N/A * @return True if the name is a property value pattern, otherwise false.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public boolean isPropertyValuePattern() {
0N/A return _property_value_pattern;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether the value associated with a key in a key
0N/A * property is a pattern.
0N/A *
0N/A * @param property The property whose value is to be checked.
0N/A *
0N/A * @return True if the value associated with the given key property
0N/A * is a pattern, otherwise false.
0N/A *
0N/A * @exception NullPointerException If <code>property</code> is null.
0N/A * @exception IllegalArgumentException If <code>property</code> is not
0N/A * a valid key property for this ObjectName.
0N/A *
0N/A * @since 1.6
0N/A */
572N/A public boolean isPropertyValuePattern(String property) {
0N/A if (property == null)
0N/A throw new NullPointerException("key property can't be null");
0N/A for (int i = 0; i < _ca_array.length; i++) {
0N/A Property prop = _ca_array[i];
1790N/A String key = prop.getKeyString(_canonicalName);
0N/A if (key.equals(property))
0N/A return (prop instanceof PatternProperty);
0N/A }
0N/A throw new IllegalArgumentException("key property not found");
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the canonical form of the name; that is, a string
0N/A * representation where the properties are sorted in lexical
0N/A * order.</p>
0N/A *
0N/A * <p>More precisely, the canonical form of the name is a String
0N/A * consisting of the <em>domain part</em>, a colon
0N/A * (<code>:</code>), the <em>canonical key property list</em>, and
0N/A * a <em>pattern indication</em>.</p>
0N/A *
0N/A * <p>The <em>canonical key property list</em> is the same string
0N/A * as described for {@link #getCanonicalKeyPropertyListString()}.</p>
0N/A *
0N/A * <p>The <em>pattern indication</em> is:
0N/A * <ul>
0N/A * <li>empty for an ObjectName
0N/A * that is not a property list pattern;
0N/A * <li>an asterisk for an ObjectName
0N/A * that is a property list pattern with no keys; or
0N/A * <li>a comma and an
0N/A * asterisk (<code>,*</code>) for an ObjectName that is a property
0N/A * list pattern with at least one key.
0N/A * </ul></p>
0N/A *
0N/A * @return The canonical form of the name.
0N/A */
0N/A public String getCanonicalName() {
0N/A return _canonicalName;
0N/A }
0N/A
0N/A /**
0N/A * Returns the domain part.
0N/A *
0N/A * @return The domain.
0N/A */
0N/A public String getDomain() {
0N/A return _canonicalName.substring(0, _domain_length);
0N/A }
0N/A
0N/A /**
0N/A * Obtains the value associated with a key in a key property.
0N/A *
0N/A * @param property The property whose value is to be obtained.
0N/A *
0N/A * @return The value of the property, or null if there is no such
0N/A * property in this ObjectName.
0N/A *
0N/A * @exception NullPointerException If <code>property</code> is null.
0N/A */
572N/A public String getKeyProperty(String property) {
0N/A return _getKeyPropertyList().get(property);
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the key properties as a Map. The returned
0N/A * value is a Map in which each key is a key in the
0N/A * ObjectName's key property list and each value is the associated
0N/A * value.</p>
0N/A *
0N/A * <p>The returned value must not be modified.</p>
0N/A *
0N/A * @return The table of key properties.
0N/A */
0N/A private Map<String,String> _getKeyPropertyList() {
0N/A synchronized (this) {
0N/A if (_propertyList == null) {
0N/A // build (lazy eval) the property list from the canonical
0N/A // properties array
0N/A _propertyList = new HashMap<String,String>();
0N/A int len = _ca_array.length;
0N/A Property prop;
0N/A for (int i = len - 1; i >= 0; i--) {
0N/A prop = _ca_array[i];
1790N/A _propertyList.put(prop.getKeyString(_canonicalName),
1790N/A prop.getValueString(_canonicalName));
0N/A }
0N/A }
0N/A }
0N/A return _propertyList;
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the key properties as a Hashtable. The returned
0N/A * value is a Hashtable in which each key is a key in the
0N/A * ObjectName's key property list and each value is the associated
0N/A * value.</p>
0N/A *
0N/A * <p>The returned value may be unmodifiable. If it is
0N/A * modifiable, changing it has no effect on this ObjectName.</p>
0N/A *
0N/A * @return The table of key properties.
0N/A */
0N/A // CR 6441274 depends on the modification property defined above
0N/A public Hashtable<String,String> getKeyPropertyList() {
0N/A return new Hashtable<String,String>(_getKeyPropertyList());
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns a string representation of the list of key
0N/A * properties specified at creation time. If this ObjectName was
0N/A * constructed with the constructor {@link #ObjectName(String)},
0N/A * the key properties in the returned String will be in the same
0N/A * order as in the argument to the constructor.</p>
0N/A *
0N/A * @return The key property list string. This string is
0N/A * independent of whether the ObjectName is a pattern.
0N/A */
0N/A public String getKeyPropertyListString() {
0N/A // BEWARE : we rebuild the propertyliststring at each call !!
0N/A if (_kp_array.length == 0) return "";
0N/A
0N/A // the size of the string is the canonical one minus domain
0N/A // part and pattern part
0N/A final int total_size = _canonicalName.length() - _domain_length - 1
0N/A - (_property_list_pattern?2:0);
0N/A
0N/A final char[] dest_chars = new char[total_size];
0N/A final char[] value = _canonicalName.toCharArray();
0N/A writeKeyPropertyListString(value,dest_chars,0);
0N/A return new String(dest_chars);
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the serialized string of the ObjectName.
0N/A * properties specified at creation time. If this ObjectName was
0N/A * constructed with the constructor {@link #ObjectName(String)},
0N/A * the key properties in the returned String will be in the same
0N/A * order as in the argument to the constructor.</p>
0N/A *
0N/A * @return The key property list string. This string is
0N/A * independent of whether the ObjectName is a pattern.
0N/A */
0N/A private String getSerializedNameString() {
0N/A
0N/A // the size of the string is the canonical one
0N/A final int total_size = _canonicalName.length();
0N/A final char[] dest_chars = new char[total_size];
0N/A final char[] value = _canonicalName.toCharArray();
0N/A final int offset = _domain_length+1;
0N/A
0N/A // copy "domain:" into dest_chars
0N/A //
0N/A System.arraycopy(value, 0, dest_chars, 0, offset);
0N/A
0N/A // Add property list string
0N/A final int end = writeKeyPropertyListString(value,dest_chars,offset);
0N/A
0N/A // Add ",*" if necessary
0N/A if (_property_list_pattern) {
0N/A if (end == offset) {
0N/A // Property list string is empty.
0N/A dest_chars[end] = '*';
0N/A } else {
0N/A // Property list string is not empty.
0N/A dest_chars[end] = ',';
0N/A dest_chars[end+1] = '*';
0N/A }
0N/A }
0N/A
1790N/A return new String(dest_chars);
0N/A }
0N/A
0N/A /**
0N/A * <p>Write a string representation of the list of key
0N/A * properties specified at creation time in the given array, starting
0N/A * at the specified offset. If this ObjectName was
0N/A * constructed with the constructor {@link #ObjectName(String)},
0N/A * the key properties in the returned String will be in the same
0N/A * order as in the argument to the constructor.</p>
0N/A *
0N/A * @return offset + #of chars written
0N/A */
0N/A private int writeKeyPropertyListString(char[] canonicalChars,
0N/A char[] data, int offset) {
0N/A if (_kp_array.length == 0) return offset;
0N/A
0N/A final char[] dest_chars = data;
528N/A final char[] value = canonicalChars;
0N/A
0N/A int index = offset;
0N/A final int len = _kp_array.length;
0N/A final int last = len - 1;
0N/A for (int i = 0; i < len; i++) {
0N/A final Property prop = _kp_array[i];
0N/A final int prop_len = prop._key_length + prop._value_length + 1;
1790N/A System.arraycopy(value, prop._key_index, dest_chars, index,
0N/A prop_len);
0N/A index += prop_len;
0N/A if (i < last ) dest_chars[index++] = ',';
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of the list of key properties,
0N/A * in which the key properties are sorted in lexical order. This
0N/A * is used in lexicographic comparisons performed in order to
0N/A * select MBeans based on their key property list. Lexical order
0N/A * is the order implied by {@link String#compareTo(String)
0N/A * String.compareTo(String)}.
0N/A *
0N/A * @return The canonical key property list string. This string is
0N/A * independent of whether the ObjectName is a pattern.
0N/A */
0N/A public String getCanonicalKeyPropertyListString() {
0N/A if (_ca_array.length == 0) return "";
0N/A
0N/A int len = _canonicalName.length();
0N/A if (_property_list_pattern) len -= 2;
0N/A return _canonicalName.substring(_domain_length +1, len);
0N/A }
0N/A // Category : Getter methods <===================================
0N/A
0N/A // Category : Utilities ---------------------------------------->
0N/A
0N/A /**
0N/A * <p>Returns a string representation of the object name. The
0N/A * format of this string is not specified, but users can expect
0N/A * that two ObjectNames return the same string if and only if they
0N/A * are equal.</p>
0N/A *
0N/A * @return a string representation of this object name.
0N/A */
11N/A @Override
0N/A public String toString() {
0N/A return getSerializedNameString();
0N/A }
0N/A
0N/A /**
0N/A * Compares the current object name with another object name. Two
0N/A * ObjectName instances are equal if and only if their canonical
0N/A * forms are equal. The canonical form is the string described
0N/A * for {@link #getCanonicalName()}.
0N/A *
0N/A * @param object The object name that the current object name is to be
0N/A * compared with.
0N/A *
0N/A * @return True if <code>object</code> is an ObjectName whose
0N/A * canonical form is equal to that of this ObjectName.
0N/A */
470N/A @Override
0N/A public boolean equals(Object object) {
0N/A
0N/A // same object case
0N/A if (this == object) return true;
0N/A
0N/A // object is not an object name case
0N/A if (!(object instanceof ObjectName)) return false;
0N/A
0N/A // equality when canonical names are the same
0N/A // (because usage of intern())
0N/A ObjectName on = (ObjectName) object;
0N/A String on_string = on._canonicalName;
528N/A if (_canonicalName == on_string) return true; // ES: OK
0N/A
0N/A // Because we are sharing canonical form between object names,
0N/A // we have finished the comparison at this stage ==> unequal
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code for this object name.
0N/A *
0N/A */
470N/A @Override
0N/A public int hashCode() {
0N/A return _canonicalName.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns a quoted form of the given String, suitable for
0N/A * inclusion in an ObjectName. The returned value can be used as
0N/A * the value associated with a key in an ObjectName. The String
0N/A * <code>s</code> may contain any character. Appropriate quoting
0N/A * ensures that the returned value is legal in an ObjectName.</p>
0N/A *
0N/A * <p>The returned value consists of a quote ('"'), a sequence of
0N/A * characters corresponding to the characters of <code>s</code>,
0N/A * and another quote. Characters in <code>s</code> appear
0N/A * unchanged within the returned value except:</p>
0N/A *
0N/A * <ul>
0N/A * <li>A quote ('"') is replaced by a backslash (\) followed by a quote.</li>
0N/A * <li>An asterisk ('*') is replaced by a backslash (\) followed by an
0N/A * asterisk.</li>
0N/A * <li>A question mark ('?') is replaced by a backslash (\) followed by
0N/A * a question mark.</li>
0N/A * <li>A backslash ('\') is replaced by two backslashes.</li>
0N/A * <li>A newline character (the character '\n' in Java) is replaced
0N/A * by a backslash followed by the character '\n'.</li>
0N/A * </ul>
0N/A *
0N/A * @param s the String to be quoted.
0N/A *
0N/A * @return the quoted String.
0N/A *
0N/A * @exception NullPointerException if <code>s</code> is null.
0N/A *
0N/A */
572N/A public static String quote(String s) {
0N/A final StringBuilder buf = new StringBuilder("\"");
0N/A final int len = s.length();
0N/A for (int i = 0; i < len; i++) {
0N/A char c = s.charAt(i);
0N/A switch (c) {
0N/A case '\n':
0N/A c = 'n';
0N/A buf.append('\\');
0N/A break;
0N/A case '\\':
0N/A case '\"':
0N/A case '*':
0N/A case '?':
0N/A buf.append('\\');
0N/A break;
0N/A }
0N/A buf.append(c);
0N/A }
0N/A buf.append('"');
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns an unquoted form of the given String. If
0N/A * <code>q</code> is a String returned by {@link #quote quote(s)},
0N/A * then <code>unquote(q).equals(s)</code>. If there is no String
0N/A * <code>s</code> for which <code>quote(s).equals(q)</code>, then
0N/A * unquote(q) throws an IllegalArgumentException.</p>
0N/A *
0N/A * <p>These rules imply that there is a one-to-one mapping between
0N/A * quoted and unquoted forms.</p>
0N/A *
0N/A * @param q the String to be unquoted.
0N/A *
0N/A * @return the unquoted String.
0N/A *
0N/A * @exception IllegalArgumentException if <code>q</code> could not
0N/A * have been returned by the {@link #quote} method, for instance
0N/A * if it does not begin and end with a quote (").
0N/A *
0N/A * @exception NullPointerException if <code>q</code> is null.
0N/A *
0N/A */
572N/A public static String unquote(String q) {
0N/A final StringBuilder buf = new StringBuilder();
0N/A final int len = q.length();
0N/A if (len < 2 || q.charAt(0) != '"' || q.charAt(len - 1) != '"')
0N/A throw new IllegalArgumentException("Argument not quoted");
0N/A for (int i = 1; i < len - 1; i++) {
0N/A char c = q.charAt(i);
0N/A if (c == '\\') {
0N/A if (i == len - 2)
0N/A throw new IllegalArgumentException("Trailing backslash");
0N/A c = q.charAt(++i);
0N/A switch (c) {
0N/A case 'n':
0N/A c = '\n';
0N/A break;
0N/A case '\\':
0N/A case '\"':
0N/A case '*':
0N/A case '?':
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException(
0N/A "Bad character '" + c + "' after backslash");
0N/A }
0N/A } else {
0N/A switch (c) {
0N/A case '*' :
0N/A case '?' :
0N/A case '\"':
0N/A case '\n':
0N/A throw new IllegalArgumentException(
0N/A "Invalid unescaped character '" + c +
0N/A "' in the string to unquote");
0N/A }
0N/A }
0N/A buf.append(c);
0N/A }
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Defines the wildcard "*:*" ObjectName.
0N/A *
0N/A * @since 1.6
0N/A */
1790N/A public static final ObjectName WILDCARD = Util.newObjectName("*:*");
0N/A
0N/A // Category : Utilities <===================================
0N/A
0N/A // Category : QueryExp Interface ---------------------------------------->
0N/A
0N/A /**
0N/A * <p>Test whether this ObjectName, which may be a pattern,
0N/A * matches another ObjectName. If <code>name</code> is a pattern,
0N/A * the result is false. If this ObjectName is a pattern, the
0N/A * result is true if and only if <code>name</code> matches the
0N/A * pattern. If neither this ObjectName nor <code>name</code> is
0N/A * a pattern, the result is true if and only if the two
0N/A * ObjectNames are equal as described for the {@link
0N/A * #equals(Object)} method.</p>
0N/A *
0N/A * @param name The name of the MBean to compare to.
0N/A *
0N/A * @return True if <code>name</code> matches this ObjectName.
0N/A *
0N/A * @exception NullPointerException if <code>name</code> is null.
0N/A *
0N/A */
572N/A public boolean apply(ObjectName name) {
0N/A
0N/A if (name == null) throw new NullPointerException();
0N/A
0N/A if (name._domain_pattern ||
0N/A name._property_list_pattern ||
0N/A name._property_value_pattern)
0N/A return false;
0N/A
0N/A // No pattern
0N/A if (!_domain_pattern &&
0N/A !_property_list_pattern &&
0N/A !_property_value_pattern)
0N/A return _canonicalName.equals(name._canonicalName);
0N/A
0N/A return matchDomains(name) && matchKeys(name);
0N/A }
0N/A
0N/A private final boolean matchDomains(ObjectName name) {
0N/A if (_domain_pattern) {
0N/A // wildmatch domains
528N/A // This ObjectName is the pattern
528N/A // The other ObjectName is the string.
1790N/A return Util.wildmatch(name.getDomain(),getDomain());
0N/A }
0N/A return getDomain().equals(name.getDomain());
0N/A }
0N/A
0N/A private final boolean matchKeys(ObjectName name) {
0N/A // If key property value pattern but not key property list
0N/A // pattern, then the number of key properties must be equal
0N/A //
0N/A if (_property_value_pattern &&
0N/A !_property_list_pattern &&
0N/A (name._ca_array.length != _ca_array.length))
0N/A return false;
0N/A
0N/A // If key property value pattern or key property list pattern,
0N/A // then every property inside pattern should exist in name
0N/A //
0N/A if (_property_value_pattern || _property_list_pattern) {
0N/A final Map<String,String> nameProps = name._getKeyPropertyList();
0N/A final Property[] props = _ca_array;
0N/A final String cn = _canonicalName;
0N/A for (int i = props.length - 1; i >= 0 ; i--) {
0N/A // Find value in given object name for key at current
0N/A // index in receiver
0N/A //
0N/A final Property p = props[i];
1790N/A final String k = p.getKeyString(cn);
0N/A final String v = nameProps.get(k);
0N/A // Did we find a value for this key ?
0N/A //
0N/A if (v == null) return false;
0N/A // If this property is ok (same key, same value), go to next
0N/A //
0N/A if (_property_value_pattern && (p instanceof PatternProperty)) {
0N/A // wildmatch key property values
528N/A // p is the property pattern, v is the string
1790N/A if (Util.wildmatch(v,p.getValueString(cn)))
0N/A continue;
0N/A else
0N/A return false;
0N/A }
1790N/A if (v.equals(p.getValueString(cn))) continue;
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A // If no pattern, then canonical names must be equal
0N/A //
0N/A final String p1 = name.getCanonicalKeyPropertyListString();
0N/A final String p2 = getCanonicalKeyPropertyListString();
0N/A return (p1.equals(p2));
0N/A }
0N/A
0N/A /* Method inherited from QueryExp, no implementation needed here
0N/A because ObjectName is not relative to an MBeanServer and does
0N/A not contain a subquery.
0N/A */
0N/A public void setMBeanServer(MBeanServer mbs) { }
0N/A
0N/A // Category : QueryExp Interface <=========================
0N/A
0N/A // Category : Comparable Interface ---------------------------------------->
0N/A
0N/A /**
0N/A * <p>Compares two ObjectName instances. The ordering relation between
0N/A * ObjectNames is not completely specified but is intended to be such
0N/A * that a sorted list of ObjectNames will appear in an order that is
0N/A * convenient for a person to read.</p>
0N/A *
0N/A * <p>In particular, if the two ObjectName instances have different
0N/A * domains then their order is the lexicographical order of the domains.
0N/A * The ordering of the key property list remains unspecified.</p>
0N/A *
0N/A * <p>For example, the ObjectName instances below:</p>
0N/A * <ul>
0N/A * <li>Shapes:type=Square,name=3</li>
0N/A * <li>Colors:type=Red,name=2</li>
0N/A * <li>Shapes:type=Triangle,side=isosceles,name=2</li>
0N/A * <li>Colors:type=Red,name=1</li>
0N/A * <li>Shapes:type=Square,name=1</li>
0N/A * <li>Colors:type=Blue,name=1</li>
0N/A * <li>Shapes:type=Square,name=2</li>
0N/A * <li>JMImplementation:type=MBeanServerDelegate</li>
0N/A * <li>Shapes:type=Triangle,side=scalene,name=1</li>
0N/A * </ul>
0N/A * <p>could be ordered as follows:</p>
0N/A * <ul>
0N/A * <li>Colors:type=Blue,name=1</li>
0N/A * <li>Colors:type=Red,name=1</li>
0N/A * <li>Colors:type=Red,name=2</li>
0N/A * <li>JMImplementation:type=MBeanServerDelegate</li>
0N/A * <li>Shapes:type=Square,name=1</li>
0N/A * <li>Shapes:type=Square,name=2</li>
0N/A * <li>Shapes:type=Square,name=3</li>
0N/A * <li>Shapes:type=Triangle,side=scalene,name=1</li>
0N/A * <li>Shapes:type=Triangle,side=isosceles,name=2</li>
0N/A * </ul>
0N/A *
0N/A * @param name the ObjectName to be compared.
0N/A *
0N/A * @return a negative integer, zero, or a positive integer as this
0N/A * ObjectName is less than, equal to, or greater than the
0N/A * specified ObjectName.
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public int compareTo(ObjectName name) {
528N/A // Quick optimization:
528N/A //
528N/A if (name == this) return 0;
528N/A
0N/A // (1) Compare domains
0N/A //
0N/A int domainValue = this.getDomain().compareTo(name.getDomain());
0N/A if (domainValue != 0)
0N/A return domainValue;
0N/A
0N/A // (2) Compare "type=" keys
0N/A //
0N/A // Within a given domain, all names with missing or empty "type="
0N/A // come before all names with non-empty type.
0N/A //
0N/A // When both types are missing or empty, canonical-name ordering
0N/A // applies which is a total order.
0N/A //
0N/A String thisTypeKey = this.getKeyProperty("type");
0N/A String anotherTypeKey = name.getKeyProperty("type");
0N/A if (thisTypeKey == null)
0N/A thisTypeKey = "";
0N/A if (anotherTypeKey == null)
0N/A anotherTypeKey = "";
0N/A int typeKeyValue = thisTypeKey.compareTo(anotherTypeKey);
0N/A if (typeKeyValue != 0)
0N/A return typeKeyValue;
0N/A
0N/A // (3) Compare canonical names
0N/A //
0N/A return this.getCanonicalName().compareTo(name.getCanonicalName());
0N/A }
0N/A
0N/A // Category : Comparable Interface <=========================
0N/A
0N/A // Public methods <========================================
0N/A
0N/A}