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:cc.kune.kunecli.JMXUtils.java

public static Object doOperation(final String objectName, final String operation) {

    final List<VirtualMachineDescriptor> vms = VirtualMachine.list();

    try {/*from  w  w  w  . j  av  a  2s.  c  o  m*/
        for (final VirtualMachineDescriptor vmd : vms) {
            final String id = vmd.id();
            LOG.debug("VM id: " + id);
            final JMXServiceURL url = getURLForPid(id.trim());

            final JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
            final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

            final ObjectName mbeanName = new ObjectName(objectName);

            MBeanInfo beanInfo;
            try {
                beanInfo = mbsc.getMBeanInfo(mbeanName);
                for (final MBeanOperationInfo currentOp : beanInfo.getOperations()) {
                    if (currentOp.getName().equals(operation)) {
                        LOG.debug("Doing operation '" + operation + "' over mbean: '" + objectName
                                + "' with id: '" + id + "'.");
                        final Object invoke = mbsc.invoke(mbeanName, currentOp.getName(), new Object[] {},
                                new String[] {});
                        return invoke;
                    }
                }
            } catch (final InstanceNotFoundException e) {
                // Ok, operation not found in this VM or domain
            }
        }
        throw new RuntimeException("JMX operation not found");
    } catch (final Exception e) {
        LOG.error("Error in jmx connection", e);
    }
    throw new RuntimeException("JMX operation failed");
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

@Nullable
protected static ObjectName getObjectName(final MBeanServerConnection connection, final Class objectClass)
        throws IOException {

    Set<ObjectName> names = connection.queryNames(null, null);
    return IterableUtils.find(names, o -> {
        MBeanInfo info;/*from ww  w .jav a2  s. c  om*/
        try {
            info = connection.getMBeanInfo(o);
        } catch (InstanceNotFoundException | UnmarshalException e) {
            return false;
        } catch (Exception e) {
            throw new JmxControlException(e);
        }
        return Objects.equals(objectClass.getName(), info.getClassName());
    });
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

protected static Collection<ObjectName> getSuitableObjectNames(final MBeanServerConnection connection,
        final Class objectClass) throws IOException {
    Set<ObjectName> names = connection.queryNames(null, null);

    // find all suitable beans
    @SuppressWarnings("unchecked")
    Collection<ObjectName> suitableNames = CollectionUtils.select(names, o -> {
        MBeanInfo info;/*from  w  ww. j  a v a  2  s .c o m*/
        try {
            info = connection.getMBeanInfo(o);
        } catch (Exception e) {
            throw new JmxControlException(e);
        }
        return Objects.equals(objectClass.getName(), info.getClassName());
    });

    return suitableNames;
}

From source file:com.haulmont.cuba.web.jmx.JmxConnectionHelper.java

protected static ObjectName getObjectName(final MBeanServerConnection connection, final String remoteContext,
        final Class objectClass) throws IOException {
    Set<ObjectName> names = connection.queryNames(null, null);
    return IterableUtils.find(names, o -> {
        if (!Objects.equals(remoteContext, o.getDomain())) {
            return false;
        }//from  w  ww .  j av a 2s  .c  o m

        MBeanInfo info;
        try {
            info = connection.getMBeanInfo(o);
        } catch (Exception e) {
            throw new JmxControlException(e);
        }
        return Objects.equals(objectClass.getName(), info.getClassName());
    });
}

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());
    }/*www . j  a v a2  s .c  o  m*/
    return allProperties;
}

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

/**
 * Dumps the details of a single MBean.//from   w  ww . ja  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:org.hyperic.hq.plugin.jboss.JBossUtil.java

public static Object invoke(Metric metric, String method, Object[] args, String[] sig)
        throws MetricUnreachableException, MetricNotFoundException, PluginException {
    try {/*from  w w  w.  ja v  a2 s .  c  o m*/
        MBeanServerConnection mServer = getMBeanServerConnection(metric);
        ObjectName obj = new ObjectName(metric.getObjectName());
        MBeanInfo info = mServer.getMBeanInfo(obj);

        if (sig.length == 0) {
            MBeanUtil.OperationParams params = MBeanUtil.getOperationParams(info, method, args);
            if (params.isAttribute) {
                return setAttribute(mServer, obj, method, params.arguments[0]);
            }
            sig = params.signature;
            args = params.arguments;
        }

        return mServer.invoke(obj, method, args, sig);
    } catch (NamingException e) {
        throw unreachable(metric, e);
    } catch (RemoteException e) {
        throw unreachable(metric, e);
    } catch (MalformedObjectNameException e) {
        throw invalid(metric, e);
    } catch (InstanceNotFoundException e) {
        throw notfound(metric, e);
    } catch (ReflectionException e) {
        throw error(metric, e, method);
    } catch (IntrospectionException e) {
        throw error(metric, e);
    } catch (MBeanException e) {
        throw error(metric, e, method);
    } catch (IOException e) {
        throw error(metric, e);
    }
}

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

/**
 * Dumps the details of a single MBean.//ww  w  . j  a v a2 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 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:com.athena.dolly.console.module.jmx.JmxClientManager.java

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

    try {/* ww  w .  j  a  va 2s  .c om*/
        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.hyperic.hq.product.jmx.MxUtil.java

public static Object invoke(Properties config, String objectName, String method, Object[] args, String[] sig)
        throws MetricUnreachableException, MetricNotFoundException, PluginException {

    JMXConnector connector = null;

    try {//from   w w  w  .j a  v a2  s . c  o m
        connector = getMBeanConnector(config);
        MBeanServerConnection mServer = connector.getMBeanServerConnection();
        ObjectName obj = new ObjectName(objectName);
        MBeanInfo info = mServer.getMBeanInfo(obj);

        if (sig.length == 0) {
            MBeanUtil.OperationParams params = MBeanUtil.getOperationParams(info, method, args);
            if (params.isAttribute) {
                if (method.startsWith("set")) {
                    return setAttribute(mServer, obj, method, params.arguments[0]);
                } else {
                    return getAttribute(mServer, obj, method);
                }
            }
            sig = params.signature;
            args = params.arguments;
        }

        return mServer.invoke(obj, method, args, sig);
    } catch (RemoteException e) {
        throw unreachable(config, e);
    } catch (MalformedObjectNameException e) {
        throw invalidObjectName(objectName, e);
    } catch (InstanceNotFoundException e) {
        throw objectNotFound(objectName, e);
    } catch (ReflectionException e) {
        throw error(objectName, e, method);
    } catch (IntrospectionException e) {
        throw error(objectName, e, method);
    } catch (MBeanException e) {
        throw error(objectName, e, method);
    } catch (IOException e) {
        throw error(objectName, e, method);
    } finally {
        close(connector, objectName, method);
    }
}