Search in sources :

Example 56 with ThreadContext

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

the class CmpContainer method ejbRemove.

private void ejbRemove(final EntityBean entityBean) throws RemoveException {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    if (isDeleted(entityBean)) {
        return;
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.REMOVE);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbRemove();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        // todo replace with interface call when CmpEntityBean interface is added
        try {
            entityBean.getClass().getMethod("OpenEJB_deleted").invoke(entityBean);
        } catch (final Exception ignored) {
        // no-op
        }
        cancelTimers(callContext);
        ThreadContext.exit(oldCallContext);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) FinderException(javax.ejb.FinderException)

Example 57 with ThreadContext

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

the class CmpContainer method invoke.

@Override
public Object invoke(final Object deployID, InterfaceType type, final Class callInterface, final Method callMethod, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = this.getBeanContext(deployID);
    if (beanContext == null) {
        throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
    }
    // Use the backup way to determine call type if null was supplied.
    if (type == null) {
        type = beanContext.getInterfaceType(callInterface);
    }
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        final boolean authorized = securityService.isCallerAuthorized(callMethod, type);
        if (!authorized) {
            throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
        }
        final Class declaringClass = callMethod.getDeclaringClass();
        final String methodName = callMethod.getName();
        if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
            if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) {
                if (methodName.startsWith("create")) {
                    return createEJBObject(callMethod, args, callContext, type);
                } else if (methodName.equals("findByPrimaryKey")) {
                    return findByPrimaryKey(callMethod, args, callContext, type);
                } else if (methodName.startsWith("find")) {
                    return findEJBObject(callMethod, args, callContext, type);
                } else {
                    return homeMethod(callMethod, args, callContext, type);
                }
            } else if (methodName.equals("remove")) {
                removeEJBObject(callMethod, callContext, type);
                return null;
            }
        } else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) {
            removeEJBObject(callMethod, callContext, type);
            return null;
        }
        // business method
        callContext.setCurrentOperation(Operation.BUSINESS);
        final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
        callContext.set(Method.class, runMethod);
        return businessMethod(callMethod, runMethod, args, callContext, type);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
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) EJBHome(javax.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) EJBAccessException(javax.ejb.EJBAccessException) EJBLocalHome(javax.ejb.EJBLocalHome)

Example 58 with ThreadContext

use of org.apache.openejb.core.ThreadContext 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 59 with ThreadContext

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

the class SingletonInstanceManager method initialize.

private void initialize(final BeanContext beanContext) throws OpenEJBException {
    try {
        final ThreadContext callContext = new ThreadContext(beanContext, null);
        final ThreadContext old = ThreadContext.enter(callContext);
        try {
            getInstance(callContext);
        } finally {
            ThreadContext.exit(old);
        }
    } catch (final OpenEJBException e) {
        throw new OpenEJBException("Singleton startup failed: " + beanContext.getDeploymentID(), e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 60 with ThreadContext

use of org.apache.openejb.core.ThreadContext 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

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