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.cyberway.issue.crawler.settings.XMLSettingsHandler.java

/**
 * Add any files being used by any of the Modules making up the settings to
 * the list./*from  w  ww.  j  av a  2 s .  c o m*/
 *
 * @param mbean A ModuleType to interrogate for files. Any child modules
 *           will be recursively interrogated.
 * @param list The list to add found files to.
 */
private void recursiveFindSecondaryFiles(ComplexType mbean, ArrayList<String> list) {
    MBeanInfo info = mbean.getMBeanInfo();
    MBeanAttributeInfo[] a = info.getAttributes();
    // Interrogate the current module
    if (mbean instanceof ModuleType) {
        ((ModuleType) mbean).listUsedFiles(list);
    }

    // Recursively interrogate all sub modules that are of ModuleType
    for (int n = 0; n < a.length; n++) {
        if (a[n] == null) {
            // Error null attribute.
        } else {
            ModuleAttributeInfo att = (ModuleAttributeInfo) a[n];
            Object currentAttribute;
            try {
                currentAttribute = mbean.getAttribute(att.getName());
                if (currentAttribute instanceof ComplexType) {
                    recursiveFindSecondaryFiles((ComplexType) currentAttribute, list);
                }
            } catch (AttributeNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MBeanException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ReflectionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

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 w  ww  . j a  v a 2s  . 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:org.kuali.test.ui.components.dialogs.JmxDlg.java

public void loadJMXData() {
    JMXConnector conn = null;//from  ww  w .  ja v a  2 s  .  co m

    try {
        Set<String> hs = new HashSet<String>();

        if (jmx.getPerformanceMonitoringAttributes() != null) {
            for (PerformanceMonitoringAttribute pma : jmx.getPerformanceMonitoringAttributes()
                    .getPerformanceMonitoringAttributeArray()) {
                hs.add(pma.getType() + "." + pma.getName());
            }
        }

        conn = Utils.getJMXConnector(getConfiguration(), jmx);

        if (conn != null) {
            MBeanServerConnection mbeanConn = conn.getMBeanServerConnection();
            if (mbeanConn != null) {
                for (int i = 0; i < tabInfo.length; ++i) {
                    MBeanInfo mbeanInfo = mbeanConn.getMBeanInfo(new ObjectName(tabInfo[i].getJmxBeanName()));

                    if (mbeanInfo != null) {
                        for (MBeanAttributeInfo att : mbeanInfo.getAttributes()) {
                            AttributeWrapper aw = new AttributeWrapper(att);

                            if (isValidAttributeType(aw.getType())) {
                                tabInfo[i].getAttributeInfo().add(aw);

                                if (hs.contains(tabInfo[i].getJmxBeanName() + "." + aw.getName())) {
                                    aw.setSelected(true);
                                }
                            }
                        }

                        getTablePanel(i).getTable().setTableData(tabInfo[i].getAttributeInfo());
                    }
                }
            }
        }
    }

    catch (Exception ex) {
        LOG.error(ex.toString(), ex);
        UIUtils.showError(this, "JMX Error", "Error occurred during JMX connection - " + ex.toString());
    }

    finally {
        if (conn != null) {
            try {
                conn.close();
            }

            catch (Exception ex) {
            }
            ;
        }
    }
}

From source file:org.apache.catalina.manager.JMXProxyServlet.java

public void listBeans(PrintWriter writer, String qry) {

    Set names = null;/*from  ww w.  j a  v a2s .  com*/
    try {
        names = mBeanServer.queryNames(new ObjectName(qry), null);
        writer.println("OK - Number of results: " + names.size());
        writer.println();
    } catch (Exception e) {
        writer.println("Error - " + e.toString());
        return;
    }

    Iterator it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = (ObjectName) it.next();
        writer.println("Name: " + oname.toString());

        try {
            MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
            // can't be null - I thinl
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mBeanServer.getAttribute(oname, "modelerType");
            }
            writer.println("modelerType: " + code);

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

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

                try {
                    value = mBeanServer.getAttribute(oname, attName);
                } catch (Throwable t) {
                    log("Error getting attribute " + oname + " " + attName + " " + t.toString());
                    continue;
                }
                if (value == null)
                    continue;
                if ("modelerType".equals(attName))
                    continue;
                String valueString = value.toString();
                writer.println(attName + ": " + escape(valueString));
            }
        } catch (Exception e) {
            // Ignore
        }
        writer.println();
    }

}

From source file:org.apache.openejb.server.cli.command.LocalJMXCommand.java

private void listMBeans() {
    final MBeanServer mBeanServer = LocalMBeanServer.get();

    final Set<ObjectName> names;
    try {/*www .jav a  2s.c  o  m*/
        names = mBeanServer.queryNames(null, null);
    } catch (Exception e) {
        streamManager.writeErr(e);
        return;
    }

    final Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        streamManager.writeOut("Name: " + oname.toString());

        try {
            final MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mBeanServer.getAttribute(oname, "modelerType");
            }
            streamManager.writeOut("  + modelerType: " + code);

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

            for (int i = 0; i < attrs.length; i++) {
                if (!attrs[i].isReadable()) {
                    continue;
                }

                final 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 (RuntimeMBeanException uoe) {
                    // ignored
                } catch (Throwable t) {
                    streamManager.writeErr(new Exception(t));
                    continue;
                }

                try {
                    String valueString = stringify(value);
                    streamManager.writeOut("  + " + attName + ": " + valueString);
                } catch (Throwable t) {
                    streamManager.writeErr(new Exception(t));
                }
            }
        } catch (Throwable t) {
            streamManager.writeErr(new Exception(t));
        }
        streamManager.writeOut("");
    }
}

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());

    ////from   ww w.  j av  a  2 s  .com
    // 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:org.springframework.jmx.access.MBeanClientInterceptor.java

