Search in sources :

Example 31 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class EjbSelect method execute_float.

public static float execute_float(final Object obj, final String methodSignature, final Object... args) throws FinderException {
    final BeanContext beanContext = (BeanContext) obj;
    final Container container = beanContext.getContainer();
    if (!(container instanceof CmpContainer)) {
        throw new FinderException("Deployment is not connected to a CmpContainer " + beanContext.getDeploymentID());
    }
    final CmpContainer cmpContainer = (CmpContainer) container;
    final Number result = (Number) cmpContainer.select(beanContext, methodSignature, "float", args);
    return result.floatValue();
}
Also used : BeanContext(org.apache.openejb.BeanContext) FinderException(javax.ejb.FinderException) CmpContainer(org.apache.openejb.core.cmp.CmpContainer) Container(org.apache.openejb.Container) CmpContainer(org.apache.openejb.core.cmp.CmpContainer)

Example 32 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class JpaCmpEngine method createBean.

public Object createBean(EntityBean bean, final ThreadContext callContext) throws CreateException {
    // TODO verify that extract primary key requires a flush followed by a merge
    final TransactionPolicy txPolicy = startTransaction("persist", callContext);
    creating.get().add(bean);
    try {
        final BeanContext beanContext = callContext.getBeanContext();
        final EntityManager entityManager = getEntityManager(beanContext);
        entityManager.persist(bean);
        entityManager.flush();
        bean = entityManager.merge(bean);
        // extract the primary key from the bean
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);
        return primaryKey;
    } finally {
        creating.get().remove(bean);
        commitTransaction("persist", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) SimpleKeyGenerator(org.apache.openejb.core.cmp.SimpleKeyGenerator) KeyGenerator(org.apache.openejb.core.cmp.KeyGenerator) ComplexKeyGenerator(org.apache.openejb.core.cmp.ComplexKeyGenerator) Cmp2KeyGenerator(org.apache.openejb.core.cmp.cmp2.Cmp2KeyGenerator)

Example 33 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class JpaCmpEngine method loadBean.

public Object loadBean(final ThreadContext callContext, final Object primaryKey) {
    final TransactionPolicy txPolicy = startTransaction("load", callContext);
    try {
        final BeanContext beanContext = callContext.getBeanContext();
        final Class<?> beanClass = beanContext.getCmpImplClass();
        // Try to load it from the entity manager
        final EntityManager entityManager = getEntityManager(beanContext);
        return entityManager.find(beanClass, primaryKey);
    } finally {
        commitTransaction("load", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)

Example 34 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class JpaCmpEngine method removeBean.

public void removeBean(final ThreadContext callContext) {
    final TransactionPolicy txPolicy = startTransaction("remove", callContext);
    try {
        final BeanContext deploymentInfo = callContext.getBeanContext();
        final Class<?> beanClass = deploymentInfo.getCmpImplClass();
        final EntityManager entityManager = getEntityManager(deploymentInfo);
        final Object primaryKey = callContext.getPrimaryKey();
        // Try to load it from the entity manager
        final Object bean = entityManager.find(beanClass, primaryKey);
        // remove the bean
        entityManager.remove(bean);
    } finally {
        commitTransaction("remove", callContext, txPolicy);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) EntityManager(javax.persistence.EntityManager) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject)

Example 35 with BeanContext

use of org.apache.openejb.BeanContext in project tomee by apache.

the class EntityContainer method invoke.

@Override
public Object invoke(final Object deployID, InterfaceType type, final Class callInterface, final Method callMethod, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = this.getBeanContext(deployID);
    if (beanContext == null) {
        throw new OpenEJBException("Deployment does not exist in this container. Deployment(id='" + deployID + "'), Container(id='" + containerID + "')");
    }
    // Use the backup way to determine call type if null was supplied.
    if (type == null) {
        type = beanContext.getInterfaceType(callInterface);
    }
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        final boolean authorized = type == InterfaceType.TIMEOUT || getSecurityService().isCallerAuthorized(callMethod, type);
        if (!authorized) {
            throw new ApplicationException(new EJBAccessException("Unauthorized Access by Principal Denied"));
        }
        final Class declaringClass = callMethod.getDeclaringClass();
        final String methodName = callMethod.getName();
        if (EJBHome.class.isAssignableFrom(declaringClass) || EJBLocalHome.class.isAssignableFrom(declaringClass)) {
            if (declaringClass != EJBHome.class && declaringClass != EJBLocalHome.class) {
                if (methodName.startsWith("create")) {
                    return createEJBObject(callMethod, args, callContext, type);
                } else if (methodName.startsWith("find")) {
                    return findMethod(callMethod, args, callContext, type);
                } else {
                    return homeMethod(callMethod, args, callContext, type);
                }
            } else if (methodName.equals("remove")) {
                removeEJBObject(callMethod, args, callContext, type);
                return null;
            }
        } else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) {
            removeEJBObject(callMethod, args, callContext, type);
            return null;
        }
        callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
        final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
        callContext.set(Method.class, runMethod);
        return invoke(type, callMethod, runMethod, args, callContext);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) EJBHome(javax.ejb.EJBHome) ThreadContext(org.apache.openejb.core.ThreadContext) Method(java.lang.reflect.Method) EJBAccessException(javax.ejb.EJBAccessException) EJBLocalHome(javax.ejb.EJBLocalHome)

Aggregations

BeanContext (org.apache.openejb.BeanContext)198 OpenEJBException (org.apache.openejb.OpenEJBException)40 ThreadContext (org.apache.openejb.core.ThreadContext)40 Method (java.lang.reflect.Method)38 ContainerSystem (org.apache.openejb.spi.ContainerSystem)28 ArrayList (java.util.ArrayList)27 AppContext (org.apache.openejb.AppContext)26 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)26 NamingException (javax.naming.NamingException)24 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)23 Context (javax.naming.Context)22 ApplicationException (org.apache.openejb.ApplicationException)20 HashMap (java.util.HashMap)19 EJBLocalObject (javax.ejb.EJBLocalObject)18 EJBObject (javax.ejb.EJBObject)18 SystemException (org.apache.openejb.SystemException)18 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)17 ModuleContext (org.apache.openejb.ModuleContext)16 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)16 FinderException (javax.ejb.FinderException)14