Java MBean queryAndGetAttribute(MBeanServerConnection connection, String domain, String name, String type, String scope, String attribute)

Here you can find the source of queryAndGetAttribute(MBeanServerConnection connection, String domain, String name, String type, String scope, String attribute)

Description

query And Get Attribute

License

Open Source License

Declaration

public static <T> T queryAndGetAttribute(MBeanServerConnection connection, String domain, String name,
            String type, String scope, String attribute) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Hashtable;

import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;

public class Main {
    public static <T> T queryAndGetAttribute(MBeanServerConnection connection, String domain, String name,
            String type, String scope, String attribute) throws Exception {
        return queryAndGetAttribute(connection, getObjectName(domain, name, type, scope), attribute);
    }/*from  w w w . j ava  2 s  . co m*/

    public static <T> T queryAndGetAttribute(MBeanServerConnection connection, ObjectName objectName,
            String attribute) throws Exception {
        Set<ObjectInstance> instances = queryConnectionBy(connection, objectName);

        if (instances != null && instances.size() == 1) {
            return getAttribute(connection, objectName, attribute);
        } else {
            return null;
        }
    }

    public static ObjectName getObjectName(String domain, String name, String type, String scope) throws Exception {
        Hashtable<String, String> map = new Hashtable<String, String>();
        if (name != null)
            map.put("name", name);
        if (type != null)
            map.put("type", type);
        if (scope != null)
            map.put("scope", scope);
        return ObjectName.getInstance(domain, map);
    }

    public static Set<ObjectInstance> queryConnectionBy(MBeanServerConnection connection, ObjectName objectName)
            throws Exception {
        return connection.queryMBeans(objectName, null);
    }

    public static Set<ObjectInstance> queryConnectionBy(MBeanServerConnection connection, String domain,
            String name, String type, String scope) throws Exception {
        return queryConnectionBy(connection, getObjectName(domain, name, type, scope));
    }

    @SuppressWarnings("unchecked")
    public static <T> T getAttribute(MBeanServerConnection connection, ObjectName objectName, String attribute)
            throws Exception {
        return (T) connection.getAttribute(objectName, attribute);
    }
}

Related

  1. newMBeanProxy(final MBeanServerConnection conn, final ObjectName objectName, final Class interfaceClass)
  2. objectNameFor(Logger logger, String mbeanName)
  3. onameForBean(final Object mbean, final String name)
  4. operationEquals(MBeanOperationInfo one, MBeanOperationInfo two)
  5. paramEquals(MBeanParameterInfo o1, MBeanParameterInfo o2)
  6. queryConnectionBy(MBeanServerConnection connection, ObjectName objectName)
  7. readAttribute(MBeanServer mbeanServer, String mbeanName, String attributeName)
  8. registerMBean(MBeanServer mbs, Object object, ObjectName name, boolean unreg)
  9. registerMBean(MBeanServer server, String agentName, Map attrs, Object mbeanObject)