Example usage for javax.management MBeanAttributeInfo getName

List of usage examples for javax.management MBeanAttributeInfo getName

Introduction

In this page you can find the example usage for javax.management MBeanAttributeInfo getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the feature.

Usage

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  w w  .ja  v a 2  s. c o m*/
    }

    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

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());
    }// w  w w .  ja va 2  s  .c  om
    return allProperties;
}

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

private static void writeAttribute(final JsonGenerator jg, final MBeanServer mBeanServer, ObjectName oname,
        final boolean description, final MBeanAttributeInfo attr) throws IOException {
    if (!attr.isReadable()) {
        return;//from w ww .j  a v a  2  s. c  om
    }
    String attName = attr.getName();
    if ("modelerType".equals(attName)) {
        return;
    }
    if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
        return;
    }
    String descriptionStr = description ? attr.getDescription() : null;
    Object value = null;
    try {
        value = mBeanServer.getAttribute(oname, attName);
    } catch (RuntimeMBeanException e) {
        // UnsupportedOperationExceptions happen in the normal course of business,
        // so no need to log them as errors all the time.
        if (e.getCause() instanceof UnsupportedOperationException) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Getting attribute " + attName + " of " + oname + " threw " + e);
            }
        } else {
            LOG.error("getting attribute " + attName + " of " + oname + " threw an exception", e);
        }
        return;
    } catch (RuntimeErrorException e) {
        // RuntimeErrorException happens when an unexpected failure occurs in getAttribute
        // for example https://issues.apache.org/jira/browse/DAEMON-120
        LOG.debug("getting attribute " + attName + " of " + oname + " threw an exception", e);
        return;
    } catch (AttributeNotFoundException e) {
        //Ignored the attribute was not found, which should never happen because the bean
        //just told us that it has this attribute, but if this happens just don't output
        //the attribute.
        return;
    } catch (MBeanException e) {
        //The code inside the attribute getter threw an exception so log it, and
        // skip outputting the attribute
        LOG.error("getting attribute " + attName + " of " + oname + " threw an exception", e);
        return;
    } catch (RuntimeException e) {
        //For some reason even with an MBeanException available to them Runtime exceptions
        //can still find their way through, so treat them the same as MBeanException
        LOG.error("getting attribute " + attName + " of " + oname + " threw an exception", e);
        return;
    } catch (ReflectionException e) {
        //This happens when the code inside the JMX bean (setter?? from the java docs)
        //threw an exception, so log it and skip outputting the attribute
        LOG.error("getting attribute " + attName + " of " + oname + " threw an exception", e);
        return;
    } catch (InstanceNotFoundException e) {
        //Ignored the mbean itself was not found, which should never happen because we
        //just accessed it (perhaps something unregistered in-between) but if this
        //happens just don't output the attribute.
        return;
    }

    writeAttribute(jg, attName, descriptionStr, value);
}

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  .  ja  v  a2s.  c  o  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:org.apache.hadoop.hdfs.server.common.MetricsLoggerTask.java

/**
 * Get the list of attributes for the MBean, filtering out a few attribute
 * types./*from  w w  w  .  j  av  a  2  s .c o m*/
 */
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:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps the details of a single MBean.//from  ww  w  . j  a v  a 2  s  .  c o  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:de.jgoldhammer.alfresco.jscript.jmx.JmxDumpUtil.java

/**
 * Dumps the details of a single MBean.//ww w. ja  v  a 2 s .  c om
 * 
 * @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 void printMBeanInfo(MBeanServerConnection connection, ObjectName objectName, PrintWriter out,
        String attributeName) throws IOException, JMException {
    Map<String, Object> attributes = new TreeMap<String, Object>();
    MBeanInfo info = connection.getMBeanInfo(objectName);
    attributes.put("** Object Name", objectName.toString());
    attributes.put("** Object Type", info.getClassName());

    if (StringUtils.isNotBlank(attributeName)) {
        Object value;
        try {
            value = connection.getAttribute(objectName, attributeName);
        } catch (Exception e) {
            value = JmxDumpUtil.UNREADABLE_VALUE;
        }
        outputValue(out, value, 10);
    } else {

        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);
        }
        tabulate(JmxDumpUtil.NAME_HEADER, JmxDumpUtil.VALUE_HEADER, attributes, out, 0);
    }
}

From source file:org.hyperic.hq.product.jmx.MBeanUtil.java

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

    if (method.startsWith("set") || method.startsWith("get")) {
        method = method.substring(3);/*from   ww  w.  j  av a2s  .c  o m*/
    }

    MBeanAttributeInfo[] attrs = info.getAttributes();
    for (int i = 0; i < attrs.length; i++) {
        MBeanAttributeInfo attr = attrs[i];
        if (!attr.getName().equals(method)) {
            continue;
        }

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

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

    return null;
}

From source file:org.jolokia.handler.list.AttributeDataUpdater.java

/** {@inheritDoc} */
@Override/*from   w w w . j a  v a2 s  .  co m*/
protected JSONObject extractData(MBeanInfo pMBeanInfo, String attribute) {
    JSONObject attrMap = new JSONObject();

    for (MBeanAttributeInfo attrInfo : pMBeanInfo.getAttributes()) {
        if (attribute == null || attrInfo.getName().equals(attribute)) {
            JSONObject map = new JSONObject();
            map.put(TYPE.getKey(), attrInfo.getType());
            map.put(DESCRIPTION.getKey(), attrInfo.getDescription());
            map.put(READ_WRITE.getKey(), Boolean.valueOf(attrInfo.isWritable() && attrInfo.isReadable()));
            attrMap.put(attrInfo.getName(), map);
        }
    }
    return attrMap;
}

From source file:org.apache.streams.jackson.MemoryUsageDeserializer.java

@Override
public MemoryUsageBroadcast deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
    try {/*from w  w w. j  a  v a  2s .co  m*/
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        MemoryUsageBroadcast memoryUsageBroadcast = new MemoryUsageBroadcast();
        JsonNode attributes = jsonParser.getCodec().readTree(jsonParser);

        ObjectName name = new ObjectName(attributes.get("canonicalName").asText());
        MBeanInfo info = server.getMBeanInfo(name);
        memoryUsageBroadcast.setName(name.toString());

        for (MBeanAttributeInfo attribute : Arrays.asList(info.getAttributes())) {
            switch (attribute.getName()) {
            case "Verbose":
                memoryUsageBroadcast.setVerbose((boolean) server.getAttribute(name, attribute.getName()));
                break;
            case "ObjectPendingFinalizationCount":
                memoryUsageBroadcast.setObjectPendingFinalizationCount(
                        Long.parseLong(server.getAttribute(name, attribute.getName()).toString()));
                break;
            case "HeapMemoryUsage":
                memoryUsageBroadcast.setHeapMemoryUsage(
                        (Long) ((CompositeDataSupport) server.getAttribute(name, attribute.getName()))
                                .get("used"));
                break;
            case "NonHeapMemoryUsage":
                memoryUsageBroadcast.setNonHeapMemoryUsage(
                        (Long) ((CompositeDataSupport) server.getAttribute(name, attribute.getName()))
                                .get("used"));
                break;
            }
        }

        return memoryUsageBroadcast;
    } catch (Exception e) {
        LOGGER.error("Exception trying to deserialize MemoryUsageDeserializer object: {}", e);
        return null;
    }
}