Search in sources :

Example 66 with ThreadContext

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

the class RunAsRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final RunAs annotation = description.getAnnotation(RunAs.class);
            final As as = description.getAnnotation(As.class);
            String currentRole = role.get();
            // no more needed
            role.remove();
            if (annotation == null && as == null && currentRole == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            if (currentRole == null) {
                if (annotation == null) {
                    currentRole = as.value();
                } else {
                    currentRole = annotation.value();
                }
            }
            final String runAs = beanContext.getRunAs();
            final String runAsUser = beanContext.getRunAsUser();
            beanContext.setRunAs(currentRole);
            final ThreadContext old = ThreadContext.enter(new ThreadContext(beanContext, null));
            try {
                base.evaluate();
            } finally {
                // reset for next test
                ThreadContext.exit(old);
                beanContext.setRunAs(runAs);
                beanContext.setRunAsUser(runAsUser);
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) RunAs(javax.annotation.security.RunAs) Statement(org.junit.runners.model.Statement) RunAs(javax.annotation.security.RunAs) ThreadContext(org.apache.openejb.core.ThreadContext)

Example 67 with ThreadContext

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

the class TransactionRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
            final Transactional annotation2 = description.getAnnotation(Transactional.class);
            if (annotation == null && annotation2 == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
            final TransactionType transactionType = TransactionType.get(annotation == null ? TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
            beanContext.getMethodContext(method).setTransactionType(transactionType);
            ThreadContext tc = ThreadContext.getThreadContext();
            final boolean tcCreated;
            if (tc == null) {
                tcCreated = true;
                tc = ThreadContext.enter(new ThreadContext(beanContext, null));
            } else {
                tcCreated = false;
            }
            final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType, tc);
            try {
                base.evaluate();
            } finally {
                if (rollback) {
                    policy.setRollbackOnly();
                }
                EjbTransactionUtil.afterInvoke(policy, tc);
                if (tcCreated) {
                    ThreadContext.exit(tc);
                }
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionType(org.apache.openejb.core.transaction.TransactionType) TransactionAttribute(javax.ejb.TransactionAttribute) Statement(org.junit.runners.model.Statement) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) Transactional(javax.transaction.Transactional)

Example 68 with ThreadContext

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

the class SingleApplicationComposerRunner method start.

private static void start(final Class<?> marker) throws Exception {
    if (APP.get() == null) {
        final Class<?> type;
        final String typeStr = JavaSecurityManagers.getSystemProperty("tomee.application-composer.application");
        if (typeStr != null) {
            try {
                type = Thread.currentThread().getContextClassLoader().loadClass(typeStr);
            } catch (final ClassNotFoundException e) {
                throw new IllegalArgumentException(e);
            }
        } else if (marker == null) {
            throw new IllegalArgumentException("set tomee.application-composer.application system property or add a marker to the rule or runner");
        } else {
            final Iterator<Class<?>> descriptors = new AnnotationFinder(new FileArchive(Thread.currentThread().getContextClassLoader(), jarLocation(marker)), false).findAnnotatedClasses(Application.class).iterator();
            if (!descriptors.hasNext()) {
                throw new IllegalArgumentException("No descriptor class using @Application");
            }
            type = descriptors.next();
            if (descriptors.hasNext()) {
                throw new IllegalArgumentException("Ambiguous @Application: " + type + ", " + descriptors.next());
            }
        }
        try {
            APP.compareAndSet(null, type.newInstance());
        } catch (final InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
    if (!started) {
        final Object app = APP.get();
        final ApplicationComposers composers = new ApplicationComposers(app.getClass()) {

            @Override
            public void deployApp(final Object inputTestInstance) throws Exception {
                super.deployApp(inputTestInstance);
                if (!started) {
                    // done here for logging
                    final ThreadContext previous = ThreadContext.getThreadContext();
                    final ApplicationComposers comp = this;
                    final Thread hook = new Thread() {

                        @Override
                        public void run() {
                            try {
                                comp.after();
                            } catch (final Exception e) {
                                ThreadContext.exit(previous);
                                throw new IllegalStateException(e);
                            }
                        }
                    };
                    HOOK.set(hook);
                    Runtime.getRuntime().addShutdownHook(hook);
                    started = true;
                }
            }
        };
        composers.before(app);
        composers.handleLifecycle(app.getClass(), app);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) Iterator(java.util.Iterator) FileArchive(org.apache.xbean.finder.archive.FileArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder)

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