Example usage for javax.management MBeanAttributeInfo MBeanAttributeInfo

List of usage examples for javax.management MBeanAttributeInfo MBeanAttributeInfo

Introduction

In this page you can find the example usage for javax.management MBeanAttributeInfo MBeanAttributeInfo.

Prototype

public MBeanAttributeInfo(String name, String description, Method getter, Method setter)
        throws IntrospectionException 

Source Link

Document

This constructor takes the name of a simple attribute, and Method objects for reading and writing the attribute.

Usage

From source file:com.ecyrd.management.SimpleMBean.java

/**
 *  Create a new SimpleMBean//w ww  .  j  a  v  a  2  s.com
 *  
 *  @throws NotCompliantMBeanException {@inheritDoc}
 */
protected SimpleMBean() throws NotCompliantMBeanException {
    //
    //  Create attributes
    //
    String[] attlist = getAttributeNames();
    MBeanAttributeInfo[] attributes = null;

    if (attlist != null) {
        attributes = new MBeanAttributeInfo[attlist.length];

        for (int i = 0; i < attlist.length; i++) {
            String name = attlist[i];
            name = StringUtils.capitalize(name);
            Method getter = findGetterSetter(getClass(), "get" + name, null);

            if (getter == null)
                getter = findGetterSetter(getClass(), "is" + name, null);

            Method setter = null;

            if (getter != null) {
                setter = findGetterSetter(getClass(), "set" + name, getter.getReturnType());
            }

            //
            //  Check, if there's a description available
            //
            Method descriptor = findGetterSetter(getClass(), "get" + name + "Description", null);
            String description = "";

            if (descriptor != null) {
                try {
                    description = (String) descriptor.invoke(this, (Object[]) null);
                } catch (Exception e) {
                    description = "Exception: " + e.getMessage();
                }
            }

            MBeanAttributeInfo info;
            try {
                info = new MBeanAttributeInfo(attlist[i], description, getter, setter);
            } catch (IntrospectionException e) {
                throw new NotCompliantMBeanException(e.getMessage());
            }

            attributes[i] = info;
        }
    }

    //
    //  Create operations.
    //
    String[] oplist = getMethodNames();
    MBeanOperationInfo[] operations = new MBeanOperationInfo[oplist.length];

    Method[] methods = getClass().getMethods();

    for (int i = 0; i < oplist.length; i++) {
        Method method = null;

        for (int m = 0; m < methods.length; m++) {
            if (methods[m].getName().equals(oplist[i])) {
                method = methods[m];
            }
        }

        if (method == null) {
            throw new NotCompliantMBeanException(
                    "Class declares method " + oplist[i] + ", yet does not implement it!");
        }

        MBeanOperationInfo info = new MBeanOperationInfo(method.getName(), method);

        operations[i] = info;
    }

    //
    //  Create the actual BeanInfo instance.
    //
    MBeanConstructorInfo[] constructors = null;
    MBeanNotificationInfo[] notifications = null;

    m_beanInfo = new MBeanInfo(getClass().getName(), getDescription(), attributes, constructors, operations,
            notifications);
}

From source file:org.apache.tomcat.util.mx.DynamicMBeanProxy.java

/**
 * @todo Find if the 'className' is the name of the MBean or
 *       the real class ( I suppose first )
 * @todo Read (optional) descriptions from a .properties, generated
 *       from source/* ww w. java 2  s  .  c o m*/
 * @todo Deal with constructors
 *       
 */
public MBeanInfo getMBeanInfo() {
    if (methods == null) {
        init();
    }
    try {
        MBeanAttributeInfo attributes[] = new MBeanAttributeInfo[attMap.size()];

        Enumeration en = attMap.keys();
        int i = 0;
        while (en.hasMoreElements()) {
            String name = (String) en.nextElement();
            attributes[i++] = new MBeanAttributeInfo(name, "Attribute " + name, (Method) getAttMap.get(name),
                    (Method) setAttMap.get(name));
        }

        MBeanOperationInfo operations[] = new MBeanOperationInfo[invokeAttMap.size()];

        en = invokeAttMap.keys();
        i = 0;
        while (en.hasMoreElements()) {
            String name = (String) en.nextElement();
            Method m = (Method) invokeAttMap.get(name);
            if (m != null && name != null) {
                operations[i++] = new MBeanOperationInfo(name, m);
            } else {
                System.out.println("Null arg " + name + " " + m);
            }
        }

        if (log.isDebugEnabled())
            log.debug(real.getClass().getName() + " getMBeanInfo()");

        return new MBeanInfo(real.getClass().getName(), /* ??? */
                "MBean for " + getName(), attributes, new MBeanConstructorInfo[0], operations,
                new MBeanNotificationInfo[0]);
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}