Java Code Examples for javax.management.MBeanServer#getMBeanInfo()

The following examples show how to use javax.management.MBeanServer#getMBeanInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImmutableNotificationInfoTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 2
Source File: ImmutableNotificationInfoTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 3
Source File: ImmutableNotificationInfoTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 4
Source File: Server.java    From ironjacamar with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Invoke an operation
 * @param name The bean name
 * @param index The method index
 * @param args The arguments
 * @return The result
 * @exception JMException Thrown if an error occurs
 */
public static OpResultInfo invokeOp(String name, int index, String[] args) throws JMException
{
   MBeanServer server = getMBeanServer();
   ObjectName objName = new ObjectName(name);
   MBeanInfo info = server.getMBeanInfo(objName);
   MBeanOperationInfo[] opInfo = info.getOperations();
   MBeanOperationInfo op = opInfo[index];
   MBeanParameterInfo[] paramInfo = op.getSignature();
   String[] argTypes = new String[paramInfo.length];

   for (int p = 0; p < paramInfo.length; p++)
      argTypes[p] = paramInfo[p].getType();
 
   return invokeOpByName(name, op.getName(), argTypes, args);
}
 
Example 5
Source File: ImmutableNotificationInfoTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 6
Source File: ImmutableNotificationInfoTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 7
Source File: MetricsReportService.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
private List<MbeanAttributeValue> getMBeanAttributeValues(String mbeanExpr, String attributeExpr) {
    List<MbeanAttributeValue> values = new ArrayList<>();
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    try {
        Set<ObjectName> mbeanNames = server.queryNames(new ObjectName(mbeanExpr), null);
        for (ObjectName mbeanName : mbeanNames) {
            MBeanInfo mBeanInfo = server.getMBeanInfo(mbeanName);
            MBeanAttributeInfo[] attributeInfos = mBeanInfo.getAttributes();
            for (MBeanAttributeInfo attributeInfo : attributeInfos) {
                if (attributeInfo.getName().equals(attributeExpr) || attributeExpr.length() == 0 || attributeExpr.equals("*")) {
                    double value = (Double) server.getAttribute(mbeanName, attributeInfo.getName());
                    values.add(new MbeanAttributeValue(mbeanName.getCanonicalName(), attributeInfo.getName(), value));
                }
            }
        }
    } catch (Exception e) {
        logger.error("", e);
    }
    return values;
}
 
Example 8
Source File: MXBeanTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void testMXBean(MBeanServer mbs, ObjectName on)
        throws Exception {
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    MBeanAttributeInfo[] attrs = mbi.getAttributes();
    int nattrs = attrs.length;
    if (mbi.getAttributes().length != 1)
        failure("wrong number of attributes: " + attrs);
    else {
        MBeanAttributeInfo mbai = attrs[0];
        if (mbai.getName().equals("Ints")
            && mbai.isReadable() && !mbai.isWritable()
            && mbai.getDescriptor().getFieldValue("openType")
                .equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
            && attrs[0].getType().equals("[I"))
            success("MBeanAttributeInfo");
        else
            failure("MBeanAttributeInfo: " + mbai);
    }

    int[] ints = (int[]) mbs.getAttribute(on, "Ints");
    if (equal(ints, new int[] {1, 2, 3}, null))
        success("getAttribute");
    else
        failure("getAttribute: " + Arrays.toString(ints));

    ExplicitMXBean proxy =
        JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
    int[] pints = proxy.getInts();
    if (equal(pints, new int[] {1, 2, 3}, null))
        success("getAttribute through proxy");
    else
        failure("getAttribute through proxy: " + Arrays.toString(pints));
}
 
Example 9
Source File: JavaMailJMSStatisticsTest.java    From javamail with Apache License 2.0 5 votes vote down vote up
@Test
public void testJmxMBeanInfo() throws Exception {
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    MBeanInfo mBeanInfo = platformMBeanServer.getMBeanInfo(JavaMailJMSStatistics.JMX_OBJECT_NAME);
    // no exceptions = ok
    assertNotNull(mBeanInfo);
}
 
