[cdi-dev] interceptor that return void, unclear in 1.0 specification, needs clarification in 1.1

Rick Hightower richardhightower at gmail.com
Mon May 16 03:08:07 EDT 2011


Ok... I missed that. There is still an example that shows a void return type
interceptor and there is some prior art along these lines. So I guess I am
still, asking that this be a feature request for CDI 1.1.

https://issues.jboss.org/browse/CDI-130

This works in Spring:

package com.arcmind.springquickstart.security;



import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @author Richard Hightower
 */
@Component
public class SecurityAdvice {

        @Autowired(required=true)
        @Qualifier ("manager")
        private SecurityService securityManager;

        public void checkSecurity(ProceedingJoinPoint joinPoint)
throws Throwable {

            /* If the user is not logged in, don't let them use this method */
            if(!securityManager.isLoggedIn()){
                throw new SecurityViolationException();
            }

            /* Get the name of the method being invoked. */
            String operationName = joinPoint.getSignature().getName();
            /* Get the name of the object being invoked. */
            String objectName = joinPoint.getThis().getClass().getName();


           /*
            * Invoke the method or next Interceptor in the list,
            * if the current user is allowed.
            */
            if (!securityManager.isAllowed(objectName, operationName)){
                throw new SecurityViolationException();
            }

            joinPoint.proceed();
        }


        /**
         * @return Returns the manager.
         */
        public SecurityService getSecurityManager() {
                return securityManager;
        }
        /**
         * @param manager The manager to set.
         */
        public void setSecurityManager(SecurityService manager) {
                this.securityManager = manager;
        }

}


On Sun, May 15, 2011 at 11:58 PM, Jaikiran Pai <jpai at redhat.com> wrote:

