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

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

    try {//w w  w .j a v a  2  s.  c om
        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.boundary.plugin.sdk.jmx.MBeansTransformer.java

/**
 * Iterates over the attributes of an MBean
 * @param name {@link ObjectName}/*from w  w  w .j  av a 2 s  . c  o  m*/
 */
private void traverseAttributes(ObjectName name) {
    MBeanServerConnection connection = this.client.getMBeanServerConnection();
    MBeanInfo info;
    HashSet<String> checkTypes = new HashSet<String>();
    checkTypes.add("long");
    checkTypes.add("int");
    checkTypes.add("javax.management.openmbean.CompositeData");
    checkTypes.add("[Ljavax.management.openmbean.CompositeData;");
    try {
        info = connection.getMBeanInfo(name);
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attrInfo : attributes) {
            if (checkTypes.contains(attrInfo.getType())) {
                transform.beginAttribute(name, attrInfo);
                transform.endAttribute();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.JmxSpySupportComponent.java

public Map<String, Object> getAttributesAsMap(String mbeanName, MBeanServerConnection mbsc) throws Exception {
    ObjectName mbean = new ObjectName(mbeanName);
    MBeanInfo info = null;//from  w  w  w .j  av a 2s.  c  om
    try {
        info = mbsc.getMBeanInfo(mbean);
    } catch (Exception e) {
        //ignore the exception
    }

    if (info != null) {
        MBeanAttributeInfo[] attributes;
        attributes = info.getAttributes();
        if (attributes == null) {
            return null;
        }

        int size = attributes.length;
        if (size == 0) {
            return null;
        }

        Map<String, Object> beansMap = new HashMap<String, Object>(size);
        for (int i = 0; i < size; i++) {
            try {
                beansMap.put(attributes[i].getName(), mbsc.getAttribute(mbean, attributes[i].getName()));
            } catch (Exception e) {
                //ignore the exception
            }
        }
        return beansMap;
    }
    return null;
}

From source file:com.stumbleupon.hbaseadmin.JMXQuery.java

protected String doSubCommand(MBeanServerConnection mbsc, ObjectInstance instance, String subCommand)
        throws Exception {
    final MBeanAttributeInfo[] attributeInfo = mbsc.getMBeanInfo(instance.getObjectName()).getAttributes();

    final MBeanOperationInfo[] operationInfo = mbsc.getMBeanInfo(instance.getObjectName()).getOperations();

    Object result = null;/* w w  w  .  j  a  va 2 s .com*/

    if (Character.isUpperCase(subCommand.charAt(0))) {
        if ((!(isFeatureInfo(attributeInfo, subCommand))) && (isFeatureInfo(operationInfo, subCommand))) {
            result = doBeanOperation(mbsc, instance, subCommand, operationInfo);
        } else {
            result = doAttributeOperation(mbsc, instance, subCommand, attributeInfo);
        }
    } else if ((!(isFeatureInfo(operationInfo, subCommand))) && (isFeatureInfo(attributeInfo, subCommand))) {
        result = doAttributeOperation(mbsc, instance, subCommand, attributeInfo);
    } else {
        result = doBeanOperation(mbsc, instance, subCommand, operationInfo);
    }

    if (result instanceof CompositeData) {
        result = recurseCompositeData(new StringBuffer("\n"), "", "", (CompositeData) result);
    } else if (result instanceof TabularData) {
        result = recurseTabularData(new StringBuffer("\n"), "", "", (TabularData) result);
    } else if (result instanceof String[]) {
        String[] strs = (String[]) (String[]) result;
        StringBuffer buffer = new StringBuffer("\n");

        for (int i = 0; i < strs.length; ++i) {
            buffer.append(strs[i]);
            buffer.append("\n");
        }

        result = buffer;
    }

    return result.toString();
}

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());
        }/*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.springframework.integration.jmx.DefaultMBeanObjectConverter.java

@Override
public Object convert(MBeanServerConnection connection, ObjectInstance instance) {
    Map<String, Object> attributeMap = new HashMap<String, Object>();

    try {/*from   w  w w  .  j  a  v a 2 s.  co m*/
        ObjectName objName = instance.getObjectName();
        if (!connection.isRegistered(objName)) {
            return attributeMap;
        }

        MBeanInfo info = connection.getMBeanInfo(objName);
        MBeanAttributeInfo[] attributeInfos = info.getAttributes();

        for (MBeanAttributeInfo attrInfo : attributeInfos) {
            // we don't need to repeat name of this as an attribute
            if ("ObjectName".equals(attrInfo.getName()) || !this.filter.accept(objName, attrInfo.getName())) {
                continue;
            }

            Object value;
            try {
                value = connection.getAttribute(objName, attrInfo.getName());
            } catch (RuntimeMBeanException e) {
                // N.B. standard MemoryUsage MBeans will throw an exception when some
                // measurement is unsupported. Logging at trace rather than debug to
                // avoid confusion.
                if (log.isTraceEnabled()) {
                    log.trace("Error getting attribute '" + attrInfo.getName() + "' on '" + objName + "'", e);
                }

                // try to unwrap the exception somewhat; not sure this is ideal
                Throwable t = e;
                while (t.getCause() != null) {
                    t = t.getCause();
                }
                value = String.format("%s[%s]", t.getClass().getName(), t.getMessage());
            }

            attributeMap.put(attrInfo.getName(), checkAndConvert(value));
        }

    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }

    return attributeMap;
}

