use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class MdbContainer method beforeDelivery.
public void beforeDelivery(final BeanContext deployInfo, final Object instance, final Method method, final XAResource xaResource) throws SystemException {
// intialize call context
final ThreadContext callContext = new ThreadContext(deployInfo, null);
final ThreadContext oldContext = ThreadContext.enter(callContext);
// create mdb context
final MdbCallContext mdbCallContext = new MdbCallContext();
callContext.set(MdbCallContext.class, mdbCallContext);
mdbCallContext.deliveryMethod = method;
mdbCallContext.oldCallContext = oldContext;
// call the tx before method
try {
mdbCallContext.txPolicy = createTransactionPolicy(deployInfo.getTransactionType(method), callContext);
// if we have an xaResource and a transaction was not imported from the adapter, enlist the xaResource
if (xaResource != null && mdbCallContext.txPolicy.isNewTransaction()) {
mdbCallContext.txPolicy.enlistResource(xaResource);
}
} catch (final ApplicationException e) {
ThreadContext.exit(oldContext);
throw new SystemException("Should never get an Application exception", e);
} catch (final SystemException e) {
ThreadContext.exit(oldContext);
throw e;
} catch (final Exception e) {
ThreadContext.exit(oldContext);
throw new SystemException("Unable to enlist xa resource in the transaction", e);
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class OpenEJBEnricher method enrich.
public static void enrich(final Object testInstance, final AppContext appCtx) {
// don't rely on arquillian since this enrichment should absolutely be done before the following ones
new MockitoEnricher().enrich(testInstance);
AppContext ctx = appCtx;
if (ctx == null) {
ctx = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE);
if (ctx == null) {
return;
}
}
final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
final WebBeansContext appWBC = ctx.getWebBeansContext();
final BeanManagerImpl bm = appWBC == null ? null : appWBC.getBeanManagerImpl();
boolean ok = false;
for (final WebContext web : ctx.getWebContexts()) {
final WebBeansContext webBeansContext = web.getWebBeansContext();
if (webBeansContext == null) {
continue;
}
final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
if (webBeansContext != appWBC && webAppBm.isInUse()) {
try {
doInject(testInstance, context, webAppBm);
ok = true;
break;
} catch (final Exception e) {
// no-op, try next
}
}
}
if (bm != null && bm.isInUse() && !ok) {
try {
doInject(testInstance, context, bm);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
if (RuntimeException.class.isInstance(e)) {
throw RuntimeException.class.cast(e);
}
throw new OpenEJBRuntimeException(e);
}
}
if (context != null) {
final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
final ThreadContext oldContext = ThreadContext.enter(callContext);
try {
final InjectionProcessor processor = new InjectionProcessor<>(testInstance, context.getInjections(), context.getJndiContext());
processor.createInstance();
} catch (final OpenEJBException e) {
// ignored
} finally {
ThreadContext.exit(oldContext);
}
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class BaseEjbProxyHandler method getLiveHandleRegistry.
public ConcurrentMap getLiveHandleRegistry() {
final BeanContext beanContext = getBeanContext();
final ThreadContext tc = ThreadContext.getThreadContext();
if (tc != null && tc.getBeanContext() != beanContext && /* parent bean */
tc.getCurrentOperation() == Operation.BUSINESS) {
ProxyRegistry registry = tc.get(ProxyRegistry.class);
if (registry == null) {
registry = new ProxyRegistry();
tc.set(ProxyRegistry.class, registry);
}
return registry.liveHandleRegistry;
} else {
// use the tx if there
final SystemInstance systemInstance = SystemInstance.get();
final TransactionManager txMgr = systemInstance.getComponent(TransactionManager.class);
try {
final Transaction tx = txMgr.getTransaction();
if (tx != null && tx.getStatus() == Status.STATUS_ACTIVE) {
final TransactionSynchronizationRegistry registry = systemInstance.getComponent(TransactionSynchronizationRegistry.class);
final String resourceKey = ProxyRegistry.class.getName();
ConcurrentMap map = ConcurrentMap.class.cast(registry.getResource(resourceKey));
if (map == null) {
map = new ConcurrentHashMap();
registry.putResource(resourceKey, map);
try {
final ConcurrentMap tmp = map;
tx.registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// no-op
}
@Override
public void afterCompletion(final int status) {
tmp.clear();
}
});
} catch (final RollbackException e) {
// not really possible since we check the status
// let it go to default
}
}
return map;
}
} catch (final SystemException e) {
// let it go to default
}
// back to default but it doesnt release the memory
ProxyRegistry proxyRegistry = beanContext.get(ProxyRegistry.class);
if (proxyRegistry == null) {
proxyRegistry = new ProxyRegistry();
beanContext.set(ProxyRegistry.class, proxyRegistry);
}
return proxyRegistry.liveHandleRegistry;
}
}
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;
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class EntityContext method getPrimaryKey.
public Object getPrimaryKey() throws IllegalStateException {
doCheck(Call.getPrimaryKey);
final ThreadContext threadContext = ThreadContext.getThreadContext();
return threadContext.getPrimaryKey();
}
Aggregations