Search in sources :

Example 26 with ThreadContext

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

the class MdbContainer method beforeDelivery.

public void beforeDelivery(final BeanContext deployInfo, final Object instance, final Method method, final XAResource xaResource) throws SystemException {
    // intialize call context
    final ThreadContext callContext = new ThreadContext(deployInfo, null);
    final ThreadContext oldContext = ThreadContext.enter(callContext);
    // create mdb context
    final MdbCallContext mdbCallContext = new MdbCallContext();
    callContext.set(MdbCallContext.class, mdbCallContext);
    mdbCallContext.deliveryMethod = method;
    mdbCallContext.oldCallContext = oldContext;
    // call the tx before method
    try {
        mdbCallContext.txPolicy = createTransactionPolicy(deployInfo.getTransactionType(method), callContext);
        // if we have an xaResource and a transaction was not imported from the adapter, enlist the xaResource
        if (xaResource != null && mdbCallContext.txPolicy.isNewTransaction()) {
            mdbCallContext.txPolicy.enlistResource(xaResource);
        }
    } catch (final ApplicationException e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Should never get an Application exception", e);
    } catch (final SystemException e) {
        ThreadContext.exit(oldContext);
        throw e;
    } catch (final Exception e) {
        ThreadContext.exit(oldContext);
        throw new SystemException("Unable to enlist xa resource in the transaction", e);
    }
}
Also used : EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ThreadContext(org.apache.openejb.core.ThreadContext) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) ConstraintViolationException(javax.validation.ConstraintViolationException)

Example 27 with ThreadContext

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

the class OpenEJBEnricher method enrich.

public static void enrich(final Object testInstance, final AppContext appCtx) {
    // don't rely on arquillian since this enrichment should absolutely be done before the following ones
    new MockitoEnricher().enrich(testInstance);
    AppContext ctx = appCtx;
    if (ctx == null) {
        ctx = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE);
        if (ctx == null) {
            return;
        }
    }
    final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
    final WebBeansContext appWBC = ctx.getWebBeansContext();
    final BeanManagerImpl bm = appWBC == null ? null : appWBC.getBeanManagerImpl();
    boolean ok = false;
    for (final WebContext web : ctx.getWebContexts()) {
        final WebBeansContext webBeansContext = web.getWebBeansContext();
        if (webBeansContext == null) {
            continue;
        }
        final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
        if (webBeansContext != appWBC && webAppBm.isInUse()) {
            try {
                doInject(testInstance, context, webAppBm);
                ok = true;
                break;
            } catch (final Exception e) {
            // no-op, try next
            }
        }
    }
    if (bm != null && bm.isInUse() && !ok) {
        try {
            doInject(testInstance, context, bm);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
            if (RuntimeException.class.isInstance(e)) {
                throw RuntimeException.class.cast(e);
            }
            throw new OpenEJBRuntimeException(e);
        }
    }
    if (context != null) {
        final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
        final ThreadContext oldContext = ThreadContext.enter(callContext);
        try {
            final InjectionProcessor processor = new InjectionProcessor<>(testInstance, context.getInjections(), context.getJndiContext());
            processor.createInstance();
        } catch (final OpenEJBException e) {
        // ignored
        } finally {
            ThreadContext.exit(oldContext);
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenEJBException(org.apache.openejb.OpenEJBException) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) InjectionProcessor(org.apache.openejb.InjectionProcessor) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) MockitoEnricher(org.apache.openejb.arquillian.common.mockito.MockitoEnricher)

Example 28 with ThreadContext

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

the class BaseEjbProxyHandler method getLiveHandleRegistry.

