Search in sources :

Example 31 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class ManagedContainer method registerSessionSynchronization.

private void registerSessionSynchronization(final Instance instance, final ThreadContext callContext) {
    final TransactionPolicy txPolicy = callContext.getTransactionPolicy();
    if (txPolicy == null) {
        throw new IllegalStateException("ThreadContext does not contain a TransactionEnvironment");
    }
    SessionSynchronizationCoordinator coordinator = (SessionSynchronizationCoordinator) txPolicy.getResource(SessionSynchronizationCoordinator.class);
    if (coordinator == null) {
        coordinator = new SessionSynchronizationCoordinator(txPolicy);
        txPolicy.registerSynchronization(coordinator);
        txPolicy.putResource(SessionSynchronizationCoordinator.class, coordinator);
    }
    // SessionSynchronization are only enabled for beans after CREATE that are not bean-managed and implement the SessionSynchronization interface
    final boolean synchronize = callContext.getCurrentOperation() != Operation.CREATE && callContext.getBeanContext().isSessionSynchronized() && txPolicy.isTransactionActive();
    coordinator.registerSessionSynchronization(instance, callContext.getBeanContext(), callContext.getPrimaryKey(), synchronize);
}
Also used : JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy)

Example 32 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class ManagedContainer method createEJBObject.

