Search in sources :

Example 6 with ProxyInfo

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

the class EjbRequestHandler method doEjbHome_CREATE.

protected void doEjbHome_CREATE(final EJBRequest req, final EJBResponse res) throws Exception {
    final CallContext call = CallContext.getCallContext();
    final RpcContainer c = (RpcContainer) call.getBeanContext().getContainer();
    Object result = c.invoke(req.getDeploymentId(), InterfaceType.EJB_HOME, req.getInterfaceClass(), req.getMethodInstance(), req.getMethodParameters(), req.getPrimaryKey());
    if (result instanceof ProxyInfo) {
        final ProxyInfo info = (ProxyInfo) result;
        res.setResponse(req.getVersion(), ResponseCodes.EJB_OK, info.getPrimaryKey());
    } else {
        result = new RemoteException("The bean is not EJB compliant.  The bean should be created or and exception should be thrown.");
        LOGGER.error(req + "The bean is not EJB compliant.  The bean should be created or and exception should be thrown.");
        res.setResponse(req.getVersion(), ResponseCodes.EJB_SYS_EXCEPTION, new ThrowableArtifact((Throwable) result));
    }
}
Also used : ProxyInfo(org.apache.openejb.ProxyInfo) RpcContainer(org.apache.openejb.RpcContainer) ThrowableArtifact(org.apache.openejb.client.ThrowableArtifact) RemoteException(java.rmi.RemoteException)

Example 7 with ProxyInfo

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

the class CmpContainer method createEJBObject.

private ProxyInfo createEJBObject(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    final EntityBean bean;
    Object primaryKey = null;
    try {
        // Obtain a bean instance from the method ready pool
        bean = createNewInstance(callContext);
        // set the entity context
        setEntityContext(bean);
        // Obtain the proper ejbCreate() method
        final Method ejbCreateMethod = beanContext.getMatchingBeanMethod(callMethod);
        // Set current operation for allowed operations
        callContext.setCurrentOperation(Operation.CREATE);
        // Invoke the proper ejbCreate() method on the instance
        ejbCreateMethod.invoke(bean, args);
        // create the new bean
        primaryKey = cmpEngine.createBean(bean, callContext);
        // 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);
        }
        // when there is not transaction, merge the data from the bean back into the cmp engine
        cmpEngine.storeBeanIfNoTx(callContext, bean);
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final ExceptionType type = callContext.getBeanContext().getExceptionType(e);
        if (type == ExceptionType.SYSTEM) {
            /* System Exception ****************************/
            handleSystemException(txPolicy, e, callContext);
        } else {
            /* Application Exception ***********************/
            handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
        }
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    return new ProxyInfo(beanContext, primaryKey);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) 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) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with ProxyInfo

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

the class CmpContainer method findByPrimaryKey.

private Object findByPrimaryKey(final Method callMethod, final Object[] args, final ThreadContext callContext, final InterfaceType interfaceType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, interfaceType), callContext);
    try {
        final EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext, args[0]);
        if (bean == null) {
            throw new ObjectNotFoundException(beanContext.getDeploymentID() + " : " + args[0]);
        }
        // rebuild the primary key
        final KeyGenerator kg = beanContext.getKeyGenerator();
        final Object primaryKey = kg.getPrimaryKey(bean);
        // create a new ProxyInfo based on the deployment info and primary key
        return new ProxyInfo(beanContext, primaryKey);
    } catch (final FinderException fe) {
        handleApplicationException(txPolicy, fe, false);
    } catch (final Throwable e) {
        // handle reflection exception
        handleSystemException(txPolicy, e, callContext);
    } finally {
        afterInvoke(txPolicy, callContext);
    }
    throw new AssertionError("Should not get here");
}
Also used : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) FinderException(javax.ejb.FinderException) EntityBean(javax.ejb.EntityBean) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject)

Example 9 with ProxyInfo

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

the class ManagedContainer method createEJBObject.

