Search in sources :

Example 16 with EntityBean

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

the class CmpContainer method homeMethod.

private Object homeMethod(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 returnValue = null;
    try {
        /*
              Obtain a bean instance from the method ready pool
            */
        bean = createNewInstance(callContext);
        // set the entity context
        setEntityContext(bean);
        try {
            callContext.setCurrentOperation(Operation.HOME);
            final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
            try {
                returnValue = runMethod.invoke(bean, args);
            } catch (final IllegalArgumentException e) {
                System.out.println("********************************************************");
                System.out.println("callMethod = " + callMethod);
                System.out.println("runMethod = " + runMethod);
                System.out.println("bean = " + bean.getClass().getName());
                throw e;
            }
        } finally {
            unsetEntityContext(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 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) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 17 with EntityBean

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

the class CmpContainer method removeEJBObject.

private void removeEJBObject(final Method callMethod, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    try {
        final EntityBean entityBean = (EntityBean) cmpEngine.loadBean(callContext, callContext.getPrimaryKey());
        if (entityBean == null) {
            throw new NoSuchObjectException(callContext.getBeanContext().getDeploymentID() + " " + callContext.getPrimaryKey());
        }
        ejbRemove(entityBean);
        cmpEngine.removeBean(callContext);
    } catch (final NoSuchObjectException e) {
        handleApplicationException(txPolicy, e, false);
    } catch (final Throwable e) {
        // handle reflection exception
        handleSystemException(txPolicy, e, callContext);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityBean(javax.ejb.EntityBean) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) NoSuchObjectException(java.rmi.NoSuchObjectException)

Example 18 with EntityBean

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

the class CmpContainer method findByPrimaryKey.

private Object findByPrimaryKey(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 EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext, args[0]);
        if (bean == null) {
            throw new ObjectNotFoundException(beanContext.getDeploymentID() + " : " + args[0]);
        }
        // rebuild the primary key
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);
        // create a new ProxyInfo based on the deployment info and primary key
        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 : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) FinderException(javax.ejb.FinderException) EntityBean(javax.ejb.EntityBean) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject)

Example 19 with EntityBean

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

the class Cmp2Util method getEntityBean.

public static <Bean extends EntityBean> Bean getEntityBean(final EJBLocalObject 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(javax.ejb.EntityBean)

Example 20 with EntityBean

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

the class CmrSet method getEntityBeans.

private static <Bean extends EntityBean> Set<Bean> getEntityBeans(final Collection<?> proxies, final Class type) {
    if (proxies == null) {
        return null;
    }
    final Set<Bean> entities = new HashSet<Bean>();
    for (final Object value : proxies) {
        if (type != null && !type.isInstance(value)) {
            throw new IllegalArgumentException("Object is not an instance of " + type.getName() + ": " + (value == null ? "null" : value.getClass().getName()));
        }
        final Bean entity = Cmp2Util.<Bean>getEntityBean((EJBLocalObject) value);
        if (entity == null) {
            throw new IllegalArgumentException("Entity has been deleted");
        }
        entities.add(entity);
    }
    return entities;
}
Also used : EJBLocalObject(javax.ejb.EJBLocalObject) EntityBean(javax.ejb.EntityBean) HashSet(java.util.HashSet)

Aggregations

EntityBean (javax.ejb.EntityBean)25 EJBLocalObject (javax.ejb.EJBLocalObject)13 EJBObject (javax.ejb.EJBObject)12 FinderException (javax.ejb.FinderException)12 RemoteException (java.rmi.RemoteException)11 BeanContext (org.apache.openejb.BeanContext)11 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)11 EJBException (javax.ejb.EJBException)10 NoSuchEntityException (javax.ejb.NoSuchEntityException)10 RemoveException (javax.ejb.RemoveException)9 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)9 CreateException (javax.ejb.CreateException)8 NoSuchObjectLocalException (javax.ejb.NoSuchObjectLocalException)8 SystemException (javax.transaction.SystemException)7 EjbInvocation (com.sun.ejb.EjbInvocation)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ObjectNotFoundException (javax.ejb.ObjectNotFoundException)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 ProxyInfo (org.apache.openejb.ProxyInfo)4 EJBLocalRemoteObject (com.sun.ejb.containers.EJBLocalRemoteObject)3