*/
private Object callInterceptorsAndDecorators(Method method, Object instance, Object[] arguments, InvocationContext ejbContext) throws Exception
{
Object rv = null;
BaseEjbBean<?> injectionTarget = this.contextual;
InterceptorDataImpl decoratorInterceptorDataImpl = null;
List<Object> decorators = null;
DelegateHandler delegateHandler = null;
List<Decorator<?>> decoratorStack = injectionTarget.getDecoratorStack();
List<InterceptorData> interceptorStack = injectionTarget.getInterceptorStack();
if (logger.isLoggable(Level.FINE))
{
logger.log(Level.FINE, "Decorator stack for target {0}", decoratorStack);
logger.log(Level.FINE, "Interceptor stack {0}", interceptorStack);
}
if (decoratorStack.size() > 0 )
{
if (logger.isLoggable(Level.FINE))
{
logger.fine("Obtaining a delegate");
}
Class<?> proxyClass = webBeansContext.getJavassistProxyFactory().getInterceptorProxyClasses().get(injectionTarget);
if (proxyClass == null)
{
JavassistProxyFactory proxyFactory = webBeansContext.getJavassistProxyFactory();
ProxyFactory delegateFactory = proxyFactory.createProxyFactory(injectionTarget);
proxyClass = proxyFactory.getProxyClass(delegateFactory);
proxyFactory.getInterceptorProxyClasses().put(injectionTarget, proxyClass);
}
Object delegate = proxyClass.newInstance();
delegateHandler = new DelegateHandler(this.contextual, ejbContext);
((ProxyObject)delegate).setHandler(delegateHandler);
// Gets component decorator stack
decorators = WebBeansDecoratorConfig.getDecoratorStack(injectionTarget, instance, delegate,
(CreationalContextImpl<?>)this.cc);
//Sets decorator stack of delegate
delegateHandler.setDecorators(decorators);
}
if (interceptorStack.size() == 0)
{
if (decoratorStack.size() == 0)
{
rv = ejbContext.proceed();
}
else
{
// We only have decorators, so run the decorator stack directly without interceptors.
// The delegate handler knows about the ejbContext.proceed()
rv = delegateHandler.invoke(instance, method, null, arguments);
}
}
else
{
// We have at least one interceptor. Our delegateHandler will need to be wrapped in an interceptor.
if (this.interceptedMethodMap.get(method) == null)
{
//Holds filtered interceptor stack
List<InterceptorData> filteredInterceptorStack = new ArrayList<InterceptorData>(interceptorStack);
// Filter both EJB and WebBeans interceptors
webBeansContext.getInterceptorUtil().filterCommonInterceptorStackList(filteredInterceptorStack, method);
webBeansContext.getInterceptorUtil().filterOverridenAroundInvokeInterceptor(injectionTarget.getBeanClass(), filteredInterceptorStack);
this.interceptedMethodMap.put(method, filteredInterceptorStack);
}
List<InterceptorData> filteredInterceptorStack = new ArrayList<InterceptorData>(this.interceptedMethodMap.get(method));
if (delegateHandler != null)
{
WebBeansDecoratorInterceptor lastInterceptor = new WebBeansDecoratorInterceptor(delegateHandler, instance);
decoratorInterceptorDataImpl = new InterceptorDataImpl(true, lastInterceptor, webBeansContext);
decoratorInterceptorDataImpl.setDefinedInInterceptorClass(true);
decoratorInterceptorDataImpl.setAroundInvoke(
webBeansContext.getSecurityService().doPrivilegedGetDeclaredMethod(lastInterceptor.getClass(),
"invokeDecorators",
new Class[] {InvocationContext.class}));
filteredInterceptorStack.add(decoratorInterceptorDataImpl);
}