use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class SingletonContainer method _invoke.
protected Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType callType) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final Duration accessTimeout = getAccessTimeout(beanContext, runMethod);
final boolean read = javax.ejb.LockType.READ.equals(beanContext.getConcurrencyAttribute(runMethod));
final Lock lock = aquireLock(read, accessTimeout, instance, runMethod);
Object returnValue;
try {
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, callType), callContext);
returnValue = null;
try {
if (callType == InterfaceType.SERVICE_ENDPOINT) {
callContext.setCurrentOperation(Operation.BUSINESS_WS);
returnValue = invokeWebService(args, beanContext, runMethod, instance);
} else {
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, callType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors);
returnValue = interceptorStack.invoke(args);
}
} catch (final Throwable e) {
// handle reflection exception
final ExceptionType type = beanContext.getExceptionType(e);
if (type == ExceptionType.SYSTEM) {
/* System Exception ****************************/
// The bean instance is not put into the pool via instanceManager.poolInstance
// and therefore the instance will be garbage collected and destroyed.
// For this reason the discardInstance method of the StatelessInstanceManager
// does nothing.
handleSystemException(txPolicy, e, callContext);
} else {
/* Application Exception ***********************/
handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
}
} finally {
afterInvoke(txPolicy, callContext);
}
} finally {
lock.unlock();
}
return returnValue;
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class SingletonInstanceManager method freeInstance.
public void freeInstance(final ThreadContext callContext) {
final BeanContext beanContext = callContext.getBeanContext();
final Data data = (Data) beanContext.getContainerData();
final Future<Instance> instanceFuture = data.singleton.get();
// Possible the instance was never created
if (instanceFuture == null) {
return;
}
final Instance instance;
try {
instance = instanceFuture.get();
} catch (final InterruptedException e) {
Thread.interrupted();
logger.error("Singleton shutdown failed because the thread was interrupted: " + beanContext.getDeploymentID(), e);
return;
} catch (final ExecutionException e) {
// Instance was never initialized
return;
}
try {
callContext.setCurrentOperation(Operation.PRE_DESTROY);
callContext.setCurrentAllowedStates(null);
final Method remove = instance.bean instanceof SessionBean ? beanContext.getCreateMethod() : null;
final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
//Transaction Demarcation for Singleton PostConstruct method
TransactionType transactionType;
if (beanContext.getComponentType() == BeanType.SINGLETON) {
final Set<Method> callbacks = callbackInterceptors.get(callbackInterceptors.size() - 1).getPreDestroy();
if (callbacks.isEmpty()) {
transactionType = TransactionType.RequiresNew;
} else {
transactionType = beanContext.getTransactionType(callbacks.iterator().next());
if (transactionType == TransactionType.Required) {
transactionType = TransactionType.RequiresNew;
}
}
} else {
transactionType = beanContext.isBeanManagedTransaction() ? TransactionType.BeanManaged : TransactionType.NotSupported;
}
final TransactionPolicy transactionPolicy = EjbTransactionUtil.createTransactionPolicy(transactionType, callContext);
try {
//Call the chain
final CdiEjbBean<Object> bean = beanContext.get(CdiEjbBean.class);
if (bean != null) {
// TODO: see if it should be called before or after next call
bean.getInjectionTarget().preDestroy(instance.bean);
}
interceptorStack.invoke();
if (instance.creationalContext != null) {
instance.creationalContext.release();
}
} catch (final Throwable e) {
//RollBack Transaction
EjbTransactionUtil.handleSystemException(transactionPolicy, e, callContext);
} finally {
EjbTransactionUtil.afterInvoke(transactionPolicy, callContext);
}
} catch (final Throwable re) {
logger.error("Singleton shutdown failed: " + beanContext.getDeploymentID(), re);
}
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class StatelessContainer method _invoke.
@SuppressWarnings("ThrowFromFinallyBlock")
private Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType type) throws OpenEJBException {
final BeanContext beanContext = callContext.getBeanContext();
final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, type), callContext);
Object returnValue = null;
try {
if (type == InterfaceType.SERVICE_ENDPOINT) {
callContext.setCurrentOperation(Operation.BUSINESS_WS);
returnValue = invokeWebService(args, beanContext, runMethod, instance);
} else {
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final Operation operation = type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS;
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, operation, interceptors, instance.interceptors);
returnValue = interceptorStack.invoke(args);
}
} catch (final Throwable re) {
// handle reflection exception
final ExceptionType exceptionType = beanContext.getExceptionType(re);
if (exceptionType == ExceptionType.SYSTEM) {
/* System Exception ****************************/
// The bean instance is not put into the pool via instanceManager.poolInstance
// and therefore the instance will be garbage collected and destroyed.
// In case of StrictPooling flag being set to true we also release the semaphore
// in the discardInstance method of the instanceManager.
callContext.setDiscardInstance(true);
handleSystemException(txPolicy, re, callContext);
} else {
/* Application Exception ***********************/
handleApplicationException(txPolicy, re, exceptionType == ExceptionType.APPLICATION_ROLLBACK);
}
} finally {
try {
afterInvoke(txPolicy, callContext);
} catch (final SystemException | RuntimeException e) {
callContext.setDiscardInstance(true);
throw e;
}
}
return returnValue;
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class StatelessInstanceManager method freeInstance.
@SuppressWarnings("unchecked")
private void freeInstance(final ThreadContext callContext, final Instance instance) {
try {
callContext.setCurrentOperation(Operation.PRE_DESTROY);
final BeanContext beanContext = callContext.getBeanContext();
final Method remove = instance.bean instanceof SessionBean ? removeSessionBeanMethod : null;
final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
final CdiEjbBean<Object> bean = beanContext.get(CdiEjbBean.class);
if (bean != null) {
// TODO: see if it should be called before or after next call
bean.getInjectionTarget().preDestroy(instance.bean);
}
interceptorStack.invoke();
if (instance.creationalContext != null) {
instance.creationalContext.release();
}
} catch (final Throwable re) {
logger.error("The bean instance " + instance + " threw a system exception:" + re, re);
}
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class StatefulContainer method createEJBObject.
protected ProxyInfo createEJBObject(final BeanContext beanContext, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
// generate a new primary key
final Object primaryKey = newPrimaryKey();
final ThreadContext createContext = new ThreadContext(beanContext, primaryKey);
final ThreadContext oldCallContext = ThreadContext.enter(createContext);
Object runAs = null;
try {
if (oldCallContext != null) {
final BeanContext oldBc = oldCallContext.getBeanContext();
if (oldBc.getRunAsUser() != null || oldBc.getRunAs() != null) {
runAs = AbstractSecurityService.class.cast(securityService).overrideWithRunAsContext(createContext, beanContext, oldBc);
}
}
// Security check
checkAuthorization(callMethod, interfaceType);
// Create the extended entity managers for this instance
final Index<EntityManagerFactory, JtaEntityManagerRegistry.EntityManagerTracker> entityManagers = createEntityManagers(beanContext);
// Register the newly created entity managers
if (entityManagers != null) {
try {
entityManagerRegistry.addEntityManagers((String) beanContext.getDeploymentID(), primaryKey, entityManagers);
} catch (final EntityManagerAlreadyRegisteredException e) {
throw new EJBException(e);
}
}
createContext.setCurrentOperation(Operation.CREATE);
createContext.setCurrentAllowedStates(null);
// Start transaction
final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(createContext.getBeanContext().getTransactionType(callMethod, interfaceType), createContext);
Instance instance = null;
try {
try {
final InstanceContext context = beanContext.newInstance();
// Wrap-up everthing into a object
instance = new Instance(beanContext, primaryKey, containerID, context.getBean(), context.getCreationalContext(), context.getInterceptors(), entityManagers, lockFactory.newLock(primaryKey.toString()));
} catch (final Throwable throwable) {
final ThreadContext callContext = ThreadContext.getThreadContext();
EjbTransactionUtil.handleSystemException(callContext.getTransactionPolicy(), throwable, callContext);
// should never be reached
throw new IllegalStateException(throwable);
}
// add to cache
if (isPassivable(beanContext)) {
// no need to cache it it will never expires
cache.add(primaryKey, instance);
}
// instance starts checked-out
checkedOutInstances.put(primaryKey, instance);
// Register for synchronization callbacks
registerSessionSynchronization(instance, createContext);
// Invoke create for legacy beans
if (!callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessRemoteHome.class) && !callMethod.getDeclaringClass().equals(BeanContext.BusinessLocalBeanHome.class)) {
// Setup for business invocation
final Method createOrInit = beanContext.getMatchingBeanMethod(callMethod);
createContext.set(Method.class, createOrInit);
// Initialize interceptor stack
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, createOrInit, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap<String, Object>());
// Invoke
if (args == null) {
interceptorStack.invoke();
} else {
interceptorStack.invoke(args);
}
}
} catch (final Throwable e) {
handleException(createContext, txPolicy, e);
} finally {
// un register EntityManager
unregisterEntityManagers(instance, createContext);
afterInvoke(createContext, txPolicy, instance);
}
return new ProxyInfo(beanContext, primaryKey);
} finally {
if (runAs != null) {
try {
securityService.associate(runAs);
} catch (final LoginException e) {
// no-op
}
}
ThreadContext.exit(oldCallContext);
}
}
Aggregations