Example usage for javax.management JMRuntimeException getCause

List of usage examples for javax.management JMRuntimeException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.apache.catalina.mbeans.MBeanDumper.java

/**
 * The following code to dump MBeans has been copied from JMXProxyServlet.
 *
 *//*from www  .  ja  va  2 s  .c o m*/
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();

}