Example usage for javax.management.modelmbean RequiredModelMBean setManagedResource

List of usage examples for javax.management.modelmbean RequiredModelMBean setManagedResource

Introduction

In this page you can find the example usage for javax.management.modelmbean RequiredModelMBean setManagedResource.

Prototype

public void setManagedResource(Object mr, String mr_type) throws MBeanException, RuntimeOperationsException,
        InstanceNotFoundException, InvalidTargetObjectTypeException 

Source Link

Document

Sets the instance handle of the object against which to execute all methods in this ModelMBean management interface (MBeanInfo and Descriptors).

Usage

From source file:org.sakaiproject.kernel.loader.server.test.DummySharedClassLoaderContainer.java

/**
 * Create a shared classloader object.//ww w.j a v a 2s  .  c o m
 * 
 * @param kernel
 *          the kernel to connect to.
 * @param shutdownService
 *          the shutdown service.
 * @throws JMRuntimeException
 * @throws JMException
 * @throws InvalidTargetObjectTypeException
 */
public DummySharedClassLoaderContainer(ClassLoader classLoader)
        throws JMRuntimeException, JMException, InvalidTargetObjectTypeException {
    this.sharedClassLoader = classLoader;
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    RequiredModelMBean model = new RequiredModelMBean(createMBeanInfo());
    model.setManagedResource(this, "objectReference");
    ObjectName common = new ObjectName(CommonObject.MBEAN_COMMON + ".sharedclassloader");
    mbs.registerMBean(model, common);
}

From source file:org.sakaiproject.kernel.component.core.SharedClassLoaderContainer.java

/**
 * Create a shared classloader object./*w w w .  j av a 2  s  .c o m*/
 * 
 * @param kernel
 *          the kernel to connect to.
 * @param shutdownService
 *          the shutdown service.
 * @throws JMRuntimeException
 * @throws JMException
 * @throws InvalidTargetObjectTypeException
 */
@Inject
public SharedClassLoaderContainer(Kernel kernel, ShutdownService shutdownService, SharedClassLoader classLoader)
        throws JMRuntimeException, JMException, InvalidTargetObjectTypeException {
    this.sharedClassLoader = classLoader;
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    RequiredModelMBean model = new RequiredModelMBean(createMBeanInfo());
    model.setManagedResource(this, "objectReference");
    ObjectName common = new ObjectName(CommonObject.MBEAN_COMMON + ".sharedclassloader");
    mbs.registerMBean(model, common);

    // Explicit register this container to be shutdown, we dont want this
    // container exposed as a service, so we register directly. Within the
    // same module, this approach should be taken to give the IoC container
    // a dependency graph.
    shutdownService.register(this);
}

From source file:org.apache.camel.management.JmxMBeanAssembler.java

public RequiredModelMBean assemble(Object obj, ObjectName name) throws JMException {
    ModelMBeanInfo mbi = null;//  w w w. j a  v a 2s.  co m

    // 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)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Assembling MBeanInfo for: " + name.toString()
                        + " from custom @ManagedResource object: " + custom);
            }
            // get the mbean info from the custom managed object
            mbi = assembler.getMBeanInfo(custom, name.toString());
            // and let the custom object be registered in JMX
            obj = custom;
        }
    }

    if (mbi == null) {
        // use the default provided mbean which has been annotated with Spring JMX annotations
        if (LOG.isTraceEnabled()) {
            LOG.trace("Assembling MBeanInfo for: " + name.toString() + " from @ManagedResource object: " + obj);
        }
        mbi = assembler.getMBeanInfo(obj, name.toString());
    }

    RequiredModelMBean mbean = (RequiredModelMBean) server.instantiate(RequiredModelMBean.class.getName());
    mbean.setModelMBeanInfo(mbi);

    try {
        mbean.setManagedResource(obj, "ObjectReference");
    } catch (InvalidTargetObjectTypeException e) {
        throw new JMException(e.getMessage());
    }

    return mbean;
}

From source file:org.apache.servicemix.nmr.management.ManagementAgent.java

public void register(Object obj, ObjectName name, boolean forceRegistration) throws JMException {
    try {/*from  ww  w .  java  2  s. c  o  m*/
        registerMBeanWithServer(obj, name, forceRegistration);
    } catch (NotCompliantMBeanException e) {
        // If this is not a "normal" MBean, then try to deploy it using JMX
        // annotations
        ModelMBeanInfo mbi = assembler.getMBeanInfo(obj, name.toString());
        RequiredModelMBean mbean = (RequiredModelMBean) mbeanServer
                .instantiate(RequiredModelMBean.class.getName());
        mbean.setModelMBeanInfo(mbi);
        try {
            mbean.setManagedResource(obj, "ObjectReference");
        } catch (InvalidTargetObjectTypeException itotex) {
            throw new JMException(itotex.getMessage());
        }
        registerMBeanWithServer(mbean, name, forceRegistration);
    }
}

From source file:org.sakaiproject.kernel.component.KernelLifecycle.java

/**
 * Execute the start phase of the lifecycle, creating the MBean and registering the newly started
 * Kernel with JMX./*from   w  w w  .  j av  a  2  s . c  o  m*/
 *
 * @see org.sakaiproject.kernel.loader.common.CommonLifecycle#start()
 */
