Search in sources :

Example 46 with ThreadContext

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

the class ManagedUserTransaction method begin.

public void begin() throws NotSupportedException, SystemException {
    userTransaction.begin();
    // get the callContext
    final ThreadContext callContext = ThreadContext.getThreadContext();
    if (callContext == null) {
        // someone is using the user transaction out side of the component
        return;
    }
    // get the deployment info
    final BeanContext beanContext = callContext.getBeanContext();
    if (beanContext.getComponentType() != BeanType.MANAGED) {
        // some other non-stateful ejb is using our user transaction
        return;
    }
    // get the primary key
    final Object primaryKey = callContext.getPrimaryKey();
    if (primaryKey == null) {
        // is is not a bean method
        return;
    }
    jtaEntityManagerRegistry.transactionStarted((String) beanContext.getDeploymentID(), primaryKey);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 47 with ThreadContext

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

the class MdbContainer 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) {
        throw new SystemException("Should never get an Application exception", e);
    } finally {
        ThreadContext.exit(mdbCallContext.oldCallContext);
    }
}
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)

Example 48 with ThreadContext

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

the class MdbContainer method release.

public void release(final BeanContext deployInfo, final Object instance) {
    // get the mdb call context
    ThreadContext callContext = ThreadContext.getThreadContext();
    boolean contextExitRequired = false;
    if (callContext == null) {
        callContext = new ThreadContext(deployInfo, null);
        ThreadContext.enter(callContext);
        contextExitRequired = true;
    }
    try {
        // if we have an mdb call context we need to invoke the after invoke method
        final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
        if (mdbCallContext != null) {
            try {
                afterInvoke(mdbCallContext.txPolicy, callContext);
            } catch (final Exception e) {
                logger.error("error while releasing message endpoint", e);
            } finally {
                final EndpointFactory endpointFactory = (EndpointFactory) deployInfo.getContainerData();
                endpointFactory.getInstanceFactory().freeInstance((Instance) instance, false);
            }
        }
    } finally {
        if (contextExitRequired) {
            ThreadContext.exit(callContext);
        }
    }
}
Also used : 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 49 with ThreadContext

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

the class MdbContainer 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);
        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) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ThreadContext(org.apache.openejb.core.ThreadContext) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method)

Example 50 with ThreadContext

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

the class MdbInstanceFactory method constructBean.

private Object constructBean() throws UnavailableException {
    final BeanContext beanContext = this.beanContext;
    final ThreadContext callContext = new ThreadContext(beanContext, null, Operation.INJECTION);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    try {
        final InstanceContext context = beanContext.newInstance();
        if (context.getBean() instanceof MessageDrivenBean) {
            callContext.setCurrentOperation(Operation.CREATE);
            final Method create = beanContext.getCreateMethod();
            final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList(), new HashMap());
            ejbCreate.invoke();
        }
        return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext());
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final String message = "The bean instance threw a system exception:" + e;
        MdbInstanceFactory.logger.error(message, e);
        throw new UnavailableException(message, e);
    } finally {
        ThreadContext.exit(oldContext);
    }
}
Also used : HashMap(java.util.HashMap) ThreadContext(org.apache.openejb.core.ThreadContext) ArrayList(java.util.ArrayList) UnavailableException(javax.resource.spi.UnavailableException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanContext(org.apache.openejb.BeanContext) InstanceContext(org.apache.openejb.core.InstanceContext) MessageDrivenBean(javax.ejb.MessageDrivenBean) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack)

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