Search in sources :

Example 36 with BeanContext

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

the class EntityContainer method ejbStore_If_No_Transaction.

public void ejbStore_If_No_Transaction(final ThreadContext callContext, final EntityBean bean) throws Exception {
    final Operation currentOp = callContext.getCurrentOperation();
    if (currentOp == Operation.BUSINESS) {
        final TransactionPolicy callerTxPolicy = callContext.getTransactionPolicy();
        if (callerTxPolicy != null && callerTxPolicy.isTransactionActive()) {
            return;
        }
        final BeanContext beanContext = callContext.getBeanContext();
        final TransactionPolicy txPolicy = beanContext.getTransactionPolicyFactory().createTransactionPolicy(TransactionType.Supports);
        try {
            // double check we don't have an active transaction
            if (!txPolicy.isTransactionActive()) {
                callContext.setCurrentOperation(Operation.STORE);
                bean.ejbStore();
            }
        } catch (final Exception e) {
            instanceManager.discardInstance(callContext, bean);
            throw e;
        } finally {
            callContext.setCurrentOperation(currentOp);
            txPolicy.commit();
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Operation(org.apache.openejb.core.Operation) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) EJBAccessException(javax.ejb.EJBAccessException) NoSuchEntityException(javax.ejb.NoSuchEntityException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(org.apache.openejb.SystemException)

Example 37 with BeanContext

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

the class EntityContainer method createEJBObject.

protected ProxyInfo createEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    callContext.setCurrentOperation(Operation.CREATE);
    /*
        * According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...)
        * method executes in the same transaction context as the previous ejbCreate(...) method."
        *
        * For this reason the TransactionScopeHandler methods usally preformed by the invoke( )
        * operation must be handled here along with the call explicitly.
        * This ensures that the afterInvoke() is not processed between the ejbCreate and ejbPostCreate methods to
        * ensure that the ejbPostCreate executes in the same transaction context of the ejbCreate.
        * This would otherwise not be possible if container-managed transactions were used because
        * the TransactionScopeManager would attempt to commit the transaction immediately after the ejbCreate
        * and before the ejbPostCreate had a chance to execute.  Once the ejbPostCreate method execute the
        * super classes afterInvoke( ) method will be executed committing the transaction if its a CMT.
        */
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
    EntityBean bean = null;
    Object primaryKey = null;
    try {
        // Get new ready instance
        bean = instanceManager.obtainInstance(callContext);
        // Obtain the proper ejbCreate() method
        final Method ejbCreateMethod = beanContext.getMatchingBeanMethod(callMethod);
        // invoke the ejbCreate which returns the primary key
        primaryKey = ejbCreateMethod.invoke(bean, args);
        didCreateBean(callContext, bean);
        // determine post create callback method
        final Method ejbPostCreateMethod = beanContext.getMatchingPostCreateMethod(ejbCreateMethod);
        // create a new context containing the pk for the post create call
        final ThreadContext postCreateContext = new ThreadContext(beanContext, primaryKey);
        postCreateContext.setCurrentOperation(Operation.POST_CREATE);
        final ThreadContext oldContext = ThreadContext.enter(postCreateContext);
        try {
            // Invoke the ejbPostCreate method on the bean instance
            ejbPostCreateMethod.invoke(bean, args);
        // According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...)
        // method executes in the same transaction context as the previous ejbCreate(...) method."
        // 
        // The bean is first insterted using db.create( ) and then after ejbPostCreate( ) its
        // updated using db.update(). This protocol allows for visablity of the bean after ejbCreate
        // within the current trasnaction.
        } finally {
            ThreadContext.exit(oldContext);
        }
        // update pool
        instanceManager.poolInstance(callContext, bean, primaryKey);
    } catch (final Throwable e) {
        handleException(txPolicy, e, callContext, bean);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    return new ProxyInfo(beanContext, primaryKey);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) EntityBean(javax.ejb.EntityBean) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) Method(java.lang.reflect.Method)

Example 38 with BeanContext

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

the class EntityContainer method homeMethod.

protected Object homeMethod(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    callContext.setCurrentOperation(Operation.HOME);
    final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
    return invoke(type, callMethod, runMethod, args, callContext);
}
Also used : BeanContext(org.apache.openejb.BeanContext) Method(java.lang.reflect.Method)

Example 39 with BeanContext

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

the class EntityContainer method findMethod.

protected Object findMethod(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    callContext.setCurrentOperation(Operation.FIND);
    final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
    Object returnValue = invoke(type, callMethod, runMethod, args, callContext);
    /*
        * Find operations return either a single primary key or a collection of primary keys.
        * The primary keys are converted to ProxyInfo objects.
        */
    if (returnValue instanceof Collection) {
        final Iterator keys = ((Collection) returnValue).iterator();
        final Vector<ProxyInfo> proxies = new Vector<>();
        while (keys.hasNext()) {
            final Object primaryKey = keys.next();
            proxies.addElement(new ProxyInfo(beanContext, primaryKey));
        }
        returnValue = proxies;
    } else if (returnValue instanceof Enumeration) {
        final Enumeration keys = (Enumeration) returnValue;
        final Vector<ProxyInfo> proxies = new Vector<>();
        while (keys.hasMoreElements()) {
            final Object primaryKey = keys.nextElement();
            proxies.addElement(new ProxyInfo(beanContext, primaryKey));
        }
        returnValue = new ArrayEnumeration<>(proxies);
    } else {
        returnValue = new ProxyInfo(beanContext, returnValue);
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) Enumeration(java.util.Enumeration) ArrayEnumeration(org.apache.openejb.util.ArrayEnumeration) Iterator(java.util.Iterator) Collection(java.util.Collection) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) Method(java.lang.reflect.Method) Vector(java.util.Vector) ArrayEnumeration(org.apache.openejb.util.ArrayEnumeration)

Example 40 with BeanContext

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

the class BaseEjbProxyHandler method invoke.

@Override
public Object invoke(final Object proxy, Method method, Object[] args) throws Throwable {
    try {
        isValidReference(method);
    } catch (final IllegalStateException ise) {
        // bean was undeployed
        if (method.getName().equals("writeReplace")) {
            // session serialization, we just need to replace this
            final BeanContext beanContext = beanContextRef.get();
            if (beanContext != null) {
                return _writeReplace(proxy);
            }
        }
        throw ise;
    }
    if (args == null) {
        args = new Object[] {};
    }
    if (method.getDeclaringClass() == Object.class) {
        final String methodName = method.getName();
        switch(methodName) {
            case "toString":
                return toString();
            case "equals":
                return equals(args[0]) ? Boolean.TRUE : Boolean.FALSE;
            case "hashCode":
                return hashCode();
            default:
                throw new UnsupportedOperationException("Unknown method: " + method);
        }
    } else if (method.getDeclaringClass() == IntraVmProxy.class) {
        final String methodName = method.getName();
        if (methodName.equals("writeReplace")) {
            return _writeReplace(proxy);
        } else {
            throw new UnsupportedOperationException("Unknown method: " + method);
        }
    } else if (method.getDeclaringClass() == BeanContext.Removable.class) {
        return _invoke(proxy, BeanContext.Removable.class, method, args);
    }
    Class interfce = getInvokedInterface(method);
    final ThreadContext callContext = ThreadContext.getThreadContext();
    final Object localClientIdentity = ClientSecurity.getIdentity();
    try {
        if (callContext == null && localClientIdentity != null) {
            final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
            securityService.associate(localClientIdentity);
        }
        if (strategy == CLASSLOADER_COPY || getBeanContext().getInterfaceType(interfce) == InterfaceType.BUSINESS_REMOTE) {
            IntraVmCopyMonitor.pre(strategy);
            final ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(getBeanContext().getClassLoader());
            try {
                args = copyArgs(args);
                method = copyMethod(method);
                interfce = copyObj(interfce);
            } finally {
                Thread.currentThread().setContextClassLoader(oldClassLoader);
                IntraVmCopyMonitor.post();
            }
        } else if (strategy == COPY && args != null && args.length > 0) {
            IntraVmCopyMonitor.pre(strategy);
            try {
                args = copyArgs(args);
            } finally {
                IntraVmCopyMonitor.post();
            }
        }
        final IntraVmCopyMonitor.State oldStrategy = strategy;
        if (getBeanContext().isAsynchronous(method) || getBeanContext().getComponentType().equals(BeanType.MANAGED)) {
            strategy = IntraVmCopyMonitor.State.NONE;
        }
        try {
            final Object returnValue = _invoke(proxy, interfce, method, args);
            return copy(strategy, returnValue);
        } catch (Throwable throwable) {
            throwable = copy(strategy, throwable);
            throw convertException(throwable, method, interfce);
        } finally {
            strategy = oldStrategy;
        }
    } finally {
        if (callContext == null && localClientIdentity != null) {
            final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
            securityService.disassociate();
        }
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) BeanContext(org.apache.openejb.BeanContext) SecurityService(org.apache.openejb.spi.SecurityService)

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