try {
// Security check
checkAuthorization(deploymentInfo, callMethod, callInterface);
// If a bean managed transaction is active, the bean can not be removed
InterfaceType interfaceType = deploymentInfo.getInterfaceType(callInterface);
if (interfaceType.isComponent()) {
Instance instance = checkedOutInstances.get(primKey);
if (instance != null && instance.getBeanTransaction() != null) {
throw new ApplicationException(new RemoveException("A stateful EJB enrolled in a bean-managed transaction can not be removed"));
}
}
// Start transaction
TransactionPolicy txPolicy = createTransactionPolicy(callContext.getDeploymentInfo().getTransactionType(callMethod), callContext);
Object returnValue = null;
boolean retain = false;
Instance instance = null;
Method runMethod = null;
try {
// Obtain instance
instance = obtainInstance(primKey, callContext);
if (instance == null) throw new ApplicationException(new javax.ejb.NoSuchEJBException());
// Resume previous Bean transaction if there was one
if (txPolicy instanceof BeanTransactionPolicy){
// Resume previous Bean transaction if there was one
SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
if (suspendedTransaction != null) {
instance.setBeanTransaction(null);
BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
beanTxEnv.resumeUserTransaction(suspendedTransaction);
}
}
// Register the entity managers
registerEntityManagers(instance, callContext);
// Register for synchronization callbacks
registerSessionSynchronization(instance, callContext);
// Setup for remove invocation
callContext.setCurrentOperation(Operation.REMOVE);
callContext.setCurrentAllowedStates(StatefulContext.getStates());
callContext.setInvokedInterface(callInterface);
runMethod = deploymentInfo.getMatchingBeanMethod(callMethod);
callContext.set(Method.class, runMethod);
// Do not pass arguments on home.remove(remote) calls
Class<?> declaringClass = callMethod.getDeclaringClass();
if (declaringClass.equals(EJBHome.class) || declaringClass.equals(EJBLocalHome.class)){
args = new Object[]{};
}
// Initialize interceptor stack
List<InterceptorData> interceptors = deploymentInfo.getMethodInterceptors(runMethod);
InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.REMOVE, interceptors, instance.interceptors);
// Invoke
if (args == null){
returnValue = interceptorStack.invoke();
} else {
returnValue = interceptorStack.invoke(args);
}
} catch (Throwable e) {
if (interfaceType.isBusiness() && deploymentInfo.getExceptionType(e) == SYSTEM) {
retain = deploymentInfo.retainIfExeption(runMethod);
}
handleException(callContext, txPolicy, e);
} finally {
if (!retain) {