>  The interceptors spec says this about @AroundInvoke methods on
> interceptors:
>
> <quote>
> Around-invoke methods have the following signature:
>
> Object <METHOD>(InvocationContext) throws Exception
>
> </quote>
>
> By the way, if Weld is failing silently, then I guess it needs to be
> improved to throw an explicit error.
>
> -Jaikiran
>
> On Monday 16 May 2011 12:06 PM, Rick Hightower wrote:
>
>  The specification has an example in 9.5.1 that returns a void.
> AspectJ/AspectWerks annotation allow this (prior art).
>
>
>  Can interceptors return void? The specification seem unclear on this.
> Well, it shows an example but there is no further verbiage.
>
>
>  I get three different behaviors for this interceptor:
>
>
>  package org.cdi.advocacy.security;
>
>
>
>
>  import javax.inject.Inject;
>
> import javax.interceptor.AroundInvoke;
>
> import javax.interceptor.Interceptor;
>
> import javax.interceptor.InvocationContext;
>
>
>  /**
>
>  * @author Richard Hightower
>
>  */
>
> @Secure @Interceptor
>
> public class SecurityAdvice {
>
>
>
>         @Inject
>
>         private SecurityService securityManager;
>
>
>          @AroundInvoke
>
>         public void checkSecurity(InvocationContext joinPoint) throwsException {
>
>
>
>         System.out.println("In SecurityAdvice");
>
>
>
>             /* If the user is not logged in, don't let them use this
> method */
>
>             if(!securityManager.isLoggedIn()){
>
>                 throw new SecurityViolationException();
>
>             }
>
>
>              /* Get the name of the method being invoked. */
>
>             String operationName = joinPoint.getMethod().getName();
>
>             /* Get the name of the object being invoked. */
>
>             String objectName = joinPoint.getTarget().getClass().getName();
>
>
>
>             /*
>
>             * Invoke the method or next Interceptor in the list,
>
>             * if the current user is allowed.
>
>             */
>
>             if (!securityManager.isAllowed(objectName, operationName)){
>
>                 throw new SecurityViolationException();
>
>             }
>
>
>
>             joinPoint.proceed();
>
>         }
>
> }
>
>
>  Weld does not recognize it because it returns void. It fails silently.
>
> Candi recognizes it and uses it.
>
> OpenWebBeans throws an exception as follows:
>
>
>
>
>  org.apache.webbeans.exception.WebBeansConfigurationException:
> @AroundInvoke annotated method : checkSecurity in class :
> org.cdi.advocacy.security.SecurityAdvice must return Object type
>
> at
> org.apache.webbeans.util.WebBeansUtil.checkAroundInvokeAnnotationCriterias(
> WebBeansUtil.java:1094)
>
> at org.apache.webbeans.util.WebBeansUtil.configureInterceptorMethods(
> WebBeansUtil.java:1241)
>
> at
> org.apache.webbeans.intercept.WebBeansInterceptorConfig.addMethodInterceptors(
> WebBeansInterceptorConfig.java:349)
>
> at org.apache.webbeans.intercept.WebBeansInterceptorConfig.configure(
> WebBeansInterceptorConfig.java:250)
>
> at org.apache.webbeans.config.DefinitionUtil.defineBeanInterceptorStack(
> DefinitionUtil.java:1058)
>
> at org.apache.webbeans.config.BeansDeployer.validate(
> BeansDeployer.java:365)
>
> at org.apache.webbeans.config.BeansDeployer.validateInjectionPoints(
> BeansDeployer.java:326)
>
> at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:179)
>
> at org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(
> AbstractLifeCycle.java:123)
>
> at org.cdiadvocate.beancontainer.OpenWebBeansBeanContainer.doStart(
> OpenWebBeansBeanContainer.java:26)
>
> at org.cdiadvocate.beancontainer.AbstractBeanContainer.start(
> AbstractBeanContainer.java:111)
>
> at org.cdi.advocacy.AtmMain.<clinit>(AtmMain.java:25)
>
> Exception in thread "main" java.lang.ExceptionInInitializerError
>
> Caused by:
> org.cdiadvocate.beancontainer.BeanContainerInitializationException: Unable
> to start BeanContainer : @AroundInvoke annotated method : checkSecurity in
> class : org.cdi.advocacy.security.SecurityAdvice must return Object type
>
> at org.cdiadvocate.beancontainer.AbstractBeanContainer.start(
> AbstractBeanContainer.java:113)
>
> at org.cdi.advocacy.AtmMain.<clinit>(AtmMain.java:25)
>
> Caused by: org.apache.webbeans.exception.WebBeansConfigurationException:
> @AroundInvoke annotated method : checkSecurity in class :
> org.cdi.advocacy.security.SecurityAdvice must return Object type
>
> at
> org.apache.webbeans.util.WebBeansUtil.checkAroundInvokeAnnotationCriterias(
> WebBeansUtil.java:1094)
>
> at org.apache.webbeans.util.WebBeansUtil.configureInterceptorMethods(
> WebBeansUtil.java:1241)
>
> at
> org.apache.webbeans.intercept.WebBeansInterceptorConfig.addMethodInterceptors(
> WebBeansInterceptorConfig.java:349)
>
> at org.apache.webbeans.intercept.WebBeansInterceptorConfig.configure(
> WebBeansInterceptorConfig.java:250)
>
> at org.apache.webbeans.config.DefinitionUtil.defineBeanInterceptorStack(
> DefinitionUtil.java:1058)
>
> at org.apache.webbeans.config.BeansDeployer.validate(
> BeansDeployer.java:365)
>
> at org.apache.webbeans.config.BeansDeployer.validateInjectionPoints(
> BeansDeployer.java:326)
>
> at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:179)
>
> at org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(
> AbstractLifeCycle.java:123)
>
> at org.cdiadvocate.beancontainer.OpenWebBeansBeanContainer.doStart(
> OpenWebBeansBeanContainer.java:26)
>
> at org.cdiadvocate.beancontainer.AbstractBeanContainer.start(
> AbstractBeanContainer.java:111)
>
> ... 1 more
>
>
>  I think CDI 1.1 should clarify the behavior and I think it should support
> return types of void.
> --
> *Rick Hightower*
> (415) 968-9037
> Profile <http://www.google.com/profiles/RichardHightower>
>
>
> _______________________________________________
> cdi-dev mailing listcdi-dev at lists.jboss.orghttps://lists.jboss.org/mailman/listinfo/cdi-dev
>
>
>
> _______________________________________________
> cdi-dev mailing list
> cdi-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/cdi-dev
>
>


-- 
*Rick Hightower*
(415) 968-9037
Profile <http://www.google.com/profiles/RichardHightower>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/cdi-dev/attachments/20110516/9b72949c/attachment-0001.html 


More information about the cdi-dev mailing list