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 MdbInstanceFactory method freeInstance.
/**
* Frees an instance no longer needed by the resource adapter. This method makes all the necessary lifecycle
* callbacks and decrements the instance count. This method should not be used to disposed of beans that have
* thrown a system exception. Instead the discardInstance method should be called.
*
* @param instance the bean instance to free
* @param ignoredInstanceCount
*/
public void freeInstance(final Instance instance, final boolean ignoredInstanceCount) {
if (instance == null) {
throw new NullPointerException("bean is null");
}
// decrement the instance count
if (!ignoredInstanceCount) {
synchronized (this) {
instanceCount--;
}
}
final ThreadContext callContext = ThreadContext.getThreadContext();
final Operation originalOperation = callContext == null ? null : callContext.getCurrentOperation();
final BaseContext.State[] originalAllowedStates = callContext == null ? null : callContext.getCurrentAllowedStates();
try {
// call post destroy method
if (callContext != null) {
callContext.setCurrentOperation(Operation.PRE_DESTROY);
}
final Method remove = instance.bean instanceof MessageDrivenBean ? MessageDrivenBean.class.getMethod("ejbRemove") : null;
final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
interceptorStack.invoke();
if (instance.creationalContext != null) {
instance.creationalContext.release();
}
} catch (final Throwable re) {
MdbInstanceFactory.logger.error("The bean instance " + instance.bean + " threw a system exception:" + re, re);
} finally {
if (callContext != null) {
callContext.setCurrentOperation(originalOperation);
callContext.setCurrentAllowedStates(originalAllowedStates);
}
}
}
use of org.apache.openejb.core.ThreadContext in project tomee by apache.
the class MdbPoolContainer 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, callContext);
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 MdbPoolContainer 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 MdbPoolContainer 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) {
callContext.setDiscardInstance(true);
throw new SystemException("Should never get an Application exception", e);
} finally {
if (instance != null) {
if (callContext.isDiscardInstance()) {
this.instanceManager.discardInstance(callContext, instance);
} else {
try {
this.instanceManager.poolInstance(callContext, instance);
} catch (OpenEJBException e) {
throw new SystemException("Should never get an OpenEJBException exception", e);
}
}
}
ThreadContext.exit(mdbCallContext.oldCallContext);
}
}
Aggregations