public void start() {
    LOG.info("==========PHASE 1 STARTING =================");
    try {
        long start = System.currentTimeMillis();
        lifecycleEvent(CommonLifecycleEvent.BEFORE_START);
        lastLoadDate = new Date();

        // Start the kernel
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        RequiredModelMBean model = new RequiredModelMBean(createMBeanInfo());
        model.setManagedResource(this, "objectReference");
        ObjectName kernelName = new ObjectName(Kernel.MBEAN_KERNEL);
        mbs.registerMBean(model, kernelName);

        kernel = new KernelImpl();

        kernel.start();

        // Start the service manager
        serviceManager = new ServiceManagerImpl(kernel);
        serviceManager.start();

        // Start the component manager.
        componentManager = new ComponentManagerImpl(kernel);
        componentManager.start();

        try {
            System.runFinalization();
            Runtime.getRuntime().gc();
            CompositeData permGen = null;
            try {
                permGen = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=Perm Gen"), "Usage");
            } catch (Exception ex) {
                permGen = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=CMS Perm Gen"), "Usage");
            }
            CompositeData tenuredGen;
            try {
                tenuredGen = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=Tenured Gen"), "Usage");
            } catch (Exception ex) {
                tenuredGen = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=CMS Old Gen"), "Usage");
            }
            CompositeData codeCache = (CompositeData) mbs
                    .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=Code Cache"), "Usage");
            CompositeData edenSpace = null;
            try {
                edenSpace = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=Eden Space"), "Usage");
            } catch (Exception ex) {
                edenSpace = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=Par Eden Space"), "Usage");

            }
            CompositeData survivorSpace = null;
            try {
                survivorSpace = (CompositeData) mbs
                        .getAttribute(new ObjectName("java.lang:type=MemoryPool,name=Survivor Space"), "Usage");
            } catch (Exception ex) {
                survivorSpace = (CompositeData) mbs.getAttribute(
                        new ObjectName("java.lang:type=MemoryPool,name=Par Survivor Space"), "Usage");
            }
            long permGenUsed = Long.parseLong(String.valueOf(permGen.get("used")));
            long codeCacheUsed = Long.parseLong(String.valueOf(codeCache.get("used")));
            long edenSpaceUsed = Long.parseLong(String.valueOf(edenSpace.get("used")));
            long tenuredGenUsed = Long.parseLong(String.valueOf(tenuredGen.get("used")));
            long survivorSpaceUsed = Long.parseLong(String.valueOf(survivorSpace.get("used")));

            LOG.info("Memory Stats after startup\n" + "\tPermgen Used " + permGenUsed / (ONEM) + " MB\n"
                    + "\tCode Cache Used " + codeCacheUsed / (ONEM) + " MB\n" + "\tEden Used "
                    + edenSpaceUsed / (ONEM) + " MB\n" + "\tTenured Used " + tenuredGenUsed / (ONEM) + " MB\n"
                    + "\tSurvivour Used " + survivorSpaceUsed / (ONEM) + " MB");
        } catch (RuntimeException ex2) {
            LOG.info("Startup Memory Stats Not available ", ex2);
        } catch (Exception ex2) {
            LOG.info("Startup Memory Stats Not available ", ex2);
        }
        lifecycleEvent(CommonLifecycleEvent.START);
        lifecycleEvent(CommonLifecycleEvent.AFTER_START);
        loadTime = System.currentTimeMillis() - start;

    } catch (Throwable ex) {
        LOG.error("Failed to start Component Lifecycle ", ex);
        throw new Error("Failed to start Component Lifecycle ", ex);
    }
    LOG.info("============END of LIFECYCLE STARTUP===============================");

}

From source file:org.jolokia.jmx.JolokiaMBeanServerTest.java

@Test
public void withModelMBean() throws MBeanException, InvalidTargetObjectTypeException, InstanceNotFoundException,
        InstanceAlreadyExistsException, NotCompliantMBeanException, MalformedObjectNameException,
        NoSuchMethodException, IntrospectionException {

    RequiredModelMBean modelMBean = new RequiredModelMBean();

    ModelMBeanInfo mbi = new ModelMBeanInfoSupport(JsonAnnoPlainTest.class.getName(), "JsonMBean Test",
            new ModelMBeanAttributeInfo[] { new ModelMBeanAttributeInfo("DeepDive", "description",
                    JsonAnnoPlainTest.class.getDeclaredMethod("getDeepDive"), null) },
            new ModelMBeanConstructorInfo[] {}, new ModelMBeanOperationInfo[] {},
            new ModelMBeanNotificationInfo[] {});
    modelMBean.setModelMBeanInfo(mbi);//  www .j  av a  2s  .c  om
    modelMBean.setManagedResource(new JsonAnnoPlainTest(), "ObjectReference");

    JolokiaMBeanServer server = new JolokiaMBeanServer();

    ObjectName oName = new ObjectName("test:type=jsonMBean");

    server.registerMBean(modelMBean, oName);
    MBeanServer plattformServer = ManagementFactory.getPlatformMBeanServer();

    Assert.assertTrue(plattformServer.isRegistered(oName));
    Assert.assertTrue(server.isRegistered(oName));

    server.unregisterMBean(oName);
    Assert.assertFalse(plattformServer.isRegistered(oName));
    Assert.assertFalse(server.isRegistered(oName));

}