/**
 * Loads the management interface info for the configured MBean into the caches.
 * This information is used by the proxy when determining whether an invocation matches
 * a valid operation or attribute on the management interface of the managed resource.
 *//*w w w  . j  ava 2s. c  o  m*/
private void retrieveMBeanInfo(MBeanServerConnection server) throws MBeanInfoRetrievalException {
    try {
        MBeanInfo info = server.getMBeanInfo(this.objectName);

        MBeanAttributeInfo[] attributeInfo = info.getAttributes();
        this.allowedAttributes = new HashMap<>(attributeInfo.length);
        for (MBeanAttributeInfo infoEle : attributeInfo) {
            this.allowedAttributes.put(infoEle.getName(), infoEle);
        }

        MBeanOperationInfo[] operationInfo = info.getOperations();
        this.allowedOperations = new HashMap<>(operationInfo.length);
        for (MBeanOperationInfo infoEle : operationInfo) {
            Class<?>[] paramTypes = JmxUtils.parameterInfoToTypes(infoEle.getSignature(), this.beanClassLoader);
            this.allowedOperations.put(new MethodCacheKey(infoEle.getName(), paramTypes), infoEle);
        }
    } catch (ClassNotFoundException ex) {
        throw new MBeanInfoRetrievalException("Unable to locate class specified in method signature", ex);
    } catch (IntrospectionException ex) {
        throw new MBeanInfoRetrievalException("Unable to obtain MBean info for bean [" + this.objectName + "]",
                ex);
    } catch (InstanceNotFoundException ex) {
        // if we are this far this shouldn't happen, but...
        throw new MBeanInfoRetrievalException(
                "Unable to obtain MBean info for bean [" + this.objectName
                        + "]: it is likely that this bean was unregistered during the proxy creation process",
                ex);
    } catch (ReflectionException ex) {
        throw new MBeanInfoRetrievalException("Unable to read MBean info for bean [ " + this.objectName + "]",
                ex);
    } catch (IOException ex) {
        throw new MBeanInfoRetrievalException("An IOException occurred when communicating with the "
                + "MBeanServer. It is likely that you are communicating with a remote MBeanServer. "
                + "Check the inner exception for exact details.", ex);
    }
}

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.  ja  va2  s .c om*/
        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:com.jkoolcloud.tnt4j.streams.custom.kafka.interceptors.reporters.metrics.MetricsReporter.java