Example 10
Source File: LocalJMXCommand.java    From tomee with Apache License 2.0 5 votes vote down vote up
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;
    }

    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 = propertyEditorRegistry.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());
    }
}
 
Example 11
Source File: MXBeanTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testMXBean(MBeanServer mbs, ObjectName on)
        throws Exception {
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    MBeanAttributeInfo[] attrs = mbi.getAttributes();
    int nattrs = attrs.length;
    if (mbi.getAttributes().length != 1)
        failure("wrong number of attributes: " + attrs);
    else {
        MBeanAttributeInfo mbai = attrs[0];
        if (mbai.getName().equals("Ints")
            && mbai.isReadable() && !mbai.isWritable()
            && mbai.getDescriptor().getFieldValue("openType")
                .equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
            && attrs[0].getType().equals("[I"))
            success("MBeanAttributeInfo");
        else
            failure("MBeanAttributeInfo: " + mbai);
    }

    int[] ints = (int[]) mbs.getAttribute(on, "Ints");
    if (equal(ints, new int[] {1, 2, 3}, null))
        success("getAttribute");
    else
        failure("getAttribute: " + Arrays.toString(ints));

    ExplicitMXBean proxy =
        JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
    int[] pints = proxy.getInts();
    if (equal(pints, new int[] {1, 2, 3}, null))
        success("getAttribute through proxy");
    else
        failure("getAttribute through proxy: " + Arrays.toString(pints));
}
 
Example 12
Source File: PlatformMBeanServerTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void checkMBean(MBeanServer mbs, String mbeanName)
        throws Exception {
    try {
        ObjectName objName = new ObjectName(mbeanName);
        // We could call mbs.isRegistered(objName) here.
        // Calling getMBeanInfo will throw exception if not found.
        mbs.getMBeanInfo(objName);
    } catch (Exception e) {
        throw e;
    }
}
 
