Search in sources :

Example 21 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jvm-tools by aragozin.

the class MBeanHelper method describe.

public String describe(ObjectName bean) throws Exception {
    MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
    StringBuilder sb = new StringBuilder();
    sb.append(bean);
    sb.append('\n');
    sb.append(mbinfo.getClassName());
    sb.append('\n');
    sb.append(" - " + mbinfo.getDescription());
    sb.append('\n');
    for (MBeanAttributeInfo ai : mbinfo.getAttributes()) {
        sb.append(" (A) ");
        sb.append(ai.getName()).append(" : ").append(toPrintableType(ai.getType())).append("");
        if (!ai.isReadable()) {
            sb.append(" - WRITEONLY");
        } else if (ai.isWritable()) {
            sb.append(" - WRITEABLE");
        }
        sb.append('\n');
        if (!ai.getName().equals(ai.getDescription())) {
            sb.append("  - " + ai.getDescription());
            sb.append('\n');
        }
    }
    for (MBeanOperationInfo oi : mbinfo.getOperations()) {
        sb.append(" (O) ");
        sb.append(oi.getName()).append("(");
        for (MBeanParameterInfo pi : oi.getSignature()) {
            String name = pi.getName();
            String type = toPrintableType(pi.getType());
            sb.append(type).append(' ').append(name).append(", ");
        }
        if (oi.getSignature().length > 0) {
            sb.setLength(sb.length() - 2);
        }
        sb.append(") : ").append(toPrintableType(oi.getReturnType()));
        sb.append('\n');
        if (!oi.getName().equals(oi.getDescription())) {
            sb.append("  - " + oi.getDescription());
            sb.append('\n');
        }
    }
    return sb.toString();
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 22 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project twitter4j by yusuke.

the class MBeansTest method testAPIStatisticsOpenMBean.

/**
     * Tests exposure of API statistics via a dynamic MBean
     */
public void testAPIStatisticsOpenMBean() throws Exception {
    APIStatistics stats = new APIStatistics(5);
    APIStatisticsOpenMBean openMBean = new APIStatisticsOpenMBean(stats);
    // sanity check to ensure metadata accurately describes dynamic attributes
    MBeanInfo info = openMBean.getMBeanInfo();
    assertEquals(5, info.getAttributes().length);
    assertEquals(1, info.getOperations().length);
    List<String> attrNames = new ArrayList<String>();
    for (MBeanAttributeInfo attr : info.getAttributes()) {
        assertNotNull(openMBean.getAttribute(attr.getName()));
        attrNames.add(attr.getName());
    }
    AttributeList attrList = openMBean.getAttributes(attrNames.toArray(new String[attrNames.size()]));
    assertNotNull(attrList);
    assertEquals(5, attrList.size());
    // check stats (empty case)
    Long callCount = (Long) openMBean.getAttribute("callCount");
    assertEquals(0, callCount.longValue());
    Long errorCount = (Long) openMBean.getAttribute("errorCount");
    assertEquals(0, callCount.longValue());
    Long totalTime = (Long) openMBean.getAttribute("totalTime");
    assertEquals(0, totalTime.longValue());
    Long averageTime = (Long) openMBean.getAttribute("averageTime");
    assertEquals(0, averageTime.longValue());
    // check table (empty case)
    TabularData table = (TabularData) openMBean.getAttribute("statisticsTable");
    assertTrue(table.isEmpty());
    stats.methodCalled("foo", 100, true);
    // check stats (populated case)
    callCount = (Long) openMBean.getAttribute("callCount");
    assertEquals(1, callCount.longValue());
    errorCount = (Long) openMBean.getAttribute("errorCount");
    assertEquals(0, errorCount.longValue());
    totalTime = (Long) openMBean.getAttribute("totalTime");
    assertEquals(100, totalTime.longValue());
    averageTime = (Long) openMBean.getAttribute("averageTime");
    assertEquals(100, averageTime.longValue());
    // check table (populated  case)
    table = (TabularData) openMBean.getAttribute("statisticsTable");
    assertFalse(table.isEmpty());
    assertEquals(1, table.keySet().size());
    CompositeData data = table.get(new Object[] { "foo" });
    assertNotNull(data);
    String[] columnNames = new String[] { "methodName", "callCount", "totalTime", "avgTime" };
    Object[] columnValues = data.getAll(columnNames);
    assertEquals(columnNames.length, columnValues.length);
    assertEquals("foo", columnValues[0]);
    assertEquals(1, ((Long) columnValues[1]).longValue());
    assertEquals(100, ((Long) columnValues[2]).longValue());
    assertEquals(100, ((Long) columnValues[3]).longValue());
    // check reset
    openMBean.invoke("reset", new Object[0], new String[0]);
    checkCalculator(stats, 0, 0, 0, 0);
    assertFalse(stats.getInvocationStatistics().iterator().hasNext());
}
Also used : MBeanInfo(javax.management.MBeanInfo) AttributeList(javax.management.AttributeList) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) TabularData(javax.management.openmbean.TabularData)

Example 23 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project pulsar by yahoo.

the class MBeanStatsGenerator method convert.

private Metrics convert(ObjectInstance instance) {
    ObjectName objName = instance.getObjectName();
    MBeanInfo info = null;
    try {
        info = mbs.getMBeanInfo(objName);
    } catch (Exception e) {
        // [Bug 6158364] skipping MBean if access failed
        return null;
    }
    Metrics metrics = null;
    // create a metrics instance by MBean dimension
    metrics = createMetricsByDimension(objName);
    // get each of attribute,value from the MBean
    for (MBeanAttributeInfo attr : info.getAttributes()) {
        Object value;
        try {
            value = mbs.getAttribute(instance.getObjectName(), attr.getName());
            metrics.put(attr.getName(), value);
        } catch (Exception e) {
        // skip
        }
    }
    return metrics;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName)

