Java MBean readAttribute(MBeanServer mbeanServer, String mbeanName, String attributeName)

Here you can find the source of readAttribute(MBeanServer mbeanServer, String mbeanName, String attributeName)

Description

read Attribute

License

Apache License

Declaration

public static Object readAttribute(MBeanServer mbeanServer,
            String mbeanName, String attributeName)
            throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;

public class Main {
    public static Object readAttribute(MBeanServer mbeanServer,
            String mbeanName, String attributeName)
            throws IllegalArgumentException {

        try {/*from  www .  j av  a 2  s  .  c  o m*/
            ObjectName objectName = new ObjectName(mbeanName);
            return readAttribute(mbeanServer, objectName, attributeName);
        } catch (MalformedObjectNameException e) {
            throw new IllegalArgumentException(e);
        }

    }

    public static Object readAttribute(MBeanServer mbeanServer,
            ObjectName objectName, String attributeName)
            throws IllegalArgumentException {
        try {
            int slashPos = attributeName.indexOf("/");
            Object attribute;
            if (slashPos < 0) {
                attribute = mbeanServer.getAttribute(objectName,
                        attributeName);
            } else {
                String prefix = attributeName.substring(0, slashPos);
                attribute = mbeanServer.getAttribute(objectName, prefix);
                if (attribute instanceof CompositeData) {
                    CompositeData data = (CompositeData) attribute;
                    attribute = data.get(attributeName
                            .substring(slashPos + 1));
                } else {
                    attribute = "No CompositeData found on attribute named "
                            + prefix
                            + ". Type is "
                            + attribute.getClass().getName();
                }
            }
            return attribute;
        } catch (AttributeNotFoundException | InstanceNotFoundException
                | MBeanException | ReflectionException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

  1. onameForBean(final Object mbean, final String name)
  2. operationEquals(MBeanOperationInfo one, MBeanOperationInfo two)
  3. paramEquals(MBeanParameterInfo o1, MBeanParameterInfo o2)
  4. queryAndGetAttribute(MBeanServerConnection connection, String domain, String name, String type, String scope, String attribute)
  5. queryConnectionBy(MBeanServerConnection connection, ObjectName objectName)
  6. registerMBean(MBeanServer mbs, Object object, ObjectName name, boolean unreg)
  7. registerMBean(MBeanServer server, String agentName, Map attrs, Object mbeanObject)
  8. registerMBean(Object bean, MBeanServer mBeanServer, ObjectName objectName)
  9. removeTimerNotification(ObjectName timer, Integer id, MBeanServer server)