下面列出了怎么用javax.management.modelmbean.ModelMBeanInfoSupport的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public MBeanInfo getMBeanInfo() {
try {
ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[0];
ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[] {
new ModelMBeanConstructorInfo("-", this.getClass().getConstructor())
};
ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[] {
new ModelMBeanOperationInfo("info", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.INFO),
new ModelMBeanOperationInfo("action", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION),
new ModelMBeanOperationInfo("actionInfo", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION_INFO),
new ModelMBeanOperationInfo("unknown", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.UNKNOWN)
};
ModelMBeanNotificationInfo[] notifications = new ModelMBeanNotificationInfo[0];
return new ModelMBeanInfoSupport(this.getClass().getName(), "-", attributes, constructors, operations, notifications);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public ModelMBeanInfo buildModelMBeanInfo(Descriptor desc) {
ModelMBeanOperationInfo[] ops =
operations.values().toArray(new ModelMBeanOperationInfo[operations.values().size()]);
ModelMBeanAttributeInfo[] atts =
attributes.values().toArray(new ModelMBeanAttributeInfo[attributes.values().size()]);
ModelMBeanConstructorInfo[] cons =
constructors.values().toArray(new ModelMBeanConstructorInfo[constructors.values().size()]);
ModelMBeanNotificationInfo[] notifs =
notifications.values().toArray(new ModelMBeanNotificationInfo[notifications.values().size()]);
return new ModelMBeanInfoSupport("javax.management.modelmbean.ModelMBeanInfo",
"description",
atts,
cons,
ops,
notifs, desc);
}
@Test
public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception {
ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null);
RequiredModelMBean bean = new RequiredModelMBean(info);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.registerManagedResource(bean, objectName);
MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName);
assertEquals(info, infoFromServer);
}
@Test
public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception {
ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null);
RequiredModelMBean bean = new RequiredModelMBean(info);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.registerManagedResource(bean, objectName);
MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName);
assertEquals(info, infoFromServer);
}
@Test
public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception {
ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo");
ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null);
RequiredModelMBean bean = new RequiredModelMBean(info);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(getServer());
exporter.registerManagedResource(bean, objectName);
MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName);
assertEquals(info, infoFromServer);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException {
if ((defaultManagedBean == null && customManagedBean == null) || objectName == null)
return null;
// skip proxy classes
if (defaultManagedBean != null && Proxy.isProxyClass(defaultManagedBean.getClass())) {
LOG.trace("Skip creating ModelMBeanInfo due proxy class {}", defaultManagedBean.getClass());
return null;
}
// maps and lists to contain information about attributes and operations
Map<String, ManagedAttributeInfo> attributes = new LinkedHashMap<String, ManagedAttributeInfo>();
Set<ManagedOperationInfo> operations = new LinkedHashSet<ManagedOperationInfo>();
Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<ModelMBeanAttributeInfo>();
Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<ModelMBeanOperationInfo>();
Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<ModelMBeanNotificationInfo>();
// extract details from default managed bean
if (defaultManagedBean != null) {
extractAttributesAndOperations(defaultManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(defaultManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(defaultManagedBean, operations, mBeanOperations);
extractMbeanNotifications(defaultManagedBean, mBeanNotifications);
}
// extract details from custom managed bean
if (customManagedBean != null) {
extractAttributesAndOperations(customManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(customManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(customManagedBean, operations, mBeanOperations);
extractMbeanNotifications(customManagedBean, mBeanNotifications);
}
// create the ModelMBeanInfo
String name = getName(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
String description = getDescription(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
ModelMBeanAttributeInfo[] arrayAttributes = mBeanAttributes.toArray(new ModelMBeanAttributeInfo[mBeanAttributes.size()]);
ModelMBeanOperationInfo[] arrayOperations = mBeanOperations.toArray(new ModelMBeanOperationInfo[mBeanOperations.size()]);
ModelMBeanNotificationInfo[] arrayNotifications = mBeanNotifications.toArray(new ModelMBeanNotificationInfo[mBeanNotifications.size()]);
ModelMBeanInfo info = new ModelMBeanInfoSupport(name, description, arrayAttributes, null, arrayOperations, arrayNotifications);
LOG.trace("Created ModelMBeanInfo {}", info);
return info;
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException {
if ((defaultManagedBean == null && customManagedBean == null) || objectName == null)
return null;
// skip proxy classes
if (defaultManagedBean != null && Proxy.isProxyClass(defaultManagedBean.getClass())) {
LOGGER.trace("Skip creating ModelMBeanInfo due proxy class {}", defaultManagedBean.getClass());
return null;
}
// maps and lists to contain information about attributes and operations
Map<String, ManagedAttributeInfo> attributes = new LinkedHashMap<>();
Set<ManagedOperationInfo> operations = new LinkedHashSet<>();
Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<>();
Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<>();
Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<>();
// extract details from default managed bean
if (defaultManagedBean != null) {
extractAttributesAndOperations(defaultManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(defaultManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(defaultManagedBean, operations, mBeanOperations);
extractMbeanNotifications(defaultManagedBean, mBeanNotifications);
}
// extract details from custom managed bean
if (customManagedBean != null) {
extractAttributesAndOperations(customManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(customManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(customManagedBean, operations, mBeanOperations);
extractMbeanNotifications(customManagedBean, mBeanNotifications);
}
// create the ModelMBeanInfo
String name = getName(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
String description = getDescription(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
ModelMBeanAttributeInfo[] arrayAttributes = mBeanAttributes.toArray(new ModelMBeanAttributeInfo[mBeanAttributes.size()]);
ModelMBeanOperationInfo[] arrayOperations = mBeanOperations.toArray(new ModelMBeanOperationInfo[mBeanOperations.size()]);
ModelMBeanNotificationInfo[] arrayNotifications = mBeanNotifications.toArray(new ModelMBeanNotificationInfo[mBeanNotifications.size()]);
ModelMBeanInfo info = new ModelMBeanInfoSupport(name, description, arrayAttributes, null, arrayOperations, arrayNotifications);
LOGGER.trace("Created ModelMBeanInfo {}", info);
return info;
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
/**
* Create an instance of the {@code ModelMBeanInfoSupport} class supplied with all
* JMX implementations and populates the metadata through calls to the subclass.
* @param managedBean the bean that will be exposed (might be an AOP proxy)
* @param beanKey the key associated with the managed bean
* @return the populated ModelMBeanInfo instance
* @throws JMException in case of errors
* @see #getDescription(Object, String)
* @see #getAttributeInfo(Object, String)
* @see #getConstructorInfo(Object, String)
* @see #getOperationInfo(Object, String)
* @see #getNotificationInfo(Object, String)
* @see #populateMBeanDescriptor(javax.management.Descriptor, Object, String)
*/
@Override
public ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws JMException {
checkManagedBean(managedBean);
ModelMBeanInfo info = new ModelMBeanInfoSupport(
getClassName(managedBean, beanKey), getDescription(managedBean, beanKey),
getAttributeInfo(managedBean, beanKey), getConstructorInfo(managedBean, beanKey),
getOperationInfo(managedBean, beanKey), getNotificationInfo(managedBean, beanKey));
Descriptor desc = info.getMBeanDescriptor();
populateMBeanDescriptor(desc, managedBean, beanKey);
info.setMBeanDescriptor(desc);
return info;
}
/**
* Create an instance of the {@code ModelMBeanInfoSupport} class supplied with all
* JMX implementations and populates the metadata through calls to the subclass.
* @param managedBean the bean that will be exposed (might be an AOP proxy)
* @param beanKey the key associated with the managed bean
* @return the populated ModelMBeanInfo instance
* @throws JMException in case of errors
* @see #getDescription(Object, String)
* @see #getAttributeInfo(Object, String)
* @see #getConstructorInfo(Object, String)
* @see #getOperationInfo(Object, String)
* @see #getNotificationInfo(Object, String)
* @see #populateMBeanDescriptor(javax.management.Descriptor, Object, String)
*/
@Override
public ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws JMException {
checkManagedBean(managedBean);
ModelMBeanInfo info = new ModelMBeanInfoSupport(
getClassName(managedBean, beanKey), getDescription(managedBean, beanKey),
getAttributeInfo(managedBean, beanKey), getConstructorInfo(managedBean, beanKey),
getOperationInfo(managedBean, beanKey), getNotificationInfo(managedBean, beanKey));
Descriptor desc = info.getMBeanDescriptor();
populateMBeanDescriptor(desc, managedBean, beanKey);
info.setMBeanDescriptor(desc);
return info;
}
/**
* Create an instance of the {@code ModelMBeanInfoSupport} class supplied with all
* JMX implementations and populates the metadata through calls to the subclass.
* @param managedBean the bean that will be exposed (might be an AOP proxy)
* @param beanKey the key associated with the managed bean
* @return the populated ModelMBeanInfo instance
* @throws JMException in case of errors
* @see #getDescription(Object, String)
* @see #getAttributeInfo(Object, String)
* @see #getConstructorInfo(Object, String)
* @see #getOperationInfo(Object, String)
* @see #getNotificationInfo(Object, String)
* @see #populateMBeanDescriptor(javax.management.Descriptor, Object, String)
*/
@Override
public ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws JMException {
checkManagedBean(managedBean);
ModelMBeanInfo info = new ModelMBeanInfoSupport(
getClassName(managedBean, beanKey), getDescription(managedBean, beanKey),
getAttributeInfo(managedBean, beanKey), getConstructorInfo(managedBean, beanKey),
getOperationInfo(managedBean, beanKey), getNotificationInfo(managedBean, beanKey));
Descriptor desc = info.getMBeanDescriptor();
populateMBeanDescriptor(desc, managedBean, beanKey);
info.setMBeanDescriptor(desc);
return info;
}
/**
* Create an instance of the {@code ModelMBeanInfoSupport} class supplied with all
* JMX implementations and populates the metadata through calls to the subclass.
* @param managedBean the bean that will be exposed (might be an AOP proxy)
* @param beanKey the key associated with the managed bean
* @return the populated ModelMBeanInfo instance
* @throws JMException in case of errors
* @see #getDescription(Object, String)
* @see #getAttributeInfo(Object, String)
* @see #getConstructorInfo(Object, String)
* @see #getOperationInfo(Object, String)
* @see #getNotificationInfo(Object, String)
* @see #populateMBeanDescriptor(javax.management.Descriptor, Object, String)
*/
@Override
public ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws JMException {
checkManagedBean(managedBean);
ModelMBeanInfo info = new ModelMBeanInfoSupport(
getClassName(managedBean, beanKey), getDescription(managedBean, beanKey),
getAttributeInfo(managedBean, beanKey), getConstructorInfo(managedBean, beanKey),
getOperationInfo(managedBean, beanKey), getNotificationInfo(managedBean, beanKey));
Descriptor desc = info.getMBeanDescriptor();
populateMBeanDescriptor(desc, managedBean, beanKey);
info.setMBeanDescriptor(desc);
return info;
}