Search in sources :

Example 11 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack in project tomee by apache.

the class SingletonInstanceManager method freeInstance.

public void freeInstance(final ThreadContext callContext) {
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    final Future<Instance> instanceFuture = data.singleton.get();
    // Possible the instance was never created
    if (instanceFuture == null) {
        return;
    }
    final Instance instance;
    try {
        instance = instanceFuture.get();
    } catch (final InterruptedException e) {
        Thread.interrupted();
        logger.error("Singleton shutdown failed because the thread was interrupted: " + beanContext.getDeploymentID(), e);
        return;
    } catch (final ExecutionException e) {
        // Instance was never initialized
        return;
    }
    try {
        callContext.setCurrentOperation(Operation.PRE_DESTROY);
        callContext.setCurrentAllowedStates(null);
        final Method remove = instance.bean instanceof SessionBean ? beanContext.getCreateMethod() : null;
        final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
        final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
        // Transaction Demarcation for Singleton PostConstruct method
        TransactionType transactionType;
        if (beanContext.getComponentType() == BeanType.SINGLETON) {
            final Set<Method> callbacks = callbackInterceptors.get(callbackInterceptors.size() - 1).getPreDestroy();
            if (callbacks.isEmpty()) {
                transactionType = TransactionType.RequiresNew;
            } else {
                transactionType = beanContext.getTransactionType(callbacks.iterator().next());
                if (transactionType == TransactionType.Required) {
                    transactionType = TransactionType.RequiresNew;
                }
            }
        } else {
            transactionType = beanContext.isBeanManagedTransaction() ? TransactionType.BeanManaged : TransactionType.NotSupported;
        }
        final TransactionPolicy transactionPolicy = EjbTransactionUtil.createTransactionPolicy(transactionType, callContext);
        try {
            // Call the chain
            final CdiEjbBean<Object> bean = beanContext.get(CdiEjbBean.class);
            if (bean != null) {
                // TODO: see if it should be called before or after next call
                bean.getInjectionTarget().preDestroy(instance.bean);
            }
            interceptorStack.invoke();
            if (instance.creationalContext != null) {
                instance.creationalContext.release();
            }
        } catch (final Throwable e) {
            // RollBack Transaction
            EjbTransactionUtil.handleSystemException(transactionPolicy, e, callContext);
        } finally {
            EjbTransactionUtil.afterInvoke(transactionPolicy, callContext);
        }
    } catch (final Throwable re) {
        logger.error("Singleton shutdown failed: " + beanContext.getDeploymentID(), re);
    }
}
Also used : TransactionType(org.apache.openejb.core.transaction.TransactionType) SystemInstance(org.apache.openejb.loader.SystemInstance) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Method(java.lang.reflect.Method) SessionBean(javax.ejb.SessionBean) BeanContext(org.apache.openejb.BeanContext) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack 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<InterceptorData>(), new HashMap<String, Object>());
                // 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) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 13 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack in project tomee by apache.

the class ManagedContainer method businessMethod.

protected Object businessMethod(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
    final ThreadContext callContext = new ThreadContext(beanContext, primKey);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        // Security check
        checkAuthorization(callMethod, interfaceType);
        // Start transaction
        final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
        Object returnValue = null;
        Instance instance = null;
        try {
            // Obtain instance
            instance = obtainInstance(primKey, callContext);
            // Resume previous Bean transaction if there was one
            if (txPolicy instanceof BeanTransactionPolicy) {
                final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
                if (suspendedTransaction != null) {
                    instance.setBeanTransaction(null);
                    final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
                    beanTxEnv.resumeUserTransaction(suspendedTransaction);
                }
            }
            // Register the entity managers
            registerEntityManagers(instance, callContext);
            // Register for synchronization callbacks
            registerSessionSynchronization(instance, callContext);
            // Setup for business invocation
            callContext.setCurrentOperation(Operation.BUSINESS);
            callContext.setCurrentAllowedStates(null);
            callContext.setInvokedInterface(callInterface);
            final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
            callContext.set(Method.class, runMethod);
            // Initialize interceptor stack
            final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
            final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.BUSINESS, interceptors, instance.interceptors);
            // Invoke
            returnValue = interceptorStack.invoke(args);
        } catch (final Throwable e) {
            handleException(callContext, txPolicy, e);
        } finally {
            // Commit transaction
            afterInvoke(callContext, txPolicy, instance);
        }
        return returnValue;
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : BeanTransactionPolicy(org.apache.openejb.core.transaction.BeanTransactionPolicy) SystemInstance(org.apache.openejb.loader.SystemInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) 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) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) Method(java.lang.reflect.Method)

