Search in sources :

Example 16 with ThreadContext

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

the class MdbContainer method beforeDelivery.

public void beforeDelivery(final BeanContext deployInfo, final Object instance, final Method method, final XAResource xaResource) throws SystemException {
    // intialize call context
    final ThreadContext callContext = new ThreadContext(deployInfo, null);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    // create mdb context
    final MdbCallContext mdbCallContext = new MdbCallContext();
    callContext.set(MdbCallContext.class, mdbCallContext);
    mdbCallContext.deliveryMethod = method;
    mdbCallContext.oldCallContext = oldContext;
    // call the tx before method
    try {
        mdbCallContext.txPolicy = createTransactionPolicy(deployInfo.getTransactionType(method), callContext);
        // if we have an xaResource and a transaction was not imported from the adapter, enlist the xaResource
        if (xaResource != null && mdbCallContext.txPolicy.isNewTransaction()) {
            mdbCallContext.txPolicy.enlistResource(xaResource);
        }
    } catch (final ApplicationException e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Should never get an Application exception", e);
    } catch (final SystemException e) {
        ThreadContext.exit(oldContext);
        throw e;
    } catch (final Exception e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Unable to enlist xa resource in the transaction", e);
    }
}
Also used : EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ThreadContext(org.apache.openejb.core.ThreadContext) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ReflectionException(javax.management.ReflectionException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ConstraintViolationException(javax.validation.ConstraintViolationException)

Example 17 with ThreadContext

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

the class MdbInstanceFactory method freeInstance.

/**
 * Frees an instance no longer needed by the resource adapter.  This method makes all the necessary lifecycle
 * callbacks and decrements the instance count.  This method should not be used to disposed of beans that have
 * thrown a system exception.  Instead the discardInstance method should be called.
 *
 * @param instance             the bean instance to free
 * @param ignoredInstanceCount
 */
public void freeInstance(final Instance instance, final boolean ignoredInstanceCount) {
    if (instance == null) {
        throw new NullPointerException("bean is null");
    }
    // decrement the instance count
    if (!ignoredInstanceCount) {
        synchronized (this) {
            instanceCount--;
        }
    }
    final ThreadContext callContext = ThreadContext.getThreadContext();
    final Operation originalOperation = callContext == null ? null : callContext.getCurrentOperation();
    final BaseContext.State[] originalAllowedStates = callContext == null ? null : callContext.getCurrentAllowedStates();
    try {
        // call post destroy method
        if (callContext != null) {
            callContext.setCurrentOperation(Operation.PRE_DESTROY);
        }
        final Method remove = instance.bean instanceof MessageDrivenBean ? MessageDrivenBean.class.getMethod("ejbRemove") : null;
        final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
        final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
        interceptorStack.invoke();
        if (instance.creationalContext != null) {
            instance.creationalContext.release();
        }
    } catch (final Throwable re) {
        MdbInstanceFactory.logger.error("The bean instance " + instance.bean + " threw a system exception:" + re, re);
    } finally {
        if (callContext != null) {
            callContext.setCurrentOperation(originalOperation);
            callContext.setCurrentAllowedStates(originalAllowedStates);
        }
    }
}
Also used : MessageDrivenBean(javax.ejb.MessageDrivenBean) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) ThreadContext(org.apache.openejb.core.ThreadContext) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method)

Example 18 with ThreadContext

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

the class MdbPoolContainer method invoke.

