Example usage for javax.management.modelmbean ModelMBean setManagedResource

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

Introduction

In this page you can find the example usage for javax.management.modelmbean ModelMBean 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:com.brienwheeler.lib.jmx.MBeanRegistrationSupport.java

public static void registerMBean(Object mbean) {
    try {//from   ww  w . ja  va  2s .  c  om
        ObjectNamingStrategy namingStrategy = new IdentityNamingStrategy();
        ObjectName objectName = namingStrategy.getObjectName(mbean, null);

        MBeanRegistrationSupport registrar = new MBeanRegistrationSupport();
        registrar.setServer(JmxUtils.locateMBeanServer());

        // if item qualifies as MBean, export it directly
        if (JmxUtils.isMBean(mbean.getClass())) {
            registrar.doRegister(mbean, objectName);
            return;
        }

        // assemble MBean info (from annotations by default)
        ModelMBean modelMBean = new RequiredModelMBean();
        modelMBean.setManagedResource(mbean, MR_TYPE_OBJECT_REFERENCE);
        MBeanInfoAssembler mBeanInfoAssembler = new MetadataMBeanInfoAssembler(
                new AnnotationJmxAttributeSource());
        modelMBean.setModelMBeanInfo(mBeanInfoAssembler.getMBeanInfo(mbean, objectName.getCanonicalName()));
        registrar.doRegister(modelMBean, objectName);
    } catch (Exception e) {
        throw new JmxRegisterException("error registering MBean", e);
    }
}

From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java

private static ModelMBean createModelMBean(Object o) {
    try {/*from  ww  w  .j  a va  2s  .  co m*/
        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 RuntimeException(e);
    } catch (InvalidTargetObjectTypeException e) {
        throw new RuntimeException(e);
    } catch (InstanceNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java

/** Create MBean for Object.
 * Attempts to create an MBean for the object by searching the
 * package and class name space.  For example an object of the
 * type <PRE>//w ww  . j a va  2s  .  c  o m
 *   class com.acme.MyClass extends com.acme.util.BaseClass
 * </PRE>
 * Then this method would look for the following
 * classes:<UL>
 * <LI>com.acme.MyClassMBean
 * <LI>com.acme.jmx.MyClassMBean
 * <LI>com.acme.util.BaseClassMBean
 * <LI>com.acme.util.jmx.BaseClassMBean
 * </UL>
 * @param o The object
 * @return A new instance of an MBean for the object or null.
 */
public static ModelMBean mbeanFor(Object o) {
    try {
        Class oClass = o.getClass();
        ClassLoader loader = oClass.getClassLoader();

        ModelMBean mbean = null;
        boolean jmx = false;
        Class[] interfaces = null;
        int i = 0;

        while (mbean == null && oClass != null) {
            Class focus = interfaces == null ? oClass : interfaces[i];
            String pName = focus.getPackage().getName();
            String cName = focus.getName().substring(pName.length() + 1);
            String mName = pName + (jmx ? ".jmx." : ".") + cName + "MBean";

            try {
                Class mClass = loader.loadClass(mName);
                if (log.isTraceEnabled())
                    log.trace("mbeanFor " + o + " mClass=" + mClass);
                mbean = (ModelMBean) mClass.newInstance();
                mbean.setManagedResource(o, "objectReference");
                if (log.isDebugEnabled())
                    log.debug("mbeanFor " + o + " is " + mbean);
                return mbean;
            } catch (ClassNotFoundException e) {
                if (e.toString().endsWith("MBean")) {
                    if (log.isTraceEnabled())
                        log.trace(e.toString());
                } else
                    log.warn(LogSupport.EXCEPTION, e);
            } catch (Error e) {
                log.warn(LogSupport.EXCEPTION, e);
                mbean = null;
            } catch (Exception e) {
                log.warn(LogSupport.EXCEPTION, e);
                mbean = null;
            }

            if (jmx) {
                if (interfaces != null) {
                    i++;
                    if (i >= interfaces.length) {
                        interfaces = null;
                        oClass = oClass.getSuperclass();
                    }
                } else {
                    interfaces = oClass.getInterfaces();
                    i = 0;
                    if (interfaces == null || interfaces.length == 0) {
                        interfaces = null;
                        oClass = oClass.getSuperclass();
                    }
                }
            }
            jmx = !jmx;
        }
    } catch (Exception e) {
        LogSupport.ignore(log, e);
    }
    return null;
}

From source file:org.springframework.jmx.export.MBeanExporter.java

/**
 * Registers a plain bean as MBean with the <code>MBeanServer</code>.
 * The management interface for the bean is created by the configured
 * <code>MBeanInfoAssembler</code>.
 * @param bean the bean instance to register
 * @param beanKey the key associated with this bean in the beans map
 * @return the <code>ObjectName</code> under which the bean was registered
 * with the <code>MBeanServer</code>
 * @throws JMException in case of an error in the underlying JMX infrastructure
 * @throws InvalidTargetObjectTypeException an error in the definition of the MBean resource
 *//*from   w w  w .j a  v a 2  s  .c  o  m*/
private ObjectName registerSimpleBean(Object bean, String beanKey)
        throws JMException, InvalidTargetObjectTypeException {

    ObjectName objectName = getObjectName(bean, beanKey);
    if (logger.isDebugEnabled()) {
        logger.debug("Registering and assembling MBean [" + objectName + "]");
    }

    ModelMBean mbean = createModelMBean();
    mbean.setModelMBeanInfo(getMBeanInfo(bean, beanKey));
    mbean.setManagedResource(bean, MR_TYPE_OBJECT_REFERENCE);

    return doRegister(mbean, objectName);
}

From source file:org.springframework.jmx.export.MBeanExporter.java

/**
 * Registers beans that are configured for lazy initialization with the
 * <code>MBeanServer<code> indirectly through a proxy.
 * @param beanName the name of the bean in the <code>BeanFactory</code>
 * @param beanKey the key associated with this bean in the beans map
 * @return the <code>ObjectName</code> under which the bean was registered
 * with the <code>MBeanServer</code>
 * @throws JMException an error in the underlying JMX infrastructure
 * @throws InvalidTargetObjectTypeException an error in the definition of the MBean resource
 *//*from w  w w  .  j a v  a  2s.  c om*/
private ObjectName registerLazyInit(String beanName, String beanKey)
        throws JMException, InvalidTargetObjectTypeException {

    LazyInitTargetSource targetSource = new LazyInitTargetSource();
    targetSource.setTargetBeanName(beanName);
    targetSource.setBeanFactory(this.beanFactory);

    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTargetSource(targetSource);
    proxyFactory.setProxyTargetClass(true);
    proxyFactory.setFrozen(true);

    Object proxy = proxyFactory.getProxy();
    ObjectName objectName = getObjectName(proxy, beanKey);
    if (logger.isDebugEnabled()) {
        logger.debug("Registering lazy-init MBean [" + objectName + "]");
    }

    ModelMBean mbean = createModelMBean();
    mbean.setModelMBeanInfo(getMBeanInfo(proxy, beanKey));
    mbean.setManagedResource(proxy, MR_TYPE_OBJECT_REFERENCE);

    return doRegister(mbean, objectName);
}