Example 14 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack in project tomee by apache.

the class MdbContainer method _invoke.

private Object _invoke(final Object instance, final Method runMethod, final Object[] args, final BeanContext beanContext, final InterfaceType interfaceType, final MdbCallContext mdbCallContext) throws SystemException, ApplicationException {
    final Object returnValue;
    try {
        final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
        final InterceptorStack interceptorStack = new InterceptorStack(((Instance) instance).bean, runMethod, interfaceType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, ((Instance) instance).interceptors);
        returnValue = interceptorStack.invoke(args);
        return returnValue;
    } catch (Throwable e) {
        // unwrap invocation target exception
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        // Any exception thrown by reflection; not by the enterprise bean. Possible
        // Exceptions are:
        // IllegalAccessException - if the underlying method is inaccessible.
        // IllegalArgumentException - if the number of actual and formal parameters differ, or if an unwrapping conversion fails.
        // NullPointerException - if the specified object is null and the method is an instance method.
        // ExceptionInInitializerError - if the initialization provoked by this method fails.
        final ExceptionType type = beanContext.getExceptionType(e);
        if (type == ExceptionType.SYSTEM) {
            // 
            // / System Exception ****************************
            handleSystemException(mdbCallContext.txPolicy, e, ThreadContext.getThreadContext());
        } else {
            // 
            // Application Exception ***********************
            handleApplicationException(mdbCallContext.txPolicy, e, false);
        }
    }
    throw new AssertionError("Should not get here");
}
Also used : ExceptionType(org.apache.openejb.core.ExceptionType) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 15 with InterceptorStack

use of org.apache.openejb.core.interceptor.InterceptorStack in project tomee by apache.

the class MdbInstanceFactory method constructBean.

private Object constructBean() throws UnavailableException {
    final BeanContext beanContext = this.beanContext;
    final ThreadContext callContext = new ThreadContext(beanContext, null, Operation.INJECTION);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    try {
        final InstanceContext context = beanContext.newInstance();
        if (context.getBean() instanceof MessageDrivenBean) {
            callContext.setCurrentOperation(Operation.CREATE);
            final Method create = beanContext.getCreateMethod();
            final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList(), new HashMap());
            ejbCreate.invoke();
        }
        return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext());
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final String message = "The bean instance threw a system exception:" + e;
        MdbInstanceFactory.logger.error(message, e);
        throw new UnavailableException(message, e);
    } finally {
        ThreadContext.exit(oldContext);
    }
}
Also used : HashMap(java.util.HashMap) ThreadContext(org.apache.openejb.core.ThreadContext) ArrayList(java.util.ArrayList) UnavailableException(javax.resource.spi.UnavailableException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanContext(org.apache.openejb.BeanContext) InstanceContext(org.apache.openejb.core.InstanceContext) MessageDrivenBean(javax.ejb.MessageDrivenBean) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack)

Aggregations

InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)19 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)18 Method (java.lang.reflect.Method)14 BeanContext (org.apache.openejb.BeanContext)11 ThreadContext (org.apache.openejb.core.ThreadContext)10 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)10 SystemInstance (org.apache.openejb.loader.SystemInstance)8 SessionBean (javax.ejb.SessionBean)6 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)6 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)6 HashMap (java.util.HashMap)5 InstanceContext (org.apache.openejb.core.InstanceContext)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 ExceptionType (org.apache.openejb.core.ExceptionType)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 EJBLocalObject (javax.ejb.EJBLocalObject)3 EJBObject (javax.ejb.EJBObject)3 EntityManagerFactory (javax.persistence.EntityManagerFactory)3 LoginException (javax.security.auth.login.LoginException)3