Search in sources :

Example 36 with ThreadContext

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

the class CmpContainer method ejbPassivate.

private void ejbPassivate(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.PASSIVATE);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbPassivate();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 37 with ThreadContext

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

the class CmpContainer method ejbActivate.

private void ejbActivate(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.ACTIVATE);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbActivate();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 38 with ThreadContext

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

the class CmpContainer method ejbLoad.

private void ejbLoad(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.LOAD);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.ejbLoad();
    } catch (final RemoteException e) {
        throw new EJBException(e);
    } finally {
        ThreadContext.exit(oldCallContext);
    }
    // if we call load we must call store
    try {
        // noinspection unchecked
        Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE);
        if (registeredEntities == null) {
            registeredEntities = new LinkedHashSet<EntityBean>();
            synchronizationRegistry.putResource(ENTITIES_TO_STORE, registeredEntities);
            synchronizationRegistry.registerInterposedSynchronization(new Synchronization() {

                @Override
                public void beforeCompletion() {
                    // noinspection unchecked
                    final Set<EntityBean> registeredEntities = (LinkedHashSet<EntityBean>) synchronizationRegistry.getResource(ENTITIES_TO_STORE);
                    if (registeredEntities == null) {
                        return;
                    }
                    for (final EntityBean entityBean : registeredEntities) {
                        ejbStore(entityBean);
                    }
                }

                @Override
                public void afterCompletion(final int i) {
                }
            });
        }
        registeredEntities.add(entityBean);
    } catch (final Exception e) {
    // no-op
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ThreadContext(org.apache.openejb.core.ThreadContext) Synchronization(javax.transaction.Synchronization) ObjectNotFoundException(javax.ejb.ObjectNotFoundException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) NoSuchObjectException(java.rmi.NoSuchObjectException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) FinderException(javax.ejb.FinderException) EntityBean(javax.ejb.EntityBean) RemoteException(java.rmi.RemoteException) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException)

Example 39 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)

Example 40 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)

Aggregations

ThreadContext (org.apache.openejb.core.ThreadContext)68 BeanContext (org.apache.openejb.BeanContext)35 OpenEJBException (org.apache.openejb.OpenEJBException)23 Method (java.lang.reflect.Method)20 ApplicationException (org.apache.openejb.ApplicationException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)10 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)10 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 EJBException (javax.ejb.EJBException)9 SystemException (org.apache.openejb.SystemException)9 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)9 RemoteException (java.rmi.RemoteException)7 NamingException (javax.naming.NamingException)6 BeanTransactionPolicy (org.apache.openejb.core.transaction.BeanTransactionPolicy)6 JtaTransactionPolicy (org.apache.openejb.core.transaction.JtaTransactionPolicy)6 SystemInstance (org.apache.openejb.loader.SystemInstance)6 ArrayList (java.util.ArrayList)5 EJBAccessException (javax.ejb.EJBAccessException)5