Search in sources :

Example 1 with EntityBean

use of jakarta.ejb.EntityBean in project tomee by apache.

the class EntityContainer method removeEJBObject.

protected void removeEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    callContext.setCurrentOperation(Operation.REMOVE);
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
    EntityBean bean = null;
    try {
        bean = instanceManager.obtainInstance(callContext);
        ejbLoad_If_No_Transaction(callContext, bean);
        bean.ejbRemove();
        didRemove(bean, callContext);
        instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
    } catch (final Throwable e) {
        handleException(txPolicy, e, callContext, bean);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityBean(jakarta.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)

Example 2 with EntityBean

use of jakarta.ejb.EntityBean in project tomee by apache.

the class EntityContainer method createEJBObject.

protected ProxyInfo createEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    callContext.setCurrentOperation(Operation.CREATE);
    /*
        * 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."
        *
        * For this reason the TransactionScopeHandler methods usally preformed by the invoke( )
        * operation must be handled here along with the call explicitly.
        * This ensures that the afterInvoke() is not processed between the ejbCreate and ejbPostCreate methods to
        * ensure that the ejbPostCreate executes in the same transaction context of the ejbCreate.
        * This would otherwise not be possible if container-managed transactions were used because
        * the TransactionScopeManager would attempt to commit the transaction immediately after the ejbCreate
        * and before the ejbPostCreate had a chance to execute.  Once the ejbPostCreate method execute the
        * super classes afterInvoke( ) method will be executed committing the transaction if its a CMT.
        */
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
    EntityBean bean = null;
    Object primaryKey = null;
    try {
        // Get new ready instance
        bean = instanceManager.obtainInstance(callContext);
        // Obtain the proper ejbCreate() method
        final Method ejbCreateMethod = beanContext.getMatchingBeanMethod(callMethod);
        // invoke the ejbCreate which returns the primary key
        primaryKey = ejbCreateMethod.invoke(bean, args);
        didCreateBean(callContext, bean);
        // 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);
        }
        // update pool
        instanceManager.poolInstance(callContext, bean, primaryKey);
    } catch (final Throwable e) {
        handleException(txPolicy, e, callContext, bean);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    return new ProxyInfo(beanContext, primaryKey);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) EntityBean(jakarta.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(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject) Method(java.lang.reflect.Method)

Example 3 with EntityBean

use of jakarta.ejb.EntityBean 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(jakarta.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject) NoSuchObjectException(java.rmi.NoSuchObjectException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with EntityBean

use of jakarta.ejb.EntityBean in project tomee by apache.

the class CmpContainer method findEJBObject.

private Object findEJBObject(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);
    try {
        final List<Object> results = cmpEngine.queryBeans(callContext, callMethod, args);
        final KeyGenerator kg = beanContext.getKeyGenerator();
        // single ProxyInfo object is returned.
        if (callMethod.getReturnType() == Collection.class || callMethod.getReturnType() == Enumeration.class) {
            final List<ProxyInfo> proxies = new ArrayList<>();
            for (final Object value : results) {
                final EntityBean bean = (EntityBean) value;
                if (value == null) {
                    proxies.add(null);
                } else {
                    // get the primary key
                    final Object primaryKey = kg.getPrimaryKey(bean);
                    // create a new ProxyInfo based on the deployment info and primary key and add it to the vector
                    proxies.add(new ProxyInfo(beanContext, primaryKey));
                }
            }
            if (callMethod.getReturnType() == Enumeration.class) {
                return new Enumerator(proxies);
            } else {
                return proxies;
            }
        } else {
            if (results.size() != 1) {
                throw new ObjectNotFoundException("A Enteprise bean with deployment_id = " + beanContext.getDeploymentID() + (args != null && args.length >= 1 ? " and primarykey = " + args[0] : "") + " Does not exist");
            }
            // create a new ProxyInfo based on the deployment info and primary key
            final EntityBean bean = (EntityBean) results.get(0);
            if (bean == null) {
                return null;
            } else {
                final Object primaryKey = kg.getPrimaryKey(bean);
                return new ProxyInfo(beanContext, primaryKey);
            }
        }
    } catch (final FinderException fe) {
        handleApplicationException(txPolicy, fe, false);
    } catch (final Throwable e) {
        // handle reflection exception
        handleSystemException(txPolicy, e, callContext);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    throw new AssertionError("Should not get here");
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) FinderException(jakarta.ejb.FinderException) Enumerator(org.apache.openejb.util.Enumerator) EntityBean(jakarta.ejb.EntityBean) ObjectNotFoundException(jakarta.ejb.ObjectNotFoundException) Collection(java.util.Collection) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject)

Example 5 with EntityBean

use of jakarta.ejb.EntityBean in project tomee by apache.

the class Cmp2Util method getEntityBean.

public static <Bean extends EntityBean> Bean getEntityBean(final EJBObject proxy) {
    if (proxy == null) {
        return null;
    }
    final EjbObjectProxyHandler handler = (EjbObjectProxyHandler) ProxyManager.getInvocationHandler(proxy);
    if (handler.container == null) {
        return null;
    }
    if (!(handler.container instanceof CmpContainer)) {
        throw new IllegalArgumentException("Proxy is not connected to a CMP container but is conect to " + handler.container.getClass().getName());
    }
    final CmpContainer container = (CmpContainer) handler.container;
    final Bean entity = (Bean) container.getEjbInstance(handler.getBeanContext(), handler.primaryKey);
    return entity;
}
Also used : CmpContainer(org.apache.openejb.core.cmp.CmpContainer) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler) EntityBean(jakarta.ejb.EntityBean)

Aggregations

EntityBean (jakarta.ejb.EntityBean)17 BeanContext (org.apache.openejb.BeanContext)11 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)11 EJBLocalObject (jakarta.ejb.EJBLocalObject)10 EJBObject (jakarta.ejb.EJBObject)9 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)9 FinderException (jakarta.ejb.FinderException)4 ObjectNotFoundException (jakarta.ejb.ObjectNotFoundException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 ProxyInfo (org.apache.openejb.ProxyInfo)4 Method (java.lang.reflect.Method)3 NoSuchObjectException (java.rmi.NoSuchObjectException)3 RemoteException (java.rmi.RemoteException)3 ApplicationException (org.apache.openejb.ApplicationException)3 ExceptionType (org.apache.openejb.core.ExceptionType)3 ThreadContext (org.apache.openejb.core.ThreadContext)3 EJBException (jakarta.ejb.EJBException)2 NoSuchEntityException (jakarta.ejb.NoSuchEntityException)2 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)2