Example usage for javax.management MBeanInfo getAttributes

List of usage examples for javax.management MBeanInfo getAttributes

Introduction

In this page you can find the example usage for javax.management MBeanInfo getAttributes.

Prototype

public MBeanAttributeInfo[] getAttributes() 

Source Link

Document

Returns the list of attributes exposed for management.

Usage

From source file:com.googlecode.psiprobe.tools.JmxTools.java

public static boolean hasAttribute(MBeanServer server, ObjectName mbean, String attrName) throws Exception {
    MBeanInfo info = server.getMBeanInfo(mbean);
    MBeanAttributeInfo[] ai = info.getAttributes();
    for (int i = 0; i < ai.length; i++) {
        if (ai[i].getName().equals(attrName)) {
            return true;
        }/*from  ww w .  j av a2s. c  om*/
    }
    return false;
}

From source file:org.apache.hadoop.hbase.util.JSONMetricUtil.java

public static MBeanAttributeInfo[] getMBeanAttributeInfo(ObjectName bean)
        throws IntrospectionException, InstanceNotFoundException, ReflectionException, IntrospectionException,
        javax.management.IntrospectionException {
    MBeanInfo mbinfo = mbServer.getMBeanInfo(bean);
    return mbinfo.getAttributes();
}

From source file:org.apache.hadoop.hdfs.server.common.MetricsLoggerTask.java

/**
 * Get the list of attributes for the MBean, filtering out a few attribute
 * types./* w  w w  .j a v a2 s  .  com*/
 */
private static Set<String> getFilteredAttributes(MBeanInfo mBeanInfo) {
    Set<String> attributeNames = new HashSet<>();
    for (MBeanAttributeInfo attributeInfo : mBeanInfo.getAttributes()) {
        if (!attributeInfo.getType().equals("javax.management.openmbean.TabularData")
                && !attributeInfo.getType().equals("javax.management.openmbean.CompositeData")
                && !attributeInfo.getType().equals("[Ljavax.management.openmbean.CompositeData;")) {
            attributeNames.add(attributeInfo.getName());
        }
    }
    return attributeNames;
}

From source file:com.athena.dolly.console.module.jmx.JmxClientManager.java

public static HashMap<String, Object> getObjectNameInfo(ObjectName objName, String nodeName) {
    JmxClient jmxClient = jmxClientMap.get(nodeName);

    try {/*from w w w . j  ava2s  .co  m*/
        MBeanServerConnection connection = jmxClient.getJmxConnector().getMBeanServerConnection();
        HashMap<String, Object> infoMap = new HashMap<String, Object>();
        Set<ObjectName> names = new TreeSet<ObjectName>(connection.queryNames(objName, null));

        for (ObjectName name : names) {
            logger.info("#######################");
            logger.info("\tObjectName = " + name);

            MBeanInfo info = connection.getMBeanInfo(name);
            MBeanAttributeInfo[] attributes = info.getAttributes();

            for (MBeanAttributeInfo attr : attributes) {
                logger.info("==========================");
                logger.info("attrName = " + attr.getName());
                logger.info("attrType = " + attr.getType());
                logger.info("connection.getAttribute = " + connection.getAttribute(name, attr.getName()));

                infoMap.put(attr.getName(), connection.getAttribute(name, attr.getName()));
            }
        }

        return infoMap;
    } catch (Exception e) {
        logger.error("unhandled exception has errored : ", e);
    }

    return null;
}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

public static List<String> getAllAttributeNames(MBeanServerConnection connection, ObjectName objectName)
        throws IOException, JMException {
    List<String> allProperties = new ArrayList<String>();
    MBeanInfo info = connection.getMBeanInfo(objectName);
    for (MBeanAttributeInfo element : info.getAttributes()) {
        allProperties.add(element.getName());
    }//ww  w  .  j ava 2  s. c  o  m
    return allProperties;
}

From source file:Utilities.java

/**
 * Prints bean attributes to a {@link VarOutputSink}.
 * //from w  w  w .  ja  v  a  2  s.c  om
 * @param sink The {@link VarOutputSink} to which attributes will be sent
 * @param mbs The {@link MBeanServer} with respect to which the
 *          {@code objectName} is accessed
 * @param objectName The {@link ObjectName} that identifies this bean
 */