protected ProxyInfo createEJBObject(final BeanContext beanContext, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    // generate a new primary key
    final Object primaryKey = newPrimaryKey();
    final ThreadContext createContext = new ThreadContext(beanContext, primaryKey);
    final ThreadContext oldCallContext = ThreadContext.enter(createContext);
    try {
        // Security check
        checkAuthorization(callMethod, interfaceType);
        // Create the extended entity managers for this instance
        final Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = createEntityManagers(beanContext);
        // Register the newly created entity managers
        if (entityManagers != null) {
            try {
                entityManagerRegistry.addEntityManagers((String) beanContext.getDeploymentID(), primaryKey, entityManagers);
            } catch (final EntityManagerAlreadyRegisteredException e) {
                throw new EJBException(e);
            }
        }
        createContext.setCurrentOperation(Operation.CREATE);
        createContext.setCurrentAllowedStates(null);
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(createContext.getBeanContext().getTransactionType(callMethod, interfaceType), createContext);
        Instance instance = null;
        try {
            try {
                final InstanceContext context = beanContext.newInstance();
                // Wrap-up everthing into a object
                instance = new Instance(beanContext, primaryKey, context.getBean(), context.getInterceptors(), context.getCreationalContext(), entityManagers);
            } catch (final Throwable throwable) {
                final ThreadContext callContext = ThreadContext.getThreadContext();
                EjbTransactionUtil.handleSystemException(callContext.getTransactionPolicy(), throwable, callContext);
                // should never be reached
                throw new IllegalStateException(throwable);
            }
            // add to cache
            cache.add(primaryKey, instance);
            // instance starts checked-out
            checkedOutInstances.put(primaryKey, instance);
            // Register for synchronization callbacks
            registerSessionSynchronization(instance, createContext);
            // Invoke create for legacy beans
            if (!callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessRemoteHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalBeanHome.class)) {
                // Setup for business invocation
                final Method createOrInit = beanContext.getMatchingBeanMethod(callMethod);
                createContext.set(Method.class, createOrInit);
                // Initialize interceptor stack
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, createOrInit, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap<String, Object>());
                // Invoke
                if (args == null) {
                    interceptorStack.invoke();
                } else {
                    interceptorStack.invoke(args);
                }
            }
        } catch (final Throwable e) {
            handleException(createContext, txPolicy, e);
        } finally {
            afterInvoke(createContext, txPolicy, instance);
        }
        return new ProxyInfo(beanContext, primaryKey);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) SystemInstance(org.apache.openejb.loader.SystemInstance) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) Method(java.lang.reflect.Method) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) InstanceContext(org.apache.openejb.core.InstanceContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 33 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class ManagedContainer method businessMethod.

protected Object businessMethod(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        // Security check
        checkAuthorization(callMethod, interfaceType);
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
        Object returnValue = null;
        Instance instance = null;
        try {
            // Obtain instance
            instance = obtainInstance(primKey, callContext);
            // Resume previous Bean transaction if there was one
            if (txPolicy instanceof BeanTransactionPolicy) {
                final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
                if (suspendedTransaction != null) {
                    instance.setBeanTransaction(null);
                    final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
                    beanTxEnv.resumeUserTransaction(suspendedTransaction);
                }
            }
            // Register the entity managers
            registerEntityManagers(instance, callContext);
            // Register for synchronization callbacks
            registerSessionSynchronization(instance, callContext);
            // Setup for business invocation
            callContext.setCurrentOperation(Operation.BUSINESS);
            callContext.setCurrentAllowedStates(null);
            callContext.setInvokedInterface(callInterface);
            final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
            callContext.set(Method.class, runMethod);
            // Initialize interceptor stack
            final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
            final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.BUSINESS, interceptors, instance.interceptors);
            // Invoke
            returnValue = interceptorStack.invoke(args);
        } catch (final Throwable e) {
            handleException(callContext, txPolicy, e);
        } finally {
            // Commit transaction
            afterInvoke(callContext, txPolicy, instance);
        }
        return returnValue;
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SystemInstance(org.apache.openejb.loader.SystemInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) Method(java.lang.reflect.Method)

Example 34 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy 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 35 with TransactionPolicy

use of org.apache.openejb.core.transaction.TransactionPolicy in project tomee by apache.

the class StatefulContainer method businessMethod.

protected Object businessMethod(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    final CurrentCreationalContext currentCreationalContext = beanContext.get(CurrentCreationalContext.class);
    Object runAs = null;
    try {
        if (oldCallContext != null) {
            final BeanContext oldBc = oldCallContext.getBeanContext();
            if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
                runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(callContext, beanContext, oldBc);
            }
        }
        // Security check
        checkAuthorization(callMethod, interfaceType);
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
        Object returnValue = null;
        Instance instance = null;
        try {
            // Obtain instance
            instance = obtainInstance(primKey, callContext, callMethod, true);
            // Resume previous Bean transaction if there was one
            if (txPolicy instanceof BeanTransactionPolicy) {
                final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
                if (suspendedTransaction != null) {
                    instance.setBeanTransaction(null);
                    final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
                    beanTxEnv.resumeUserTransaction(suspendedTransaction);
                }
            }
            // Register the entity managers
            registerEntityManagers(instance, callContext);
            // Register for synchronization callbacks
            registerSessionSynchronization(instance, callContext);
            // Setup for business invocation
            callContext.setCurrentOperation(Operation.BUSINESS);
            callContext.setCurrentAllowedStates(null);
            callContext.setInvokedInterface(callInterface);
            final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
            callContext.set(Method.class, runMethod);
            if (currentCreationalContext != null) {
                currentCreationalContext.set(instance.creationalContext);
            }
            // Initialize interceptor stack
            final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
            final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.BUSINESS, interceptors, instance.interceptors);
            // Invoke
            returnValue = interceptorStack.invoke(args);
        } catch (final Throwable e) {
            handleException(callContext, txPolicy, e);
        } finally {
            // un register EntityManager
            unregisterEntityManagers(instance, callContext);
            // Commit transaction
            afterInvoke(callContext, txPolicy, instance);
        }
        return returnValue;
    } finally {
        if (runAs != null) {
            try {
                securityService.associate(runAs);
            } catch (final LoginException e) {
            // no-op
            }
        }
        ThreadContext.exit(oldCallContext);
        if (currentCreationalContext != null) {
            currentCreationalContext.remove();
        }
    }
}
Also used : BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SystemInstance(org.apache.openejb.loader.SystemInstance) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) Method(java.lang.reflect.Method) CurrentCreationalContext(org.apache.openejb.cdi.CurrentCreationalContext) BeanContext(org.apache.openejb.BeanContext) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) LoginException(javax.security.auth.login.LoginException)

Aggregations

TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)38 BeanContext (org.apache.openejb.BeanContext)26 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)17 Method (java.lang.reflect.Method)14 EJBLocalObject (javax.ejb.EJBLocalObject)11 EJBObject (javax.ejb.EJBObject)11 EntityBean (javax.ejb.EntityBean)11 ThreadContext (org.apache.openejb.core.ThreadContext)10 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)10 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)10 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)10 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)10 ApplicationException (org.apache.openejb.ApplicationException)8 SystemInstance (org.apache.openejb.loader.SystemInstance)7 OpenEJBException (org.apache.openejb.OpenEJBException)6 ProxyInfo (org.apache.openejb.ProxyInfo)6 SystemException (org.apache.openejb.SystemException)6 Operation (org.apache.openejb.core.Operation)6 SuspendedTransaction (org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5