Example usage for javax.management MBeanServerConnection getMBeanInfo

List of usage examples for javax.management MBeanServerConnection getMBeanInfo

Introduction

In this page you can find the example usage for javax.management MBeanServerConnection getMBeanInfo.

Prototype

public MBeanInfo getMBeanInfo(ObjectName name)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException;

Source Link

Document

This method discovers the attributes and operations that an MBean exposes for management.

Usage

From source file:org.kuali.test.ui.components.dialogs.JmxDlg.java

public void loadJMXData() {
    JMXConnector conn = null;/* ww  w  .  ja va  2 s.c o 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:net.sf.ehcache.management.ManagementServiceTest.java

private void traverseMBeanAttributes(MBeanServerConnection connection, String type)
        throws JMException, IOException {
    Set objectNames = connection.queryNames(new ObjectName("net.sf.ehcache:type=" + type + ",*"), null);
    for (Iterator iterator = objectNames.iterator(); iterator.hasNext();) {
        ObjectName objectName = (ObjectName) iterator.next();
        MBeanInfo mBeanInfo = connection.getMBeanInfo(objectName);
        MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            MBeanAttributeInfo attribute = attributes[i];
            LOG.info(attribute.getName() + " " + connection.getAttribute(objectName, attribute.getName()));
        }//from w  ww  . ja va2s. c  om
    }
}

From source file:com.springsource.hq.plugin.tcserver.plugin.appmgmt.TomcatJmxScriptingApplicationManager.java

private boolean isTcRuntime250OrLater(String objectName, String operationName,
        int expected25OrLaterArgumentCount, ConfigResponse config) throws JMException, IOException {
    MBeanServerConnection mBeanServer = mxUtil.getMBeanServer(config.toProperties());
    MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(new ObjectName(objectName));
    for (MBeanOperationInfo operationInfo : mBeanInfo.getOperations()) {
        if (operationInfo.getName().equals(operationName)
                && (expected25OrLaterArgumentCount == operationInfo.getSignature().length)) {
            return true;
        }//www .  j a  v a  2 s.  c o  m
    }
    return false;
}

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.
 *///from  w  ww  . j a  v a2  s. 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:org.apache.hadoop.hbase.TestStochasticBalancerJmxMetrics.java

/**
 * Read the attributes from Hadoop->HBase->Master->Balancer in JMX
 * @throws IOException /*from   w  ww .  jav  a 2s .c  om*/
 */
private Set<String> readJmxMetrics() throws IOException {
    JMXConnector connector = null;
    ObjectName target = null;
    MBeanServerConnection mb = null;
    try {
        connector = JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(connectorPort, connectorPort));
        mb = connector.getMBeanServerConnection();

        Hashtable<String, String> pairs = new Hashtable<>();
        pairs.put("service", "HBase");
        pairs.put("name", "Master");
        pairs.put("sub", "Balancer");
        target = new ObjectName("Hadoop", pairs);
        MBeanInfo beanInfo = mb.getMBeanInfo(target);

        Set<String> existingAttrs = new HashSet<String>();
        for (MBeanAttributeInfo attrInfo : beanInfo.getAttributes()) {
            existingAttrs.add(attrInfo.getName());
        }
        return existingAttrs;
    } catch (Exception e) {
        LOG.warn("Failed to get bean!!! " + target, e);
        if (mb != null) {
            Set<ObjectInstance> instances = mb.queryMBeans(null, null);
            Iterator<ObjectInstance> iterator = instances.iterator();
            System.out.println("MBean Found:");
            while (iterator.hasNext()) {
                ObjectInstance instance = iterator.next();
                System.out.println("Class Name: " + instance.getClassName());
                System.out.println("Object Name: " + instance.getObjectName());
            }
        }
    } finally {
        if (connector != null) {
            try {
                connector.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

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

/**
 * Creates a Set of ServiceTypes from a Set of Services, ignoring multiple
 * services of the same ServiceType (determined by fully qualified service
 * type name)//w  w  w.j a v  a  2s  .c  om
 *
 * @param productPlugin
 *            The plugin of the product containing this service type
 * @param serverType
 *            The type of service containing this service type
 * @param mServer
 * @param serviceInfo
 *            The unique info of the service type
 * @param objectNames
 *            The {@link ObjectName}s of the associated services whose
 *            metadata is to be inspected
 * @return A Set of created {@link ServiceType}s created
 * @throws InstanceNotFoundException
 * @throws IntrospectionException
 * @throws ReflectionException
 * @throws IOException
 */
public Set create(ProductPlugin productPlugin, ServerTypeInfo serverType, MBeanServerConnection mServer,
        Set objectNames)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    final Set serviceTypes = new HashSet();
    for (Iterator iterator = objectNames.iterator(); iterator.hasNext();) {
        final ObjectName objectName = (ObjectName) iterator.next();
        final MBeanInfo serviceInfo = mServer.getMBeanInfo(objectName);
        if (serviceInfo instanceof ModelMBeanInfo) {
            ServiceType identityType = getServiceType(productPlugin.getName(), serverType,
                    (ModelMBeanInfo) serviceInfo, objectName);
            if (identityType != null && !serviceTypes.contains(identityType)) {
                final ServiceType serviceType = create(productPlugin, serverType, (ModelMBeanInfo) serviceInfo,
                        objectName);
                if (serviceType != null) {
                    serviceTypes.add(serviceType);
                }
            }
        }
    }
    return serviceTypes;
}

From source file:com.springsource.hq.plugin.tcserver.plugin.appmgmt.TomcatJmxApplicationManager.java

private boolean isTcRuntime250OrLater(String objectName, String operationName,
        int expected25OrLaterArgumentCount, Properties config) throws PluginException {

    try {//from  www .  j a v  a2 s.  co  m
        MBeanServerConnection mBeanServer = mxUtil.getMBeanServer(config);
        MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(new ObjectName(objectName));
        for (MBeanOperationInfo operationInfo : mBeanInfo.getOperations()) {
            if (operationName.equals(operationInfo.getName())
                    && expected25OrLaterArgumentCount == operationInfo.getSignature().length) {
                return true;
            }
        }
        return false;
    } catch (Exception e) {
        throw createPluginException(e);
    }
}

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

private Object queryMBeans(String pattern, Properties props) throws PluginException {

    MBeanServerConnection mServer;
    try {/*from  w w  w  .ja v  a  2  s  .c o m*/
        mServer = MxUtil.getMBeanServer(props);
    } catch (Exception e) {
        throw new PluginException("getMBeanServer(" + props.getProperty(MxUtil.PROP_JMX_URL) + "): " + e, e);
    }
    ObjectName query;
    try {
        query = new ObjectName(pattern);
    } catch (Exception e) {
        throw new PluginException("Invalid query '" + pattern + "': " + e);
    }
    Map res = new HashMap();
    try {
        Iterator beans = mServer.queryNames(query, null).iterator();
        while (beans.hasNext()) {
            ObjectName obj = (ObjectName) beans.next();
            Map bean = new HashMap();
            Map attrs = new LinkedHashMap();
            bean.put(PROP_ATTRIBUTE + "s", attrs);
            res.put(obj.toString(), bean);

            MBeanInfo info = mServer.getMBeanInfo(obj);
            MBeanAttributeInfo[] attrInfo = info.getAttributes();
            for (int i = 0; i < attrInfo.length; i++) {
                MBeanAttributeInfo mia = attrInfo[i];
                String name = mia.getName();
                Map attr = new HashMap();
                Object val;
                try {
                    val = mServer.getAttribute(obj, name);
                } catch (Exception e) {
                    continue; //XXX
                }

                if (val == null) {
                    val = "-";
                }
                attr.put("Value", val);
                attr.put("Description", mia.getDescription());
                attr.put("isWritable", new Boolean(mia.isWritable()));
                attrs.put(name, attr);
            }

            bean.put(PROP_METHOD + "s", info.getOperations());
        }
    } catch (Exception e) {
        throw new PluginException("Error in query '" + pattern + "': " + e, e);
    }

    return res;
}

From source file:org.jumpmind.symmetric.JmxCommand.java

@Override
protected boolean executeWithOptions(final CommandLine line) throws Exception {
    if (line.hasOption(OPTION_LISTBEANS)) {
        execute(new IJmxTemplate<Object>() {
            @Override/*from  w  w w  .ja va  2s. c om*/
            public Object execute(String engineName, MBeanServerConnection mbeanConn) throws Exception {
                Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
                for (ObjectName objectName : beanSet) {
                    if (objectName.getDomain().startsWith("org.jumpmind.symmetric." + engineName)) {
                        System.out.println(objectName.toString());
                    }
                }
                return null;
            }
        });
    } else if (line.hasOption(OPTION_LISTMETHODS) || line.hasOption(OPTION_METHOD)) {
        if (line.hasOption(OPTION_BEAN)) {

            execute(new IJmxTemplate<Object>() {
                @Override
                public Object execute(String engineName, MBeanServerConnection mbeanConn) throws Exception {
                    String beanName = line.getOptionValue(OPTION_BEAN);
                    MBeanInfo info = mbeanConn.getMBeanInfo(new ObjectName(beanName));
                    if (info != null) {
                        if (line.hasOption(OPTION_LISTMETHODS)) {
                            MBeanOperationInfo[] operations = info.getOperations();
                            Map<String, MBeanOperationInfo> orderedMap = new TreeMap<String, MBeanOperationInfo>();
                            for (MBeanOperationInfo methodInfo : operations) {
                                orderedMap.put(methodInfo.getName(), methodInfo);
                            }
                            for (MBeanOperationInfo methodInfo : orderedMap.values()) {
                                System.out.print(methodInfo.getName() + "(");
                                MBeanParameterInfo[] params = methodInfo.getSignature();
                                int index = 0;
                                for (MBeanParameterInfo p : params) {
                                    if (index > 0) {
                                        System.out.print(", ");
                                    }
                                    System.out.print(p.getType() + " " + p.getName());
                                    index++;
                                }
                                System.out.print(")");
                                if (methodInfo.getReturnType() != null
                                        && !methodInfo.getReturnType().equals("void")) {
                                    System.out.print(" : " + methodInfo.getReturnType());
                                }
                                System.out.println();
                            }
                        } else if (line.hasOption(OPTION_METHOD)) {
                            String argsDelimiter = line.getOptionValue(OPTION_ARGS_DELIM);
                            if (isBlank(argsDelimiter)) {
                                argsDelimiter = ",";
                            } else {
                                argsDelimiter = argsDelimiter.trim();
                            }
                            String methodName = line.getOptionValue(OPTION_METHOD);
                            String[] args = null;
                            if (line.hasOption(OPTION_ARGS)) {
                                String argLine = line.getOptionValue(OPTION_ARGS);
                                args = argsDelimiter == "," ? CsvUtils.tokenizeCsvData(argLine)
                                        : argLine.split(argsDelimiter);
                                ;
                            } else {
                                args = new String[0];
                            }

                            MBeanOperationInfo[] operations = info.getOperations();
                            for (MBeanOperationInfo methodInfo : operations) {
                                MBeanParameterInfo[] paramInfos = methodInfo.getSignature();
                                if (methodInfo.getName().equals(methodName)
                                        && paramInfos.length == args.length) {
                                    String[] signature = new String[args.length];
                                    Object[] objArgs = new Object[args.length];
                                    int index = 0;
                                    for (MBeanParameterInfo paramInfo : paramInfos) {
                                        signature[index] = paramInfo.getType();
                                        if (!paramInfo.getType().equals(String.class.getName())) {
                                            Class<?> clazz = Class.forName(paramInfo.getType());
                                            Constructor<?> constructor = clazz.getConstructor(String.class);
                                            objArgs[index] = constructor.newInstance(args[index]);
                                        } else {
                                            objArgs[index] = args[index];
                                        }
                                        index++;
                                    }
                                    Object returnValue = mbeanConn.invoke(new ObjectName(beanName), methodName,
                                            objArgs, signature);
                                    if (methodInfo.getReturnType() != null
                                            && !methodInfo.getReturnType().equals("void")) {
                                        System.out.println(returnValue);
                                    }
                                    System.exit(0);
                                }
                            }

                            System.out.println("ERROR: Could not locate a JMX method named: " + methodName
                                    + " with " + args.length + " arguments on bean: " + beanName);
                            System.exit(1);

                            return null;

                        }
                    } else {
                        System.out.println("ERROR: Could not locate a JMX bean with the name of: " + beanName);
                        System.exit(1);
                    }
                    return null;
                }
            });
        } else {
            System.out.println("ERROR: Must specifiy the --bean option.");
            System.exit(1);
        }
    } else {
        return false;
    }

    return true;
}

From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java

/**
 * {@inheritDoc}/*from www.j  ava  2s  .  c  o  m*/
 * @see com.heliosapm.opentsdb.TSDBSubmitter#getAttributeNames(javax.management.MBeanServerConnection, javax.management.ObjectName)
 */
@Override
public String[] getAttributeNames(final MBeanServerConnection conn, final ObjectName target) {
    try {
        MBeanAttributeInfo[] attrInfos = conn.getMBeanInfo(target).getAttributes();
        String[] attrNames = new String[attrInfos.length];
        for (int i = 0; i < attrInfos.length; i++) {
            attrNames[i] = attrInfos[i].getName();
        }
        return attrNames;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}