use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class ManagedUserTransaction method begin.
public void begin() throws NotSupportedException, SystemException {
userTransaction.begin();
// get the callContext
final ThreadContext callContext = ThreadContext.getThreadContext();
if (callContext == null) {
// someone is using the user transaction out side of the component
return;
}
// get the deployment info
final BeanContext beanContext = callContext.getBeanContext();
if (beanContext.getComponentType() != BeanType.MANAGED) {
// some other non-stateful ejb is using our user transaction
return;
}
// get the primary key
final Object primaryKey = callContext.getPrimaryKey();
if (primaryKey == null) {
// is is not a bean method
return;
}
jtaEntityManagerRegistry.transactionStarted((String) beanContext.getDeploymentID(), primaryKey);
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class MdbContainer method afterDelivery.
public void afterDelivery(final Object instance) throws SystemException {
// get the mdb call context
final ThreadContext callContext = ThreadContext.getThreadContext();
final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
// invoke the tx after method
try {
afterInvoke(mdbCallContext.txPolicy, callContext);
} catch (final ApplicationException e) {
throw new SystemException("Should never get an Application exception", e);
} finally {
ThreadContext.exit(mdbCallContext.oldCallContext);
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class MdbContainer method release.
public void release(final BeanContext deployInfo, final Object instance) {
// get the mdb call context
ThreadContext callContext = ThreadContext.getThreadContext();
boolean contextExitRequired = false;
if (callContext == null) {
callContext = new ThreadContext(deployInfo, null);
ThreadContext.enter(callContext);
contextExitRequired = true;
}
try {
// if we have an mdb call context we need to invoke the after invoke method
final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
if (mdbCallContext != null) {
try {
afterInvoke(mdbCallContext.txPolicy, callContext);
} catch (final Exception e) {
logger.error("error while releasing message endpoint", e);
} finally {
final EndpointFactory endpointFactory = (EndpointFactory) deployInfo.getContainerData();
endpointFactory.getInstanceFactory().freeInstance((Instance) instance, false);
}
}
} finally {
if (contextExitRequired) {
ThreadContext.exit(callContext);
}
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class MdbContainer method invoke.
public Object invoke(final Object instance, final Method method, final InterfaceType type, Object... args) throws SystemException, ApplicationException {
if (args == null) {
args = NO_ARGS;
}
// get the context data
final ThreadContext callContext = ThreadContext.getThreadContext();
final BeanContext deployInfo = callContext.getBeanContext();
final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
if (mdbCallContext == null) {
throw new IllegalStateException("beforeDelivery was not called");
}
// verify the delivery method passed to beforeDeliver is the same method that was invoked
if (!mdbCallContext.deliveryMethod.getName().equals(method.getName()) || !Arrays.deepEquals(mdbCallContext.deliveryMethod.getParameterTypes(), method.getParameterTypes())) {
throw new IllegalStateException("Delivery method specified in beforeDelivery is not the delivery method called");
}
// remember the return value or exception so it can be logged
Object returnValue = null;
OpenEJBException openEjbException = null;
final Operation oldOperation = callContext.getCurrentOperation();
callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
try {
if (logger.isDebugEnabled()) {
logger.info("invoking method " + method.getName() + " on " + deployInfo.getDeploymentID());
}
// determine the target method on the bean instance class
final Method targetMethod = deployInfo.getMatchingBeanMethod(method);
callContext.set(Method.class, targetMethod);
// invoke the target method
returnValue = _invoke(instance, targetMethod, args, deployInfo, type, mdbCallContext);
return returnValue;
} catch (final ApplicationException | SystemException e) {
openEjbException = e;
throw e;
} finally {
callContext.setCurrentOperation(oldOperation);
// Log the invocation results
if (logger.isDebugEnabled()) {
if (openEjbException == null) {
logger.debug("finished invoking method " + method.getName() + ". Return value:" + returnValue);
} else {
final Throwable exception = openEjbException.getRootCause() != null ? openEjbException.getRootCause() : openEjbException;
logger.debug("finished invoking method " + method.getName() + " with exception " + exception);
}
}
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class MdbInstanceFactory method constructBean.
private Object constructBean() throws UnavailableException {
final BeanContext beanContext = this.beanContext;
final ThreadContext callContext = new ThreadContext(beanContext, null, Operation.INJECTION);
final ThreadContext oldContext = ThreadContext.enter(callContext);
try {
final InstanceContext context = beanContext.newInstance();
if (context.getBean() instanceof MessageDrivenBean) {
callContext.setCurrentOperation(Operation.CREATE);
final Method create = beanContext.getCreateMethod();
final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList(), new HashMap());
ejbCreate.invoke();
}
return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext());
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
final String message = "The bean instance threw a system exception:" + e;
MdbInstanceFactory.logger.error(message, e);
throw new UnavailableException(message, e);
} finally {
ThreadContext.exit(oldContext);
}
}
Aggregations