public static void printMBeanAttributes(VarOutputSink sink, MBeanServer mbs, ObjectName objectName) {
    MBeanInfo info = getMBeanInfoSafely(sink, mbs, objectName);
    if (info == null) {
        sink.printVariable(objectName.getCanonicalName(), "can't fetch info");
        return;
    }
    MBeanAttributeInfo[] attrInfo = info.getAttributes();
    if (attrInfo.length > 0) {
        for (int i = 0; i < attrInfo.length; i++) {
            String attrName = attrInfo[i].getName();
            Object attrValue = null;
            String attrValueString = null;
            try {
                attrValue = mbs.getAttribute(objectName, attrName);
            } catch (AttributeNotFoundException e) {
                attrValueString = "AttributeNotFoundException";
            } catch (InstanceNotFoundException e) {
                attrValueString = "InstanceNotFoundException";
            } catch (MBeanException e) {
                attrValueString = "MBeanException";
            } catch (ReflectionException e) {
                attrValueString = "ReflectionException";
            }
            if (attrValueString == null) {
                attrValueString = attrValue.toString();
            }
            sink.printVariable(attrName, attrValueString);
        }
    }
}

From source file:Utilities.java

/**
 * Prints info about a bean to a {@link VarOutputSink}.
 * /*from  w ww .  ja va 2  s .  c o m*/
 * @param sink The {@link VarOutputSink} to which info will be sent
 * @param mbs The {@link MBeanServer} with respect to which the
 *          {@code objectName} is accessed
 * @param objectName The {@link ObjectName} that identifies this bean
 */
public static void printMBeanInfo(VarOutputSink sink, MBeanServer mbs, ObjectName objectName) {
    MBeanInfo info = getMBeanInfoSafely(sink, mbs, objectName);
    if (info == null) {
        return;
    }

    sink.echo("\nCLASSNAME: \t" + info.getClassName());
    sink.echo("\nDESCRIPTION: \t" + info.getDescription());
    sink.echo("\nATTRIBUTES");
    MBeanAttributeInfo[] attrInfo = info.getAttributes();
    sink.printVariable("attrcount", Integer.toString(attrInfo.length));
    if (attrInfo.length > 0) {
        for (int i = 0; i < attrInfo.length; i++) {
            sink.echo(" ** NAME: \t" + attrInfo[i].getName());
            sink.echo("    DESCR: \t" + attrInfo[i].getDescription());
            sink.echo("    TYPE: \t" + attrInfo[i].getType() + "\tREAD: " + attrInfo[i].isReadable()
                    + "\tWRITE: " + attrInfo[i].isWritable());
        }
    } else
        sink.echo(" ** No attributes **");
    sink.echo("\nCONSTRUCTORS");
    MBeanConstructorInfo[] constrInfo = info.getConstructors();
    for (int i = 0; i < constrInfo.length; i++) {
        sink.echo(" ** NAME: \t" + constrInfo[i].getName());
        sink.echo("    DESCR: \t" + constrInfo[i].getDescription());
        sink.echo("    PARAM: \t" + constrInfo[i].getSignature().length + " parameter(s)");
    }
    sink.echo("\nOPERATIONS");
    MBeanOperationInfo[] opInfo = info.getOperations();
    if (opInfo.length > 0) {
        for (int i = 0; i < opInfo.length; i++) {
            sink.echo(" ** NAME: \t" + opInfo[i].getName());
            sink.echo("    DESCR: \t" + opInfo[i].getDescription());
            sink.echo("    PARAM: \t" + opInfo[i].getSignature().length + " parameter(s)");
        }
    } else
        sink.echo(" ** No operations ** ");
    sink.echo("\nNOTIFICATIONS");
    MBeanNotificationInfo[] notifInfo = info.getNotifications();
    if (notifInfo.length > 0) {
        for (int i = 0; i < notifInfo.length; i++) {
            sink.echo(" ** NAME: \t" + notifInfo[i].getName());
            sink.echo("    DESCR: \t" + notifInfo[i].getDescription());
            String notifTypes[] = notifInfo[i].getNotifTypes();
            for (int j = 0; j < notifTypes.length; j++) {
                sink.echo("    TYPE: \t" + notifTypes[j]);
            }
        }
    } else
        sink.echo(" ** No notifications **");
}

From source file:org.hyperic.hq.plugin.jboss.MBeanUtil.java

