2111 lines · 81.6 KB · Java
RawDownload
    }     /**     * Checks whether the value associated with a key in a key     * property is a pattern.     *     * @param property The property whose value is to be checked.     *     * @return True if the value associated with the given key property     * is a pattern, otherwise false.     *     * @exception NullPointerException If <code>property</code> is null.     * @exception IllegalArgumentException If <code>property</code> is not     * a valid key property for this ObjectName.     *     * @since 1.6     */    public boolean isPropertyValuePattern(String property) {        if (property == null)            throw new NullPointerException("key property can't be null");        for (int i = 0; i < _ca_array.length; i++) {            Property prop = _ca_array[i];            String key = prop.getKeyString(_canonicalName);            if (key.equals(property))                return (prop instanceof PatternProperty);        }        throw new IllegalArgumentException("key property not found");    }     /**     * <p>Returns the canonical form of the name; that is, a string     * representation where the properties are sorted in lexical     * order.</p>     *     * <p>More precisely, the canonical form of the name is a String     * consisting of the <em>domain part</em>, a colon     * (<code>:</code>), the <em>canonical key property list</em>, and     * a <em>pattern indication</em>.</p>     *     * <p>The <em>canonical key property list</em> is the same string     * as described for {@link #getCanonicalKeyPropertyListString()}.</p>     *     * <p>The <em>pattern indication</em> is:     * <ul>     * <li>empty for an ObjectName     * that is not a property list pattern;     * <li>an asterisk for an ObjectName     * that is a property list pattern with no keys; or     * <li>a comma and an     * asterisk (<code>,*</code>) for an ObjectName that is a property     * list pattern with at least one key.     * </ul></p>     *     * @return The canonical form of the name.     */    public String getCanonicalName()  {        return _canonicalName;    }     /**     * Returns the domain part.     *     * @return The domain.     */    public String getDomain()  {        return _canonicalName.substring(0, _domain_length);    }     /**     * Obtains the value associated with a key in a key property.     *     * @param property The property whose value is to be obtained.     *     * @return The value of the property, or null if there is no such     * property in this ObjectName.     *     * @exception NullPointerException If <code>property</code> is null.     */    public String getKeyProperty(String property) {        return _getKeyPropertyList().get(property);    }     /**     * <p>Returns the key properties as a Map.  The returned     * value is a Map in which each key is a key in the     * ObjectName's key property list and each value is the associated     * value.</p>     *     * <p>The returned value must not be modified.</p>     *     * @return The table of key properties.     */    private Map<String,String> _getKeyPropertyList()  {        synchronized (this) {            if (_propertyList == null) {                // build (lazy eval) the property list from the canonical                // properties array                _propertyList = new HashMap<String,String>();                int len = _ca_array.length;                Property prop;                for (int i = len - 1; i >= 0; i--) {                    prop = _ca_array[i];                    _propertyList.put(prop.getKeyString(_canonicalName),                                      prop.getValueString(_canonicalName));                }            }        }        return _propertyList;    }     /**     * <p>Returns the key properties as a Hashtable.  The returned     * value is a Hashtable in which each key is a key in the     * ObjectName's key property list and each value is the associated     * value.</p>     *     * <p>The returned value may be unmodifiable.  If it is     * modifiable, changing it has no effect on this ObjectName.</p>     *     * @return The table of key properties.     */    // CR 6441274 depends on the modification property defined above    public Hashtable<String,String> getKeyPropertyList()  {        return new Hashtable<String,String>(_getKeyPropertyList());    }     /**     * <p>Returns a string representation of the list of key     * properties specified at creation time.  If this ObjectName was     * constructed with the constructor {@link #ObjectName(String)},     * the key properties in the returned String will be in the same     * order as in the argument to the constructor.</p>     *     * @return The key property list string.  This string is     * independent of whether the ObjectName is a pattern.     */    public String getKeyPropertyListString()  {        // BEWARE : we rebuild the propertyliststring at each call !!        if (_kp_array.length == 0) return "";         // the size of the string is the canonical one minus domain        // part and pattern part        final int total_size = _canonicalName.length() - _domain_length - 1            - (_property_list_pattern?2:0);         final char[] dest_chars = new char[total_size];        final char[] value = _canonicalName.toCharArray();        writeKeyPropertyListString(value,dest_chars,0);        return new String(dest_chars);    }     /**     * <p>Returns the serialized string of the ObjectName.     * properties specified at creation time.  If this ObjectName was     * constructed with the constructor {@link #ObjectName(String)},     * the key properties in the returned String will be in the same     * order as in the argument to the constructor.</p>     *     * @return The key property list string.  This string is     * independent of whether the ObjectName is a pattern.     */    private String getSerializedNameString()  {         // the size of the string is the canonical one        final int total_size = _canonicalName.length();        final char[] dest_chars = new char[total_size];        final char[] value = _canonicalName.toCharArray();        final int offset = _domain_length+1;         // copy "domain:" into dest_chars        //        System.arraycopy(value, 0, dest_chars, 0, offset);         // Add property list string        final int end = writeKeyPropertyListString(value,dest_chars,offset);         // Add ",*" if necessary        if (_property_list_pattern) {            if (end == offset)  {                // Property list string is empty.                dest_chars[end] = '*';            } else {                // Property list string is not empty.                dest_chars[end]   = ',';                dest_chars[end+1] = '*';            }        }         return new String(dest_chars);    }     /**     * <p>Write a string representation of the list of key     * properties specified at creation time in the given array, starting     * at the specified offset.  If this ObjectName was     * constructed with the constructor {@link #ObjectName(String)},     * the key properties in the returned String will be in the same     * order as in the argument to the constructor.</p>     *     * @return offset + #of chars written     */    private int writeKeyPropertyListString(char[] canonicalChars,                                           char[] data, int offset)  {        if (_kp_array.length == 0) return offset;         final char[] dest_chars = data;        final char[] value = canonicalChars;         int index = offset;        final int len = _kp_array.length;        final int last = len - 1;        for (int i = 0; i < len; i++) {            final Property prop = _kp_array[i];            final int prop_len = prop._key_length + prop._value_length + 1;            System.arraycopy(value, prop._key_index, dest_chars, index,                             prop_len);            index += prop_len;            if (i < last ) dest_chars[index++] = ',';        }        return index;    }       /**     * Returns a string representation of the list of key properties,     * in which the key properties are sorted in lexical order. This     * is used in lexicographic comparisons performed in order to     * select MBeans based on their key property list.  Lexical order     * is the order implied by {@link String#compareTo(String)     * String.compareTo(String)}.     *     * @return The canonical key property list string.  This string is     * independent of whether the ObjectName is a pattern.     */    public String getCanonicalKeyPropertyListString()  {        if (_ca_array.length == 0) return "";         int len = _canonicalName.length();        if (_property_list_pattern) len -= 2;        return _canonicalName.substring(_domain_length +1, len);    }    // Category : Getter methods <===================================     // Category : Utilities ---------------------------------------->     /**     * <p>Returns a string representation of the object name.  The     * format of this string is not specified, but users can expect     * that two ObjectNames return the same string if and only if they     * are equal.</p>     *     * @return a string representation of this object name.     */    @Override    public String toString()  {        return getSerializedNameString();    }     /**     * Compares the current object name with another object name.  Two     * ObjectName instances are equal if and only if their canonical     * forms are equal.  The canonical form is the string described     * for {@link #getCanonicalName()}.     *     * @param object  The object name that the current object name is to be     *        compared with.     *     * @return True if <code>object</code> is an ObjectName whose     * canonical form is equal to that of this ObjectName.     */    @Override    public boolean equals(Object object)  {         // same object case        if (this == object) return true;         // object is not an object name case        if (!(object instanceof ObjectName)) return false;         // equality when canonical names are the same        // (because usage of intern())        ObjectName on = (ObjectName) object;        String on_string = on._canonicalName;        if (_canonicalName == on_string) return true;  // ES: OK         // Because we are sharing canonical form between object names,        // we have finished the comparison at this stage ==> unequal        return false;   }     /**     * Returns a hash code for this object name.     *     */    @Override    public int hashCode() {        return _canonicalName.hashCode();    }     /**     * <p>Returns a quoted form of the given String, suitable for     * inclusion in an ObjectName.  The returned value can be used as     * the value associated with a key in an ObjectName.  The String     * <code>s</code> may contain any character.  Appropriate quoting     * ensures that the returned value is legal in an ObjectName.</p>     *     * <p>The returned value consists of a quote ('"'), a sequence of     * characters corresponding to the characters of <code>s</code>,     * and another quote.  Characters in <code>s</code> appear     * unchanged within the returned value except:</p>     *     * <ul>     * <li>A quote ('"') is replaced by a backslash (\) followed by a quote.</li>     * <li>An asterisk ('*') is replaced by a backslash (\) followed by an     * asterisk.</li>     * <li>A question mark ('?') is replaced by a backslash (\) followed by     * a question mark.</li>     * <li>A backslash ('\') is replaced by two backslashes.</li>     * <li>A newline character (the character '\n' in Java) is replaced     * by a backslash followed by the character '\n'.</li>     * </ul>     *     * @param s the String to be quoted.     *     * @return the quoted String.     *     * @exception NullPointerException if <code>s</code> is null.     *     */    public static String quote(String s) {        final StringBuilder buf = new StringBuilder("\"");        final int len = s.length();        for (int i = 0; i < len; i++) {            char c = s.charAt(i);            switch (c) {            case '\n':                c = 'n';                buf.append('\\');                break;            case '\\':            case '\"':            case '*':            case '?':                buf.append('\\');                break;            }            buf.append(c);        }        buf.append('"');        return buf.toString();    }     /**     * <p>Returns an unquoted form of the given String.  If     * <code>q</code> is a String returned by {@link #quote quote(s)},     * then <code>unquote(q).equals(s)</code>.  If there is no String     * <code>s</code> for which <code>quote(s).equals(q)</code>, then     * unquote(q) throws an IllegalArgumentException.</p>     *     * <p>These rules imply that there is a one-to-one mapping between     * quoted and unquoted forms.</p>     *     * @param q the String to be unquoted.     *     * @return the unquoted String.     *     * @exception IllegalArgumentException if <code>q</code> could not     * have been returned by the {@link #quote} method, for instance     * if it does not begin and end with a quote (").     *     * @exception NullPointerException if <code>q</code> is null.     *     */    public static String unquote(String q) {        final StringBuilder buf = new StringBuilder();        final int len = q.length();        if (len < 2 || q.charAt(0) != '"' || q.charAt(len - 1) != '"')            throw new IllegalArgumentException("Argument not quoted");        for (int i = 1; i < len - 1; i++) {            char c = q.charAt(i);            if (c == '\\') {                if (i == len - 2)                    throw new IllegalArgumentException("Trailing backslash");                c = q.charAt(++i);                switch (c) {                case 'n':                    c = '\n';                    break;                case '\\':                case '\"':                case '*':                case '?':                    break;                default:                  throw new IllegalArgumentException(                                   "Bad character '" + c + "' after backslash");                }            } else {                switch (c) {                    case '*' :                    case '?' :                    case '\"':                    case '\n':                         throw new IllegalArgumentException(                                          "Invalid unescaped character '" + c +                                          "' in the string to unquote");                }            }            buf.append(c);        }        return buf.toString();    }     /**     * Defines the wildcard "*:*" ObjectName.     *     * @since 1.6     */    public static final ObjectName WILDCARD = Util.newObjectName("*:*");     // Category : Utilities <===================================     // Category : QueryExp Interface ---------------------------------------->     /**     * <p>Test whether this ObjectName, which may be a pattern,     * matches another ObjectName.  If <code>name</code> is a pattern,     * the result is false.  If this ObjectName is a pattern, the     * result is true if and only if <code>name</code> matches the     * pattern.  If neither this ObjectName nor <code>name</code> is     * a pattern, the result is true if and only if the two     * ObjectNames are equal as described for the {@link     * #equals(Object)} method.</p>     *     * @param name The name of the MBean to compare to.     *     * @return True if <code>name</code> matches this ObjectName.     *     * @exception NullPointerException if <code>name</code> is null.     *     */    public boolean apply(ObjectName name) {         if (name == null) throw new NullPointerException();         if (name._domain_pattern ||            name._property_list_pattern ||            name._property_value_pattern)            return false;         // No pattern        if (!_domain_pattern &&            !_property_list_pattern &&            !_property_value_pattern)            return _canonicalName.equals(name._canonicalName);         return matchDomains(name) && matchKeys(name);    }     private final boolean matchDomains(ObjectName name) {        if (_domain_pattern) {            // wildmatch domains            // This ObjectName is the pattern            // The other ObjectName is the string.            return Util.wildmatch(name.getDomain(),getDomain());        }        return getDomain().equals(name.getDomain());    }     private final boolean matchKeys(ObjectName name) {        // If key property value pattern but not key property list        // pattern, then the number of key properties must be equal        //        if (_property_value_pattern &&            !_property_list_pattern &&            (name._ca_array.length != _ca_array.length))                return false;         // If key property value pattern or key property list pattern,        // then every property inside pattern should exist in name        //        if (_property_value_pattern || _property_list_pattern) {            final Map<String,String> nameProps = name._getKeyPropertyList();            final Property[] props = _ca_array;            final String cn = _canonicalName;            for (int i = props.length - 1; i >= 0 ; i--) {                // Find value in given object name for key at current                // index in receiver                //                final Property p = props[i];                final String   k = p.getKeyString(cn);                final String   v = nameProps.get(k);                // Did we find a value for this key ?                //                if (v == null) return false;                // If this property is ok (same key, same value), go to next                //                if (_property_value_pattern && (p instanceof PatternProperty)) {                    // wildmatch key property values

Lines 1501–2000 of 2111. Previous lines