use of org.apache.openejb.SystemException in project tomee by apache.
the class EntityContainer method handleException.
private void handleException(final TransactionPolicy txPolicy, Throwable e, final ThreadContext callContext, final EntityBean bean) throws OpenEJBException {
final ExceptionType type;
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
type = callContext.getBeanContext().getExceptionType(e);
} else if (e instanceof ApplicationException) {
e = ((ApplicationException) e).getRootCause();
type = ExceptionType.APPLICATION;
} else if (e instanceof SystemException) {
e = ((SystemException) e).getRootCause();
type = ExceptionType.SYSTEM;
} else {
type = ExceptionType.SYSTEM;
}
if (type == ExceptionType.SYSTEM) {
// System Exception
if (bean != null) {
try {
instanceManager.discardInstance(callContext, bean);
} catch (final SystemException e1) {
logger.error("The instance manager encountered an unkown system exception while trying to discard the entity instance with primary key " + callContext.getPrimaryKey());
}
}
handleSystemException(txPolicy, e, callContext);
} else {
// Application Exception
instanceManager.poolInstance(callContext, bean, callContext.getPrimaryKey());
handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
}
}
use of org.apache.openejb.SystemException in project tomee by apache.
the class EjbHomeProxyHandler method _invoke.
@Override
protected Object _invoke(final Object proxy, final Class interfce, final Method method, final Object[] args) throws Throwable {
final String methodName = method.getName();
if (logger.isDebugEnabled()) {
logger.debug("EjbHomeProxyHandler: invoking method " + methodName + " on " + deploymentID);
}
try {
final Object retValue;
final MethodType operation = dispatchTable.get(methodName);
if (operation == null) {
retValue = homeMethod(interfce, method, args, proxy);
} else {
switch(operation) {
/*-- CREATE ------------- <HomeInterface>.create(<x>) ---*/
case CREATE:
retValue = create(interfce, method, args, proxy);
break;
case FIND:
retValue = findX(interfce, method, args, proxy);
break;
/*-- GET EJB METADATA ------ EJBHome.getEJBMetaData() ---*/
case META_DATA:
retValue = getEJBMetaData(method, args, proxy);
break;
/*-- GET HOME HANDLE -------- EJBHome.getHomeHandle() ---*/
case HOME_HANDLE:
retValue = getHomeHandle(method, args, proxy);
break;
/*-- REMOVE ------------------------ EJBHome.remove() ---*/
case REMOVE:
{
final Class type = method.getParameterTypes()[0];
/*-- HANDLE ------- EJBHome.remove(Handle handle) ---*/
if (Handle.class.isAssignableFrom(type)) {
retValue = removeWithHandle(interfce, method, args, proxy);
} else {
/*-- PRIMARY KEY ----- EJBHome.remove(Object key) ---*/
retValue = removeByPrimaryKey(interfce, method, args, proxy);
}
break;
}
default:
throw new OpenEJBRuntimeException("Inconsistent internal state: value " + operation + " for operation " + methodName);
}
}
if (logger.isDebugEnabled()) {
logger.debug("EjbHomeProxyHandler: finished invoking method " + method.getName() + ". Return value:" + retValue);
}
return retValue;
/*
* The ire is thrown by the container system and propagated by
* the server to the stub.
*/
} catch (final RemoteException re) {
if (interfaceType.isLocal()) {
throw new EJBException(re.getMessage()).initCause(re.detail);
} else {
throw re;
}
} catch (final InvalidateReferenceException ire) {
Throwable cause = ire.getRootCause();
if (cause instanceof RemoteException && interfaceType.isLocal()) {
final RemoteException re = (RemoteException) cause;
final Throwable detail = re.detail != null ? re.detail : re;
cause = new EJBException(re.getMessage()).initCause(detail);
}
throw cause;
/*
* Application exceptions must be reported dirctly to the client. They
* do not impact the viability of the proxy.
*/
} catch (final ApplicationException ae) {
final Throwable exc = ae.getRootCause() != null ? ae.getRootCause() : ae;
if (exc instanceof EJBAccessException) {
if (interfaceType.isBusiness()) {
throw exc;
} else {
if (interfaceType.isLocal()) {
throw (AccessLocalException) new AccessLocalException(exc.getMessage()).initCause(exc);
} else {
try {
throw new AccessException(exc.getMessage()).initCause(exc);
} catch (final IllegalStateException vmbug) {
// bug affects using initCause on any RemoteException subclasses in Sun 1.5_07 or lower
throw new AccessException(exc.getMessage(), (Exception) exc);
}
}
}
}
throw exc;
/*
* A system exception would be highly unusual and would indicate a sever
* problem with the container system.
*/
} catch (final SystemException se) {
if (interfaceType.isLocal()) {
throw new EJBException("Container has suffered a SystemException").initCause(se.getRootCause());
} else {
throw new RemoteException("Container has suffered a SystemException", se.getRootCause());
}
} catch (final OpenEJBException oe) {
if (interfaceType.isLocal()) {
throw new EJBException("Unknown Container Exception").initCause(oe.getRootCause());
} else {
throw new RemoteException("Unknown Container Exception", oe.getRootCause());
}
} catch (final Throwable t) {
logger.debug("EjbHomeProxyHandler: finished invoking method " + method.getName() + " with exception:" + t, t);
throw t;
}
}
use of org.apache.openejb.SystemException in project tomee by apache.
the class EntityInstanceManager method freeInstance.
public void freeInstance(final ThreadContext callContext, final EntityBean bean) throws SystemException {
discardInstance(callContext, bean);
final Operation currentOp = callContext.getCurrentOperation();
callContext.setCurrentOperation(Operation.UNSET_CONTEXT);
try {
/*
* unsetEntityContext executes in an unspecified transactional context. In this case we choose to
* allow it to have what every transaction context is current. Better then suspending it
* unnecessarily.
*
* We also chose not to invoke EntityContainer.invoke( ) method, which duplicate the exception handling
* logic but also attempt to manage the begining and end of a transaction. It its a container managed transaciton
* we don't want the TransactionScopeHandler commiting the transaction in afterInvoke() which is what it would attempt
* to do.
*/
bean.unsetEntityContext();
} catch (final Exception e) {
/*
* The EJB 1.1 specification does not specify how exceptions thrown by unsetEntityContext impact the
* transaction, if there is one. In this case we choose to do nothing since the instance is being disposed
* of anyway.
*/
logger.info(getClass().getName() + ".freeInstance: ignoring exception " + e + " on bean instance " + bean);
} finally {
callContext.setCurrentOperation(currentOp);
}
}
use of org.apache.openejb.SystemException 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.SystemException 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);
}
}
}
}
Aggregations