Search in sources :

Example 11 with EJBLocalObject

use of javax.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(javax.ejb.FinderException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) EJBObject(javax.ejb.EJBObject) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) EJBLocalObject(javax.ejb.EJBLocalObject)

Example 12 with EJBLocalObject

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

the class EntityEjbHomeHandler method removeByPrimaryKey.

protected Object removeByPrimaryKey(final Class interfce, final Method method, final Object[] args, final Object proxy) throws Throwable {
    final Object primKey = args[0];
    // Check for the common mistake of passing the ejbObject instead of ejbObject.getPrimaryKey()
    if (primKey instanceof EJBLocalObject) {
        final Class ejbObjectProxyClass = primKey.getClass();
        String ejbObjectName = null;
        for (final Class clazz : ejbObjectProxyClass.getInterfaces()) {
            if (EJBLocalObject.class.isAssignableFrom(clazz)) {
                ejbObjectName = clazz.getSimpleName();
                break;
            }
        }
        throw new RemoveException("Invalid argument '" + ejbObjectName + "', expected primary key.  Update to ejbLocalHome.remove(" + lcfirst(ejbObjectName) + ".getPrimaryKey())");
    } else if (primKey instanceof EJBObject) {
        final Class ejbObjectProxyClass = primKey.getClass();
        String ejbObjectName = null;
        for (final Class clazz : ejbObjectProxyClass.getInterfaces()) {
            if (EJBObject.class.isAssignableFrom(clazz)) {
                ejbObjectName = clazz.getSimpleName();
                break;
            }
        }
        throw new RemoveException("Invalid argument '" + ejbObjectName + "', expected primary key.  Update to ejbHome.remove(" + lcfirst(ejbObjectName) + ".getPrimaryKey())");
    }
    container.invoke(deploymentID, interfaceType, interfce, method, args, primKey);
    /* 
        * This operation takes care of invalidating all the EjbObjectProxyHandlers associated with
        * the same RegistryId. See this.createProxy().
        */
    invalidateAllHandlers(EntityEjbObjectHandler.getRegistryId(container, deploymentID, primKey));
    return null;
}
Also used : RemoveException(javax.ejb.RemoveException) EJBObject(javax.ejb.EJBObject) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) EJBLocalObject(javax.ejb.EJBLocalObject)

Example 13 with EJBLocalObject

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

Example 14 with EJBLocalObject

use of javax.ejb.EJBLocalObject in project Payara by payara.

the class EJBLocalObjectInvocationHandler method invokeEJBLocalObjectMethod.

private Object invokeEJBLocalObjectMethod(String methodName, Object[] args) throws Exception {
    // Return value is null if target method returns void.
    Object returnValue = null;
    int methodIndex = -1;
    Exception caughtException = null;
    // so optimize by comparing as few characters as possible.
    try {
        switch(methodName.charAt(0)) {
            case 'r':
                // void remove();
                methodIndex = container.EJBLocalObject_remove;
                container.onEjbMethodStart(methodIndex);
                super.remove();
                break;
            case 'i':
                // boolean isIdentical(EJBLocalObject)
                // Convert the param into an EJBLocalObjectImpl.  Can't
                // assume it's an EJBLocalObject for an ejb that was deployed
                // using dynamic proxies.
                EJBLocalObject other = (EJBLocalObject) args[0];
                EJBLocalObjectImpl otherImpl = EJBLocalObjectImpl.toEJBLocalObjectImpl(other);
                methodIndex = container.EJBLocalObject_isIdentical;
                container.onEjbMethodStart(methodIndex);
                returnValue = super.isIdentical(otherImpl);
                break;
            case 'g':
                if (methodName.charAt(3) == 'E') {
                    // EJBLocalHome getEJBLocalHome();
                    methodIndex = container.EJBLocalObject_getEJBLocalHome;
                    container.onEjbMethodStart(methodIndex);
                    returnValue = super.getEJBLocalHome();
                } else {
                    // Object getPrimaryKey();
                    methodIndex = container.EJBLocalObject_getPrimaryKey;
                    container.onEjbMethodStart(methodIndex);
                    returnValue = super.getPrimaryKey();
                }
                break;
            default:
                throw new EJBException("unknown method = " + methodName);
        }
    } catch (Exception ex) {
        caughtException = ex;
        throw ex;
    } finally {
        if (methodIndex != -1) {
            container.onEjbMethodEnd(methodIndex, caughtException);
        }
    }
    return returnValue;
}
Also used : EJBLocalObject(javax.ejb.EJBLocalObject) EJBException(javax.ejb.EJBException) EJBLocalObject(javax.ejb.EJBLocalObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) EJBException(javax.ejb.EJBException)

Example 15 with EJBLocalObject

use of javax.ejb.EJBLocalObject in project Payara by payara.

the class EJBHashSet method add.

// -------------------------Public Methods------------------
/**
 * Adds the specified element to this set if it is not already
 * present.
 *
 * @param o element to be added to this set.
 * @return <tt>true</tt> if the set did not already contain the specified
 * element.
 * @see java.util.HashSet
 */
public boolean add(Object o) {
    // NOI18N
    logger.finest("---EJBHashSet.add---");
    assertIsValid();
    assertInTransaction();
    helper.assertInstanceOfLocalInterfaceImpl(o);
    Object pc = helper.convertEJBLocalObjectToPC((EJBLocalObject) o, pm, true);
    return pcSet.add(pc);
}
Also used : EJBLocalObject(javax.ejb.EJBLocalObject)

Aggregations

EJBLocalObject (javax.ejb.EJBLocalObject)17 EJBObject (javax.ejb.EJBObject)6 EJBException (javax.ejb.EJBException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 EntityBean (javax.ejb.EntityBean)2 BeanContext (org.apache.openejb.BeanContext)2 EjbContainerUtil (com.sun.ejb.containers.EjbContainerUtil)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CreateException (javax.ejb.CreateException)1 FinderException (javax.ejb.FinderException)1 RemoveException (javax.ejb.RemoveException)1 NamingException (javax.naming.NamingException)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 InternalErrorException (org.apache.openejb.InternalErrorException)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 CmpContainer (org.apache.openejb.core.cmp.CmpContainer)1