Search in sources :

Example 1 with ExceptionType

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

the class EntityContainer method handleException.

private void handleException(final TransactionPolicy txPolicy, Throwable e, final ThreadContext callContext, final EntityBean bean) throws OpenEJBException {
    final ExceptionType type;
    if (e instanceof InvocationTargetException) {
        e = ((InvocationTargetException) e).getTargetException();
        type = callContext.getBeanContext().getExceptionType(e);
    } else if (e instanceof ApplicationException) {
        e = ((ApplicationException) e).getRootCause();
        type = ExceptionType.APPLICATION;
    } else if (e instanceof SystemException) {
        e = ((SystemException) e).getRootCause();
        type = ExceptionType.SYSTEM;
    } else {
        type = ExceptionType.SYSTEM;
    }
    if (type == ExceptionType.SYSTEM) {
        // System Exception
        if (bean != null) {
            try {
                instanceManager.discardInstance(callContext, bean);
            } catch (final SystemException e1) {
                logger.error("The instance manager encountered an unkown system exception while trying to discard the entity instance with primary key " + callContext.getPrimaryKey());
            }
        }
        handleSystemException(txPolicy, e, callContext);
    } else {
        // Application Exception
        instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
        handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
    }
}
Also used : ExceptionType(org.apache.openejb.core.ExceptionType) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemException(org.apache.openejb.SystemException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ExceptionType

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

the class CmpContainer method businessMethod.

private Object businessMethod(final Method callMethod, final Method runMethod, 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;
    entrancyTracker.enter(beanContext, callContext.getPrimaryKey());
    try {
        bean = (EntityBean) cmpEngine.loadBean(callContext, callContext.getPrimaryKey());
        if (bean == null) {
            throw new NoSuchObjectException(beanContext.getDeploymentID() + " : " + callContext.getPrimaryKey());
        }
        returnValue = runMethod.invoke(bean, args);
        // when there is not transaction, merge the data from the bean back into the cmp engine
        cmpEngine.storeBeanIfNoTx(callContext, bean);
    } catch (final NoSuchObjectException e) {
        handleApplicationException(txPolicy, e, false);
    } 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 {
        entrancyTracker.exit(beanContext, callContext.getPrimaryKey());
        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) NoSuchObjectException(java.rmi.NoSuchObjectException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with ExceptionType

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

the class SingletonContainer method _invoke.

protected Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType callType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Duration accessTimeout = getAccessTimeout(beanContext, runMethod);
    final boolean read = javax.ejb.LockType.READ.equals(beanContext.getConcurrencyAttribute(runMethod));
    final Lock lock = aquireLock(read, accessTimeout, instance, runMethod);
    Object returnValue;
    try {
        final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, callType), callContext);
        returnValue = null;
        try {
            if (callType == InterfaceType.SERVICE_ENDPOINT) {
                callContext.setCurrentOperation(Operation.BUSINESS_WS);
                returnValue = invokeWebService(args, beanContext, runMethod, instance);
            } else {
                final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, callType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors);
                returnValue = interceptorStack.invoke(args);
            }
        } catch (final Throwable e) {
            // handle reflection exception
            final ExceptionType type = beanContext.getExceptionType(e);
            if (type == ExceptionType.SYSTEM) {
                /* System Exception ****************************/
                // The bean instance is not put into the pool via instanceManager.poolInstance
                // and therefore the instance will be garbage collected and destroyed.
                // For this reason the discardInstance method of the StatelessInstanceManager
                // does nothing.
                handleSystemException(txPolicy, e, callContext);
            } else {
                /* Application Exception ***********************/
                handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
            }
        } finally {
            afterInvoke(txPolicy, callContext);
        }
    } finally {
        lock.unlock();
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Duration(org.apache.openejb.util.Duration) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) Lock(java.util.concurrent.locks.Lock)

Example 4 with ExceptionType

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

the class StatelessContainer method _invoke.

@SuppressWarnings("ThrowFromFinallyBlock")
private Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
    Object returnValue = null;
    try {
        if (type == InterfaceType.SERVICE_ENDPOINT) {
            callContext.setCurrentOperation(Operation.BUSINESS_WS);
            returnValue = invokeWebService(args, beanContext, runMethod, instance);
        } else {
            final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
            final Operation operation = type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS;
            final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, operation, interceptors, instance.interceptors);
            returnValue = interceptorStack.invoke(args);
        }
    } catch (final Throwable re) {
        // handle reflection exception
        final ExceptionType exceptionType = beanContext.getExceptionType(re);
        if (exceptionType == ExceptionType.SYSTEM) {
            /* System Exception ****************************/
            // The bean instance is not put into the pool via instanceManager.poolInstance
            // and therefore the instance will be garbage collected and destroyed.
            // In case of StrictPooling flag being set to true we also release the semaphore
            // in the discardInstance method of the instanceManager.
            callContext.setDiscardInstance(true);
            handleSystemException(txPolicy, re, callContext);
        } else {
            /* Application Exception ***********************/
            handleApplicationException(txPolicy, re, exceptionType == ExceptionType.APPLICATION_ROLLBACK);
        }
    } finally {
        try {
            afterInvoke(txPolicy, callContext);
        } catch (final SystemException | RuntimeException e) {
            callContext.setDiscardInstance(true);
            throw e;
        }
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemException(org.apache.openejb.SystemException) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Operation(org.apache.openejb.core.Operation)

Example 5 with ExceptionType

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

the class StatefulContainer 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.getPrimaryKey(), null);
        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)

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