Example 13
Source File: TooManyFooTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void test(Object child, String name, boolean mxbean)
    throws Exception {
    final ObjectName childName =
            new ObjectName("test:type=Child,name="+name);
    final MBeanServer server =
            ManagementFactory.getPlatformMBeanServer();
    server.registerMBean(child,childName);
    try {
        final MBeanInfo info = server.getMBeanInfo(childName);
        System.out.println(name+": " + info.getDescriptor());
        final int len = info.getOperations().length;
        if (len == OPCOUNT) {
            System.out.println(name+": OK, only "+OPCOUNT+
                    " operations here...");
        } else {
            final String qual = (len>OPCOUNT)?"many":"few";
            System.err.println(name+": Too "+qual+" foos! Found "+
                    len+", expected "+OPCOUNT);
            for (MBeanOperationInfo op : info.getOperations()) {
                System.err.println("public "+op.getReturnType()+" "+
                        op.getName()+"();");
            }
            throw new RuntimeException("Too " + qual +
                    " foos for "+name);
        }

        final Descriptor d = info.getDescriptor();
        final String mxstr = String.valueOf(d.getFieldValue("mxbean"));
        final boolean mxb =
                (mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();
        System.out.println(name+": mxbean="+mxb);
        if (mxbean && !mxb)
            throw new AssertionError("MXBean is not OpenMBean?");

        for (MBeanOperationInfo mboi : info.getOperations()) {

            // Sanity check
            if (mxbean && !mboi.getName().equals("foo")) {
                // The spec doesn't guarantee that the MBeanOperationInfo
                // of an MXBean will be an OpenMBeanOperationInfo, and in
                // some circumstances in our implementation it will not.
                // However, in thsi tests, for all methods but foo(),
                // it should.
                //
                if (!(mboi instanceof OpenMBeanOperationInfo))
                    throw new AssertionError("Operation "+mboi.getName()+
                            "() is not Open?");
            }

            final String exp = EXPECTED_TYPES.get(mboi.getName());

            // For MXBeans, we need to compare 'exp' with the original
            // type - because mboi.getReturnType() returns the OpenType
            //
            String type = (String)mboi.getDescriptor().
                        getFieldValue("originalType");
            if (type == null) type = mboi.getReturnType();
            if (type.equals(exp)) continue;
            System.err.println("Bad return type for "+
                    mboi.getName()+"! Found "+type+
                    ", expected "+exp);
            throw new RuntimeException("Bad return type for "+
                    mboi.getName());
        }
    } finally {
        server.unregisterMBean(childName);
    }
}
 
Example 14
Source File: MBeanCommand.java    From arthas with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    if (count >= getNumOfExecutions()) {
        // stop the timer
        timer.cancel();
        timer.purge();
        process.write("Process ends after " + getNumOfExecutions() + " time(s).\n");
        process.end();
        return;
    }

    try {
        MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = queryObjectNames();
        TableElement table = createTable();
        for (ObjectName objectName : objectNames) {
            MBeanInfo mBeanInfo = platformMBeanServer.getMBeanInfo(objectName);
            MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
            for (MBeanAttributeInfo attribute : attributes) {
                String attributeName = attribute.getName();
                if (!getAttributeMatcher().matching(attributeName)) {
                    continue;
                }
                String value;
                if (!attribute.isReadable()) {
                    value = RenderUtil.render(new LabelElement("Unavailable").style(Decoration.bold_off.fg(Color.red)));
                } else {
                    Object attributeObj = platformMBeanServer.getAttribute(objectName, attributeName);
                    value = String.valueOf(attributeObj);
                }
                table.row(attributeName, value);
            }
            process.write(RenderUtil.render(table, process.width()));
            process.write("\n");
        }
    } catch (Throwable e) {
        logger.warn("mbean error", e);
    }

    count++;
    process.times().incrementAndGet();

    if (getInterval() <= 0) {
        stop();
        process.end();
    }
}
 
