Search in sources :

Example 1 with RuntimeMBeanException

use of javax.management.RuntimeMBeanException in project hbase by apache.

the class JSONBean method write.

/**
   * @param mBeanServer
   * @param qry
   * @param attribute
   * @param description
   * @return Return non-zero if failed to find bean. 0
   * @throws IOException
   */
private static int write(final JsonGenerator jg, final MBeanServer mBeanServer, ObjectName qry, String attribute, final boolean description) throws IOException {
    LOG.trace("Listing beans for " + qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    jg.writeArrayFieldStart("beans");
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        MBeanInfo minfo;
        String code = "";
        String descriptionStr = null;
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            if (description)
                descriptionStr = minfo.getDescription();
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    prs = attribute;
                    attributeinfo = mBeanServer.getAttribute(oname, prs);
                }
            } catch (RuntimeMBeanException e) {
                // so no need to log them as errors all the time.
                if (e.getCause() instanceof UnsupportedOperationException) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Getting attribute " + prs + " of " + oname + " threw " + e);
                    }
                } else {
                    LOG.error("Getting attribute " + prs + " of " + oname + " threw an exception", e);
                }
                return 0;
            } catch (AttributeNotFoundException e) {
                // If the modelerType attribute was not found, the class name is used
                // instead.
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (MBeanException e) {
                // The code inside the attribute getter threw an exception so log it,
                // and fall back on the class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (RuntimeException e) {
                // For some reason even with an MBeanException available to them
                // Runtime exceptionscan still find their way through, so treat them
                // the same as MBeanException
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (ReflectionException e) {
                // This happens when the code inside the JMX bean (setter?? from the
                // java docs) threw an exception, so log it and fall back on the
                // class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            }
        } catch (InstanceNotFoundException e) {
            //Ignored for some reason the bean was not found so don't output it
            continue;
        } catch (IntrospectionException e) {
            // This is an internal error, something odd happened with reflection so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        } catch (ReflectionException e) {
            // This happens when the code inside the JMX bean threw an exception, so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        }
        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        if (description && descriptionStr != null && descriptionStr.length() > 0) {
            jg.writeStringField("description", descriptionStr);
        }
        jg.writeStringField("modelerType", code);
        if (attribute != null && attributeinfo == null) {
            jg.writeStringField("result", "ERROR");
            jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
            jg.writeEndObject();
            jg.writeEndArray();
            jg.close();
            return -1;
        }
        if (attribute != null) {
            writeAttribute(jg, attribute, descriptionStr, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, mBeanServer, oname, description, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
    return 0;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Example 2 with RuntimeMBeanException

use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.

the class MBeanExceptionTest method main.

public static void main(String[] args) throws Exception {
    System.out.println("Test that if an MBean throws RuntimeException " + "it is wrapped in RuntimeMBeanException,");
    System.out.println("and if a Standard MBean throws Exception " + "it is wrapped in MBeanException");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Object standard = new Except();
    ObjectName standardName = new ObjectName(":name=Standard MBean");
    Object standardMBean = new StandardMBean(new Except(), ExceptMBean.class);
    ObjectName standardMBeanName = new ObjectName(":name=Instance of StandardMBean");
    Object dynamic = new DynamicExcept();
    ObjectName dynamicName = new ObjectName(":name=Dynamic MBean");
    mbs.registerMBean(standard, standardName);
    mbs.registerMBean(standardMBean, standardMBeanName);
    mbs.registerMBean(dynamic, dynamicName);
    int failures = 0;
    failures += test(mbs, standardName, true);
    failures += test(mbs, standardMBeanName, true);
    failures += test(mbs, dynamicName, false);
    final boolean[] booleans = { false, true };
    for (boolean runtimeX : booleans) {
        Class<? extends Exception> excC = runtimeX ? RuntimeMBeanException.class : MBeanException.class;
        String excS = runtimeX ? "a RuntimeMBeanException" : "an MBeanException";
        String mbsS = "a plain MBeanServer";
        System.out.println("Test that, with " + mbsS + ", " + excS + " is wrapped " + "in " + excS);
        // is wrapped in an MBeanException".
        try {
            mbs.createMBean(Except.class.getName(), new ObjectName(":name=Oops"), new Object[] { runtimeX }, new String[] { boolean.class.getName() });
            System.out.println("FAIL: createMBean succeeded but should not have");
            failures++;
        } catch (Exception e) {
            if (!excC.isInstance(e)) {
                System.out.println("FAIL: expected " + excC.getName() + " from " + "createMBean, got " + e);
                failures++;
            } else {
                Throwable cause = e.getCause();
                if (!excC.isInstance(cause)) {
                    System.out.println("FAIL: expected " + excC.getName() + " as cause of " + excC.getName() + ", got " + e);
                    failures++;
                } else
                    System.out.println("...ok");
            }
        }
    }
    if (failures == 0)
        System.out.println("Test passed");
    else {
        System.out.println("TEST FAILED: " + failures + " failure(s)");
        System.exit(1);
    }
}
Also used : StandardMBean(javax.management.StandardMBean) MBeanException(javax.management.MBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 3 with RuntimeMBeanException

use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.

the class MBeanExceptionTest method test.

private static int test(MBeanServer mbs, ObjectName name, boolean testChecked) throws Exception {
    System.out.println("--------" + name + "--------");
    int failures = 0;
    final String[] ops = { "getAttribute", "setAttribute", "invoke" };
    final int GET = 0, SET = 1, INVOKE = 2;
    final String[] targets = { "UncheckedException", "CheckedException" };
    final int UNCHECKED = 0, CHECKED = 1;
    for (int i = 0; i < ops.length; i++) {
        for (int j = 0; j < targets.length; j++) {
            if (j == CHECKED && !testChecked)
                continue;
            String target = targets[j];
            String what = ops[i] + "/" + target;
            System.out.println(what);
            try {
                switch(i) {
                    case GET:
                        mbs.getAttribute(name, target);
                        break;
                    case SET:
                        mbs.setAttribute(name, new Attribute(target, "x"));
                        break;
                    case INVOKE:
                        mbs.invoke(name, target, null, null);
                        break;
                    default:
                        throw new AssertionError();
                }
                System.out.println("failure: " + what + " returned!");
                failures++;
            } catch (RuntimeMBeanException e) {
                if (j == CHECKED) {
                    System.out.println("failure: RuntimeMBeanException " + "when checked expected: " + e);
                    failures++;
                } else {
                    Throwable cause = e.getCause();
                    if (cause == theUncheckedException)
                        System.out.println("ok: " + what);
                    else {
                        System.out.println("failure: " + what + " wrapped " + cause);
                        failures++;
                    }
                }
            } catch (MBeanException e) {
                if (j == UNCHECKED) {
                    System.out.println("failure: checked exception " + "when unchecked expected: " + e);
                    failures++;
                } else {
                    Throwable cause = e.getCause();
                    if (cause == theCheckedException)
                        System.out.println("ok: " + what);
                    else {
                        System.out.println("failure: " + what + " wrapped " + cause);
                        failures++;
                    }
                }
            } catch (Throwable t) {
                System.out.println("failure: " + what + " threw: " + t);
                while ((t = t.getCause()) != null) System.out.println("  ... " + t);
                failures++;
            }
        }
    }
    return failures;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) Attribute(javax.management.Attribute) MBeanException(javax.management.MBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException)

Example 4 with RuntimeMBeanException

use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.

the class MBeanInstantiator method instantiate.

/**
     * Instantiates an object given its class, the parameters and
     * signature of its constructor The call returns a reference to
     * the newly created object.
     */
public Object instantiate(Class<?> theClass, Object[] params, String[] signature, ClassLoader loader) throws ReflectionException, MBeanException {
    checkMBeanPermission(theClass, null, null, "instantiate");
    // Instantiate the new object
    // ------------------------------
    // ------------------------------
    final Class<?>[] tab;
    Object moi;
    try {
        // Build the signature of the method
        //
        ClassLoader aLoader = theClass.getClassLoader();
        // Build the signature of the method
        //
        tab = ((signature == null) ? null : findSignatureClasses(signature, aLoader));
    }// Exception IllegalArgumentException raised in Jdk1.1.8
     catch (IllegalArgumentException e) {
        throw new ReflectionException(e, "The constructor parameter classes could not be loaded");
    }
    // Query the metadata service to get the right constructor
    Constructor<?> cons = findConstructor(theClass, tab);
    if (cons == null) {
        throw new ReflectionException(new NoSuchMethodException("No such constructor"));
    }
    try {
        ReflectUtil.checkPackageAccess(theClass);
        ensureClassAccess(theClass);
        moi = cons.newInstance(params);
    } catch (NoSuchMethodError error) {
        throw new ReflectionException(new NoSuchMethodException("No such constructor found"), "No such constructor");
    } catch (InstantiationException e) {
        throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's constructor");
    } catch (IllegalAccessException e) {
        throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's constructor");
    } catch (InvocationTargetException e) {
        // Wrap the exception.
        Throwable th = e.getTargetException();
        if (th instanceof RuntimeException) {
            throw new RuntimeMBeanException((RuntimeException) th, "RuntimeException thrown in the MBean's constructor");
        } else if (th instanceof Error) {
            throw new RuntimeErrorException((Error) th, "Error thrown in the MBean's constructor");
        } else {
            throw new MBeanException((Exception) th, "Exception thrown in the MBean's constructor");
        }
    }
    return moi;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) ReflectionException(javax.management.ReflectionException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Example 5 with RuntimeMBeanException

use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.

the class MBeanInstantiator method instantiate.

/**
     * Instantiates an object given its class, using its empty constructor.
     * The call returns a reference to the newly created object.
     */
public Object instantiate(Class<?> theClass) throws ReflectionException, MBeanException {
    checkMBeanPermission(theClass, null, null, "instantiate");
    Object moi;
    // ------------------------------
    // ------------------------------
    Constructor<?> cons = findConstructor(theClass, null);
    if (cons == null) {
        throw new ReflectionException(new NoSuchMethodException("No such constructor"));
    }
    // Instantiate the new object
    try {
        ReflectUtil.checkPackageAccess(theClass);
        ensureClassAccess(theClass);
        moi = cons.newInstance();
    } catch (InvocationTargetException e) {
        // Wrap the exception.
        Throwable t = e.getTargetException();
        if (t instanceof RuntimeException) {
            throw new RuntimeMBeanException((RuntimeException) t, "RuntimeException thrown in the MBean's empty constructor");
        } else if (t instanceof Error) {
            throw new RuntimeErrorException((Error) t, "Error thrown in the MBean's empty constructor");
        } else {
            throw new MBeanException((Exception) t, "Exception thrown in the MBean's empty constructor");
        }
    } catch (NoSuchMethodError error) {
        throw new ReflectionException(new NoSuchMethodException("No constructor"), "No such constructor");
    } catch (InstantiationException e) {
        throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor");
    } catch (IllegalAccessException e) {
        throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor");
    } catch (IllegalArgumentException e) {
        throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor");
    }
    return moi;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) ReflectionException(javax.management.ReflectionException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Aggregations

RuntimeMBeanException (javax.management.RuntimeMBeanException)8 MBeanException (javax.management.MBeanException)5 MBeanInfo (javax.management.MBeanInfo)3 ObjectName (javax.management.ObjectName)3 ReflectionException (javax.management.ReflectionException)3 RuntimeErrorException (javax.management.RuntimeErrorException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)2 MBeanServer (javax.management.MBeanServer)2 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)2 Attribute (javax.management.Attribute)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 DynamicMBean (javax.management.DynamicMBean)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 IntrospectionException (javax.management.IntrospectionException)1 JMRuntimeException (javax.management.JMRuntimeException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 StandardMBean (javax.management.StandardMBean)1 LocalMBeanServer (org.apache.openejb.monitoring.LocalMBeanServer)1 Test (org.junit.Test)1