From source file:com.stumbleupon.hbaseadmin.JMXQuery.java

protected void listOptions(MBeanServerConnection mbsc, ObjectInstance instance)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    final MBeanInfo info = mbsc.getMBeanInfo(instance.getObjectName());
    final MBeanAttributeInfo[] attributes = info.getAttributes();

    if (attributes.length > 0) {
        System.out.println("Attributes:");

        for (int i = 0; i < attributes.length; ++i) {
            System.out.println(' ' + attributes[i].getName() + ": " + attributes[i].getDescription() + " (type="
                    + attributes[i].getType() + ")");
        }// ww  w . j  av  a 2  s  . c  o m
    }

    MBeanOperationInfo[] operations = info.getOperations();

    if (operations.length > 0) {
        System.out.println("Operations:");

        for (int i = 0; i < operations.length; ++i) {
            final MBeanParameterInfo[] params = operations[i].getSignature();
            final StringBuffer paramsStrBuffer = new StringBuffer();

            if (params != null) {
                for (int j = 0; j < params.length; ++j) {
                    paramsStrBuffer.append("\n   name=");
                    paramsStrBuffer.append(params[j].getName());
                    paramsStrBuffer.append(" type=");
                    paramsStrBuffer.append(params[j].getType());
                    paramsStrBuffer.append(" ");
                    paramsStrBuffer.append(params[j].getDescription());
                }
            }
            System.out.println(' ' + operations[i].getName() + ": " + operations[i].getDescription()
                    + "\n  Parameters " + params.length + ", return type=" + operations[i].getReturnType()
                    + paramsStrBuffer.toString());
        }
    }
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.JmxSpySupportComponent.java

public Map<String, Object> getAttributesAsMap(String mbeanName, MBeanServerConnection mbsc, Set<String> keys)
        throws Exception {
    if (CollectionUtils.isBlankCollection(keys)) {
        return null;
    }/*from  w  w w  .  j  av  a2s . com*/

    ObjectName mbean = new ObjectName(mbeanName);
    MBeanInfo info = mbsc.getMBeanInfo(mbean);
    if (info != null) {
        MBeanAttributeInfo[] attributes;
        attributes = info.getAttributes();
        if (attributes == null) {
            return null;
        }

        int size = attributes.length;
        if (size == 0) {
            return null;
        }

        Map<String, Object> beansMap = new HashMap<String, Object>(keys.size());

        String name;
        for (int i = 0; i < size; i++) {
            name = attributes[i].getName();
            if (keys.contains(name)) {
                try {
                    beansMap.put(attributes[i].getName(), mbsc.getAttribute(mbean, attributes[i].getName()));
                } catch (Exception e) {
                    //ignore it
                }
            }
        }
        return beansMap;
    }
    return null;
}

From source file:org.wso2.carbon.analytics.common.jmx.agent.JmxAgentWebInterface.java