public static OperationParams getAttributeParams(MBeanInfo info, String method, Object args[])
        throws PluginException {

    if (method.startsWith("set")) {
        method = method.substring(3);//from w  ww . ja  v  a  2 s  .c om
    }

    MBeanAttributeInfo[] attrs = info.getAttributes();
    for (int i = 0; i < attrs.length; i++) {
        MBeanAttributeInfo attr = attrs[i];
        if (!attr.getName().equals(method)) {
            continue;
        }
        if (!attr.isWritable()) {
            throw new PluginException("Attribute '" + method + "' is not writable");
        }

        String sig = attr.getType();
        if (!hasConverter(sig)) {
            String msg = "Cannot convert String argument to " + sig;
            throw new PluginException(msg);
        }

        if (args.length != 1) {
            String msg = "setAttribute(" + method + ") takes [1] argument, [" + args.length + "] given";
            throw new PluginException(msg);
        }

        OperationParams params = new OperationParams();
        Object value;
        try {
            value = convert(sig, (String) args[0]);
        } catch (Exception e) {
            String msg = "Exception converting param '" + args[0] + "' to type '" + sig + "'";
            throw new PluginException(msg + ": " + e);
        }
        params.arguments = new Object[] { value };
        params.isAttribute = true;
        return params;
    }

    return null;
}

From source file:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps the details of a single MBean.//w  ww .j  av a  2 s . co  m
 * 
 * @param connection
 *            the server connection (or server itself)
 * @param objectName
 *            the object name
 * @param out
 *            PrintWriter to write the output to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws JMException
 *             Signals a JMX error
 */
public static Map<Object, Object> getSimpleMBeanInfo(MBeanServerConnection connection, ObjectName objectName)
        throws IOException, JMException {
    Map<Object, Object> attributes = new TreeMap<Object, Object>();
    MBeanInfo info = connection.getMBeanInfo(objectName);
    attributes.put("** Object Name", objectName.toString());
    attributes.put("** Object Type", info.getClassName());

    for (MBeanAttributeInfo element : info.getAttributes()) {
        Object value;
        if (element.isReadable()) {
            try {
                value = connection.getAttribute(objectName, element.getName());
            } catch (Exception e) {
                value = JmxDumpUtil.UNREADABLE_VALUE;
            }
        } else {
            value = JmxDumpUtil.UNREADABLE_VALUE;
        }
        attributes.put(element.getName(), value);
    }
    return attributes;
}

From source file:org.apache.catalina.mbeans.MBeanDumper.java

/**
 * The following code to dump MBeans has been copied from JMXProxyServlet.
 *
 *///from   ww w  .  j  a  v  a 2 s. co  m
public static String dumpBeans(MBeanServer mbeanServer, Set<ObjectName> names) {
    StringBuilder buf = new StringBuilder();
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        buf.append("Name: ");
        buf.append(oname.toString());
        buf.append(CRLF);

        try {
            MBeanInfo minfo = mbeanServer.getMBeanInfo(oname);
            // can't be null - I think
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mbeanServer.getAttribute(oname, "modelerType");
            }
            buf.append("modelerType: ");
            buf.append(code);
            buf.append(CRLF);

            MBeanAttributeInfo attrs[] = minfo.getAttributes();
            Object value = null;

            for (int i = 0; i < attrs.length; i++) {
                if (!attrs[i].isReadable())
                    continue;
                String attName = attrs[i].getName();
                if ("modelerType".equals(attName))
                    continue;
                if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
                    continue;
                }

                try {
                    value = mbeanServer.getAttribute(oname, attName);
                } catch (JMRuntimeException rme) {
                    Throwable cause = rme.getCause();
                    if (cause instanceof UnsupportedOperationException) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error getting attribute " + oname + " " + attName, rme);
                        }
                    } else if (cause instanceof NullPointerException) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error getting attribute " + oname + " " + attName, rme);
                        }
                    } else {
                        log.error("Error getting attribute " + oname + " " + attName, rme);
                    }
                    continue;
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    log.error("Error getting attribute " + oname + " " + attName, t);
                    continue;
                }
                if (value == null)
                    continue;
                String valueString;
                try {
                    Class<?> c = value.getClass();
                    if (c.isArray()) {
                        int len = Array.getLength(value);
                        StringBuilder sb = new StringBuilder(
                                "Array[" + c.getComponentType().getName() + "] of length " + len);
                        if (len > 0) {
                            sb.append(CRLF);
                        }
                        for (int j = 0; j < len; j++) {
                            sb.append("\t");
                            Object item = Array.get(value, j);
                            if (item == null) {
                                sb.append("NULL VALUE");
                            } else {
                                try {
                                    sb.append(escape(item.toString()));
                                } catch (Throwable t) {
                                    ExceptionUtils.handleThrowable(t);
                                    sb.append("NON-STRINGABLE VALUE");
                                }
                            }
                            if (j < len - 1) {
                                sb.append(CRLF);
                            }
                        }
                        valueString = sb.toString();
                    } else {
                        valueString = escape(value.toString());
                    }
                    buf.append(attName);
                    buf.append(": ");
                    buf.append(valueString);
                    buf.append(CRLF);
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                }
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        }
        buf.append(CRLF);
    }
    return buf.toString();

}