Search in sources :

Example 21 with ApplicationException

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

the class ManagedContainer method handleException.

private void handleException(final ThreadContext callContext, final TransactionPolicy txPolicy, final Throwable e) throws ApplicationException {
    if (e instanceof ApplicationException) {
        throw (ApplicationException) e;
    }
    final ExceptionType type = callContext.getBeanContext().getExceptionType(e);
    if (type == ExceptionType.SYSTEM) {
        discardInstance(callContext);
        EjbTransactionUtil.handleSystemException(txPolicy, e, callContext);
    } else {
        EjbTransactionUtil.handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
    }
}
Also used : ExceptionType(org.apache.openejb.core.ExceptionType) ApplicationException(org.apache.openejb.ApplicationException)

Example 22 with ApplicationException

use of org.apache.openejb.ApplicationException 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 23 with ApplicationException

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

the class SingletonInstanceManager method getInstance.

public Instance getInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    final AtomicReference<Future<Instance>> singleton = data.singleton;
    try {
        // Has the singleton been created yet?
        // If there is a Future object in the AtomicReference, then
        // it's either been created or is being created now.
        Future<Instance> singletonFuture = singleton.get();
        if (singletonFuture != null) {
            return singletonFuture.get();
        }
        // The singleton has not been created nor is being created
        // We will construct this FutureTask and compete with the
        // other threads for the right to create the singleton
        final FutureTask<Instance> task = new FutureTask<Instance>(new Callable<Instance>() {

            public Instance call() throws Exception {
                return createInstance(callContext, beanContext);
            }
        });
        do {
            // the singleton while the others wait.
            if (singleton.compareAndSet(null, task)) {
                task.run();
            }
        // If we didn't win the slot and no other FutureTask
        // has been set by a different thread, than we need
        // to try again.
        } while ((singletonFuture = singleton.get()) == null);
        // At this point we can safely return the singleton
        return singletonFuture.get();
    } catch (final InterruptedException e) {
        Thread.interrupted();
        throw new ApplicationException(new NoSuchEJBException("Singleton initialization interrupted").initCause(e));
    } catch (final ExecutionException e) {
        final Throwable throwable = e.getCause();
        if (throwable instanceof ApplicationException) {
            throw (ApplicationException) throwable;
        }
        throw new ApplicationException(new NoSuchEJBException("Singleton initialization failed").initCause(e.getCause()));
    }
}
Also used : NoSuchEJBException(javax.ejb.NoSuchEJBException) SystemInstance(org.apache.openejb.loader.SystemInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) NoSuchEJBException(javax.ejb.NoSuchEJBException) NamingException(javax.naming.NamingException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException) BeanContext(org.apache.openejb.BeanContext) ApplicationException(org.apache.openejb.ApplicationException) FutureTask(java.util.concurrent.FutureTask) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 24 with ApplicationException

use of org.apache.openejb.ApplicationException 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)

Example 25 with ApplicationException

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

the class CmpContainer 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 = securityService.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.equals("findByPrimaryKey")) {
                    return findByPrimaryKey(callMethod, args, callContext, type);
                } else if (methodName.startsWith("find")) {
                    return findEJBObject(callMethod, args, callContext, type);
                } else {
                    return homeMethod(callMethod, args, callContext, type);
                }
            } else if (methodName.equals("remove")) {
                removeEJBObject(callMethod, callContext, type);
                return null;
            }
        } else if ((EJBObject.class == declaringClass || EJBLocalObject.class == declaringClass) && methodName.equals("remove")) {
            removeEJBObject(callMethod, callContext, type);
            return null;
        }
        // business method
        callContext.setCurrentOperation(Operation.BUSINESS);
        final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
        callContext.set(Method.class, runMethod);
        return businessMethod(callMethod, runMethod, args, callContext, type);
    } 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

ApplicationException (org.apache.openejb.ApplicationException)26 OpenEJBException (org.apache.openejb.OpenEJBException)12 SystemException (org.apache.openejb.SystemException)11 BeanContext (org.apache.openejb.BeanContext)10 RemoteException (java.rmi.RemoteException)8 EJBAccessException (javax.ejb.EJBAccessException)7 InvalidateReferenceException (org.apache.openejb.InvalidateReferenceException)7 ThreadContext (org.apache.openejb.core.ThreadContext)7 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)7 Method (java.lang.reflect.Method)6 Operation (org.apache.openejb.core.Operation)6 SystemInstance (org.apache.openejb.loader.SystemInstance)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)5 EJBException (javax.ejb.EJBException)4 EJBHome (javax.ejb.EJBHome)4 EJBLocalHome (javax.ejb.EJBLocalHome)4 RemoveException (javax.ejb.RemoveException)4 NamingException (javax.naming.NamingException)4 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)4