SimpleModelMBeanCommand.java revision 2362
1612N/A/*
1879N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
1612N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1612N/A *
1612N/A * This code is free software; you can redistribute it and/or modify it
1612N/A * under the terms of the GNU General Public License version 2 only, as
1612N/A * published by the Free Software Foundation.
1612N/A *
1612N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1612N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1612N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1612N/A * version 2 for more details (a copy is included in the LICENSE file that
1612N/A * accompanied this code).
1612N/A *
1612N/A * You should have received a copy of the GNU General Public License version
1612N/A * 2 along with this work; if not, write to the Free Software Foundation,
1612N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1612N/A *
1612N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1612N/A * or visit www.oracle.com if you need additional information or have any
1612N/A * questions.
1612N/A */
1612N/A
1612N/A/*
1612N/A * @test
1879N/A * @bug 4874819
1879N/A * @summary Test that MBeanInfo classes no longer throw an
1879N/A * IllegalArgumentException when attribute names, operation names, and
1879N/A * Java type names do not strictly follow the expected Java syntax.
1879N/A * @author Eamonn McManus, Daniel Fuchs
1879N/A * @run clean SimpleModelMBeanCommand
1612N/A * @run build SimpleModelMBeanCommand
1612N/A * @run main/othervm/policy=policy SimpleModelMBeanCommand
1612N/A */
1612N/A
1612N/Aimport java.lang.reflect.*;
1612N/Aimport java.util.*;
1612N/Aimport javax.management.*;
1612N/Aimport javax.management.modelmbean.*;
1612N/A
1612N/Apublic class SimpleModelMBeanCommand {
1612N/A
1612N/A public static class Resource {
1612N/A public int getNumber() {
1612N/A return number;
1612N/A }
1612N/A
1612N/A public void setNumber(int n) {
1612N/A number = n;
1612N/A }
1612N/A
1612N/A public int addOne(int x) {
1612N/A return x + 1;
1612N/A }
1612N/A
1612N/A public Object[] getArray() {
1612N/A return (Object[]) array.clone();
1612N/A }
1612N/A
1612N/A // doesn't look like an attribute so not seen by caching logic
1612N/A public void tweakArray(Object[] array) {
1612N/A this.array = (Object[]) array.clone();
1612N/A }
1612N/A
1612N/A private int number = 1234;
1612N/A private Object[] array = {"hello", "world"};
1612N/A }
1612N/A
1612N/A public static void main(String[] args) {
1612N/A int errorCount = 0;
1612N/A for (int i = 0; i < NTESTS; i++) {
1612N/A try {
1612N/A System.out.println("Test " + i + ":");
1612N/A test(i);
1612N/A } catch (Throwable e) {
1612N/A errorCount++;
1612N/A boolean first = true;
1612N/A do {
1612N/A System.err.println(first ? "Exception:" : "Caused by:");
1612N/A first = false;
1612N/A e.printStackTrace();
1612N/A Throwable nexte;
1612N/A nexte = e.getCause();
1612N/A if (nexte == null) { // old JMX
1612N/A if (e instanceof MBeanException)
1612N/A nexte = ((MBeanException) e).getTargetException();
1612N/A }
1612N/A e = nexte;
1612N/A } while (e != null);
1612N/A }
1612N/A }
1612N/A if (errorCount == 0) {
1612N/A System.out.println("All ModelMBean tests successfuly passed");
1612N/A System.out.println("Bye! Bye!");
1612N/A // JTReg doesn't like System.exit(0);
1612N/A return;
1612N/A } else {
1612N/A System.err.println("ERROR: " + errorCount + " tests failed");
1612N/A System.exit(errorCount);
1612N/A }
1612N/A
1612N/A }
1612N/A
1612N/A private static void test(int testno) throws Exception {
1612N/A // com.sun.jmx.trace.TraceImplementation.init(2);
1612N/A Resource resource = new Resource();
1612N/A Class resourceClass = Resource.class;
1612N/A Class rmmbClass = RequiredModelMBean.class;
1612N/A Method setManagedResource =
1612N/A rmmbClass.getMethod("setManagedResource",
1612N/A new Class[] {Object.class,
1612N/A String.class});
1612N/A Method sendNotification =
1612N/A rmmbClass.getMethod("sendNotification",
1612N/A new Class[] {Notification.class});
1612N/A Method addAttributeChangeNL =
1612N/A rmmbClass.getMethod("addAttributeChangeNotificationListener",
1612N/A new Class[] {NotificationListener.class,
1612N/A String.class,
1612N/A Object.class});
1612N/A Method getArray = resourceClass.getMethod("getArray", new Class[0]);
1612N/A Method getNumber = resourceClass.getMethod("getNumber", new Class[0]);
1612N/A Method setNumber =
1612N/A resourceClass.getMethod("setNumber", new Class[] {Integer.TYPE});
1612N/A Method tweakArray =
1612N/A resourceClass.getMethod("tweakArray",
1612N/A new Class[] {Object[].class});
1612N/A Method addOne =
1612N/A resourceClass.getMethod("addOne", new Class[] {Integer.TYPE});
1612N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
1612N/A ObjectName on = new ObjectName("a:b=c");
1612N/A Descriptor attrDescr = new DescriptorSupport();
1612N/A attrDescr.setField("name", "Array");
1612N/A attrDescr.setField("descriptorType", "attribute");
1612N/A attrDescr.setField("getMethod", "getArray");
1612N/A ModelMBeanAttributeInfo attrInfo =
1612N/A new ModelMBeanAttributeInfo("Array", "array attr", getArray,
1612N/A null, attrDescr);
1612N/A Descriptor attrDescr2 = new DescriptorSupport();
1612N/A attrDescr2.setField("name", "Number");
1612N/A attrDescr2.setField("descriptorType", "attribute");
1612N/A attrDescr2.setField("getMethod", "getNumber");
1612N/A attrDescr2.setField("setMethod", "setNumber");
1612N/A ModelMBeanAttributeInfo attrInfo2 =
1612N/A new ModelMBeanAttributeInfo("Number", "number attr", getNumber,
1612N/A setNumber, attrDescr2);
1612N/A Descriptor attrDescr3 = new DescriptorSupport();
1612N/A attrDescr3.setField("name", "Local");
1612N/A attrDescr3.setField("descriptorType", "attribute");
1612N/A attrDescr3.setField("currencyTimeLimit", "" + Integer.MAX_VALUE);
1612N/A ModelMBeanAttributeInfo attrInfo3 =
1612N/A new ModelMBeanAttributeInfo("Local", "java.lang.String",
1612N/A "local attr", true, true, false,
1612N/A attrDescr3);
1612N/A Descriptor attrDescr4 = new DescriptorSupport();
1612N/A attrDescr4.setField("name", "Local2");
1612N/A attrDescr4.setField("descriptorType", "attribute");
1612N/A ModelMBeanAttributeInfo attrInfo4 =
1612N/A new ModelMBeanAttributeInfo("Local2", "java.lang.String",
1612N/A "local attr 2", true, true, false,
1612N/A attrDescr4);
1612N/A ModelMBeanAttributeInfo[] attrs =
1612N/A new ModelMBeanAttributeInfo[] {attrInfo, attrInfo2, attrInfo3,
1612N/A attrInfo4};
1612N/A ModelMBeanOperationInfo operInfo =
1612N/A new ModelMBeanOperationInfo("getArray descr", getArray);
1612N/A ModelMBeanOperationInfo operInfo2 =
1612N/A new ModelMBeanOperationInfo("getNumber descr", getNumber);
1612N/A ModelMBeanOperationInfo operInfo3 =
1612N/A new ModelMBeanOperationInfo("addOne descr", addOne);
1612N/A ModelMBeanOperationInfo operInfo4 =
1612N/A new ModelMBeanOperationInfo("setNumber descr", setNumber);
1612N/A ModelMBeanOperationInfo operInfo5 =
1612N/A new ModelMBeanOperationInfo("tweakArray descr", tweakArray);
1612N/A ModelMBeanOperationInfo operInfoSetManagedResource =
1612N/A new ModelMBeanOperationInfo("setManagedResource descr",
1612N/A setManagedResource);
1612N/A ModelMBeanOperationInfo operInfoSendNotification =
1612N/A new ModelMBeanOperationInfo("sendNotification descr",
1612N/A sendNotification);
1612N/A ModelMBeanOperationInfo operInfoAddAttributeChangeNL =
1612N/A new ModelMBeanOperationInfo("AddAttributeChangeNL descr",
1612N/A addAttributeChangeNL);
1612N/A ModelMBeanOperationInfo[] opers =
new ModelMBeanOperationInfo[] {operInfo, operInfo2, operInfo3,
operInfo4, operInfo5,
operInfoSetManagedResource,
operInfoSendNotification,
operInfoAddAttributeChangeNL};
ModelMBeanInfo info =
new ModelMBeanInfoSupport(Resource.class.getName(),
"Resourcish resource",
attrs, null, opers, null,
null);
mbs.createMBean(RequiredModelMBean.class.getName(),
on,
new Object[] {info},
new String[] {ModelMBeanInfo.class.getName()});
mbs.invoke(on, "setManagedResource",
new Object[] {resource, "objectReference"},
new String[] {"java.lang.Object", "java.lang.String"});
switch (testno) {
case 0:
/* Check that we can get an attribute of type Object[] */
Object[] objs = (Object[]) mbs.getAttribute(on, "Array");
for (int i = 0; i < objs.length; i++)
System.out.println(objs[i]);
break;
case 1:
/* Check that we can get an attribute of type int */
Integer n = (Integer) mbs.getAttribute(on, "Number");
System.out.println(n);
break;
case 2:
/* Check that we can call an operation that returns int */
Integer n1 =
(Integer) mbs.invoke(on, "addOne",
new Integer[] {new Integer(1233)},
new String[] {"int"});
System.out.println(n1);
break;
case 3:
/* Check that we don't get an exception if you sendNotification
without any listeners. */
Notification notif = new Notification("type", "source", 123L);
mbs.invoke(on, "sendNotification", new Object[] {notif},
new String[] {"javax.management.Notification"});
System.out.println("Successfully sent notification");
break;
case 4:
/* Check that we can call addAttributeChangeNotificationListener
with null attribute. */
NotificationListener listener = new NotificationListener() {
public void handleNotification(Notification notif,
Object handback) {
System.out.println("Got notif: " + notif +
" with handback: " + handback);
}
};
mbs.invoke(on, "addAttributeChangeNotificationListener",
new Object[] {listener, null, "the-handback"},
new String[] {
"javax.management.NotificationListener",
"java.lang.String",
"java.lang.Object",
});
mbs.setAttribute(on, new Attribute("Number", new Integer(4321)));
System.out.println("Attribute value now: " +
mbs.getAttribute(on, "Number"));
break;
case 5:
/* Check that the default caching behaviour is not to cache. */
Object[] firstGot = (Object[]) mbs.getAttribute(on, "Array");
System.out.println("First got: " + Arrays.asList(firstGot));
ModelMBeanInfo mmbi = (ModelMBeanInfo) mbs.getMBeanInfo(on);
System.out.println(mmbi.getDescriptor("Array", "attribute"));
mbs.invoke(on, "tweakArray", new Object[] {new Object[] {"x"}},
new String[] {Object[].class.getName()});
Object[] secondGot = (Object[]) mbs.getAttribute(on, "Array");
System.out.println("Second got: " + Arrays.asList(secondGot));
if (secondGot.length != 1)
throw new Exception("Got value: " + Arrays.asList(secondGot));
break;
case 6:
/* Check that attributes without getters or setters work.
The value is stored in the descriptor. This test includes
an explicit currencyTimeLimit attribute. */
mbs.setAttribute(on, new Attribute("Local", "string value"));
ModelMBeanInfo mmbi2 = (ModelMBeanInfo) mbs.getMBeanInfo(on);
System.out.println(mmbi2.getDescriptor("Local", "attribute"));
Object gotback = mbs.getAttribute(on, "Local");
if (!"string value".equals(gotback))
throw new Exception("Got value: " + gotback);
break;
case 7:
/* Check that attributes without getters or setters work.
The value is stored in the descriptor. This test does
not have an explicit currencyTimeLimit attribute. */
mbs.setAttribute(on, new Attribute("Local2", "thing value"));
ModelMBeanInfo mmbi3 = (ModelMBeanInfo) mbs.getMBeanInfo(on);
System.out.println(mmbi3.getDescriptor("Local2", "attribute"));
Object gotback2 = mbs.getAttribute(on, "Local2");
if (!"thing value".equals(gotback2))
throw new Exception("Got value: " + gotback2);
break;
default:
System.err.println("UNKNOWN TEST NUMBER " + testno);
break;
}
}
private static final int NTESTS = 8;
}