protected ProxyInfo createEJBObject(final BeanContext beanContext, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    // generate a new primary key
    final Object primaryKey = newPrimaryKey();
    final ThreadContext createContext = new ThreadContext(beanContext, primaryKey);
    final ThreadContext oldCallContext = ThreadContext.enter(createContext);
    try {
        // Security check
        checkAuthorization(callMethod, interfaceType);
        // Create the extended entity managers for this instance
        final Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = createEntityManagers(beanContext);
        // Register the newly created entity managers
        if (entityManagers != null) {
            try {
                entityManagerRegistry.addEntityManagers((String) beanContext.getDeploymentID(), primaryKey, entityManagers);
            } catch (final EntityManagerAlreadyRegisteredException e) {
                throw new EJBException(e);
            }
        }
        createContext.setCurrentOperation(Operation.CREATE);
        createContext.setCurrentAllowedStates(null);
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(createContext.getBeanContext().getTransactionType(callMethod, interfaceType), createContext);
        Instance instance = null;
        try {
            try {
                final InstanceContext context = beanContext.newInstance();
                // Wrap-up everthing into a object
                instance = new Instance(beanContext, primaryKey, context.getBean(), context.getInterceptors(), context.getCreationalContext(), entityManagers);
            } catch (final Throwable throwable) {
                final ThreadContext callContext = ThreadContext.getThreadContext();
                EjbTransactionUtil.handleSystemException(callContext.getTransactionPolicy(), throwable, callContext);
                // should never be reached
                throw new IllegalStateException(throwable);
            }
            // add to cache
            cache.add(primaryKey, instance);
            // instance starts checked-out
            checkedOutInstances.put(primaryKey, instance);
            // Register for synchronization callbacks
            registerSessionSynchronization(instance, createContext);
            // Invoke create for legacy beans
            if (!callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessRemoteHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalBeanHome.class)) {
                // Setup for business invocation
                final Method createOrInit = beanContext.getMatchingBeanMethod(callMethod);
                createContext.set(Method.class, createOrInit);
                // Initialize interceptor stack
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, createOrInit, Operation.CREATE, new ArrayList<>(), new HashMap<>());
                // Invoke
                if (args == null) {
                    interceptorStack.invoke();
                } else {
                    interceptorStack.invoke(args);
                }
            }
        } catch (final Throwable e) {
            handleException(createContext, txPolicy, e);
        } finally {
            afterInvoke(createContext, txPolicy, instance);
        }
        return new ProxyInfo(beanContext, primaryKey);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) SystemInstance(org.apache.openejb.loader.SystemInstance) ThreadContext(org.apache.openejb.core.ThreadContext) JtaTransactionPolicy(org.apache.openejb.core.transaction.JtaTransactionPolicy) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) Method(java.lang.reflect.Method) BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo) InstanceContext(org.apache.openejb.core.InstanceContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 10 with ProxyInfo

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

the class EjbHomeProxyHandler method create.

protected Object create(final Class interfce, final Method method, final Object[] args, final Object proxy) throws Throwable {
    if (container.getBeanContext(deploymentID) == null) {
        final BeanContext bc = getBeanContext();
        synchronized (bc.getId()) {
            if (container.getBeanContext(deploymentID) == null) {
                container.deploy(bc);
                container.start(bc);
            }
        }
    }
    final ProxyInfo proxyInfo = (ProxyInfo) container.invoke(deploymentID, interfaceType, interfce, method, args, null);
    assert proxyInfo != null : "Container returned a null ProxyInfo: ContainerID=" + container.getContainerID();
    return createProxy(proxyInfo.getPrimaryKey(), getMainInterface());
}
Also used : BeanContext(org.apache.openejb.BeanContext) ProxyInfo(org.apache.openejb.ProxyInfo)

Aggregations

ProxyInfo (org.apache.openejb.ProxyInfo)13 BeanContext (org.apache.openejb.BeanContext)10 Method (java.lang.reflect.Method)7 EJBLocalObject (javax.ejb.EJBLocalObject)6 EJBObject (javax.ejb.EJBObject)6 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)6 ThreadContext (org.apache.openejb.core.ThreadContext)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 EntityBean (javax.ejb.EntityBean)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)4 Enumeration (java.util.Enumeration)3 RemoteException (java.rmi.RemoteException)2 Vector (java.util.Vector)2 EJBException (javax.ejb.EJBException)2 FinderException (javax.ejb.FinderException)2 ObjectNotFoundException (javax.ejb.ObjectNotFoundException)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 LoginException (javax.security.auth.login.LoginException)2