use of javax.management.openmbean.OpenType in project jdk8u_jdk by JetBrains.
the class MXBeanNotifTest method run.
public void run(Map<String, Object> args) {
System.out.println("MXBeanNotifTest::run: Start");
int errorCount = 0;
try {
parseArgs(args);
notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println("MXBeanNotifTest::run: Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi");
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println("---- OK\n");
// ----
System.out.println("MXBeanNotifTest::run: Add me as notification listener");
mbsc.addNotificationListener(objName, this, null, null);
// ----
System.out.println("MXBeanNotifTest::run: Retrieve the Descriptor" + " that should be in MBeanNotificationInfo");
TabularData tabData = (TabularData) mbsc.getAttribute(objName, "NotifDescriptorAsMapAtt");
Map<String, String> descrMap = new HashMap<>();
for (Iterator<?> it = tabData.values().iterator(); it.hasNext(); ) {
CompositeData compData = (CompositeData) it.next();
descrMap.put((String) compData.get("key"), (String) compData.get("value"));
}
Descriptor refNotifDescriptor = new ImmutableDescriptor(descrMap);
System.out.println("---- OK\n");
// ----
// Because the MBean holding the targeted attribute is MXBean, we
// should use for the setAttribute a converted form for the
// attribute value as described by the MXBean mapping rules.
// This explains all that lovely stuff for creating a
// TabularDataSupport.
//
// WARNING : the MBeanInfo of the MXBean used on opposite side
// is computed when the MBean is registered.
// It means the Descriptor considered for the MBeanNotificationInfo
// is not the one we set in the lines below, it is too late.
// However, we check that set is harmless when we check
// the MBeanNotificationInfo.
//
System.out.println("MXBeanNotifTest::run: Set a Map<String, String>" + " attribute");
String typeName = "java.util.Map<java.lang.String,java.lang.String>";
String[] keyValue = new String[] { "key", "value" };
OpenType<?>[] openTypes = new OpenType<?>[] { SimpleType.STRING, SimpleType.STRING };
CompositeType rowType = new CompositeType(typeName, typeName, keyValue, keyValue, openTypes);
TabularType tabType = new TabularType(typeName, typeName, rowType, new String[] { "key" });
TabularDataSupport convertedDescrMap = new TabularDataSupport(tabType);
for (int i = 0; i < numOfNotifDescriptorElements; i++) {
Object[] descrValue = { "field" + i, "value" + i };
CompositeData data = new CompositeDataSupport(rowType, keyValue, descrValue);
convertedDescrMap.put(data);
}
Attribute descrAtt = new Attribute("NotifDescriptorAsMapAtt", convertedDescrMap);
mbsc.setAttribute(objName, descrAtt);
System.out.println("---- OK\n");
// ----
System.out.println("MXBeanNotifTest::run: Compare the Descriptor from" + " the MBeanNotificationInfo against a reference");
MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
errorCount += checkMBeanInfo(mbInfo, refNotifDescriptor);
System.out.println("---- DONE\n");
// ----
System.out.println("Check isInstanceOf(Basic)");
if (!mbsc.isInstanceOf(objName, BASIC_MXBEAN_CLASS_NAME)) {
errorCount++;
System.out.println("---- ERROR isInstanceOf returned false\n");
} else {
System.out.println("---- OK\n");
}
// ----
System.out.println("Check isInstanceOf(BasicMXBean)");
if (!mbsc.isInstanceOf(objName, BASIC_MXBEAN_INTERFACE_NAME)) {
errorCount++;
System.out.println("---- ERROR isInstanceOf returned false\n");
} else {
System.out.println("---- OK\n");
}
// ----
System.out.println("MXBeanNotifTest::run: Ask for " + numOfNotifications + " notification(s)");
Object[] sendNotifParam = new Object[1];
String[] sendNotifSig = new String[] { "java.lang.String" };
for (int i = 0; i < numOfNotifications; i++) {
// Select which type of notification we ask for
if (i % 2 == 0) {
sendNotifParam[0] = Basic.NOTIF_TYPE_0;
} else {
sendNotifParam[0] = Basic.NOTIF_TYPE_1;
}
// Trigger notification emission
mbsc.invoke(objName, "sendNotification", sendNotifParam, sendNotifSig);
// Wait for it then check it when it comes early enough
Notification notif = notifList.poll(timeForNotificationInSeconds, TimeUnit.SECONDS);
// notifications are delivered with, we prefer to secure it.
if (i == 0 && notif == null) {
System.out.println("MXBeanNotifTest::run: Wait extra " + timeForNotificationInSeconds + " second(s) the " + " very first notification");
notif = notifList.poll(timeForNotificationInSeconds, TimeUnit.SECONDS);
}
if (notif == null) {
errorCount++;
System.out.println("---- ERROR No notification received" + " within allocated " + timeForNotificationInSeconds + " second(s) !");
} else {
errorCount += checkNotification(notif, (String) sendNotifParam[0], Basic.NOTIFICATION_MESSAGE, objName);
}
}
int toc = 0;
while (notifList.size() < 2 && toc < 10) {
Thread.sleep(499);
toc++;
}
System.out.println("---- DONE\n");
} catch (Exception e) {
Utils.printThrowable(e, true);
throw new RuntimeException(e);
}
if (errorCount == 0) {
System.out.println("MXBeanNotifTest::run: Done without any error");
} else {
System.out.println("MXBeanNotifTest::run: Done with " + errorCount + " error(s)");
throw new RuntimeException("errorCount = " + errorCount);
}
}
use of javax.management.openmbean.OpenType in project jdk8u_jdk by JetBrains.
the class MXBeanTest method testInterface.
private static <T> void testInterface(Class<T> c, MBeanServerConnection mbsc, ObjectName on, NamedMXBeans namedMXBeans, boolean nullTest) throws Exception {
System.out.println("Type check...");
MBeanInfo mbi = mbsc.getMBeanInfo(on);
MBeanAttributeInfo[] mbais = mbi.getAttributes();
for (int i = 0; i < mbais.length; i++) {
MBeanAttributeInfo mbai = mbais[i];
String name = mbai.getName();
Field typeField = c.getField(name + "Type");
OpenType typeValue = (OpenType) typeField.get(null);
OpenType openType = (OpenType) mbai.getDescriptor().getFieldValue("openType");
if (typeValue.equals(openType))
success("attribute " + name);
else {
final String msg = "Wrong type attribute " + name + ": " + openType + " should be " + typeValue;
failure(msg);
}
}
MBeanOperationInfo[] mbois = mbi.getOperations();
for (int i = 0; i < mbois.length; i++) {
MBeanOperationInfo mboi = mbois[i];
String oname = mboi.getName();
if (!oname.startsWith("op"))
throw new Error();
OpenType retType = (OpenType) mboi.getDescriptor().getFieldValue("openType");
MBeanParameterInfo[] params = mboi.getSignature();
MBeanParameterInfo p1i = params[0];
MBeanParameterInfo p2i = params[1];
OpenType p1Type = (OpenType) p1i.getDescriptor().getFieldValue("openType");
OpenType p2Type = (OpenType) p2i.getDescriptor().getFieldValue("openType");
if (!retType.equals(p1Type) || !p1Type.equals(p2Type)) {
final String msg = "Parameter and return open types should all be same " + "but are not: " + retType + " " + oname + "(" + p1Type + ", " + p2Type + ")";
failure(msg);
continue;
}
String name = oname.substring(2);
Field typeField = c.getField(name + "Type");
OpenType typeValue = (OpenType) typeField.get(null);
if (typeValue.equals(retType))
success("operation " + oname);
else {
final String msg = "Wrong type operation " + oname + ": " + retType + " should be " + typeValue;
failure(msg);
}
}
System.out.println("Mapping check...");
Object proxy = JMX.newMXBeanProxy(mbsc, on, c);
Method[] methods = c.getMethods();
for (int i = 0; i < methods.length; i++) {
final Method method = methods[i];
if (method.getDeclaringClass() != c)
// skip hashCode() etc inherited from Object
continue;
final String mname = method.getName();
final int what = getType(method);
final String name = getName(method);
final Field refField = c.getField(name);
if (nullTest && refField.getType().isPrimitive())
continue;
final Field openTypeField = c.getField(name + "Type");
final OpenType openType = (OpenType) openTypeField.get(null);
final Object refValue = nullTest ? null : refField.get(null);
Object setValue = refValue;
try {
Field onField = c.getField(name + "ObjectName");
String refName = (String) onField.get(null);
ObjectName refObjName = ObjectName.getInstance(refName);
Class<?> mxbeanInterface = refField.getType();
setValue = nullTest ? null : JMX.newMXBeanProxy(mbsc, refObjName, mxbeanInterface);
} catch (Exception e) {
// no xObjectName field, setValue == refValue
}
boolean ok = true;
try {
switch(what) {
case GET:
final Object gotOpen = mbsc.getAttribute(on, name);
if (nullTest) {
if (gotOpen != null) {
failure(mname + " got non-null value " + gotOpen);
ok = false;
}
} else if (!openType.isValue(gotOpen)) {
if (gotOpen instanceof TabularData) {
// detail the mismatch
TabularData gotTabular = (TabularData) gotOpen;
compareTabularType((TabularType) openType, gotTabular.getTabularType());
}
failure(mname + " got open data " + gotOpen + " not valid for open type " + openType);
ok = false;
}
final Object got = method.invoke(proxy, (Object[]) null);
if (!equal(refValue, got, namedMXBeans)) {
failure(mname + " got " + string(got) + ", should be " + string(refValue));
ok = false;
}
break;
case SET:
method.invoke(proxy, new Object[] { setValue });
break;
case OP:
final Object opped = method.invoke(proxy, new Object[] { setValue, setValue });
if (!equal(refValue, opped, namedMXBeans)) {
failure(mname + " got " + string(opped) + ", should be " + string(refValue));
ok = false;
}
break;
default:
throw new Error();
}
if (ok)
success(mname);
} catch (Exception e) {
failure(mname, e);
}
}
}
use of javax.management.openmbean.OpenType in project jdk8u_jdk by JetBrains.
the class MXBeanLoadingTest1 method run.
public void run(Map<String, Object> args) {
System.out.println("MXBeanLoadingTest1::run: Start");
try {
System.out.println("We ensure no reference is retained on MXBean class" + " after it is unregistered. We take time to perform" + " some little extra check of Descriptors, MBean*Info.");
ClassLoader myClassLoader = MXBeanLoadingTest1.class.getClassLoader();
if (!(myClassLoader instanceof URLClassLoader)) {
String message = "(ERROR) Test's class loader is not " + "a URLClassLoader";
System.out.println(message);
throw new RuntimeException(message);
}
URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;
URL[] urls = myURLClassLoader.getURLs();
PrivateMLet mlet = new PrivateMLet(urls, null, false);
Class<?> shadowClass = mlet.loadClass(TestMXBean.class.getName());
if (shadowClass == TestMXBean.class) {
String message = "(ERROR) MLet got original TestMXBean, not shadow";
System.out.println(message);
throw new RuntimeException(message);
}
shadowClass = null;
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ObjectName mletName = new ObjectName("x:type=mlet");
mbs.registerMBean(mlet, mletName);
ObjectName testName = new ObjectName("x:type=test");
mbs.createMBean(Test.class.getName(), testName, mletName);
// That test fails because the MXBean instance is accessed via
// a delegate OpenMBean which has
ClassLoader testLoader = mbs.getClassLoaderFor(testName);
if (testLoader != mlet) {
System.out.println("MLet " + mlet);
String message = "(ERROR) MXBean's class loader is not MLet: " + testLoader;
System.out.println(message);
throw new RuntimeException(message);
}
testLoader = null;
// Cycle get/set/get of the attribute of type Luis.
// We check the set is effective.
CompositeData cd_B = (CompositeData) mbs.getAttribute(testName, "B");
CompositeType compType_B = cd_B.getCompositeType();
CompositeDataSupport cds_B = new CompositeDataSupport(compType_B, new String[] { "something" }, new Object[] { Integer.valueOf(13) });
Attribute myAtt = new Attribute("B", cds_B);
mbs.setAttribute(testName, myAtt);
CompositeData cd_B2 = (CompositeData) mbs.getAttribute(testName, "B");
if (((Integer) cd_B2.get("something")).intValue() != 13) {
String message = "(ERROR) The setAttribute of att B did not work;" + " expect Luis.something = 13 but got " + cd_B2.get("something");
System.out.println(message);
throw new RuntimeException(message);
}
MBeanInfo info = mbs.getMBeanInfo(testName);
String mxbeanField = (String) info.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
if (mxbeanField == null || !mxbeanField.equals("true")) {
String message = "(ERROR) Improper mxbean field value " + mxbeanField;
System.out.println(message);
throw new RuntimeException(message);
}
// Check the 2 attributes.
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs.length == 2) {
for (MBeanAttributeInfo mbai : attrs) {
String originalTypeFieldValue = (String) mbai.getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
OpenType<?> openTypeFieldValue = (OpenType<?>) mbai.getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
if (mbai.getName().equals("A")) {
if (!mbai.isReadable() || !mbai.isWritable() || mbai.isIs() || !mbai.getType().equals("int")) {
String message = "(ERROR) Unexpected MBeanAttributeInfo for A " + mbai;
System.out.println(message);
throw new RuntimeException(message);
}
if (!originalTypeFieldValue.equals("int")) {
String message = "(ERROR) Unexpected originalType in Descriptor for A " + originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
if (!openTypeFieldValue.equals(SimpleType.INTEGER)) {
String message = "(ERROR) Unexpected openType in Descriptor for A " + originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
} else if (mbai.getName().equals("B")) {
if (!mbai.isReadable() || !mbai.isWritable() || mbai.isIs() || !mbai.getType().equals("javax.management.openmbean.CompositeData")) {
String message = "(ERROR) Unexpected MBeanAttributeInfo for B " + mbai;
System.out.println(message);
throw new RuntimeException(message);
}
if (!originalTypeFieldValue.equals(Luis.class.getName())) {
String message = "(ERROR) Unexpected originalType in Descriptor for B " + originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
if (!openTypeFieldValue.equals(compType_B)) {
String message = "(ERROR) Unexpected openType in Descriptor for B " + compType_B;
System.out.println(message);
throw new RuntimeException(message);
}
} else {
String message = "(ERROR) Unknown attribute name";
System.out.println(message);
throw new RuntimeException(message);
}
}
} else {
String message = "(ERROR) Unexpected MBeanAttributeInfo array" + Arrays.deepToString(attrs);
System.out.println(message);
throw new RuntimeException(message);
}
// Check the MXBean operation.
MBeanOperationInfo[] ops = info.getOperations();
// logged 6320104.
if (ops.length != 1 || !ops[0].getName().equals("bogus") || ops[0].getSignature().length > 0 || !ops[0].getReturnType().equals("void")) {
String message = "(ERROR) Unexpected MBeanOperationInfo array " + Arrays.deepToString(ops);
System.out.println(message);
throw new RuntimeException(message);
}
String originalTypeFieldValue = (String) ops[0].getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
OpenType<?> openTypeFieldValue = (OpenType<?>) ops[0].getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
if (!originalTypeFieldValue.equals("void")) {
String message = "(ERROR) Unexpected originalType in Descriptor for bogus " + originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
if (!openTypeFieldValue.equals(SimpleType.VOID)) {
String message = "(ERROR) Unexpected openType in Descriptor for bogus " + originalTypeFieldValue;
System.out.println(message);
throw new RuntimeException(message);
}
// Check there is 2 constructors.
if (info.getConstructors().length != 2) {
String message = "(ERROR) Wrong number of constructors " + "in introspected bean: " + Arrays.asList(info.getConstructors());
System.out.println(message);
throw new RuntimeException(message);
}
// Check MXBean class name.
if (!info.getClassName().endsWith("Test")) {
String message = "(ERROR) Wrong info class name: " + info.getClassName();
System.out.println(message);
throw new RuntimeException(message);
}
mbs.unregisterMBean(testName);
mbs.unregisterMBean(mletName);
WeakReference<PrivateMLet> mletRef = new WeakReference<PrivateMLet>(mlet);
mlet = null;
System.out.println("MXBean registered and unregistered, waiting for " + "garbage collector to collect class loader");
for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
System.gc();
Thread.sleep(1);
}
if (mletRef.get() == null)
System.out.println("(OK) class loader was GC'd");
else {
String message = "(ERROR) Class loader was not GC'd";
System.out.println(message);
throw new RuntimeException(message);
}
} catch (Exception e) {
Utils.printThrowable(e, true);
throw new RuntimeException(e);
}
System.out.println("MXBeanLoadingTest1::run: Done without any error");
}
use of javax.management.openmbean.OpenType in project jdk8u_jdk by JetBrains.
the class MXBeanIntrospector method getMBeanOperationInfo.
@Override
MBeanOperationInfo getMBeanOperationInfo(String operationName, ConvertingMethod operation) {
final Method method = operation.getMethod();
final String description = operationName;
/* Ideally this would be an empty string, but
OMBOperationInfo constructor forbids that. Also, we
could consult an annotation to get a useful
description. */
final int impact = MBeanOperationInfo.UNKNOWN;
final OpenType<?> returnType = operation.getOpenReturnType();
final Type originalReturnType = operation.getGenericReturnType();
final OpenType<?>[] paramTypes = operation.getOpenParameterTypes();
final Type[] originalParamTypes = operation.getGenericParameterTypes();
final MBeanParameterInfo[] params = new MBeanParameterInfo[paramTypes.length];
boolean openReturnType = canUseOpenInfo(originalReturnType);
boolean openParameterTypes = true;
Annotation[][] annots = method.getParameterAnnotations();
for (int i = 0; i < paramTypes.length; i++) {
final String paramName = "p" + i;
final String paramDescription = paramName;
final OpenType<?> openType = paramTypes[i];
final Type originalType = originalParamTypes[i];
Descriptor descriptor = typeDescriptor(openType, originalType);
descriptor = ImmutableDescriptor.union(descriptor, Introspector.descriptorForAnnotations(annots[i]));
final MBeanParameterInfo pi;
if (canUseOpenInfo(originalType)) {
pi = new OpenMBeanParameterInfoSupport(paramName, paramDescription, openType, descriptor);
} else {
openParameterTypes = false;
pi = new MBeanParameterInfo(paramName, originalTypeString(originalType), paramDescription, descriptor);
}
params[i] = pi;
}
Descriptor descriptor = typeDescriptor(returnType, originalReturnType);
descriptor = ImmutableDescriptor.union(descriptor, Introspector.descriptorForElement(method));
final MBeanOperationInfo oi;
if (openReturnType && openParameterTypes) {
/* If the return value and all the parameters can be faithfully
* represented as OpenType then we return an OpenMBeanOperationInfo.
* If any of them is a primitive type, we can't. Compatibility
* with JSR 174 means that we must return an MBean*Info where
* the getType() is the primitive type, not its wrapped type as
* we would get with an OpenMBean*Info. The OpenType is available
* in the Descriptor in either case.
*/
final OpenMBeanParameterInfo[] oparams = new OpenMBeanParameterInfo[params.length];
System.arraycopy(params, 0, oparams, 0, params.length);
oi = new OpenMBeanOperationInfoSupport(operationName, description, oparams, returnType, impact, descriptor);
} else {
oi = new MBeanOperationInfo(operationName, description, params, openReturnType ? returnType.getClassName() : originalTypeString(originalReturnType), impact, descriptor);
}
return oi;
}
use of javax.management.openmbean.OpenType in project tomee by apache.
the class SimpleRouter method getActiveRoutes.
@ManagedAttribute
public TabularData getActiveRoutes() {
if (routes.length == 0) {
return null;
}
final OpenType<?>[] types = new OpenType<?>[routes.length];
final String[] keys = new String[types.length];
final String[] values = new String[types.length];
for (int i = 0; i < types.length; i++) {
types[i] = SimpleType.STRING;
keys[i] = routes[i].getOrigin().substring(prefix.length());
values[i] = routes[i].getRawDestination().substring(prefix.length());
}
try {
final CompositeType ct = new CompositeType("routes", "routes", keys, keys, types);
final TabularType type = new TabularType("router", "routes", ct, keys);
final TabularDataSupport data = new TabularDataSupport(type);
final CompositeData line = new CompositeDataSupport(ct, keys, values);
data.put(line);
return data;
} catch (final OpenDataException e) {
return null;
}
}
Aggregations