Java MBean getMatchingObjectNames(CharSequence wildcardEq, CharSequence wildcardWc, MBeanServerConnection conn)

Here you can find the source of getMatchingObjectNames(CharSequence wildcardEq, CharSequence wildcardWc, MBeanServerConnection conn)

Description

Returns a set of ObjectNames matching the passed wildcard object names

License

Open Source License

Parameter

Parameter Description
wildcardEq The ObjectName equals
wildcardWc The ObjectName wildcard
conn The MBeanServer connection

Return

a set of ObjectNames matching the passed wildcard object name

Declaration

public static Set<ObjectName> getMatchingObjectNames(CharSequence wildcardEq, CharSequence wildcardWc,
        MBeanServerConnection conn) 

Method Source Code

//package com.java2s;
/**/* ww w.  j  a  va  2s .c  om*/
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import javax.management.*;

import java.util.*;

import javax.management.*;

public class Main {
    /**
     * Returns a set of ObjectNames matching the passed wildcard object names
     * @param wildcardEq The ObjectName equals
     * @param wildcardWc The ObjectName wildcard
     * @param conn The MBeanServer connection
     * @return a set of ObjectNames matching the passed wildcard object name
     */
    public static Set<ObjectName> getMatchingObjectNames(CharSequence wildcardEq, CharSequence wildcardWc,
            MBeanServerConnection conn) {
        ObjectName wildcardEquals = objectName(wildcardEq);
        ObjectName wildcard = objectName(wildcardWc);

        final String wc = new StringBuilder("(").append(wildcardEquals).append("$)").toString();
        Set<ObjectName> names = new HashSet<ObjectName>();
        // A map of regex patterns to match on, keyed by the actual property key
        Map<String, Pattern> wildcardQueryProps = new HashMap<String, Pattern>();
        // the original wildcard object's key properties
        Hashtable<String, String> wildcardProps = objectName(wildcard).getKeyPropertyList();
        // the non wildcarded property keys we will query the mbean server with
        Hashtable<String, String> queryProps = new Hashtable<String, String>();
        queryProps.putAll(wildcardProps);
        // Extract the wildcarded property keys, ie, where the key is KEY<wildcardEquals>
        for (Map.Entry<String, String> prop : wildcard.getKeyPropertyList().entrySet()) {
            if (prop.getKey().endsWith(wc)) {
                String actualKey = prop.getKey().replaceFirst(wc, "");
                wildcardQueryProps.put(actualKey, Pattern.compile(prop.getValue()));
                queryProps.remove(actualKey);
            }
        }
        // Build the lookup query
        StringBuilder b = new StringBuilder(wildcard.getDomain());
        b.append(":");
        // Append the non regex wildcarded properties
        for (Map.Entry<String, String> qp : queryProps.entrySet()) {
            b.append(qp.getKey()).append("=").append(qp.getValue()).append(",");
        }
        // Append the regex wildcarded property keys and "*" as the value
        for (String key : wildcardQueryProps.keySet()) {
            b.append(key).append("=*,");
        }
        // Append a property wild card if the wildcard objectName had:
        //   Property Pattern:true
        //   PropertyList Pattern:true
        //   PropertyValue Pattern:false
        if (wildcard.isPropertyPattern() && wildcard.isPropertyListPattern()
                && !wildcard.isPropertyValuePattern()) {
            b.append("*");
        }
        if (b.toString().endsWith(",")) {
            b.deleteCharAt(b.length() - 1);
        }
        // Create the query object
        try {
            ObjectName queryObjectName = objectName(b);
            for (ObjectName qon : conn.queryNames(queryObjectName, null)) {
                boolean match = true;
                for (Map.Entry<String, Pattern> pattern : wildcardQueryProps.entrySet()) {
                    match = pattern.getValue().matcher(qon.getKeyProperty(pattern.getKey())).matches();
                    if (!match)
                        break;
                }
                if (match) {
                    names.add(qon);
                }
            }
        } catch (Exception e) {
        }

        // Remove all the wildcarded properties from the wildcard objectname's props

        //ObjectName query = new ObjectName(wildcard.getDomain());

        return names;
    }

    /**
     * Creates a new JMX object name.
     * @param on A string type representing the ObjectName string.
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(CharSequence on) {
        try {
            return new ObjectName(on.toString().trim());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param on An object representing the ObjectName
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(Object on) {
        try {
            return new ObjectName(on.toString().trim());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }

    /**
     * Creates a new JMX object name by appending properties on the end of an existing name
     * @param on An existing ObjectName
     * @param props Appended properties in the for {@code key=value}
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(ObjectName on, CharSequence... props) {
        StringBuilder b = new StringBuilder(on.toString());
        try {
            if (props != null) {
                for (CharSequence prop : props) {
                    b.append(",").append(prop);
                }
            }
            return new ObjectName(b.toString());
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name from [" + b + "]", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param domain A string type representing the ObjectName domain
     * @param properties A hash table of the Object name's properties
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(CharSequence domain, Hashtable<String, String> properties) {
        try {
            return new ObjectName(domain.toString(), properties);
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }

    /**
     * Creates a new JMX object name.
     * @param domain The ObjectName domain
     * @param nameValuePairs an (even lengthed) array of name value pairs making up the key properties
     * @return an ObjectName the created ObjectName
     */
    public static ObjectName objectName(CharSequence domain, CharSequence... nameValuePairs) {
        if (domain == null || domain.toString().length() < 1)
            throw new IllegalArgumentException("Null or zero length domain name");
        if (nameValuePairs == null || nameValuePairs.length < 1 || nameValuePairs.length % 2 != 0) {
            throw new IllegalArgumentException("Invalid number of namevaluepairs ["
                    + (nameValuePairs == null ? 0 : nameValuePairs.length) + "]");
        }
        try {
            Hashtable<String, String> props = new Hashtable<String, String>();
            for (int i = 0; i < nameValuePairs.length; i++) {
                if (nameValuePairs[i] == null || nameValuePairs[i].toString().length() < 1) {
                    throw new IllegalArgumentException("Null or blank nameValuePair entry at index [" + i + "]");
                }
                String key = nameValuePairs[i].toString();
                i++;
                if (nameValuePairs[i] == null || nameValuePairs[i].toString().length() < 1) {
                    throw new IllegalArgumentException("Null or blank nameValuePair entry at index [" + i + "]");
                }
                String value = nameValuePairs[i].toString();
                props.put(key, value);
            }
            return new ObjectName(domain.toString(), props);
        } catch (IllegalArgumentException iae) {
            throw iae;
        } catch (Exception e) {
            throw new RuntimeException("Failed to create Object Name", e);
        }
    }
}

Related

  1. getClassFromInfo(MBeanAttributeInfo attributeInfo)
  2. getCompatibleData(Object object, MBeanParameterInfo beanParameterInfo)
  3. getDescription(MBeanFeatureInfo element)
  4. getDynamicMBean(String mbeanName)
  5. getJmxNameInternal(final Class mBeanClass, final String name)
  6. getMBeanAttribute(final MBeanServerConnection connection, final String nameString, final String attributeName)
  7. getMBeanAttributeMap(MBeanServerConnection server, ObjectName objectName, String delimeter, String... attributeNames)
  8. getMBeanAttributes(final MBeanServer mBeanServer, final String objectNameString)
  9. getMBeanAttributeValue(final MBeanServerConnection connection, final ObjectName objName, final String attributeName)