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);
}
}
};
}
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);
}
}
}
};
}
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);
}
}
Aggregations