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:net.tzolov.geode.jmx.JmxInfluxLoader.java

private String[] attributeNames(MBeanServerConnection connection, String objectName,
        MBeanAttributeInfoFilter attributeFilter) {

    try {/*from ww w .  j a v a 2  s. c o m*/
        ImmutableList.Builder<String> builder = ImmutableList.builder();
        for (MBeanAttributeInfo attr : connection.getMBeanInfo(new ObjectName(objectName)).getAttributes()) {
            if (!attributeFilter.filter(attr)) {
                builder.add(attr.getName());
            }
        }
        ImmutableList<String> names = builder.build();
        return names.toArray(new String[names.size()]);
    } catch (Exception ex) {
        throw new RuntimeException((ex));
    }
}

From source file:com.proofpoint.jmx.MBeanRepresentation.java

public MBeanRepresentation(MBeanServer mbeanServer, ObjectName objectName, ObjectMapper objectMapper)
        throws JMException {
    this.objectName = objectName;

    MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName);

    className = mbeanInfo.getClassName();
    description = mbeanInfo.getDescription();
    descriptor = toMap(mbeanInfo.getDescriptor());

    ///*  www .j a va 2 s  . c o m*/
    // Attributes
    //
    LinkedHashMap<String, MBeanAttributeInfo> attributeInfos = Maps.newLinkedHashMap();
    for (MBeanAttributeInfo attributeInfo : mbeanInfo.getAttributes()) {
        attributeInfos.put(attributeInfo.getName(), attributeInfo);
    }

    String[] attributeNames = attributeInfos.keySet().toArray(new String[attributeInfos.size()]);
    ImmutableList.Builder<AttributeRepresentation> attributes = ImmutableList.builder();
    for (Attribute attribute : mbeanServer.getAttributes(objectName, attributeNames).asList()) {
        String attributeName = attribute.getName();

        // use remove so we only include one value for each attribute
        MBeanAttributeInfo attributeInfo = attributeInfos.remove(attributeName);
        if (attributeInfo == null) {
            // unknown extra attribute, could have been added after MBeanInfo was fetched
            continue;
        }

        Object attributeValue = attribute.getValue();
        AttributeRepresentation attributeRepresentation = new AttributeRepresentation(attributeInfo,
                attributeValue, objectMapper);
        attributes.add(attributeRepresentation);
    }
    this.attributes = attributes.build();

    //
    // Operations
    //
    ImmutableList.Builder<OperationRepresentation> operations = ImmutableList.builder();
    for (MBeanOperationInfo operationInfo : mbeanInfo.getOperations()) {
        operations.add(new OperationRepresentation(operationInfo));
    }
    this.operations = operations.build();
}

From source file:com.googlecode.jmxtrans.model.Query.java

public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    MBeanInfo info = mbeanServer.getMBeanInfo(queryName);
    ObjectInstance oi = mbeanServer.getObjectInstance(queryName);

    List<String> attributes;
    if (attr.isEmpty()) {
        attributes = new ArrayList<>();
        for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
            attributes.add(attrInfo.getName());
        }/*from   ww  w  . j  a v a  2  s  . c o m*/
    } else {
        attributes = attr;
    }

    try {
        if (!attributes.isEmpty()) {
            logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this);

            AttributeList al = mbeanServer.getAttributes(queryName,
                    attributes.toArray(new String[attributes.size()]));

            return new JmxResultProcessor(this, oi, al.asList(), info.getClassName(), queryName.getDomain())
                    .getResults();
        }
    } catch (UnmarshalException ue) {
        if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) {
            logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: "
                    + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52",
                    ue.getMessage());
        } else {
            throw ue;
        }
    }
    return ImmutableList.of();
}

From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java

