Search in sources :

Example 1 with RuntimeErrorException

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

the class BaseModelMBean method setAttribute.

/**
     * Set the value of a specific attribute of this MBean.
     *
     * @param attribute The identification of the attribute to be set
     *  and the new value
     *
     * @exception AttributeNotFoundException if this attribute is not
     *  supported by this MBean
     * @exception MBeanException if the initializer of an object
     *  throws an exception
     * @exception ReflectionException if a Java reflection exception
     *  occurs when invoking the getter
     */
@Override
public void setAttribute(Attribute attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    if (log.isDebugEnabled())
        log.debug("Setting attribute " + this + " " + attribute);
    if ((resource instanceof DynamicMBean) && !(resource instanceof BaseModelMBean)) {
        try {
            ((DynamicMBean) resource).setAttribute(attribute);
        } catch (InvalidAttributeValueException e) {
            throw new MBeanException(e);
        }
        return;
    }
    // Validate the input parameters
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute is null"), "Attribute is null");
    String name = attribute.getName();
    Object value = attribute.getValue();
    if (name == null)
        throw new RuntimeOperationsException(new IllegalArgumentException("Attribute name is null"), "Attribute name is null");
    Object oldValue = null;
    //if( getAttMap.get(name) != null )
    //    oldValue=getAttribute( name );
    Method m = managedBean.getSetter(name, this, resource);
    try {
        if (m.getDeclaringClass().isAssignableFrom(this.getClass())) {
            m.invoke(this, new Object[] { value });
        } else {
            m.invoke(resource, new Object[] { value });
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t == null)
            t = e;
        if (t instanceof RuntimeException)
            throw new RuntimeOperationsException((RuntimeException) t, "Exception invoking method " + name);
        else if (t instanceof Error)
            throw new RuntimeErrorException((Error) t, "Error invoking method " + name);
        else
            throw new MBeanException(e, "Exception invoking method " + name);
    } catch (Exception e) {
        log.error("Exception invoking method " + name, e);
        throw new MBeanException(e, "Exception invoking method " + name);
    }
    try {
        sendAttributeChangeNotification(new Attribute(name, oldValue), attribute);
    } catch (Exception ex) {
        log.error("Error sending notification " + name, ex);
    }
//attributes.put( name, value );
//        if( source != null ) {
//            // this mbean is associated with a source - maybe we want to persist
//            source.updateField(oname, name, value);
//        }
}
Also used : DynamicMBean(javax.management.DynamicMBean) RuntimeErrorException(javax.management.RuntimeErrorException) Attribute(javax.management.Attribute) Method(java.lang.reflect.Method) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 2 with RuntimeErrorException

use of javax.management.RuntimeErrorException in project ninja by ninjaframework.

the class NinjaDefault method readNinjaVersion.

/**
     * Simply reads a property resource file that contains the version of this
     * Ninja build. Helps to identify the Ninja version currently running.
     * 
     * @return The version of Ninja. Eg. "1.6-SNAPSHOT" while developing of "1.6" when released.
     */
private final String readNinjaVersion() {
    // location of the properties file
    String LOCATION_OF_NINJA_BUILTIN_PROPERTIES = "ninja/ninja-builtin.properties";
    // and the key inside the properties file.
    String NINJA_VERSION_PROPERTY_KEY = "ninja.version";
    String ninjaVersion;
    try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(LOCATION_OF_NINJA_BUILTIN_PROPERTIES)) {
        Properties prop = new Properties();
        prop.load(stream);
        ninjaVersion = prop.getProperty(NINJA_VERSION_PROPERTY_KEY);
    } catch (Exception e) {
        //this should not happen. Never.
        throw new RuntimeErrorException(new Error("Something is wrong with your build. Cannot find resource " + LOCATION_OF_NINJA_BUILTIN_PROPERTIES));
    }
    return ninjaVersion;
}
Also used : RuntimeErrorException(javax.management.RuntimeErrorException) InputStream(java.io.InputStream) DiagnosticError(ninja.diagnostics.DiagnosticError) Properties(java.util.Properties) NinjaProperties(ninja.utils.NinjaProperties) RenderingException(ninja.exceptions.RenderingException) RuntimeErrorException(javax.management.RuntimeErrorException) BadRequestException(ninja.exceptions.BadRequestException)

Example 3 with RuntimeErrorException

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

the class MXBeanExceptionHandlingTest method run.

