Search in sources :

Example 6 with ExceptionType

use of org.apache.openejb.core.ExceptionType in project tomee by apache.

the class MdbContainer method _invoke.

private Object _invoke(final Object instance, final Method runMethod, final Object[] args, final BeanContext beanContext, final InterfaceType interfaceType, final MdbCallContext mdbCallContext) throws SystemException, ApplicationException {
    final Object returnValue;
    try {
        final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
        final InterceptorStack interceptorStack = new InterceptorStack(((Instance) instance).bean, runMethod, interfaceType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, ((Instance) instance).interceptors);
        returnValue = interceptorStack.invoke(args);
        return returnValue;
    } catch (Throwable e) {
        // unwrap invocation target exception
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        //  Any exception thrown by reflection; not by the enterprise bean. Possible
        //  Exceptions are:
        //    IllegalAccessException - if the underlying method is inaccessible.
        //    IllegalArgumentException - if the number of actual and formal parameters differ, or if an unwrapping conversion fails.
        //    NullPointerException - if the specified object is null and the method is an instance method.
        //    ExceptionInInitializerError - if the initialization provoked by this method fails.
        final ExceptionType type = beanContext.getExceptionType(e);
        if (type == ExceptionType.SYSTEM) {
            //
            /// System Exception ****************************
            handleSystemException(mdbCallContext.txPolicy, e, ThreadContext.getThreadContext());
        } else {
            //
            // Application Exception ***********************
            handleApplicationException(mdbCallContext.txPolicy, e, false);
        }
    }
    throw new AssertionError("Should not get here");
}
Also used : ExceptionType(org.apache.openejb.core.ExceptionType) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with ExceptionType

use of org.apache.openejb.core.ExceptionType in project tomee by apache.

the class ManagedContainer method handleException.

private void handleException(final ThreadContext callContext, final TransactionPolicy txPolicy, final Throwable e) throws ApplicationException {
    if (e instanceof ApplicationException) {
        throw (ApplicationException) e;
    }
    final ExceptionType type = callContext.getBeanContext().getExceptionType(e);
    if (type == ExceptionType.SYSTEM) {
        discardInstance(callContext);
        EjbTransactionUtil.handleSystemException(txPolicy, e, callContext);
    } else {
        EjbTransactionUtil.handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
    }
}
Also used : ExceptionType(org.apache.openejb.core.ExceptionType) ApplicationException(org.apache.openejb.ApplicationException)

Example 8 with ExceptionType

use of org.apache.openejb.core.ExceptionType in project tomee by apache.

the class BeanContext method getExceptionType.

public ExceptionType getExceptionType(final Throwable e) {
    // Errors are always system exceptions
    if (!(e instanceof Exception)) {
        return ExceptionType.SYSTEM;
    }
    // check the registered app exceptions
    Class<?> exceptionClass = e.getClass();
    boolean inherited = false;
    while (exceptionClass != Object.class) {
        final ExceptionType type = exceptions.get(exceptionClass);
        if (type == ExceptionType.APPLICATION || type == ExceptionType.APPLICATION_ROLLBACK) {
            return type;
        }
        if (type != null) {
            if (inherited) {
                return ExceptionType.SYSTEM;
            }
            if (type == ExceptionType.APPLICATION_NOT_INHERITED) {
                return ExceptionType.APPLICATION;
            }
            return ExceptionType.APPLICATION_ROLLBACK;
        }
        exceptionClass = exceptionClass.getSuperclass();
        inherited = true;
    }
    // Unregistered - runtime exceptions are system exception and the rest are application exceptions
    final Class<? extends Throwable> eClass = e.getClass();
    final ApplicationException applicationException = eClass.getAnnotation(ApplicationException.class);
    if (applicationException != null) {
        addApplicationException(eClass, applicationException.rollback(), applicationException.inherited());
        return getExceptionType(e);
    }
    if (e instanceof RuntimeException) {
        return ExceptionType.SYSTEM;
    }
    return ExceptionType.APPLICATION;
}
Also used : ExceptionType(org.apache.openejb.core.ExceptionType) ApplicationException(javax.ejb.ApplicationException) ApplicationException(javax.ejb.ApplicationException) ConstructionException(org.apache.xbean.recipe.ConstructionException)

Example 9 with ExceptionType

