use of javax.management.modelmbean.RequiredModelMBean in project camel by apache.
the class DefaultManagementMBeanAssembler method assemble.
public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
ModelMBeanInfo mbi = null;
ModelMBeanInfo standardMbi = null;
Object custom = null;
// prefer to use the managed instance if it has been annotated with JMX annotations
if (obj instanceof ManagedInstance) {
// there may be a custom embedded instance which have additional methods
custom = ((ManagedInstance) obj).getInstance();
if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
LOG.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
// get the mbean info into different groups (mbi = both, standard = standard out of the box mbi)
mbi = assembler.getMBeanInfo(obj, custom, name.toString());
standardMbi = assembler.getMBeanInfo(obj, null, name.toString());
}
}
if (mbi == null) {
// use the default provided mbean which has been annotated with JMX annotations
LOG.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
mbi = assembler.getMBeanInfo(obj, null, name.toString());
}
if (mbi == null) {
return null;
}
RequiredModelMBean mbean;
RequiredModelMBean mixinMBean = null;
boolean sanitize = camelContext.getManagementStrategy().getManagementAgent().getMask() != null && camelContext.getManagementStrategy().getManagementAgent().getMask();
// as we want a combined mbean that has both the custom and the standard
if (standardMbi != null) {
mixinMBean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
mixinMBean.setModelMBeanInfo(standardMbi);
try {
mixinMBean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException e) {
throw new JMException(e.getMessage());
}
// use custom as the object to call
obj = custom;
}
// use a mixin mbean model to combine the custom and standard (custom is optional)
mbean = new MixinRequiredModelMBean(mbi, sanitize, standardMbi, mixinMBean);
try {
mbean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException e) {
throw new JMException(e.getMessage());
}
// Allows the managed object to send notifications
if (obj instanceof NotificationSenderAware) {
((NotificationSenderAware) obj).setNotificationSender(new NotificationSenderAdapter(mbean));
}
return mbean;
}
use of javax.management.modelmbean.RequiredModelMBean in project spring-framework by spring-projects.
the class MBeanExporterOperationsTests method testRegisterExistingMBeanWithUserSuppliedObjectName.
@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);
}
use of javax.management.modelmbean.RequiredModelMBean in project camel by apache.
the class SpringManagementMBeanAssembler method assemble.
public ModelMBean assemble(MBeanServer mBeanServer, Object obj, ObjectName name) throws JMException {
ModelMBeanInfo mbi = null;
// prefer to use the managed instance if it has been annotated with Spring JMX annotations
if (obj instanceof ManagedInstance) {
Object custom = ((ManagedInstance) obj).getInstance();
if (custom != null && ObjectHelper.hasAnnotation(custom.getClass().getAnnotations(), ManagedResource.class)) {
LOG.trace("Assembling MBeanInfo for: {} from custom @ManagedResource object: {}", name, custom);
// get the mbean info from the custom managed object
mbi = springAssembler.getMBeanInfo(custom, name.toString());
// and let the custom object be registered in JMX
obj = custom;
}
}
if (mbi == null) {
if (ObjectHelper.hasAnnotation(obj.getClass().getAnnotations(), ManagedResource.class)) {
// the object has a Spring ManagedResource annotations so assemble the MBeanInfo
LOG.trace("Assembling MBeanInfo for: {} from @ManagedResource object: {}", name, obj);
mbi = springAssembler.getMBeanInfo(obj, name.toString());
} else {
// fallback and let the default mbean assembler handle this instead
return super.assemble(mBeanServer, obj, name);
}
}
LOG.trace("Assembled MBeanInfo {}", mbi);
RequiredModelMBean mbean = (RequiredModelMBean) mBeanServer.instantiate(RequiredModelMBean.class.getName());
mbean.setModelMBeanInfo(mbi);
try {
mbean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException e) {
throw new JMException(e.getMessage());
}
// Allows the managed object to send notifications
if (obj instanceof NotificationSenderAware) {
((NotificationSenderAware) obj).setNotificationSender(new NotificationSenderAdapter(mbean));
}
return mbean;
}
use of javax.management.modelmbean.RequiredModelMBean in project jdk8u_jdk by JetBrains.
the class LoggingExceptionTest method main.
public static void main(String[] args) {
Handler handler = new ConsoleHandler();
Logger logger = Logger.getLogger("javax.management.modelmbean");
logger.addHandler(handler);
logger.setLevel(Level.FINEST);
try {
for (int i = 0; i < tests.length; i++) {
System.out.println(">>> DescriptorSupportLoggingTest: Test Case " + i);
DescriptorSupport ds;
String msg = "Instantiate " + tests[i];
System.out.println(msg);
switch(i) {
case 0:
ds = new DescriptorSupport();
break;
case 1:
ds = new DescriptorSupport(10);
break;
case 2:
ds = new DescriptorSupport(new DescriptorSupport().toXMLString());
break;
case 3:
ds = new DescriptorSupport("name1=value1", "name2=value2");
break;
case 4:
ds = new DescriptorSupport(new String[] { "name" }, new Object[] { "value" });
break;
case 5:
ds = new DescriptorSupport(new DescriptorSupport());
break;
case 6:
RequiredModelMBean mbean = new RequiredModelMBean();
NotificationListener nl = new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
}
};
mbean.addAttributeChangeNotificationListener(nl, null, null);
break;
default:
throw new AssertionError();
}
System.out.println(msg + " OK");
}
} catch (Exception e) {
System.out.println("Got unexpected exception = " + e);
String msg = "Test FAILED!";
System.out.println(msg);
throw new IllegalArgumentException(msg);
}
System.out.println("Test PASSED!");
}
use of javax.management.modelmbean.RequiredModelMBean in project jdk8u_jdk by JetBrains.
the class UnserializableTargetObjectTest method main.
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 }, // no constructors
null, new ModelMBeanOperationInfo[] { operationInfo, getCountInfo, setCountInfo }, // no notifications
null);
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");
}
Aggregations