Search in sources :

Example 11 with EntityBean

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

the class CmpContainer method select.

public Object select(final BeanContext beanContext, final String methodSignature, final String returnType, final Object... args) throws FinderException {
    final String signature = beanContext.getAbstractSchemaName() + "." + methodSignature;
    try {
        // execute the select query
        final Collection<Object> results = cmpEngine.queryBeans(beanContext, signature, args);
        // 
        // process the results
        // 
        // If we need to return a set...
        final Collection<Object> proxies;
        if (returnType.equals("java.util.Set")) {
            // we collect values into a LinkedHashSet to preserve ordering
            proxies = new LinkedHashSet<>();
        } else {
            // otherwise use a simple array list
            proxies = new ArrayList<>();
        }
        final boolean isSingleValued = !returnType.equals("java.util.Collection") && !returnType.equals("java.util.Set");
        ProxyFactory proxyFactory = null;
        for (Object value : results) {
            // if this is a single valued query and we already have results, throw FinderException
            if (isSingleValued && !proxies.isEmpty()) {
                throw new FinderException("The single valued query " + methodSignature + "returned more than one item");
            }
            // if we have an EntityBean, we need to proxy it
            if (value instanceof EntityBean) {
                final EntityBean entityBean = (EntityBean) value;
                if (proxyFactory == null) {
                    final BeanContext result = getBeanContextByClass(entityBean.getClass());
                    if (result != null) {
                        proxyFactory = new ProxyFactory(result);
                    }
                }
                if (proxyFactory != null) {
                    if (beanContext.isRemoteQueryResults(methodSignature)) {
                        value = proxyFactory.createRemoteProxy(entityBean, this);
                    } else {
                        value = proxyFactory.createLocalProxy(entityBean, this);
                    }
                }
            }
            proxies.add(value);
        }
        // if not single valued, return the set
        if (!isSingleValued) {
            return proxies;
        }
        // single valued query that returned no rows, is an exception
        if (proxies.isEmpty()) {
            throw new ObjectNotFoundException();
        }
        // return the single item.... multiple return values was handled in for loop above
        return proxies.iterator().next();
    } catch (final RuntimeException e) {
        throw new EJBException(e);
    }
}
Also used : FinderException(jakarta.ejb.FinderException) BeanContext(org.apache.openejb.BeanContext) EntityBean(jakarta.ejb.EntityBean) ObjectNotFoundException(jakarta.ejb.ObjectNotFoundException) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject) EJBException(jakarta.ejb.EJBException) OpenEJBException(org.apache.openejb.OpenEJBException)

Example 12 with EntityBean

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

the class JpaCmpEngine method executeSelectQuery.

private List<Object> executeSelectQuery(final Query query, Object[] args) {
    // process args
    if (args == null) {
        args = NO_ARGS;
    }
    for (int i = 0; i < args.length; i++) {
        Object arg = args[i];
        // ejb proxies need to be swapped out for real instance classes
        if (arg instanceof EJBObject) {
            arg = Cmp2Util.getEntityBean((EJBObject) arg);
        }
        if (arg instanceof EJBLocalObject) {
            arg = Cmp2Util.getEntityBean((EJBLocalObject) arg);
        }
        try {
            query.getParameter(i + 1);
        } catch (final IllegalArgumentException e) {
            // specified position does not exist
            continue;
        }
        query.setParameter(i + 1, arg);
    }
    // todo results should not be iterated over, but should instead
    // perform all work in a wrapper list on demand by the application code
    final List results = query.getResultList();
    for (final Object value : results) {
        if (value instanceof EntityBean) {
            // todo don't activate beans already activated
            final EntityBean entity = (EntityBean) value;
            cmpCallback.setEntityContext(entity);
            cmpCallback.ejbActivate(entity);
        }
    }
    // noinspection unchecked
    return results;
}
Also used : EJBObject(jakarta.ejb.EJBObject) EntityBean(jakarta.ejb.EntityBean) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject) List(java.util.List) EJBLocalObject(jakarta.ejb.EJBLocalObject)

Example 13 with EntityBean

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

the class EntityContainer method invoke.

protected Object invoke(final InterfaceType type, final Method callMethod, final Method runMethod, final Object[] args, final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
    EntityBean bean = null;
    Object returnValue = null;
    entrancyTracker.enter(callContext.getBeanContext(), callContext.getPrimaryKey());
    try {
        bean = instanceManager.obtainInstance(callContext);
        ejbLoad_If_No_Transaction(callContext, bean);
        returnValue = runMethod.invoke(bean, args);
        ejbStore_If_No_Transaction(callContext, bean);
        instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
    } catch (final Throwable e) {
        handleException(txPolicy, e, callContext, bean);
    } finally {
        entrancyTracker.exit(callContext.getBeanContext(), callContext.getPrimaryKey());
        afterInvoke(txPolicy, callContext);
    }
    return returnValue;
}
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) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject)

Example 14 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 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(jakarta.ejb.EntityBean)

Example 15 with EntityBean

use of jakarta.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<>();
    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(jakarta.ejb.EJBLocalObject) EntityBean(jakarta.ejb.EntityBean) HashSet(java.util.HashSet)

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