Search in sources :

Example 31 with ThreadContext

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

the class Injector method cdiInjections.

private static <T> void cdiInjections(final BeanContext context, final T object) {
    if (context == null || context.getWebBeansContext() == null) {
        return;
    }
    ThreadContext oldContext = null;
    final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
    oldContext = ThreadContext.enter(callContext);
    try {
        OWBInjector.inject(context.getWebBeansContext().getBeanManagerImpl(), object, null);
    } catch (final Throwable t) {
        logger().warning("an error occured while injecting the class '" + object.getClass().getName() + "': " + t.getMessage());
    } finally {
        ThreadContext.exit(oldContext);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext)

Example 32 with ThreadContext

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

the class TestObserver method switchLoader.

private void switchLoader(final EventContext<?> event) {
    if (!SystemInstance.isInitialized()) {
        event.proceed();
        return;
    }
    final BeanContext context = beanContext();
    ThreadContext oldCtx = null;
    ClassLoader oldCl = null;
    if (context != null) {
        oldCtx = ThreadContext.enter(new ThreadContext(context, null));
    } else {
        oldCl = Thread.currentThread().getContextClassLoader();
        final ClassLoaders classLoaders = classLoader.get();
        if (classLoaders != null) {
            final ClassLoader loader = classLoaders.classloaders.size() == 1 ? /*assume it is the one we want*/
            classLoaders.classloaders.values().iterator().next() : oldCl;
            setTCCL(loader);
        }
    }
    try {
        event.proceed();
    } finally {
        if (context != null) {
            ThreadContext.exit(oldCtx);
        } else {
            setTCCL(oldCl);
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 33 with ThreadContext

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

the class IsLoggedTest method isLogged.

@Test
public void isLogged() throws LoginException {
    final ThreadContext testContext = ThreadContext.getThreadContext();
    testContext.set(AbstractSecurityService.SecurityContext.class, null);
    final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
    final Object id = securityService.login("jonathan", "secret");
    securityService.associate(id);
    assertTrue(bean.isinRole("**"));
    assertFalse(bean.isinRole("whatever"));
    securityService.disassociate();
    securityService.logout(id);
    ThreadContext.enter(testContext);
}
Also used : SecurityService(org.apache.openejb.spi.SecurityService) ThreadContext(org.apache.openejb.core.ThreadContext) Test(org.junit.Test)

Example 34 with ThreadContext

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

the class ValidatorUtil method proxy.

// proxy because depending on when injection/threadcontext is set
// it is better to do it lazily
// this is mainly done for tests since the first lookup will work in TomEE
private static <T> T proxy(final Class<T> t, final String jndi) {
    return t.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { t }, new InvocationHandler() {

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            if (Object.class.equals(method.getDeclaringClass())) {
                return method.invoke(this);
            }
            final ThreadContext ctx = ThreadContext.getThreadContext();
            if (ctx != null) {
                return method.invoke(ctx.getBeanContext().getJndiContext().lookup(jndi), args);
            }
            // try to find from current ClassLoader
            // can lead to find the bad validator regarding module separation
            // but since it shares the same classloader
            // it will probably share the same config
            // so the behavior will be the same
            // + this code should rarely be used
            final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
            if (tccl == null) {
                return null;
            }
            final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
            Object value = null;
            for (final AppContext appContext : containerSystem.getAppContexts()) {
                final ClassLoader appContextClassLoader = appContext.getClassLoader();
                if (tccl.equals(appContextClassLoader) || appContextClassLoader.equals(tccl)) {
                    final Collection<String> tested = new ArrayList<String>();
                    for (final BeanContext bean : appContext.getBeanContexts()) {
                        if (BeanContext.Comp.class.equals(bean.getBeanClass())) {
                            final String uniqueId = bean.getModuleContext().getUniqueId();
                            if (tested.contains(uniqueId)) {
                                continue;
                            }
                            tested.add(uniqueId);
                            try {
                                value = containerSystem.getJNDIContext().lookup((jndi.endsWith("Factory") ? Assembler.VALIDATOR_FACTORY_NAMING_CONTEXT : Assembler.VALIDATOR_NAMING_CONTEXT) + uniqueId);
                                break;
                            } catch (final NameNotFoundException nnfe) {
                            // no-op
                            }
                        }
                    }
                    if (ClassLoader.getSystemClassLoader() != appContextClassLoader) {
                        break;
                    }
                // else we surely have a single AppContext so let's try WebContext
                }
                for (final WebContext web : appContext.getWebContexts()) {
                    final ClassLoader webClassLoader = web.getClassLoader();
                    if (webClassLoader.equals(tccl) || tccl.equals(webClassLoader)) {
                        value = web.getJndiEnc().lookup(jndi);
                        break;
                    }
                }
                if (value != null) {
                    break;
                }
            }
            if (value != null) {
                return method.invoke(value, args);
            }
            return null;
        }

        @Override
        public String toString() {
            return "Proxy::" + t.getName();
        }
    }));
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) NameNotFoundException(javax.naming.NameNotFoundException) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) BeanContext(org.apache.openejb.BeanContext)

Example 35 with ThreadContext

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

the class CmpContainer method unsetEntityContext.

private void unsetEntityContext(final EntityBean entityBean) {
    if (entityBean == null) {
        throw new NullPointerException("entityBean is null");
    }
    final ThreadContext callContext = createThreadContext(entityBean);
    callContext.setCurrentOperation(Operation.UNSET_CONTEXT);
    final ThreadContext oldCallContext = ThreadContext.enter(callContext);
    try {
        entityBean.unsetEntityContext();
    } 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)

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