private void writeJmx(BundleWriter writer) throws IOException {
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

    try (JsonGenerator generator = writer.createGenerator("runtime/jmx.json")) {
        generator.useDefaultPrettyPrinter();
        generator.writeStartObject();//  w  w w.j  a  va  2 s .c o m
        generator.writeArrayFieldStart("beans");

        for (ObjectName objectName : mBeanServer.queryNames(null, null)) {
            MBeanInfo info;
            try {
                info = mBeanServer.getMBeanInfo(objectName);
            } catch (InstanceNotFoundException | IntrospectionException | ReflectionException ex) {
                LOG.warn("Exception accessing MBeanInfo ", ex);
                continue;
            }

            generator.writeStartObject();
            generator.writeStringField("name", objectName.toString());
            generator.writeObjectFieldStart("attributes");

            for (MBeanAttributeInfo attr : info.getAttributes()) {
                try {
                    writeAttribute(generator, attr.getName(),
                            mBeanServer.getAttribute(objectName, attr.getName()));
                } catch (MBeanException | AttributeNotFoundException | InstanceNotFoundException
                        | ReflectionException | RuntimeMBeanException ex) {
                    generator.writeStringField(attr.getName(), "Exception: " + ex.toString());
                }
            }

            generator.writeEndObject();
            generator.writeEndObject();
            writer.writeLn("");
        }

        generator.writeEndArray();
        generator.writeEndObject();
    } finally {
        writer.markEndOfFile();
    }
}

From source file:org.apache.hadoop.jmx.JMXJsonServlet.java

private void writeAttribute(JsonGenerator jg, ObjectName oname, MBeanAttributeInfo attr) throws IOException {
    if (!attr.isReadable()) {
        return;//  www  .  j  a  v  a 2 s .  c  o m
    }
    String attName = attr.getName();
    if ("modelerType".equals(attName)) {
        return;
    }
    if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
        return;
    }
    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) {
            LOG.debug("getting attribute " + attName + " of " + oname + " threw an exception", e);
        } else {
            LOG.error("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, value);
}

From source file:com.streamsets.datacollector.http.JMXJsonServlet.java

private void writeAttribute(JsonGenerator jg, ObjectName oname, MBeanAttributeInfo attr) throws IOException {
    if (!attr.isReadable()) {
        return;//from   w  ww.  j  a  va  2 s  .  co  m
    }
    String attName = attr.getName();
    if ("modelerType".equals(attName)) {
        return;
    }
    if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
        return;
    }
    Object value = null;
    try {
        value = mBeanServer.getAttribute(oname, attName);
    } catch (RuntimeMBeanException e) {
        // UnsupportedOperationExceptions happen in the normal course of business,
        // so no need to 
        if (e.getCause() instanceof UnsupportedOperationException) {

        } else {

        }
        return;
    } catch (RuntimeErrorException e) {
        // RuntimeErrorException happens when an unexpected failure occurs in getAttribute
        // for example https://issues.apache.org/jira/browse/DAEMON-120

        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 
        // skip outputting the attribute

        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

        return;
    } catch (ReflectionException e) {
        //This happens when the code inside the JMX bean (setter?? from the java docs)
        //threw an exception, so 

        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, value);
}

From source file:org.opennms.tools.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator.java

private Attrib createAttr(MBeanAttributeInfo jmxMBeanAttributeInfo) {
    Attrib xmlJmxAttribute = xmlObjectFactory.createAttrib();

    xmlJmxAttribute.setType("gauge");
    xmlJmxAttribute.setName(jmxMBeanAttributeInfo.getName());
    String alias = NameTools.trimByDictionary(jmxMBeanAttributeInfo.getName());
    alias = createAndRegisterUniceAlias(alias);
    xmlJmxAttribute.setAlias(alias);/*from  ww  w  .j a v a 2s . com*/

    return xmlJmxAttribute;
}

From source file:com.zabbix.gateway.JMXItemChecker.java

