Search in sources :

Example 6 with RequiredModelMBean

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);
    }
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Hashtable(java.util.Hashtable) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Method(java.lang.reflect.Method) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ObjectName(javax.management.ObjectName) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) Map(java.util.Map) MBeanServer(javax.management.MBeanServer)

Example 7 with RequiredModelMBean

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);
    }
}
Also used : RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) JmxManaged(voldemort.annotations.jmx.JmxManaged) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) VoldemortException(voldemort.VoldemortException) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean)

Example 8 with RequiredModelMBean

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;
}
Also used : JMException(javax.management.JMException) NotificationSenderAware(org.activiti.management.jmx.annotations.NotificationSenderAware) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean)

Example 9 with RequiredModelMBean

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");
    }
}
Also used : RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Attribute(javax.management.Attribute) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ObjectName(javax.management.ObjectName) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ServiceNotFoundException(javax.management.ServiceNotFoundException) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) MBeanServer(javax.management.MBeanServer)

Aggregations

RequiredModelMBean (javax.management.modelmbean.RequiredModelMBean)9 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)8 ModelMBeanInfoSupport (javax.management.modelmbean.ModelMBeanInfoSupport)5 ObjectName (javax.management.ObjectName)4 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)4 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)4 ModelMBean (javax.management.modelmbean.ModelMBean)4 Descriptor (javax.management.Descriptor)3 JMException (javax.management.JMException)3 MBeanServer (javax.management.MBeanServer)3 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)3 Method (java.lang.reflect.Method)2 Attribute (javax.management.Attribute)2 MBeanException (javax.management.MBeanException)2 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)2 ManagedInstance (org.apache.camel.api.management.ManagedInstance)2 NotificationSenderAware (org.apache.camel.api.management.NotificationSenderAware)2 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 ConsoleHandler (java.util.logging.ConsoleHandler)1