public void run(Map<String, Object> args) {
    System.out.println("MXBeanExceptionHandlingTest::run: Start");
    int errorCount = 0;
    try {
        parseArgs(args);
        notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // ----
        System.out.println("Add me as notification listener");
        mbsc.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, null, null);
        System.out.println("---- OK\n");
        // ----
        System.out.println("Create and register the MBean");
        ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi");
        mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
        System.out.println("---- OK\n");
        // ----
        System.out.println("Call method throwException on our MXBean");
        try {
            mbsc.invoke(objName, "throwException", null, null);
            errorCount++;
            System.out.println("(ERROR) Did not get awaited MBeanException");
        } catch (MBeanException mbe) {
            System.out.println("(OK) Got awaited MBeanException");
            Throwable cause = mbe.getCause();
            if (cause instanceof java.lang.Exception) {
                System.out.println("(OK) Cause is of the right class");
                String mess = cause.getMessage();
                if (mess.equals(Basic.EXCEPTION_MESSAGE)) {
                    System.out.println("(OK) Cause message is fine");
                } else {
                    errorCount++;
                    System.out.println("(ERROR) Cause has message " + cause.getMessage() + " as we expect " + Basic.EXCEPTION_MESSAGE);
                }
            } else {
                errorCount++;
                System.out.println("(ERROR) Cause is of  class " + cause.getClass().getName() + " as we expect java.lang.Exception");
            }
        } catch (Exception e) {
            errorCount++;
            System.out.println("(ERROR) Did not get awaited MBeanException but " + e);
            Utils.printThrowable(e, true);
        }
        System.out.println("---- DONE\n");
        // ----
        System.out.println("Call method throwError on our MXBean");
        try {
            mbsc.invoke(objName, "throwError", null, null);
            errorCount++;
            System.out.println("(ERROR) Did not get awaited RuntimeErrorException");
        } catch (RuntimeErrorException ree) {
            System.out.println("(OK) Got awaited RuntimeErrorException");
            Throwable cause = ree.getCause();
            if (cause instanceof java.lang.InternalError) {
                System.out.println("(OK) Cause is of the right class");
                String mess = cause.getMessage();
                if (mess.equals(Basic.EXCEPTION_MESSAGE)) {
                    System.out.println("(OK) Cause message is fine");
                } else {
                    errorCount++;
                    System.out.println("(ERROR) Cause has message " + cause.getMessage() + " as we expect " + Basic.EXCEPTION_MESSAGE);
                }
            } else {
                errorCount++;
                System.out.println("(ERROR) Cause is of  class " + cause.getClass().getName() + " as we expect java.lang.InternalError");
            }
        } catch (Exception e) {
            errorCount++;
            System.out.println("(ERROR) Did not get awaited RuntimeErrorException but " + e);
            Utils.printThrowable(e, true);
        }
        System.out.println("---- DONE\n");
        // ----
        System.out.println("Unregister the MBean");
        mbsc.unregisterMBean(objName);
        System.out.println("---- OK\n");
        Thread.sleep(timeForNotificationInSeconds * 1000);
        int numOfReceivedNotif = notifList.size();
        if (numOfReceivedNotif == numOfNotifications) {
            System.out.println("(OK) We received " + numOfNotifications + " Notifications");
        } else {
            errorCount++;
            System.out.println("(ERROR) We received " + numOfReceivedNotif + " Notifications in place of " + numOfNotifications);
        }
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException(e);
    }
    if (errorCount == 0) {
        System.out.println("MXBeanExceptionHandlingTest::run: Done without any error");
    } else {
        System.out.println("MXBeanExceptionHandlingTest::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) RuntimeErrorException(javax.management.RuntimeErrorException) Notification(javax.management.Notification) RuntimeErrorException(javax.management.RuntimeErrorException) MBeanException(javax.management.MBeanException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanException(javax.management.MBeanException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 4 with RuntimeErrorException

use of javax.management.RuntimeErrorException 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 RuntimeErrorException

use of javax.management.RuntimeErrorException 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

RuntimeErrorException (javax.management.RuntimeErrorException)10 MBeanException (javax.management.MBeanException)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ReflectionException (javax.management.ReflectionException)7 Method (java.lang.reflect.Method)4 AttributeNotFoundException (javax.management.AttributeNotFoundException)4 DynamicMBean (javax.management.DynamicMBean)4 InstanceNotFoundException (javax.management.InstanceNotFoundException)4 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)4 ListenerNotFoundException (javax.management.ListenerNotFoundException)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 RuntimeMBeanException (javax.management.RuntimeMBeanException)3 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)3 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)2 ServiceNotFoundException (javax.management.ServiceNotFoundException)2 InputStream (java.io.InputStream)1 AccessControlContext (java.security.AccessControlContext)1 Properties (java.util.Properties)1 Attribute (javax.management.Attribute)1 JMRuntimeException (javax.management.JMRuntimeException)1