Example 15
Source File: TooManyFooTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void test(Object child, String name, boolean mxbean)
    throws Exception {
    final ObjectName childName =
            new ObjectName("test:type=Child,name="+name);
    final MBeanServer server =
            ManagementFactory.getPlatformMBeanServer();
    server.registerMBean(child,childName);
    try {
        final MBeanInfo info = server.getMBeanInfo(childName);
        System.out.println(name+": " + info.getDescriptor());
        final int len = info.getOperations().length;
        if (len == OPCOUNT) {
            System.out.println(name+": OK, only "+OPCOUNT+
                    " operations here...");
        } else {
            final String qual = (len>OPCOUNT)?"many":"few";
            System.err.println(name+": Too "+qual+" foos! Found "+
                    len+", expected "+OPCOUNT);
            for (MBeanOperationInfo op : info.getOperations()) {
                System.err.println("public "+op.getReturnType()+" "+
                        op.getName()+"();");
            }
            throw new RuntimeException("Too " + qual +
                    " foos for "+name);
        }

        final Descriptor d = info.getDescriptor();
        final String mxstr = String.valueOf(d.getFieldValue("mxbean"));
        final boolean mxb =
                (mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();
        System.out.println(name+": mxbean="+mxb);
        if (mxbean && !mxb)
            throw new AssertionError("MXBean is not OpenMBean?");

        for (MBeanOperationInfo mboi : info.getOperations()) {

            // Sanity check
            if (mxbean && !mboi.getName().equals("foo")) {
                // The spec doesn't guarantee that the MBeanOperationInfo
                // of an MXBean will be an OpenMBeanOperationInfo, and in
                // some circumstances in our implementation it will not.
                // However, in thsi tests, for all methods but foo(),
                // it should.
                //
                if (!(mboi instanceof OpenMBeanOperationInfo))
                    throw new AssertionError("Operation "+mboi.getName()+
                            "() is not Open?");
            }

            final String exp = EXPECTED_TYPES.get(mboi.getName());

            // For MXBeans, we need to compare 'exp' with the original
            // type - because mboi.getReturnType() returns the OpenType
            //
            String type = (String)mboi.getDescriptor().
                        getFieldValue("originalType");
            if (type == null) type = mboi.getReturnType();
            if (type.equals(exp)) continue;
            System.err.println("Bad return type for "+
                    mboi.getName()+"! Found "+type+
                    ", expected "+exp);
            throw new RuntimeException("Bad return type for "+
                    mboi.getName());
        }
    } finally {
        server.unregisterMBean(childName);
    }
}
 
Example 16
Source File: TypeNameTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    TestMXBean testImpl = (TestMXBean) Proxy.newProxyInstance(
            TestMXBean.class.getClassLoader(), new Class<?>[] {TestMXBean.class}, nullIH);
    Object mxbean = new StandardMBean(testImpl, TestMXBean.class, true);
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(mxbean, name);
    MBeanInfo mbi = mbs.getMBeanInfo(name);
    MBeanAttributeInfo[] mbais = mbi.getAttributes();
    boolean sawTabular = false;
    for (MBeanAttributeInfo mbai : mbais) {
        String attrName = mbai.getName();
        String attrTypeName = (String) mbai.getDescriptor().getFieldValue("originalType");
        String fieldName = attrName + "Name";
        Field nameField = TestMXBean.class.getField(fieldName);
        String expectedTypeName = (String) nameField.get(null);

        if (expectedTypeName.equals(attrTypeName)) {
            System.out.println("OK: " + attrName + ": " + attrTypeName);
        } else {
            fail("For attribute " + attrName + " expected type name \"" +
                    expectedTypeName + "\", found type name \"" + attrTypeName +
                    "\"");
        }

        if (mbai.getType().equals(TabularData.class.getName())) {
            sawTabular = true;
            TabularType tt = (TabularType) mbai.getDescriptor().getFieldValue("openType");
            if (tt.getTypeName().equals(attrTypeName)) {
                System.out.println("OK: TabularType name for " + attrName);
            } else {
                fail("For attribute " + attrName + " expected TabularType " +
                        "name \"" + attrTypeName + "\", found \"" +
                        tt.getTypeName());
            }
        }
    }

    if (!sawTabular)
        fail("Test bug: did not test TabularType name");

    if (failure == null)
        System.out.println("TEST PASSED");
    else
        throw new Exception("TEST FAILED: " + failure);
}
 
Example 17
Source File: TestHtmlMBeansReport.java    From javamelody with Apache License 2.0 4 votes vote down vote up
/** Test.
 * @throws IOException e
 * @throws JMException e */