/**
 * Collects JMX attributes of MBeans defined by <tt>objNameStr</tt>.
 *
 * @param objNameStr//  w w w  .  jav  a  2 s.  com
 *            MBeans object name pattern to query
 * @param mBeanServer
 *            MBean server instance to use
 * @param activity
 *            activity instance to put JMX metrics snapshots
 * @throws Exception
 *             if JMX attributes collecting fails
 */
public void collectMetricsJMX(String objNameStr, MBeanServer mBeanServer, Activity activity) throws Exception {
    ObjectName oName = new ObjectName(objNameStr);
    Set<ObjectName> metricsBeans = mBeanServer.queryNames(oName, null);

    for (ObjectName mBeanName : metricsBeans) {
        try {
            PropertySnapshot snapshot = new PropertySnapshot(mBeanName.getCanonicalName());
            MBeanInfo metricsBean = mBeanServer.getMBeanInfo(mBeanName);
            MBeanAttributeInfo[] pMetricsAttrs = metricsBean.getAttributes();
            for (MBeanAttributeInfo pMetricsAttr : pMetricsAttrs) {
                try {
                    String attrName = pMetricsAttr.getName();
                    Object attrValue = mBeanServer.getAttribute(mBeanName, attrName);
                    processAttrValue(snapshot, new PropertyNameBuilder(pMetricsAttr.getName()), attrValue);
                } catch (Exception exc) {
                    Utils.logThrowable(LOGGER, OpLevel.WARNING,
                            StreamsResources.getBundle(KafkaStreamConstants.RESOURCE_BUNDLE_NAME),
                            "MetricsReporter.bean.attr.fail", mBeanName, pMetricsAttr.getName(), exc);
                }
            }

            if (getSnapshotPropIgnoreCase(snapshot, OBJ_NAME_ENTRY_KEY) == null) {
                snapshot.add(OBJ_NAME_ENTRY_KEY, mBeanName.getCanonicalName());
            }
            if (useObjectNameProperties) {
                snapshot.add("domain", mBeanName.getDomain()); // NON-NLS
                Map<String, String> objNameProps = mBeanName.getKeyPropertyList();
                for (Map.Entry<String, String> objNameProp : objNameProps.entrySet()) {
                    String propKey = objNameProp.getKey();
                    Object mv = snapshot.get(propKey);
                    snapshot.add(propKey + (mv == null ? "" : "_"), objNameProp.getValue()); // NON-NLS
                }
            }
            activity.addSnapshot(snapshot);
        } catch (Exception exc) {
            Utils.logThrowable(LOGGER, OpLevel.WARNING,
                    StreamsResources.getBundle(KafkaStreamConstants.RESOURCE_BUNDLE_NAME),
                    "MetricsReporter.bean.info.fail", mBeanName, exc);
        }
    }
}

From source file:org.apache.geode.management.internal.security.MBeanServerWrapper.java

private ResourcePermission getOperationContext(ObjectName objectName, String featureName, boolean isOp)
        throws InstanceNotFoundException, ReflectionException {
    MBeanInfo beanInfo;
    try {/*from   ww w . j ava 2  s .  c  om*/
        beanInfo = mbs.getMBeanInfo(objectName);
    } catch (IntrospectionException e) {
        throw new GemFireSecurityException("error getting beanInfo of " + objectName, e);
    }
    // If there is no annotation defined either in the class level or method level, we should
    // consider this operation/attribute freely accessible
    ResourcePermission result = null;

    // find the context in the beanInfo if defined in the class level
    result = getOperationContext(beanInfo.getDescriptor(), result);

    MBeanFeatureInfo[] featureInfos;
    if (isOp) {
        featureInfos = beanInfo.getOperations();
    } else {
        featureInfos = beanInfo.getAttributes();
    }
    // still look into the attributes/operations to see if it's defined in the method level
    for (MBeanFeatureInfo info : featureInfos) {
        if (info.getName().equals(featureName)) {
            // found the featureInfo of this method on the bean
            result = getOperationContext(info.getDescriptor(), result);
            break;
        }
    }
    return result;
}