use of javax.management.modelmbean.RequiredModelMBean in project jdk8u_jdk by JetBrains.
the class RequiredModelMBeanGetAttributeTest method main.
public static void main(String[] args) throws Exception {
boolean ok = true;
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// Resource methods
Method nullGetter = Resource.class.getMethod("getNull", (Class[]) null);
Method integerGetter = Resource.class.getMethod("getInteger", (Class[]) null);
Method hashtableGetter = Resource.class.getMethod("getHashtable", (Class[]) null);
Method mapGetter = Resource.class.getMethod("getMap", (Class[]) null);
// ModelMBeanOperationInfo
Descriptor nullOperationDescriptor = new DescriptorSupport(new String[] { "name=getNull", "descriptorType=operation", "role=getter" });
ModelMBeanOperationInfo nullOperationInfo = new ModelMBeanOperationInfo("Null attribute", nullGetter, nullOperationDescriptor);
Descriptor integerOperationDescriptor = new DescriptorSupport(new String[] { "name=getInteger", "descriptorType=operation", "role=getter" });
ModelMBeanOperationInfo integerOperationInfo = new ModelMBeanOperationInfo("Integer attribute", integerGetter, integerOperationDescriptor);
Descriptor hashtableOperationDescriptor = new DescriptorSupport(new String[] { "name=getHashtable", "descriptorType=operation", "role=getter" });
ModelMBeanOperationInfo hashtableOperationInfo = new ModelMBeanOperationInfo("Hashtable attribute", hashtableGetter, hashtableOperationDescriptor);
Descriptor mapOperationDescriptor = new DescriptorSupport(new String[] { "name=getMap", "descriptorType=operation", "role=getter" });
ModelMBeanOperationInfo mapOperationInfo = new ModelMBeanOperationInfo("Map attribute", mapGetter, mapOperationDescriptor);
// ModelMBeanAttributeInfo
Descriptor nullAttributeDescriptor = new DescriptorSupport(new String[] { "name=Null", "descriptorType=attribute", "getMethod=getNull" });
ModelMBeanAttributeInfo nullAttributeInfo = new ModelMBeanAttributeInfo("Null", "java.lang.Object", "Null attribute", true, false, false, nullAttributeDescriptor);
Descriptor integerAttributeDescriptor = new DescriptorSupport(new String[] { "name=Integer", "descriptorType=attribute", "getMethod=getInteger" });
ModelMBeanAttributeInfo integerAttributeInfo = new ModelMBeanAttributeInfo("Integer", "int", "Integer attribute", true, false, false, integerAttributeDescriptor);
Descriptor hashtableAttributeDescriptor = new DescriptorSupport(new String[] { "name=Hashtable", "descriptorType=attribute", "getMethod=getHashtable" });
ModelMBeanAttributeInfo hashtableAttributeInfo = new ModelMBeanAttributeInfo("Hashtable", "java.util.Hashtable", "Hashtable attribute", true, false, false, hashtableAttributeDescriptor);
Descriptor mapAttributeDescriptor = new DescriptorSupport(new String[] { "name=Map", "descriptorType=attribute", "getMethod=getMap" });
ModelMBeanAttributeInfo mapAttributeInfo = new ModelMBeanAttributeInfo("Map", "java.util.Map", "Map attribute", true, false, false, mapAttributeDescriptor);
Descriptor null2AttributeDescriptor = new DescriptorSupport(new String[] { "name=Null2", "descriptorType=attribute" });
null2AttributeDescriptor.setField("default", null);
ModelMBeanAttributeInfo null2AttributeInfo = new ModelMBeanAttributeInfo("Null2", "java.lang.Object", "Null2 attribute", true, false, false, null2AttributeDescriptor);
Descriptor integer2AttributeDescriptor = new DescriptorSupport(new String[] { "name=Integer2", "descriptorType=attribute" });
integer2AttributeDescriptor.setField("default", 10);
ModelMBeanAttributeInfo integer2AttributeInfo = new ModelMBeanAttributeInfo("Integer2", "int", "Integer2 attribute", true, false, false, integer2AttributeDescriptor);
Descriptor hashtable2AttributeDescriptor = new DescriptorSupport(new String[] { "name=Hashtable2", "descriptorType=attribute" });
hashtable2AttributeDescriptor.setField("default", new Hashtable());
ModelMBeanAttributeInfo hashtable2AttributeInfo = new ModelMBeanAttributeInfo("Hashtable2", "java.util.Hashtable", "Hashtable2 attribute", true, false, false, hashtable2AttributeDescriptor);
Descriptor map2AttributeDescriptor = new DescriptorSupport(new String[] { "name=Map2", "descriptorType=attribute" });
map2AttributeDescriptor.setField("default", new Hashtable());
ModelMBeanAttributeInfo map2AttributeInfo = new ModelMBeanAttributeInfo("Map2", "java.util.Map", "Map2 attribute", true, false, false, map2AttributeDescriptor);
// ModelMBeanInfo
ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(Resource.class.getName(), "Resource MBean", new ModelMBeanAttributeInfo[] { nullAttributeInfo, integerAttributeInfo, hashtableAttributeInfo, mapAttributeInfo, null2AttributeInfo, integer2AttributeInfo, hashtable2AttributeInfo, map2AttributeInfo }, null, new ModelMBeanOperationInfo[] { nullOperationInfo, integerOperationInfo, hashtableOperationInfo, mapOperationInfo }, null);
// RequiredModelMBean
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource, "ObjectReference");
ObjectName mmbName = new ObjectName(":type=ResourceMBean");
mbs.registerMBean(mmb, mmbName);
// Run tests
System.out.println("\nTesting that we can call getNull()... ");
try {
Object o = mbs.getAttribute(mmbName, "Null");
System.out.println("getNull() = " + o);
System.out.println("Attribute's declared type = java.lang.Object");
System.out.println("Returned value's type = null");
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getInteger()... ");
try {
Integer i = (Integer) mbs.getAttribute(mmbName, "Integer");
System.out.println("getInteger() = " + i);
System.out.println("Attribute's declared type = int");
System.out.println("Returned value's type = " + i.getClass().getName());
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getHashtable()... ");
try {
Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable");
System.out.println("getHashtable() = " + h);
System.out.println("Attribute's declared type = " + "java.util.Hashtable");
System.out.println("Returned value's type = " + h.getClass().getName());
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getMap()... ");
try {
Map m = (Map) mbs.getAttribute(mmbName, "Map");
System.out.println("getMap() = " + m);
System.out.println("Attribute's declared type = " + "java.util.Map");
System.out.println("Returned value's type = " + m.getClass().getName());
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getNull2()... ");
try {
Object o = mbs.getAttribute(mmbName, "Null2");
System.out.println("getNull2() = " + o);
System.out.println("Attribute's declared type = java.lang.Object");
System.out.println("Returned value's type = null");
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getInteger2()... ");
try {
Integer i = (Integer) mbs.getAttribute(mmbName, "Integer2");
System.out.println("getInteger2() = " + i);
System.out.println("Attribute's declared type = int");
System.out.println("Returned value's type = " + i.getClass().getName());
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getHashtable2()... ");
try {
Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable2");
System.out.println("getHashtable2() = " + h);
System.out.println("Attribute's declared type = " + "java.util.Hashtable");
System.out.println("Returned value's type = " + h.getClass().getName());
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
System.out.println("\nTesting that we can call getMap2()... ");
try {
Map m = (Map) mbs.getAttribute(mmbName, "Map2");
System.out.println("getMap2() = " + m);
System.out.println("Attribute's declared type = " + "java.util.Map");
System.out.println("Returned value's type = " + m.getClass().getName());
} catch (Exception e) {
System.out.println("TEST FAILED: Caught exception:");
e.printStackTrace(System.out);
ok = false;
}
if (ok)
System.out.println("\nTest passed.\n");
else {
System.out.println("\nTest failed.\n");
System.exit(1);
}
}
use of javax.management.modelmbean.RequiredModelMBean in project voldemort by voldemort.
the class JmxUtils method createModelMBean.
/**
* Create a model mbean from an object using the description given in the
* Jmx annotation if present. Only operations are supported so far, no
* attributes, constructors, or notifications
*
* @param o The object to create an MBean for
* @return The ModelMBean for the given object
*/
public static ModelMBean createModelMBean(Object o) {
try {
ModelMBean mbean = new RequiredModelMBean();
JmxManaged annotation = o.getClass().getAnnotation(JmxManaged.class);
String description = annotation == null ? "" : annotation.description();
ModelMBeanInfo info = new ModelMBeanInfoSupport(o.getClass().getName(), description, extractAttributeInfo(o), new ModelMBeanConstructorInfo[0], extractOperationInfo(o), new ModelMBeanNotificationInfo[0]);
mbean.setModelMBeanInfo(info);
mbean.setManagedResource(o, "ObjectReference");
return mbean;
} catch (MBeanException e) {
throw new VoldemortException(e);
} catch (InvalidTargetObjectTypeException e) {
throw new VoldemortException(e);
} catch (InstanceNotFoundException e) {
throw new VoldemortException(e);
}
}
use of javax.management.modelmbean.RequiredModelMBean in project Activiti by Activiti.
the class DefaultManagementMBeanAssembler method assemble.
public ModelMBean assemble(Object obj, ObjectName name) throws JMException {
ModelMBeanInfo 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 = new RequiredModelMBean(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 RequiredModelMBeanSetAttributeTest method main.
public static void main(String[] args) throws Exception {
boolean ok = true;
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
// ModelMBeanAttributeInfo
Descriptor somethingAttributeDescriptor = new DescriptorSupport(new String[] { "name=Something", "descriptorType=attribute", "getMethod=getSomething" });
ModelMBeanAttributeInfo somethingAttributeInfo = new ModelMBeanAttributeInfo("Something", "java.lang.String", "Something attribute", true, true, false, somethingAttributeDescriptor);
Descriptor somethingCachedAttributeDescriptor = new DescriptorSupport(new String[] { "name=SomethingCached", "descriptorType=attribute", "getMethod=getSomethingCached", "currencyTimeLimit=5000" });
ModelMBeanAttributeInfo somethingCachedAttributeInfo = new ModelMBeanAttributeInfo("SomethingCached", "java.lang.String", "Something cached attribute", true, true, false, somethingCachedAttributeDescriptor);
// ModelMBeanInfo
ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(Resource.class.getName(), "Resource MBean", new ModelMBeanAttributeInfo[] { somethingAttributeInfo, somethingCachedAttributeInfo }, null, new ModelMBeanOperationInfo[] {}, null);
// RequiredModelMBean
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource, "ObjectReference");
ObjectName mmbName = new ObjectName(":type=ResourceMBean");
mbs.registerMBean(mmb, mmbName);
// Run tests
System.out.println("\nTest that we receive ServiceNotFoundException");
try {
Attribute attr = new Attribute("Something", "Some string");
mbs.setAttribute(mmbName, attr);
System.out.println("TEST FAILED: Didn't caught exception");
ok = false;
} catch (MBeanException mbex) {
Exception e = mbex.getTargetException();
if (e == null || !(e instanceof ServiceNotFoundException)) {
System.out.println("TEST FAILED: Caught wrong exception:" + e);
ok = false;
} else
System.out.println("Received expected ServiceNotFoundException");
} catch (Exception e) {
System.out.println("TEST FAILED: Caught wrong exception: " + e);
e.printStackTrace(System.out);
ok = false;
}
//Now check that when caching is enabled, setAttribute is working
System.out.println("\nTest that we are not receiving ServiceNotFoundException");
try {
Attribute attr = new Attribute("SomethingCached", "Some string");
mbs.setAttribute(mmbName, attr);
System.out.println("No exception thrown");
} catch (Exception e) {
System.out.println("TEST FAILED: Caught an exception: " + e);
e.printStackTrace(System.out);
ok = false;
}
if (ok)
System.out.println("Test passed");
else {
System.out.println("TEST FAILED");
throw new Exception("TEST FAILED");
}
}
Aggregations