Search in sources :

Example 51 with BeanContext

use of org.apache.openejb.BeanContext 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 52 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class CmpContainer method createThreadContext.

private ThreadContext createThreadContext(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    final BeanContext beanContext = getBeanContextByClass(entityBean.getClass());
    final KeyGenerator keyGenerator = beanContext.getKeyGenerator();
    final Object primaryKey = keyGenerator.getPrimaryKey(entityBean);
    return new ThreadContext(beanContext, primaryKey);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject)

Example 53 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class CmpContainer method setEntityContext.

private void setEntityContext(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    // activating entity doen't have a primary key
    final BeanContext beanContext = getBeanContextByClass(entityBean.getClass());
    final ThreadContext callContext = new ThreadContext(beanContext, null);
    callContext.setCurrentOperation(Operation.SET_CONTEXT);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.setEntityContext(new EntityContext(securityService));
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext) EntityContext(org.apache.openejb.core.entity.EntityContext) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 54 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class EntityContext method getEJBLocalObject.

public EJBLocalObject getEJBLocalObject() throws IllegalStateException {
    doCheck(Call.getEJBLocalObject);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext di = threadContext.getBeanContext();
    if (di.getLocalInterface() == null) {
        throw new IllegalStateException("EJB " + di.getDeploymentID() + " does not have a local interface");
    }
    final EjbObjectProxyHandler handler = new EntityEjbObjectHandler(di, threadContext.getPrimaryKey(), InterfaceType.EJB_LOCAL, new ArrayList<>(), di.getLocalInterface());
    try {
        final Class[] interfaces = new Class[] { di.getLocalInterface(), IntraVmProxy.class };
        return (EJBLocalObject) ProxyManager.newProxyInstance(interfaces, handler);
    } catch (final IllegalAccessException iae) {
        throw new InternalErrorException("Could not create IVM proxy for " + di.getLocalInterface() + " interface", iae);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext) InternalErrorException(org.apache.openejb.InternalErrorException) IntraVmProxy(org.apache.openejb.core.ivm.IntraVmProxy) EJBLocalObject(javax.ejb.EJBLocalObject) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler)

Example 55 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class EntityContext method getEJBObject.

public EJBObject getEJBObject() throws IllegalStateException {
    doCheck(Call.getEJBObject);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext di = threadContext.getBeanContext();
    if (di.getRemoteInterface() == null) {
        throw new IllegalStateException("EJB " + di.getDeploymentID() + " does not have a remote interface");
    }
    final EjbObjectProxyHandler handler = new EntityEjbObjectHandler(di.getContainer().getBeanContext(di.getDeploymentID()), threadContext.getPrimaryKey(), InterfaceType.EJB_OBJECT, new ArrayList<>(), di.getRemoteInterface());
    try {
        final Class[] interfaces = new Class[] { di.getRemoteInterface(), IntraVmProxy.class };
        return (EJBObject) ProxyManager.newProxyInstance(interfaces, handler);
    } catch (final IllegalAccessException iae) {
        throw new InternalErrorException("Could not create IVM proxy for " + di.getRemoteInterface() + " interface", iae);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EJBObject(javax.ejb.EJBObject) ThreadContext(org.apache.openejb.core.ThreadContext) InternalErrorException(org.apache.openejb.InternalErrorException) IntraVmProxy(org.apache.openejb.core.ivm.IntraVmProxy) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler)

Aggregations

BeanContext (org.apache.openejb.BeanContext)198 OpenEJBException (org.apache.openejb.OpenEJBException)40 ThreadContext (org.apache.openejb.core.ThreadContext)40 Method (java.lang.reflect.Method)38 ContainerSystem (org.apache.openejb.spi.ContainerSystem)28 ArrayList (java.util.ArrayList)27 AppContext (org.apache.openejb.AppContext)26 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)26 NamingException (javax.naming.NamingException)24 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)23 Context (javax.naming.Context)22 ApplicationException (org.apache.openejb.ApplicationException)20 HashMap (java.util.HashMap)19 EJBLocalObject (javax.ejb.EJBLocalObject)18 EJBObject (javax.ejb.EJBObject)18 SystemException (org.apache.openejb.SystemException)18 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)17 ModuleContext (org.apache.openejb.ModuleContext)16 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)16 FinderException (javax.ejb.FinderException)14