Search in sources :

Example 61 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project tomcat by apache.

the class ManagedBean method getMBeanInfo.

/**
     * Create and return a <code>ModelMBeanInfo</code> object that
     * describes this entire managed bean.
     * @return the MBean info
     */
MBeanInfo getMBeanInfo() {
    // Return our cached information (if any)
    mBeanInfoLock.readLock().lock();
    try {
        if (info != null) {
            return info;
        }
    } finally {
        mBeanInfoLock.readLock().unlock();
    }
    mBeanInfoLock.writeLock().lock();
    try {
        if (info == null) {
            // Create subordinate information descriptors as required
            AttributeInfo[] attrs = getAttributes();
            MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[attrs.length];
            for (int i = 0; i < attrs.length; i++) attributes[i] = attrs[i].createAttributeInfo();
            OperationInfo[] opers = getOperations();
            MBeanOperationInfo[] operations = new MBeanOperationInfo[opers.length];
            for (int i = 0; i < opers.length; i++) operations[i] = opers[i].createOperationInfo();
            NotificationInfo[] notifs = getNotifications();
            MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[notifs.length];
            for (int i = 0; i < notifs.length; i++) notifications[i] = notifs[i].createNotificationInfo();
            // Construct and return a new ModelMBeanInfo object
            info = new MBeanInfo(getClassName(), getDescription(), attributes, new MBeanConstructorInfo[] {}, operations, notifications);
        }
        return info;
    } finally {
        mBeanInfoLock.writeLock().unlock();
    }
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo)

Example 62 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project tomcat by apache.

the class MBeanDumper method dumpBeans.

/**
     * The following code to dump MBeans has been copied from JMXProxyServlet.
     * @param mbeanServer the MBean server
     * @param names a set of object names for which to dump the info
     * @return a string representation of the MBeans
     */
public static String dumpBeans(MBeanServer mbeanServer, Set<ObjectName> names) {
    StringBuilder buf = new StringBuilder();
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        buf.append("Name: ");
        buf.append(oname.toString());
        buf.append(CRLF);
        try {
            MBeanInfo minfo = mbeanServer.getMBeanInfo(oname);
            // can't be null - I think
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mbeanServer.getAttribute(oname, "modelerType");
            }
            buf.append("modelerType: ");
            buf.append(code);
            buf.append(CRLF);
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            Object value = null;
            for (int i = 0; i < attrs.length; i++) {
                if (!attrs[i].isReadable())
                    continue;
                String attName = attrs[i].getName();
                if ("modelerType".equals(attName))
                    continue;
                if (attName.indexOf('=') >= 0 || attName.indexOf(':') >= 0 || attName.indexOf(' ') >= 0) {
                    continue;
                }
                try {
                    value = mbeanServer.getAttribute(oname, attName);
                } catch (JMRuntimeException rme) {
                    Throwable cause = rme.getCause();
                    if (cause instanceof UnsupportedOperationException) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error getting attribute " + oname + " " + attName, rme);
                        }
                    } else if (cause instanceof NullPointerException) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error getting attribute " + oname + " " + attName, rme);
                        }
                    } else {
                        log.error("Error getting attribute " + oname + " " + attName, rme);
                    }
                    continue;
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    log.error("Error getting attribute " + oname + " " + attName, t);
                    continue;
                }
                if (value == null)
                    continue;
                String valueString;
                try {
                    Class<?> c = value.getClass();
                    if (c.isArray()) {
                        int len = Array.getLength(value);
                        StringBuilder sb = new StringBuilder("Array[" + c.getComponentType().getName() + "] of length " + len);
                        if (len > 0) {
                            sb.append(CRLF);
                        }
                        for (int j = 0; j < len; j++) {
                            sb.append("\t");
                            Object item = Array.get(value, j);
                            if (item == null) {
                                sb.append("NULL VALUE");
                            } else {
                                try {
                                    sb.append(escape(item.toString()));
                                } catch (Throwable t) {
                                    ExceptionUtils.handleThrowable(t);
                                    sb.append("NON-STRINGABLE VALUE");
                                }
                            }
                            if (j < len - 1) {
                                sb.append(CRLF);
                            }
                        }
                        valueString = sb.toString();
                    } else {
                        valueString = escape(value.toString());
                    }
                    buf.append(attName);
                    buf.append(": ");
                    buf.append(valueString);
                    buf.append(CRLF);
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                }
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        }
        buf.append(CRLF);
    }
    return buf.toString();
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) JMRuntimeException(javax.management.JMRuntimeException)

Example 63 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jvm-tools by aragozin.

the class MBeanHelper method get.

public String get(ObjectName bean, String attr) throws Exception {
    MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
    MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
    if (ai == null) {
        throw new IllegalArgumentException("No such attribute '" + attr + "'");
    }
    if (!ai.isReadable()) {
        throw new IllegalArgumentException("Attribute '" + attr + "' is write-only");
    }
    Object v = mserver.getAttribute(bean, attr);
    String type = ai.getType();
    String text = format(v, type);
    return text;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 64 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jvm-tools by aragozin.

the class MBeanHelper method set.

public void set(ObjectName bean, String attr, String value) throws Exception {
    MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
    MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
    if (ai == null) {
        throw new IllegalArgumentException("No such attribute '" + attr + "'");
    }
    if (!ai.isWritable()) {
        throw new IllegalArgumentException("Attribute '" + attr + "' is not writeable");
    }
    String type = ai.getType();
    Object ov = convert(value, type);
    mserver.setAttribute(bean, new Attribute(attr, ov));
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 65 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jcollectd by collectd.

the class CollectdMBean method getAttributeInfo.

protected MBeanAttributeInfo[] getAttributeInfo() {
    MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[_metrics.size()];
    int i = 0;
    for (String name : _metrics.keySet()) {
        attrs[i++] = new MBeanAttributeInfo(name, getAttributeType(name), getAttributeDescription(name), // isReadable
        true, // isWritable
        false, // isIs
        false);
    }
    return attrs;
}
Also used : MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Aggregations

MBeanAttributeInfo (javax.management.MBeanAttributeInfo)106 MBeanInfo (javax.management.MBeanInfo)75 ObjectName (javax.management.ObjectName)45 MBeanOperationInfo (javax.management.MBeanOperationInfo)24 Test (org.junit.Test)21 MBeanServer (javax.management.MBeanServer)15 ArrayList (java.util.ArrayList)13 AttributeNotFoundException (javax.management.AttributeNotFoundException)12 ReflectionException (javax.management.ReflectionException)12 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)11 IOException (java.io.IOException)10 AttributeList (javax.management.AttributeList)10 Attribute (javax.management.Attribute)9 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 IntrospectionException (javax.management.IntrospectionException)9 MBeanParameterInfo (javax.management.MBeanParameterInfo)9 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)9 HashMap (java.util.HashMap)8 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7 MBeanException (javax.management.MBeanException)7