public ConcurrentMap getLiveHandleRegistry() {
    final BeanContext beanContext = getBeanContext();
    final ThreadContext tc = ThreadContext.getThreadContext();
    if (tc != null && tc.getBeanContext() != beanContext && /* parent bean */
    tc.getCurrentOperation() == Operation.BUSINESS) {
        ProxyRegistry registry = tc.get(ProxyRegistry.class);
        if (registry == null) {
            registry = new ProxyRegistry();
            tc.set(ProxyRegistry.class, registry);
        }
        return registry.liveHandleRegistry;
    } else {
        // use the tx if there
        final SystemInstance systemInstance = SystemInstance.get();
        final TransactionManager txMgr = systemInstance.getComponent(TransactionManager.class);
        try {
            final Transaction tx = txMgr.getTransaction();
            if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
                final TransactionSynchronizationRegistry registry = systemInstance.getComponent(TransactionSynchronizationRegistry.class);
                final String resourceKey = ProxyRegistry.class.getName();
                ConcurrentMap map = ConcurrentMap.class.cast(registry.getResource(resourceKey));
                if (map == null) {
                    map = new ConcurrentHashMap();
                    registry.putResource(resourceKey, map);
                    try {
                        final ConcurrentMap tmp = map;
                        tx.registerSynchronization(new Synchronization() {

                            @Override
                            public void beforeCompletion() {
                            // no-op
                            }

                            @Override
                            public void afterCompletion(final int status) {
                                tmp.clear();
                            }
                        });
                    } catch (final RollbackException e) {
                    // not really possible since we check the status
                    // let it go to default
                    }
                }
                return map;
            }
        } catch (final SystemException e) {
        // let it go to default
        }
        // back to default but it doesnt release the memory
        ProxyRegistry proxyRegistry = beanContext.get(ProxyRegistry.class);
        if (proxyRegistry == null) {
            proxyRegistry = new ProxyRegistry();
            beanContext.set(ProxyRegistry.class, proxyRegistry);
        }
        return proxyRegistry.liveHandleRegistry;
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) ConcurrentMap(java.util.concurrent.ConcurrentMap) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) BeanContext(org.apache.openejb.BeanContext) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) SystemInstance(org.apache.openejb.loader.SystemInstance) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 29 with ThreadContext

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

the class ContextHandler method lookup.

@Override
public Object lookup(final String name) throws NamingException {
    try {
        if ("java:".equals(name)) {
            return context;
        }
        return context.lookup(name);
    } catch (final UndeclaredThrowableException ute) {
        Throwable e = ute.getUndeclaredThrowable();
        while (e != null) {
            if (InvocationTargetException.class.isInstance(e)) {
                final Throwable unwrap = InvocationTargetException.class.cast(e).getCause();
                if (e == unwrap) {
                    throw new NameNotFoundException(name);
                }
                e = unwrap;
            } else if (UndeclaredThrowableException.class.isInstance(e)) {
                final Throwable unwrap = UndeclaredThrowableException.class.cast(e).getUndeclaredThrowable();
                if (e == unwrap) {
                    throw new NameNotFoundException(name);
                }
                e = unwrap;
            } else {
                break;
            }
            if (NameNotFoundException.class.isInstance(e)) {
                throw NameNotFoundException.class.cast(e);
            }
        }
        throw new NameNotFoundException(name);
    } catch (final NameNotFoundException nnfe) {
        try {
            return SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup(name);
        } catch (final NameNotFoundException nnfe2) {
        // ignore, let it be thrown
        }
        try {
            final ThreadContext threadContext = ThreadContext.getThreadContext();
            if (threadContext != null) {
                return threadContext.getBeanContext().getModuleContext().getModuleJndiContext().lookup(name);
            }
        } catch (final Exception nnfe3) {
        // ignore, let it be thrown
        }
        throw nnfe;
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) NameNotFoundException(javax.naming.NameNotFoundException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ThreadContext(org.apache.openejb.core.ThreadContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 30 with ThreadContext

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

the class EntityContext method getPrimaryKey.

public Object getPrimaryKey() throws IllegalStateException {
    doCheck(Call.getPrimaryKey);
    final ThreadContext threadContext = ThreadContext.getThreadContext();
    return threadContext.getPrimaryKey();
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext)

Aggregations

ThreadContext (org.apache.openejb.core.ThreadContext)62 BeanContext (org.apache.openejb.BeanContext)33 Method (java.lang.reflect.Method)19 OpenEJBException (org.apache.openejb.OpenEJBException)18 ApplicationException (org.apache.openejb.ApplicationException)10 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)10 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)10 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)10 EJBException (javax.ejb.EJBException)9 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 RemoteException (java.rmi.RemoteException)7 SystemInstance (org.apache.openejb.loader.SystemInstance)7 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)6 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)6 ArrayList (java.util.ArrayList)5 EJBAccessException (javax.ejb.EJBAccessException)5 EJBHome (javax.ejb.EJBHome)5 EJBLocalHome (javax.ejb.EJBLocalHome)5 EJBLocalObject (javax.ejb.EJBLocalObject)5