@Override
protected String getStringValue(String key) throws Exception {
    ZabbixItem item = new ZabbixItem(key);

    if (item.getKeyId().equals("jmx")) {
        if (2 != item.getArgumentCount())
            throw new ZabbixException("required key format: jmx[<object name>,<attribute name>]");

        ObjectName objectName = new ObjectName(item.getArgument(1));
        String attributeName = item.getArgument(2);
        String realAttributeName;
        String fieldNames = "";

        // Attribute name and composite data field names are separated by dots. On the other hand the
        // name may contain a dot too. In this case user needs to escape it with a backslash. Also the
        // backslash symbols in the name must be escaped. So a real separator is unescaped dot and
        // separatorIndex() is used to locate it.

        int sep = HelperFunctionChest.separatorIndex(attributeName);

        if (-1 != sep) {
            logger.trace("'{}' contains composite data", attributeName);

            realAttributeName = attributeName.substring(0, sep);
            fieldNames = attributeName.substring(sep + 1);
        } else//from  w  w w.ja  v  a  2  s . co  m
            realAttributeName = attributeName;

        // unescape possible dots or backslashes that were escaped by user
        realAttributeName = HelperFunctionChest.unescapeUserInput(realAttributeName);

        logger.trace("attributeName:'{}'", realAttributeName);
        logger.trace("fieldNames:'{}'", fieldNames);

        return getPrimitiveAttributeValue(mbsc.getAttribute(objectName, realAttributeName), fieldNames);
    } else if (item.getKeyId().equals("jmx.discovery")) {
        ObjectName objectName = null;
        String attributeName = null;
        if (item.getArgumentCount() >= 1) {
            objectName = new ObjectName(item.getArgument(1));
        }

        if (item.getArgumentCount() >= 2) {
            attributeName = item.getArgument(2);
        }

        JSONArray counters = new JSONArray();

        logger.trace("attributeName = {}, item.getArgumentCount() = " + item.getArgumentCount(), attributeName);

        for (ObjectName name : mbsc.queryNames(objectName, null)) {
            logger.trace("discovered object '{}'", name);

            for (MBeanAttributeInfo attrInfo : mbsc.getMBeanInfo(name).getAttributes()) {
                logger.trace("discovered attribute '{}'", attrInfo.getName());

                if (!attrInfo.isReadable()) {
                    logger.trace("attribute not readable, skipping");
                    continue;
                }

                try {
                    if (null == attributeName || attrInfo.getName().equals(attributeName)) {
                        logger.trace("looking for attributes of primitive types");
                        String descr = (attrInfo.getName().equals(attrInfo.getDescription()) ? null
                                : attrInfo.getDescription());
                        findPrimitiveAttributes(counters, name, descr, attrInfo.getName(),
                                mbsc.getAttribute(name, attrInfo.getName()));
                    }
                } catch (Exception e) {
                    Object[] logInfo = { name, attrInfo.getName(), e };
                    logger.trace("processing '{},{}' failed", logInfo);
                }
            }
        }

        JSONObject mapping = new JSONObject();
        mapping.put(ItemChecker.JSON_TAG_DATA, counters);
        return mapping.toString(2);
    } else
        throw new ZabbixException("key ID '%s' is not supported", item.getKeyId());
}

From source file:org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator.java

private Attrib createAttr(MBeanAttributeInfo jmxMBeanAttributeInfo) {
    Attrib xmlJmxAttribute = xmlObjectFactory.createAttrib();

    xmlJmxAttribute.setType("gauge");
    xmlJmxAttribute.setName(jmxMBeanAttributeInfo.getName());
    String alias = nameCutter.trimByDictionary(jmxMBeanAttributeInfo.getName());
    alias = createAndRegisterUniqueAlias(alias);
    xmlJmxAttribute.setAlias(alias);//from w w w .  j  a  v  a 2 s . co m

    return xmlJmxAttribute;
}

From source file:org.apache.hive.http.JMXJsonServlet.java

private void writeAttribute(JsonGenerator jg, ObjectName oname, MBeanAttributeInfo attr) throws IOException {
    if (!attr.isReadable()) {
        return;//w  w  w.  j  a  v  a2 s.  c  o  m
    }
    String attName = attr.getName();
    if ("modelerType".equals(attName)) {
        return;
    }
    if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
        return;
    }
    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) {
            LOG.debug("getting attribute " + attName + " of " + oname + " threw an exception", 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, value);
}