Search in sources :

Example 11 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class ManagedContainer method obtainInstance.

private Instance obtainInstance(final Object primaryKey, final ThreadContext callContext) throws OpenEJBException {
    if (primaryKey == null) {
        throw new SystemException(new NullPointerException("Cannot obtain an instance of the stateful session bean with a null session id"));
    }
    final Transaction currentTransaction = getTransaction(callContext);
    // Find the instance
    Instance instance = checkedOutInstances.get(primaryKey);
    if (instance == null) {
        try {
            instance = cache.checkOut(primaryKey);
        } catch (final OpenEJBException e) {
            throw e;
        } catch (final Exception e) {
            throw new SystemException("Unexpected load exception", e);
        }
        // Did we find the instance?
        if (instance == null) {
            throw new InvalidateReferenceException(new NoSuchObjectException("Not Found"));
        }
        // remember instance until it is returned to the cache
        checkedOutInstances.put(primaryKey, instance);
    }
    synchronized (this) {
        if (instance.isInUse()) {
            // the bean is already being invoked; the only reentrant/concurrent operations allowed are Session synchronization callbacks
            final Operation currentOperation = callContext.getCurrentOperation();
            if (currentOperation != Operation.AFTER_COMPLETION && currentOperation != Operation.BEFORE_COMPLETION) {
                throw new ApplicationException(new RemoteException("Concurrent calls not allowed."));
            }
        }
        if (instance.getTransaction() != null) {
            if (!instance.getTransaction().equals(currentTransaction) && !instance.getLock().tryLock()) {
                throw new ApplicationException(new RemoteException("Instance is in a transaction and cannot be invoked outside that transaction.  See EJB 3.0 Section 4.4.4"));
            }
        } else {
            instance.setTransaction(currentTransaction);
        }
        // Mark the instance in use so we can detect reentrant calls
        instance.setInUse(true);
        return instance;
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) Transaction(javax.transaction.Transaction) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) EjbUserTransaction(org.apache.openejb.core.transaction.EjbUserTransaction) SystemInstance(org.apache.openejb.loader.SystemInstance) NoSuchObjectException(java.rmi.NoSuchObjectException) Operation(org.apache.openejb.core.Operation) RemoteException(java.rmi.RemoteException) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 12 with Operation

use of org.apache.openejb.core.Operation in project tomee by apache.

the class SingletonInstanceManager method createInstance.

private Instance createInstance(final ThreadContext callContext, final BeanContext beanContext) throws ApplicationException {
    try {
        initializeDependencies(beanContext);
        final InstanceContext context = beanContext.newInstance();
        if (context.getBean() instanceof SessionBean) {
            final Operation originalOperation = callContext.getCurrentOperation();
            try {
                callContext.setCurrentOperation(Operation.CREATE);
                final Method create = beanContext.getCreateMethod();
                final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap());
                ejbCreate.invoke();
            } finally {
                callContext.setCurrentOperation(originalOperation);
            }
        }
        final ReadWriteLock lock;
        if (beanContext.isBeanManagedConcurrency()) {
            // Bean-Managed Concurrency
            lock = new BeanManagedLock();
        } else {
            // Container-Managed Concurrency
            lock = new ReentrantReadWriteLock();
        }
        return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext(), lock);
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final String t = "The bean instance " + beanContext.getDeploymentID() + " threw a system exception:" + e;
        logger.error(t, e);
        throw new ApplicationException(new NoSuchEJBException("Singleton failed to initialize").initCause(e));
    }
}
Also used : NoSuchEJBException(javax.ejb.NoSuchEJBException) HashMap(java.util.HashMap) SystemInstance(org.apache.openejb.loader.SystemInstance) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method) SessionBean(javax.ejb.SessionBean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplicationException(org.apache.openejb.ApplicationException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) InstanceContext(org.apache.openejb.core.InstanceContext) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData)

Aggregations

Operation (org.apache.openejb.core.Operation)12 ApplicationException (org.apache.openejb.ApplicationException)9 SystemException (org.apache.openejb.SystemException)8 OpenEJBException (org.apache.openejb.OpenEJBException)7 RemoteException (java.rmi.RemoteException)6 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)6 NoSuchEntityException (javax.ejb.NoSuchEntityException)5 BeanContext (org.apache.openejb.BeanContext)5 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)5 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)4 TransactionRolledbackException (org.apache.openejb.core.transaction.TransactionRolledbackException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 NoSuchObjectException (java.rmi.NoSuchObjectException)3 EJBAccessException (javax.ejb.EJBAccessException)3 NoSuchObjectException (org.apache.openejb.core.NoSuchObjectException)3 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)3 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)3 EjbTransactionUtil.createTransactionPolicy (org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy)3 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)3