Search in sources :

Example 41 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 42 with ThreadContext

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

the class JndiEncArtifact method readResolve.

public Object readResolve() throws ObjectStreamException {
    final ThreadContext thrdCntx = ThreadContext.getThreadContext();
    final BeanContext deployment = thrdCntx.getBeanContext();
    final Context cntx = deployment.getJndiEnc();
    try {
        final Object obj = cntx.lookup(path);
        if (obj == null) {
            throw new InvalidObjectException("JNDI ENC context reference could not be properly resolved when bean instance was activated");
        }
        return obj;
    } catch (final NamingException e) {
        throw (InvalidObjectException) new InvalidObjectException("JNDI ENC context reference could not be properly resolved due to a JNDI exception, when bean instance was activated").initCause(e);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext) InvalidObjectException(java.io.InvalidObjectException) NamingException(javax.naming.NamingException)

Example 43 with ThreadContext

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

the class javaURLContextFactory method getContext.

public static Context getContext() {
    final ThreadContext callContext = ThreadContext.getThreadContext();
    if (callContext == null) {
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        final ClassLoader current = Thread.currentThread().getContextClassLoader();
        final Context globalContext = containerSystem.getJNDIContext();
        if (current == null) {
            return globalContext;
        }
        for (final AppContext appContext : containerSystem.getAppContexts()) {
            for (final WebContext web : appContext.getWebContexts()) {
                // more specific first
                if (current.equals(web.getClassLoader())) {
                    return new ContextHandler(web.getJndiEnc());
                }
            }
            if (current.equals(appContext.getClassLoader())) {
                return new ContextHandler(appContext.getAppJndiContext());
            }
        }
        return globalContext;
    }
    final BeanContext di = callContext.getBeanContext();
    if (di != null) {
        return di.getJndiEnc();
    } else {
        final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
        return containerSystem.getJNDIContext();
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) BeanContext(org.apache.openejb.BeanContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) ContextHandler(org.apache.openejb.core.ivm.ContextHandler) BeanContext(org.apache.openejb.BeanContext) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 44 with ThreadContext

use of org.apache.openejb.core.ThreadContext 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 45 with ThreadContext

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

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