use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class MdbContainer method _invoke.
private Object _invoke(final Object instance, final Method runMethod, final Object[] args, final BeanContext beanContext, final InterfaceType interfaceType, final MdbCallContext mdbCallContext) throws SystemException, ApplicationException {
final Object returnValue;
try {
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final InterceptorStack interceptorStack = new InterceptorStack(((Instance) instance).bean, runMethod, interfaceType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, ((Instance) instance).interceptors);
returnValue = interceptorStack.invoke(args);
return returnValue;
} catch (Throwable e) {
// unwrap invocation target exception
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
// Any exception thrown by reflection; not by the enterprise bean. Possible
// Exceptions are:
// IllegalAccessException - if the underlying method is inaccessible.
// IllegalArgumentException - if the number of actual and formal parameters differ, or if an unwrapping conversion fails.
// NullPointerException - if the specified object is null and the method is an instance method.
// ExceptionInInitializerError - if the initialization provoked by this method fails.
final ExceptionType type = beanContext.getExceptionType(e);
if (type == ExceptionType.SYSTEM) {
//
/// System Exception ****************************
handleSystemException(mdbCallContext.txPolicy, e, ThreadContext.getThreadContext());
} else {
//
// Application Exception ***********************
handleApplicationException(mdbCallContext.txPolicy, e, false);
}
}
throw new AssertionError("Should not get here");
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class ManagedContainer method businessMethod.
protected Object businessMethod(final BeanContext beanContext, final Object primKey, final Class callInterface, final Method callMethod, final Object[] args, final InterfaceType interfaceType) throws OpenEJBException {
final ThreadContext callContext = new ThreadContext(beanContext, primKey);
final ThreadContext oldCallContext = ThreadContext.enter(callContext);
try {
// Security check
checkAuthorization(callMethod, interfaceType);
// Start transaction
final TransactionPolicy txPolicy = EjbTransactionUtil.createTransactionPolicy(callContext.getBeanContext().getTransactionType(callMethod, interfaceType), callContext);
Object returnValue = null;
Instance instance = null;
try {
// Obtain instance
instance = obtainInstance(primKey, callContext);
// Resume previous Bean transaction if there was one
if (txPolicy instanceof BeanTransactionPolicy) {
final SuspendedTransaction suspendedTransaction = instance.getBeanTransaction();
if (suspendedTransaction != null) {
instance.setBeanTransaction(null);
final BeanTransactionPolicy beanTxEnv = (BeanTransactionPolicy) txPolicy;
beanTxEnv.resumeUserTransaction(suspendedTransaction);
}
}
// Register the entity managers
registerEntityManagers(instance, callContext);
// Register for synchronization callbacks
registerSessionSynchronization(instance, callContext);
// Setup for business invocation
callContext.setCurrentOperation(Operation.BUSINESS);
callContext.setCurrentAllowedStates(null);
callContext.setInvokedInterface(callInterface);
final Method runMethod = beanContext.getMatchingBeanMethod(callMethod);
callContext.set(Method.class, runMethod);
// Initialize interceptor stack
final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, Operation.BUSINESS, interceptors, instance.interceptors);
// Invoke
returnValue = interceptorStack.invoke(args);
} catch (final Throwable e) {
handleException(callContext, txPolicy, e);
} finally {
// Commit transaction
afterInvoke(callContext, txPolicy, instance);
}
return returnValue;
} finally {
ThreadContext.exit(oldCallContext);
}
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class ManagedContainer 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);
try {
// 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, context.getBean(), context.getInterceptors(), context.getCreationalContext(), entityManagers);
} 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
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 {
afterInvoke(createContext, txPolicy, instance);
}
return new ProxyInfo(beanContext, primaryKey);
} finally {
ThreadContext.exit(oldCallContext);
}
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class SingletonInstanceManager method createInstance.
private Instance createInstance(final ThreadContext callContext, final BeanContext beanContext) throws ApplicationException {
try {
initializeDependencies(beanContext);
final InstanceContext context = beanContext.newInstance();
if (context.getBean() instanceof SessionBean) {
final Operation originalOperation = callContext.getCurrentOperation();
try {
callContext.setCurrentOperation(Operation.CREATE);
final Method create = beanContext.getCreateMethod();
final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap());
ejbCreate.invoke();
} finally {
callContext.setCurrentOperation(originalOperation);
}
}
final ReadWriteLock lock;
if (beanContext.isBeanManagedConcurrency()) {
// Bean-Managed Concurrency
lock = new BeanManagedLock();
} else {
// Container-Managed Concurrency
lock = new ReentrantReadWriteLock();
}
return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext(), lock);
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
final String t = "The bean instance " + beanContext.getDeploymentID() + " threw a system exception:" + e;
logger.error(t, e);
throw new ApplicationException(new NoSuchEJBException("Singleton failed to initialize").initCause(e));
}
}
use of org.apache.openejb.core.interceptor.InterceptorData in project tomee by apache.
the class BeanContext method mergeOWBAndOpenEJBInfo.
public void mergeOWBAndOpenEJBInfo() {
final CdiEjbBean cdiEjbBean = get(CdiEjbBean.class);
if (cdiEjbBean == null) {
return;
}
final InjectionTargetImpl<?> injectionTarget = InjectionTargetImpl.class.cast(get(CdiEjbBean.class).getInjectionTarget());
final InterceptorResolutionService.BeanInterceptorInfo info = injectionTarget.getInterceptorInfo();
if (info == null) {
return;
}
final Collection<Interceptor<?>> postConstructInterceptors = Collection.class.cast(Reflections.get(injectionTarget, "postConstructInterceptors"));
final Collection<Interceptor<?>> preDestroyInterceptors = Collection.class.cast(Reflections.get(injectionTarget, "preDestroyInterceptors"));
if (postConstructInterceptors != null) {
for (final Interceptor<?> pc : postConstructInterceptors) {
if (isEjbInterceptor(pc)) {
continue;
}
final InterceptorData interceptorData = createInterceptorData(pc);
instanceScopedInterceptors.add(interceptorData);
cdiInterceptors.add(interceptorData);
}
}
if (preDestroyInterceptors != null) {
for (final Interceptor<?> pd : preDestroyInterceptors) {
if (isEjbInterceptor(pd)) {
continue;
}
if (postConstructInterceptors.contains(pd)) {
continue;
}
final InterceptorData interceptorData = createInterceptorData(pd);
instanceScopedInterceptors.add(interceptorData);
cdiInterceptors.add(interceptorData);
}
}
for (final Map.Entry<Method, InterceptorResolutionService.BusinessMethodInterceptorInfo> entry : info.getBusinessMethodsInfo().entrySet()) {
final Interceptor<?>[] interceptors = entry.getValue().getCdiInterceptors();
if (interceptors == null) {
continue;
}
for (final Interceptor<?> i : interceptors) {
// already at class level, since we merge "hooks" in InterceptorData no need to add it again
if (postConstructInterceptors.contains(i) || preDestroyInterceptors.contains(i)) {
continue;
}
final InterceptorData data = createInterceptorData(i);
addCdiMethodInterceptor(entry.getKey(), data);
}
entry.getValue().setEjbInterceptors(new ArrayList<Interceptor<?>>());
entry.getValue().setCdiInterceptors(new ArrayList<Interceptor<?>>());
}
// handled by OpenEJB now so clean up all duplication from OWB
if (info.getSelfInterceptorBean() != null) {
try {
final Field field = InterceptorResolutionService.BeanInterceptorInfo.class.getDeclaredField("selfInterceptorBean");
field.setAccessible(true);
field.set(info, null);
} catch (final Exception e) {
// no-op
}
}
Map.class.cast(Reflections.get(injectionTarget, "methodInterceptors")).clear();
clear(Collection.class.cast(postConstructInterceptors));
clear(Collection.class.cast(preDestroyInterceptors));
clear(Collection.class.cast(Reflections.get(injectionTarget, "postConstructMethods")));
clear(Collection.class.cast(Reflections.get(injectionTarget, "preDestroyMethods")));
clear(Collection.class.cast(Reflections.get(info, "ejbInterceptors")));
clear(Collection.class.cast(Reflections.get(info, "cdiInterceptors")));
// OWB doesn't compute AROUND_INVOKE so let's do it
final Method timeout = getEjbTimeout();
if (timeout != null) {
final AnnotatedType annotatedType = cdiEjbBean.getAnnotatedType();
final AnnotationManager annotationManager = getWebBeansContext().getAnnotationManager();
final Collection<Annotation> annotations = new HashSet<>();
annotations.addAll(annotationManager.getInterceptorAnnotations(annotatedType.getAnnotations()));
final Set<AnnotatedMethod<?>> methods = annotatedType.getMethods();
for (final AnnotatedMethod<?> m : methods) {
if (timeout.equals(m.getJavaMember())) {
annotations.addAll(annotationManager.getInterceptorAnnotations(m.getAnnotations()));
break;
}
}
if (!annotations.isEmpty()) {
for (final Interceptor<?> timeoutInterceptor : getWebBeansContext().getBeanManagerImpl().resolveInterceptors(InterceptionType.AROUND_TIMEOUT, AnnotationUtil.asArray(annotations))) {
if (isEjbInterceptor(timeoutInterceptor)) {
continue;
}
final InterceptorData data = createInterceptorData(timeoutInterceptor);
addCdiMethodInterceptor(timeout, data);
}
}
}
}
Aggregations