public Object invoke(final Object instance, final Method method, final InterfaceType type, Object... args) throws SystemException, ApplicationException {
    if (args == null) {
        args = NO_ARGS;
    }
    // get the context data
    final ThreadContext callContext = ThreadContext.getThreadContext();
    final BeanContext deployInfo = callContext.getBeanContext();
    final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
    if (mdbCallContext == null) {
        throw new IllegalStateException("beforeDelivery was not called");
    }
    // verify the delivery method passed to beforeDeliver is the same method that was invoked
    if (!mdbCallContext.deliveryMethod.getName().equals(method.getName()) || !Arrays.deepEquals(mdbCallContext.deliveryMethod.getParameterTypes(), method.getParameterTypes())) {
        throw new IllegalStateException("Delivery method specified in beforeDelivery is not the delivery method called");
    }
    // remember the return value or exception so it can be logged
    Object returnValue = null;
    OpenEJBException openEjbException = null;
    final Operation oldOperation = callContext.getCurrentOperation();
    callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
    try {
        if (logger.isDebugEnabled()) {
            logger.info("invoking method " + method.getName() + " on " + deployInfo.getDeploymentID());
        }
        // determine the target method on the bean instance class
        final Method targetMethod = deployInfo.getMatchingBeanMethod(method);
        callContext.set(Method.class, targetMethod);
        // invoke the target method
        returnValue = _invoke(instance, targetMethod, args, deployInfo, type, mdbCallContext, callContext);
        return returnValue;
    } catch (final ApplicationException | SystemException e) {
        openEjbException = e;
        throw e;
    } finally {
        callContext.setCurrentOperation(oldOperation);
        // Log the invocation results
        if (logger.isDebugEnabled()) {
            if (openEjbException == null) {
                logger.debug("finished invoking method " + method.getName() + ". Return value:" + returnValue);
            } else {
                final Throwable exception = openEjbException.getRootCause() != null ? openEjbException.getRootCause() : openEjbException;
                logger.debug("finished invoking method " + method.getName() + " with exception " + exception);
            }
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) 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) ThreadContext(org.apache.openejb.core.ThreadContext) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method)

Example 19 with ThreadContext

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

the class MdbPoolContainer method beforeDelivery.

public void beforeDelivery(final BeanContext deployInfo, final Object instance, final Method method, final XAResource xaResource) throws SystemException {
    // intialize call context
    final ThreadContext callContext = new ThreadContext(deployInfo, null);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    // create mdb context
    final MdbCallContext mdbCallContext = new MdbCallContext();
    callContext.set(MdbCallContext.class, mdbCallContext);
    mdbCallContext.deliveryMethod = method;
    mdbCallContext.oldCallContext = oldContext;
    // call the tx before method
    try {
        mdbCallContext.txPolicy = createTransactionPolicy(deployInfo.getTransactionType(method), callContext);
        // if we have an xaResource and a transaction was not imported from the adapter, enlist the xaResource
        if (xaResource != null && mdbCallContext.txPolicy.isNewTransaction()) {
            mdbCallContext.txPolicy.enlistResource(xaResource);
        }
    } catch (final ApplicationException e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Should never get an Application exception", e);
    } catch (final SystemException e) {
        ThreadContext.exit(oldContext);
        throw e;
    } catch (final Exception e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Unable to enlist xa resource in the transaction", e);
    }
}
Also used : 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) ThreadContext(org.apache.openejb.core.ThreadContext) ResourceException(javax.resource.ResourceException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) NamingException(javax.naming.NamingException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConstraintViolationException(javax.validation.ConstraintViolationException) SystemException(org.apache.openejb.SystemException)

Example 20 with ThreadContext

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

the class MdbPoolContainer method afterDelivery.

public void afterDelivery(final Object instance) throws SystemException {
    // get the mdb call context
    final ThreadContext callContext = ThreadContext.getThreadContext();
    final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
    // invoke the tx after method
    try {
        afterInvoke(mdbCallContext.txPolicy, callContext);
    } catch (final ApplicationException e) {
        callContext.setDiscardInstance(true);
        throw new SystemException("Should never get an Application exception", e);
    } finally {
        if (instance != null) {
            if (callContext.isDiscardInstance()) {
                this.instanceManager.discardInstance(callContext, instance);
            } else {
                try {
                    this.instanceManager.poolInstance(callContext, instance);
                } catch (OpenEJBException e) {
                    throw new SystemException("Should never get an OpenEJBException exception", e);
                }
            }
        }
        ThreadContext.exit(mdbCallContext.oldCallContext);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) 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) ThreadContext(org.apache.openejb.core.ThreadContext)

Aggregations

ThreadContext (org.apache.openejb.core.ThreadContext)68 BeanContext (org.apache.openejb.BeanContext)35 OpenEJBException (org.apache.openejb.OpenEJBException)23 Method (java.lang.reflect.Method)20 ApplicationException (org.apache.openejb.ApplicationException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)10 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)10 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 EJBException (javax.ejb.EJBException)9 SystemException (org.apache.openejb.SystemException)9 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)9 RemoteException (java.rmi.RemoteException)7 NamingException (javax.naming.NamingException)6 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)6 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)6 SystemInstance (org.apache.openejb.loader.SystemInstance)6 ArrayList (java.util.ArrayList)5 EJBAccessException (javax.ejb.EJBAccessException)5