Search in sources :

Example 1 with RequiredModelMBean

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;
}
Also used : JMException(javax.management.JMException) NotificationSenderAware(org.apache.camel.api.management.NotificationSenderAware) ManagedInstance(org.apache.camel.api.management.ManagedInstance) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ManagedResource(org.apache.camel.api.management.ManagedResource) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean)

Example 2 with RequiredModelMBean

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);
}
Also used : ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) MBeanInfo(javax.management.MBeanInfo) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) Test(org.junit.Test)

Example 3 with RequiredModelMBean

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;
}
Also used : JMException(javax.management.JMException) NotificationSenderAware(org.apache.camel.api.management.NotificationSenderAware) ManagedInstance(org.apache.camel.api.management.ManagedInstance) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) NotificationSenderAdapter(org.apache.camel.management.NotificationSenderAdapter) ManagedResource(org.springframework.jmx.export.annotation.ManagedResource) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean)

Example 4 with RequiredModelMBean

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!");
}
Also used : ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Logger(java.util.logging.Logger) ConsoleHandler(java.util.logging.ConsoleHandler) Notification(javax.management.Notification) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) NotificationListener(javax.management.NotificationListener)

Example 5 with RequiredModelMBean

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");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Attribute(javax.management.Attribute) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Method(java.lang.reflect.Method) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) JMXConnector(javax.management.remote.JMXConnector) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) MBeanServerConnection(javax.management.MBeanServerConnection) 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