Search in sources :

Example 1 with EJBLocalObject

use of jakarta.ejb.EJBLocalObject 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(jakarta.ejb.EJBLocalObject) EjbObjectProxyHandler(org.apache.openejb.core.ivm.EjbObjectProxyHandler)

Example 2 with EJBLocalObject

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

the class BaseSessionContext method getEJBLocalObject.

public EJBLocalObject getEJBLocalObject() throws IllegalStateException {
    doCheck(Call.getEJBLocalObject);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    final BeanContext di = threadContext.getBeanContext();
    if (di.getLocalHomeInterface() == null) {
        throw new IllegalStateException("Bean does not have an EJBLocalObject interface: " + di.getDeploymentID());
    }
    return (EJBLocalObject) EjbObjectProxyHandler.createProxy(di, threadContext.getPrimaryKey(), InterfaceType.EJB_LOCAL, di.getLocalInterface());
}
Also used : BeanContext(org.apache.openejb.BeanContext) EJBLocalObject(jakarta.ejb.EJBLocalObject)

Example 3 with EJBLocalObject

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

the class Cmp2Util method getEjbProxy.

public static <Proxy extends EJBLocalObject> Proxy getEjbProxy(final BeanContext beanContext, final EntityBean entity) {
    if (entity == null) {
        return null;
    }
    // build the primary key
    final Object primaryKey = getPrimaryKey(beanContext, entity);
    // get the cmp container
    if (!(beanContext.getContainer() instanceof CmpContainer)) {
        throw new IllegalArgumentException("Proxy is not connected to a CMP container but is conect to " + beanContext.getContainer().getClass().getName());
    }
    final Proxy proxy = (Proxy) EjbObjectProxyHandler.createProxy(beanContext, primaryKey, InterfaceType.EJB_LOCAL_HOME, beanContext.getLocalInterface());
    return proxy;
}
Also used : CmpContainer(org.apache.openejb.core.cmp.CmpContainer) EJBObject(jakarta.ejb.EJBObject) EJBLocalObject(jakarta.ejb.EJBLocalObject)

Example 4 with EJBLocalObject

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

the class JpaCmpEngine method executeUpdateQuery.

public int executeUpdateQuery(final BeanContext beanContext, final String signature, Object[] args) throws FinderException {
    final EntityManager entityManager = getEntityManager(beanContext);
    Query query = createNamedQuery(entityManager, signature);
    if (query == null) {
        final int parenIndex = signature.indexOf('(');
        if (parenIndex > 0) {
            final String shortName = signature.substring(0, parenIndex);
            query = createNamedQuery(entityManager, shortName);
        }
        if (query == null) {
            throw new FinderException("No query defined for method " + signature);
        }
    }
    // 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);
        }
        query.setParameter(i + 1, arg);
    }
    final int result = query.executeUpdate();
    return result;
}
Also used : FinderException(jakarta.ejb.FinderException) EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) EJBObject(jakarta.ejb.EJBObject) EJBLocalObject(jakarta.ejb.EJBLocalObject) EJBObject(jakarta.ejb.EJBObject) EJBLocalObject(jakarta.ejb.EJBLocalObject)

Example 5 with EJBLocalObject

use of jakarta.ejb.EJBLocalObject 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)

Aggregations

EJBLocalObject (jakarta.ejb.EJBLocalObject)7 EJBObject (jakarta.ejb.EJBObject)4 EntityBean (jakarta.ejb.EntityBean)2 BeanContext (org.apache.openejb.BeanContext)2 FinderException (jakarta.ejb.FinderException)1 RemoveException (jakarta.ejb.RemoveException)1 EntityManager (jakarta.persistence.EntityManager)1 Query (jakarta.persistence.Query)1 HashSet (java.util.HashSet)1 List (java.util.List)1 InternalErrorException (org.apache.openejb.InternalErrorException)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 CmpContainer (org.apache.openejb.core.cmp.CmpContainer)1 EjbObjectProxyHandler (org.apache.openejb.core.ivm.EjbObjectProxyHandler)1 IntraVmProxy (org.apache.openejb.core.ivm.IntraVmProxy)1