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