@Test
public void testToHtml() throws IOException, JMException {
	final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
	final List<ObjectName> mBeans = new ArrayList<ObjectName>();
	try {
		final ObjectInstance mBean1 = mBeanServer.registerMBean(new ThreadPool(),
				new ObjectName("Catalina:type=ThreadPool"));
		mBeans.add(mBean1.getObjectName());

		final ObjectInstance mBean2 = mBeanServer.registerMBean(new GlobalRequestProcessor(),
				new ObjectName("Catalina:type=GlobalRequestProcessor,name=http-8080"));
		mBeans.add(mBean2.getObjectName());

		final ObjectInstance mBean3 = mBeanServer.registerMBean(new GlobalRequestProcessor(),
				new ObjectName("Catalina:type=GlobalRequestProcessor,name=http-8090"));
		mBeans.add(mBean3.getObjectName());

		final ObjectInstance mBean4 = mBeanServer.registerMBean(new EmptyAttribute(),
				new ObjectName("java.lang:type=Object"));
		mBeans.add(mBean4.getObjectName());

		// test name avec "|"
		final ObjectInstance mBean5 = mBeanServer.registerMBean(new GlobalRequestProcessor(),
				new ObjectName("testseparator" + MBeans.ATTRIBUTES_SEPARATOR
						+ "testseparator:type=X"));
		mBeans.add(mBean5.getObjectName());

		// on force à null une des descriptions de bean et une des descriptions d'attribut
		final MBeanInfo mbeanInfo = mBeanServer.getMBeanInfo(mBeans.get(0));
		setFieldValue(mbeanInfo, "description", null);
		final MBeanAttributeInfo mbeanAttributeInfo0 = mbeanInfo.getAttributes()[0];
		// même description que le nom de l'attribut
		setFieldValue(mbeanAttributeInfo0, "description", "maxThreads");
		final MBeanAttributeInfo mbeanAttributeInfo1 = mbeanInfo.getAttributes()[1];
		setFieldValue(mbeanAttributeInfo1, "description", null);

		// register another mbeanServer
		MBeanServerFactory.createMBeanServer("jboss");

		final StringWriter writer = new StringWriter();
		final List<MBeanNode> mbeans = MBeans.getAllMBeanNodes();
		final HtmlMBeansReport report = new HtmlMBeansReport(mbeans, writer);
		report.toHtml();
		assertNotEmptyAndClear(writer);
	} catch (final IllegalAccessException e) {
		throw new IllegalStateException(e);
	} finally {
		for (final ObjectName registeredMBean : mBeans) {
			mBeanServer.unregisterMBean(registeredMBean);
		}
	}
}
 
Example 18
Source File: TypeNameTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    TestMXBean testImpl = (TestMXBean) Proxy.newProxyInstance(
            TestMXBean.class.getClassLoader(), new Class<?>[] {TestMXBean.class}, nullIH);
    Object mxbean = new StandardMBean(testImpl, TestMXBean.class, true);
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(mxbean, name);
    MBeanInfo mbi = mbs.getMBeanInfo(name);
    MBeanAttributeInfo[] mbais = mbi.getAttributes();
    boolean sawTabular = false;
    for (MBeanAttributeInfo mbai : mbais) {
        String attrName = mbai.getName();
        String attrTypeName = (String) mbai.getDescriptor().getFieldValue("originalType");
        String fieldName = attrName + "Name";
        Field nameField = TestMXBean.class.getField(fieldName);
        String expectedTypeName = (String) nameField.get(null);

        if (expectedTypeName.equals(attrTypeName)) {
            System.out.println("OK: " + attrName + ": " + attrTypeName);
        } else {
            fail("For attribute " + attrName + " expected type name \"" +
                    expectedTypeName + "\", found type name \"" + attrTypeName +
                    "\"");
        }

        if (mbai.getType().equals(TabularData.class.getName())) {
            sawTabular = true;
            TabularType tt = (TabularType) mbai.getDescriptor().getFieldValue("openType");
            if (tt.getTypeName().equals(attrTypeName)) {
                System.out.println("OK: TabularType name for " + attrName);
            } else {
                fail("For attribute " + attrName + " expected TabularType " +
                        "name \"" + attrTypeName + "\", found \"" +
                        tt.getTypeName());
            }
        }
    }

    if (!sawTabular)
        fail("Test bug: did not test TabularType name");

    if (failure == null)
        System.out.println("TEST PASSED");
    else
        throw new Exception("TEST FAILED: " + failure);
}
 