/**
 * @param mBean    : The name of the MBean
 * @param url      : The URL for the JMX server
 * @param userName : The User name for the JMX server
 * @param password : The password for the JMX server
 * @return : The set of attributes in a MBean
 * @throws MalformedObjectNameException//  w ww .  j a v  a 2 s . c  o  m
 * @throws IntrospectionException
 * @throws InstanceNotFoundException
 * @throws IOException
 * @throws ReflectionException
 */
public String[][] getMBeanAttributeInfo(String mBean, String url, String userName, String password)
        throws MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, IOException,
        ReflectionException {

    JMXConnector jmxc = getJmxConnector(url, userName, password);

    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
    ObjectName mBeanName = new ObjectName(mBean);

    MBeanAttributeInfo[] attrs = mbsc.getMBeanInfo(mBeanName).getAttributes();

    ArrayList<String[]> strAttrs = new ArrayList<String[]>();

    for (MBeanAttributeInfo info : attrs) {

        //check the suitability of the attribute
        try {
            Object result = mbsc.getAttribute(mBeanName, info.getName());

            //if this is an instance of a primary data type supported by cassandra
            if (result instanceof String || result instanceof Integer || result instanceof Double
                    || result instanceof Long || result instanceof Boolean || result instanceof Float) {
                strAttrs.add(new String[] { info.getName() });
            }

            //if this is a composite data type
            if (result instanceof CompositeData) {
                CompositeData cd = (CompositeData) result;
                ArrayList<String> keys = new ArrayList<String>();
                //add the attribute name
                keys.add(info.getName());
                for (String key : cd.getCompositeType().keySet()) {
                    //check whether the key returns a primary data type
                    Object attrValue = cd.get(key);
                    if (attrValue instanceof String || attrValue instanceof Integer
                            || attrValue instanceof Double || attrValue instanceof Long
                            || attrValue instanceof Boolean || attrValue instanceof Float) {
                        keys.add(key);
                    }
                }
                //if this composite data object has keys which returns attributes with
                // primary data types
                if (keys.size() > 1) {
                    strAttrs.add(keys.toArray(new String[keys.size()]));
                }
            }
        } catch (MBeanException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (AttributeNotFoundException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (UnmarshalException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (RuntimeOperationsException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());

        } catch (RuntimeMBeanException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (ReflectionException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (Exception e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        }
    }

    //close the connection
    jmxc.close();

    return strAttrs.toArray(new String[strAttrs.size()][]);
}

From source file:org.hyperic.hq.plugin.weblogic.WeblogicServiceControlPlugin.java

protected Object invoke(MBeanServerConnection mServer, String objectName, String method, Object[] args,
        String[] sig) throws MetricUnreachableException, MetricNotFoundException, PluginException {

    ObjectName obj;//  w w  w . ja v  a 2 s  . c om
    try {
        obj = new ObjectName(objectName);
    } catch (MalformedObjectNameException e1) {
        throw new PluginException("Unable to create an ObjectName from " + objectName);
    }

    MBeanInfo info;
    try {
        info = mServer.getMBeanInfo(obj);
    } catch (Exception e1) {
        throw new PluginException("Unable to obtain MBeanInfo from " + objectName);
    }

    if (sig.length == 0) {
        MBeanUtil.OperationParams params = MBeanUtil.getOperationParams(info, method, args);
        if (params.isAttribute) {
            if (method.startsWith("set")) {
                try {
                    mServer.setAttribute(obj, new Attribute(method.substring(3), params.arguments[0]));
                } catch (AttributeNotFoundException e) {
                    throw new MetricNotFoundException(e.getMessage(), e);
                } catch (Exception e) {
                    throw new PluginException(e);
                }
                return null;
            } else {
                try {
                    return mServer.getAttribute(obj, method.substring(3));
                } catch (AttributeNotFoundException e) {
                    throw new MetricNotFoundException(e.getMessage(), e);
                } catch (Exception e) {
                    throw new PluginException(e);
                }
            }
        }
        sig = params.signature;
        args = params.arguments;
    }

    try {
        return mServer.invoke(obj, method, args, sig);
    } catch (Exception e) {
        throw new PluginException(e);
    }
}