Example 24 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jdk8u_jdk by JetBrains.

the class FeatureOrderTest method main.

public static void main(String[] args) throws Exception {
    // Build the lists of attributes and operations that we would expect
    // if they are derived by reflection and preserve the reflection order
    List<String> expectedAttributeNames = new ArrayList<String>();
    List<String> expectedOperationNames = new ArrayList<String>();
    for (Method m : OrderMXBean.class.getMethods()) {
        String name = m.getName();
        String attrName = null;
        if (name.startsWith("get") && !name.equals("get") && m.getParameterTypes().length == 0 && m.getReturnType() != void.class)
            attrName = name.substring(3);
        else if (name.startsWith("is") && !name.equals("is") && m.getParameterTypes().length == 0 && m.getReturnType() == boolean.class)
            attrName = name.substring(2);
        else if (name.startsWith("set") && !name.equals("set") && m.getReturnType() == void.class && m.getParameterTypes().length == 1)
            attrName = name.substring(3);
        if (attrName != null) {
            if (!expectedAttributeNames.contains(attrName))
                expectedAttributeNames.add(attrName);
        } else
            expectedOperationNames.add(name);
    }
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    for (boolean mxbean : booleans) {
        for (boolean withStandardMBean : booleans) {
            String testName = "MXBean: " + mxbean + "; " + "using javax.management.StandardMBean: " + withStandardMBean;
            System.out.println("Test case: " + testName);
            Object mbean;
            if (mxbean) {
                if (withStandardMBean) {
                    mbean = new StandardMBean(new OrderImpl(), OrderMXBean.class, true);
                } else
                    mbean = new OrderImpl();
            } else {
                if (withStandardMBean)
                    mbean = new StandardMBean(new Order(), OrderMBean.class);
                else
                    mbean = new Order();
            }
            ObjectName name = new ObjectName(":mxbean=" + mxbean + "," + "withStandardMBean=" + withStandardMBean);
            mbs.registerMBean(mbean, name);
            /* Make sure we are testing what we think. */
            MBeanInfo mbi = mbs.getMBeanInfo(name);
            boolean isWithStandardMBean = mbs.isInstanceOf(name, StandardMBean.class.getName());
            System.out.println("classname " + mbi.getClassName());
            String mxbeanField = (String) mbi.getDescriptor().getFieldValue("mxbean");
            boolean isMXBean = "true".equalsIgnoreCase(mxbeanField);
            if (mxbean != isMXBean)
                throw new Exception("Test error: MXBean mismatch");
            if (withStandardMBean != isWithStandardMBean)
                throw new Exception("Test error: StandardMBean mismatch");
            // Check that order of attributes and operations matches
            MBeanAttributeInfo[] mbais = mbi.getAttributes();
            checkEqual(expectedAttributeNames.size(), mbais.length, "number of attributes");
            List<String> attributeNames = new ArrayList<String>();
            for (MBeanAttributeInfo mbai : mbais) attributeNames.add(mbai.getName());
            checkEqual(expectedAttributeNames, attributeNames, "order of attributes");
            MBeanOperationInfo[] mbois = mbi.getOperations();
            checkEqual(expectedOperationNames.size(), mbois.length, "number of operations");
            List<String> operationNames = new ArrayList<String>();
            for (MBeanOperationInfo mboi : mbois) operationNames.add(mboi.getName());
            checkEqual(expectedOperationNames, operationNames, "order of operations");
            System.out.println();
        }
    }
    if (failed)
        throw new Exception("TEST FAILED");
    System.out.println("TEST PASSED");
}
Also used : MBeanInfo(javax.management.MBeanInfo) StandardMBean(javax.management.StandardMBean) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 25 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jdk8u_jdk by JetBrains.

the class AnnotationTest method check.

private static void check(MBeanServer mbs, ObjectName on) throws Exception {
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    // check the MBean itself
    check(mbi);
    // check attributes
    MBeanAttributeInfo[] attrs = mbi.getAttributes();
    for (MBeanAttributeInfo attr : attrs) {
        check(attr);
        if (attr.getName().equals("ReadOnly"))
            check("@Full", attr.getDescriptor(), expectedFullDescriptor);
    }
    // check operations
    MBeanOperationInfo[] ops = mbi.getOperations();
    for (MBeanOperationInfo op : ops) {
        check(op);
        check(op.getSignature());
    }
    MBeanConstructorInfo[] constrs = mbi.getConstructors();
    for (MBeanConstructorInfo constr : constrs) {
        check(constr);
        check(constr.getSignature());
    }
}
Also used : MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Aggregations

MBeanAttributeInfo (javax.management.MBeanAttributeInfo)106 MBeanInfo (javax.management.MBeanInfo)75 ObjectName (javax.management.ObjectName)45 MBeanOperationInfo (javax.management.MBeanOperationInfo)24 Test (org.junit.Test)21 MBeanServer (javax.management.MBeanServer)15 ArrayList (java.util.ArrayList)13 AttributeNotFoundException (javax.management.AttributeNotFoundException)12 ReflectionException (javax.management.ReflectionException)12 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)11 IOException (java.io.IOException)10 AttributeList (javax.management.AttributeList)10 Attribute (javax.management.Attribute)9 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 IntrospectionException (javax.management.IntrospectionException)9 MBeanParameterInfo (javax.management.MBeanParameterInfo)9 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)9 HashMap (java.util.HashMap)8 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7 MBeanException (javax.management.MBeanException)7