Example 19
Source File: TooManyFooTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static void test(Object child, String name, boolean mxbean)
    throws Exception {
    final ObjectName childName =
            new ObjectName("test:type=Child,name="+name);
    final MBeanServer server =
            ManagementFactory.getPlatformMBeanServer();
    server.registerMBean(child,childName);
    try {
        final MBeanInfo info = server.getMBeanInfo(childName);
        System.out.println(name+": " + info.getDescriptor());
        final int len = info.getOperations().length;
        if (len == OPCOUNT) {
            System.out.println(name+": OK, only "+OPCOUNT+
                    " operations here...");
        } else {
            final String qual = (len>OPCOUNT)?"many":"few";
            System.err.println(name+": Too "+qual+" foos! Found "+
                    len+", expected "+OPCOUNT);
            for (MBeanOperationInfo op : info.getOperations()) {
                System.err.println("public "+op.getReturnType()+" "+
                        op.getName()+"();");
            }
            throw new RuntimeException("Too " + qual +
                    " foos for "+name);
        }

        final Descriptor d = info.getDescriptor();
        final String mxstr = String.valueOf(d.getFieldValue("mxbean"));
        final boolean mxb =
                (mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();
        System.out.println(name+": mxbean="+mxb);
        if (mxbean && !mxb)
            throw new AssertionError("MXBean is not OpenMBean?");

        for (MBeanOperationInfo mboi : info.getOperations()) {

            // Sanity check
            if (mxbean && !mboi.getName().equals("foo")) {
                // The spec doesn't guarantee that the MBeanOperationInfo
                // of an MXBean will be an OpenMBeanOperationInfo, and in
                // some circumstances in our implementation it will not.
                // However, in thsi tests, for all methods but foo(),
                // it should.
                //
                if (!(mboi instanceof OpenMBeanOperationInfo))
                    throw new AssertionError("Operation "+mboi.getName()+
                            "() is not Open?");
            }

            final String exp = EXPECTED_TYPES.get(mboi.getName());

            // For MXBeans, we need to compare 'exp' with the original
            // type - because mboi.getReturnType() returns the OpenType
            //
            String type = (String)mboi.getDescriptor().
                        getFieldValue("originalType");
            if (type == null) type = mboi.getReturnType();
            if (type.equals(exp)) continue;
            System.err.println("Bad return type for "+
                    mboi.getName()+"! Found "+type+
                    ", expected "+exp);
            throw new RuntimeException("Bad return type for "+
                    mboi.getName());
        }
    } finally {
        server.unregisterMBean(childName);
    }
}
 
Example 20
Source File: TypeNameTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    TestMXBean testImpl = (TestMXBean) Proxy.newProxyInstance(
            TestMXBean.class.getClassLoader(), new Class<?>[] {TestMXBean.class}, nullIH);
    Object mxbean = new StandardMBean(testImpl, TestMXBean.class, true);
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(mxbean, name);
    MBeanInfo mbi = mbs.getMBeanInfo(name);
    MBeanAttributeInfo[] mbais = mbi.getAttributes();
    boolean sawTabular = false;
    for (MBeanAttributeInfo mbai : mbais) {
        String attrName = mbai.getName();
        String attrTypeName = (String) mbai.getDescriptor().getFieldValue("originalType");
        String fieldName = attrName + "Name";
        Field nameField = TestMXBean.class.getField(fieldName);
        String expectedTypeName = (String) nameField.get(null);

        if (expectedTypeName.equals(attrTypeName)) {
            System.out.println("OK: " + attrName + ": " + attrTypeName);
        } else {
            fail("For attribute " + attrName + " expected type name \"" +
                    expectedTypeName + "\", found type name \"" + attrTypeName +
                    "\"");
        }

        if (mbai.getType().equals(TabularData.class.getName())) {
            sawTabular = true;
            TabularType tt = (TabularType) mbai.getDescriptor().getFieldValue("openType");
            if (tt.getTypeName().equals(attrTypeName)) {
                System.out.println("OK: TabularType name for " + attrName);
            } else {
                fail("For attribute " + attrName + " expected TabularType " +
                        "name \"" + attrTypeName + "\", found \"" +
                        tt.getTypeName());
            }
        }
    }

    if (!sawTabular)
        fail("Test bug: did not test TabularType name");

    if (failure == null)
        System.out.println("TEST PASSED");
    else
        throw new Exception("TEST FAILED: " + failure);
}