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:org.springframework.integration.jmx.DefaultMBeanObjectConverter.java

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

    try {/*www . j av  a  2s. 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:org.midonet.midolman.tools.MmStat.java

public void dumpMBeans(String jmxDomain, String filter, int count, int interval) {

    ObjectName on = null;//from ww  w  .j  av  a2 s. c  o  m
    try {
        on = new ObjectName(jmxDomain + ":*");
    } catch (MalformedObjectNameException e) {
        System.err.println("Aborted: malformed domain " + jmxDomain);
        e.printStackTrace();
        System.exit(1);
    }
    java.util.Set<ObjectInstance> beans = null;
    try {
        beans = mbsc.queryMBeans(on, null);
    } catch (IOException e) {
        System.err.println("Aborted: querying Mbeans failed " + e);
        e.printStackTrace();
    }

    // Remember MBean instances to dump
    List<ObjectInstance> objectInstances = new ArrayList<>();
    for (ObjectInstance oi : beans) {
        if (filter != null) {
            String objectName = oi.getObjectName().toString();
            if (!objectName.contains(filter)) {
                continue;
            }
        }
        objectInstances.add(oi);
    }

    int attempt = 1;
    while (true) {
        // Header
        System.out.println("============================");
        System.out.println(String.format("Reading at %s (%s/%s)", new Date().toString(), attempt, count));
        System.out.println("============================");

        try {
            for (ObjectInstance oi : objectInstances) {
                on = oi.getObjectName();
                System.out.println("MBean: " + on.toString());
                MBeanInfo info = mbsc.getMBeanInfo(on);

                for (MBeanAttributeInfo attr : info.getAttributes()) {
                    System.out.print("\t" + attr.getName() + ": ");
                    System.out.println(mbsc.getAttribute(on, attr.getName()));
                }
            }
            if (attempt == count)
                System.exit(0);
            attempt++;
            //* Sleep "interval" seconds" */
            Thread.sleep(interval * 1000);
        } catch (IOException e) {
            System.err.println("Aborted: got IOException: " + e);
            e.printStackTrace();
            System.exit(1);
        } catch (JMException e) {
            System.err.println("Aborted: got JMException: " + e);
            e.printStackTrace();
            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

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

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

    MBeanServerConnection mServer;
    try {/*from   ww w  . j  a v a 2s.  c om*/
        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.apache.openejb.server.cli.command.LocalJMXCommand.java

private void set(final String cmd) {
    final String[] split = cmd.split(" ");
    if (split.length < 2) {
        streamManager.writeErr("you need to specify an attribute, an objectname and a value");
        return;/*from   w w w  .j a  v  a2s  . co m*/
    }

    final MBeanServer mBeanServer = LocalMBeanServer.get();
    final String newValue = cmd.substring(split[0].length() + split[1].length() + 1).trim();
    try {
        final ObjectName oname = new ObjectName(split[1]);
        final MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
        final MBeanAttributeInfo attrs[] = minfo.getAttributes();

        String type = String.class.getName();
        for (int i = 0; i < attrs.length; i++) {
            if (attrs[i].getName().equals(split[0])) {
                type = attrs[i].getType();
                break;
            }
        }

        final Object valueObj = PropertyEditors.getValue(type, newValue,
                Thread.currentThread().getContextClassLoader());
        mBeanServer.setAttribute(oname, new Attribute(split[0], valueObj));
        streamManager.writeOut("done");
    } catch (Exception ex) {
        streamManager.writeOut("Error - " + ex.toString());
    }
}

From source file:org.apache.hadoop.hdfs.tools.JMXGet.java

/**
 * print all attributes' values//from   www.j a  va 2s  .com
 */
public void printAllValues() throws Exception {
    err("List of all the available keys:");

    Object val = null;

    for (ObjectName oname : hadoopObjectNames) {
        err(">>>>>>>>jmx name: " + oname.getCanonicalKeyPropertyListString());
        MBeanInfo mbinfo = mbsc.getMBeanInfo(oname);
        MBeanAttributeInfo[] mbinfos = mbinfo.getAttributes();

        for (MBeanAttributeInfo mb : mbinfos) {
            val = mbsc.getAttribute(oname, mb.getName());
            System.out.format(format, mb.getName(), (val == null) ? "" : val.toString());
        }
    }
}

From source file:org.apache.synapse.commons.snmp.SNMPAgent.java

@Override
protected void registerManagedObjects() {
    log.info("Initializing Synapse SNMP MIB");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectInstance> instances = mbs.queryMBeans(null, null);

    try {//from www  .j av  a  2  s . co m
        for (ObjectInstance instance : instances) {
            ObjectName objectName = instance.getObjectName();
            if (objectName.getDomain().equals("org.apache.synapse")) {
                String oidString = SynapseMIBUtils.getOID(objectName);
                if (oidString == null) {
                    continue;
                }

                MBeanInfo info = mbs.getMBeanInfo(objectName);
                MBeanAttributeInfo[] attributes = info.getAttributes();
                List<String> attributeNames = new ArrayList<String>();
                List<String> mapAttributes = new ArrayList<String>();
                for (MBeanAttributeInfo attributeInfo : attributes) {
                    attributeNames.add(attributeInfo.getName());
                    if (Map.class.getName().equals(attributeInfo.getType())) {
                        mapAttributes.add(attributeInfo.getName());
                    }
                }
                Collections.sort(attributeNames);

                doRegister(attributeNames, mapAttributes, oidString, objectName);
            }
        }
    } catch (Exception e) {
        log.error("Error while initializing the SNMP MIB", e);
    }
}

From source file:flens.query.JMXQuery.java

private Map<String, Object> getAttributes(MBeanInfo info) {
    Map<String, Object> outc = new HashMap<>();
    MBeanAttributeInfo[] atts = info.getAttributes();
    for (MBeanAttributeInfo att : atts) {
        Map<String, Object> out = new HashMap<>();
        outc.put(att.getName(), out);/* w ww  . j  ava 2 s .c  om*/
        out.put("description", att.getDescription());
        out.put("type", att.getType());
    }
    return outc;
}

From source file:com.betfair.cougar.core.impl.jmx.HtmlAdaptorParser.java

private void appendMBean(MBeanServer server, ObjectName on, String attrName, String separator,
        StringBuilder buf) {/*from w ww.j a va  2  s .  c o  m*/
    StringBuilder local = new StringBuilder();
    try {

        MBeanInfo info = server.getMBeanInfo(on);
        local.append(on.toString());
        MBeanAttributeInfo[] attr = info.getAttributes();
        for (int i = 0; i < attr.length; i++) {
            if ((attrName == null || attrName.equals(attr[i].getName())) && attr[i].isReadable()) {
                local.append(separator);
                local.append(attr[i].getName());
                local.append(separator);
                local.append(server.getAttribute(on, attr[i].getName()));
            }
        }
    } catch (Exception e) {
        LOGGER.debug("Unable to retrieve Bean information for bean " + on, e);
        return;
    }
    buf.append(local);
}

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

public void dump(Set beans) {
    Iterator iter = beans.iterator();

    while (iter.hasNext()) {
        ObjectName obj = (ObjectName) iter.next();
        try {//w  w w. jav  a2 s . com
            MBeanInfo info = getMBeanInfo(obj);

            System.out.println("MBean: " + info.getClassName());
            System.out.println("Name:  " + obj);

            MBeanAttributeInfo[] attrs = info.getAttributes();
            for (int k = 0; k < attrs.length; k++) {
                String name = attrs[k].getName();
                String value = "null";

                try {
                    Object o = getAttribute(obj, name);
                    if (o != null) {
                        if (o.getClass().isArray()) {
                            value = Arrays.asList((Object[]) o).toString();
                        } else {
                            value = o.toString();
                        }
                    }
                } catch (Exception e) {
                    value = "ERROR";
                    if (log.isDebugEnabled()) {
                        e.printStackTrace();
                    }
                }
                String perms = "";
                if (attrs[k].isReadable()) {
                    perms += "r";
                }
                if (attrs[k].isWritable()) {
                    perms += "w";
                }
                System.out.println("\t" + k + ". Attribute: " + name + " = " + value + " (" + perms + ")");
            }

            MBeanOperationInfo[] ops = info.getOperations();

            for (int i = 0; i < ops.length; i++) {
                ArrayList sig = new ArrayList();
                MBeanParameterInfo[] params = ops[i].getSignature();
                for (int j = 0; j < params.length; j++) {
                    sig.add(params[j].getType());
                }
                System.out.println(
                        "\t Operation: " + ops[i].getReturnType() + " " + ops[i].getName() + " " + sig);
            }
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                e.printStackTrace();
            }
        }

        System.out.println("");
    }
}

From source file:org.jolokia.jmx.JsonDymamicMBeanImplTest.java

@Test
public void checkMBeanInfo() throws Exception {
    MBeanInfo info = platformServer.getMBeanInfo(testName);
    MBeanAttributeInfo aInfo[] = info.getAttributes();
    Map<String, MBeanAttributeInfo> attributes = new HashMap<String, MBeanAttributeInfo>();
    for (MBeanAttributeInfo a : aInfo) {
        attributes.put(a.getName(), a);/* ww w.  j  a  v  a 2  s.co  m*/
    }

    assertEquals(attributes.get("Chili").getType(), String.class.getName());
    assertEquals(attributes.get("Numbers").getType(), String.class.getName());
    assertEquals(attributes.get("User").getType(), String.class.getName());

    MBeanOperationInfo oInfo[] = info.getOperations();
    Map<String, MBeanOperationInfo> ops = new HashMap<String, MBeanOperationInfo>();
    for (MBeanOperationInfo o : oInfo) {
        ops.put(o.getName(), o);
    }

    assertEquals(ops.get("lookup").getReturnType(), String.class.getName());
    assertEquals(ops.get("epoch").getReturnType(), "long");
    assertEquals(ops.get("charTest").getReturnType(), "char");

    MBeanParameterInfo p[] = ops.get("lookup").getSignature();
    assertEquals(p[0].getType(), String.class.getName());
    assertEquals(p[1].getType(), String.class.getName());

    p = ops.get("epoch").getSignature();
    assertEquals(p[0].getType(), String.class.getName());

    p = ops.get("charTest").getSignature();
    assertEquals(p[0].getType(), Character.class.getName());

}