use of org.apache.openejb.core.ExceptionType in project tomee by apache.

the class CmpContainer method createEJBObject.

private ProxyInfo createEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    final EntityBean bean;
    Object primaryKey = null;
    try {
        // Obtain a bean instance from the method ready pool
        bean = createNewInstance(callContext);
        // set the entity context
        setEntityContext(bean);
        // Obtain the proper ejbCreate() method
        final Method ejbCreateMethod = beanContext.getMatchingBeanMethod(callMethod);
        // Set current operation for allowed operations
        callContext.setCurrentOperation(Operation.CREATE);
        // Invoke the proper ejbCreate() method on the instance
        ejbCreateMethod.invoke(bean, args);
        // create the new bean
        primaryKey = cmpEngine.createBean(bean, callContext);
        // determine post create callback method
        final Method ejbPostCreateMethod = beanContext.getMatchingPostCreateMethod(ejbCreateMethod);
        // create a new context containing the pk for the post create call
        final ThreadContext postCreateContext = new ThreadContext(beanContext, primaryKey);
        postCreateContext.setCurrentOperation(Operation.POST_CREATE);
        final ThreadContext oldContext = ThreadContext.enter(postCreateContext);
        try {
            // Invoke the ejbPostCreate method on the bean instance
            ejbPostCreateMethod.invoke(bean, args);
        // According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...)
        // method executes in the same transaction context as the previous ejbCreate(...) method."
        //
        // The bean is first insterted using db.create( ) and then after ejbPostCreate( ) its
        // updated using db.update(). This protocol allows for visablity of the bean after ejbCreate
        // within the current trasnaction.
        } finally {
            ThreadContext.exit(oldContext);
        }
        // when there is not transaction, merge the data from the bean back into the cmp engine
        cmpEngine.storeBeanIfNoTx(callContext, bean);
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final ExceptionType type = callContext.getBeanContext().getExceptionType(e);
        if (type == ExceptionType.SYSTEM) {
            /* System Exception ****************************/
            handleSystemException(txPolicy, e, callContext);
        } else {
            /* Application Exception ***********************/
            handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
        }
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    return new ProxyInfo(beanContext, primaryKey);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) ProxyInfo(org.apache.openejb.ProxyInfo) EntityBean(javax.ejb.EntityBean) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with ExceptionType

use of org.apache.openejb.core.ExceptionType in project tomee by apache.

the class CmpContainer method homeMethod.

private Object homeMethod(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    final EntityBean bean;
    Object returnValue = null;
    try {
        /*
              Obtain a bean instance from the method ready pool
            */
        bean = createNewInstance(callContext);
        // set the entity context
        setEntityContext(bean);
        try {
            callContext.setCurrentOperation(Operation.HOME);
            final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
            try {
                returnValue = runMethod.invoke(bean, args);
            } catch (final IllegalArgumentException e) {
                System.out.println("********************************************************");
                System.out.println("callMethod = " + callMethod);
                System.out.println("runMethod = " + runMethod);
                System.out.println("bean = " + bean.getClass().getName());
                throw e;
            }
        } finally {
            unsetEntityContext(bean);
        }
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final ExceptionType type = callContext.getBeanContext().getExceptionType(e);
        if (type == ExceptionType.SYSTEM) {
            /* System Exception ****************************/
            handleSystemException(txPolicy, e, callContext);
        } else {
            /* Application Exception ***********************/
            handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
        }
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) EntityBean(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ExceptionType (org.apache.openejb.core.ExceptionType)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 BeanContext (org.apache.openejb.BeanContext)5 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)5 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)5 EJBLocalObject (javax.ejb.EJBLocalObject)4 EJBObject (javax.ejb.EJBObject)4 EntityBean (javax.ejb.EntityBean)3 ApplicationException (org.apache.openejb.ApplicationException)3 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)3 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)3 Method (java.lang.reflect.Method)2 SystemException (org.apache.openejb.SystemException)2 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)2 NoSuchObjectException (java.rmi.NoSuchObjectException)1 Lock (java.util.concurrent.locks.Lock)1 ApplicationException (javax.ejb.ApplicationException)1 ProxyInfo (org.apache.openejb.ProxyInfo)1 Operation (org.apache.openejb.core.Operation)1 ThreadContext (org.apache.openejb.core.ThreadContext)1