6 Replies Latest reply on Nov 28, 2007 1:27 PM by b.reeve

    Intercepting Seam components/Support for AspectJ

    b.reeve

      I am working on a Seam project with Facelets and Spring on Tomcat. Enviroment is Seam 2.0.0.CR2, Tomcat 6.0.10, Facelets 1.2, Spring 2.0.

      Could anyone please tell me if there is a way to do intercepting on Seam components using AspectJ just as we do with Spring beans? Is there a way to create custom Seam interceptors and configure them for intercepting the Seam components. I dont have EJBs in my project.

      Here is what I tried to do

      public class LoggingInterceptor {
       @Logger
       private Log log;
      
       @AroundInvoke
       public Object componentLog(InvocationContext invocation) throws Exception{
       log.info("Logging for component " + invocation.getTarget().getClass().getCanonicalName());
       Object returnType = invocation.proceed();
       log.info("Logging for component " + invocation.getTarget().getClass().getCanonicalName());
       return returnType;
       }
      
      }
      


      @Interceptors(LoggingInterceptor.class)
      public @interface Logging {}
      


      
      @Name("loginBean")
      @Scope(ScopeType.SESSION)
      @Logging
      public class LoginBean {
      
       private String username;
       private String password;
      
       public String login() {
       return loginAction.login(username, password);
       }
      
       public String getUsername() {
       return username;
       }
       public void setUsername(String username) {
       this.username = username;
       }
       public String getPassword() {
       return password;
       }
       public void setPassword(String password) {
       this.password = password;
       }
      }
      


      I dont see the interceptor class being called. How do I make the method login() being intercepted? Where do I configure the interceptor class to be instantiated? Is there any sample code that I can refer to.

      Thanks in Advance...

        • 1. Re: Intercepting Seam components/Support for AspectJ

          Maybe it was snipped from the code but I do not see the RetentionPolicy. Try the following:

          @Target(ElementType.TYPE)
          @Retention(RetentionPolicy.RUNTIME)
          @Interceptors(LoggingInterceptor.class)
          public @interface Logging {}


          • 2. Re: Intercepting Seam components/Support for AspectJ
            b.reeve

            Hi Jacob,
            Thanks for the reply. I tried it that way and it worked.
            I had few doubts too.

            1. Is there a way to intercept just the methods and not the class.
            2. Is there any way to collectively configure a set of classes or pattern in the interceptor as we do with Spring. (So that we dont have to modify the seam component at all)

            Thanks in advance...

            • 3. Re: Intercepting Seam components/Support for AspectJ

               

              "b.reeve" wrote:
              1. Is there a way to intercept just the methods and not the class.

              Seam does not support method level interceptors, but you can always place the interceptor at the class level and use a method-level annotation to define the method(s) that should be intercepted. I provide an example of this on my blog (in the second half of the posting):

              http://solutionsfit.com/blog/2007/11/09/thoughts-on-webbeans-and-stereotypes/

              If this is not appealing you could use regexes on the InvocationContext to achieve the same results (i.e. if method being invoked matches pattern, run interceptor code).

              "b.reeve" wrote:
              2. Is there any way to collectively configure a set of classes or pattern in the interceptor as we do with Spring. (So that we dont have to modify the seam component at all)


              You can do this type of thing through an EJB interceptor and configure it in your ejb-jar.xml, but to my knowledge pattern matching is not directly supported by Seam interceptors. The direction of Web Beans (http://jcp.org/en/jsr/detail?id=299 submitted based on Seam) covers this case through the use of stereotypes. In the blog-posting above I describe how to achieve stereotypes with Seam which is an option for collective configuration. Hope that helps.


              • 4. Re: Intercepting Seam components/Support for AspectJ
                b.reeve

                Thank you Jacob for the solution.

                I tired implementing the method-level annotation as explained in your blog. It totally works.

                But still it needs to be configured from the component perspective.

                I dont have EJB's in my application. So using the EJB interceptor for configuring the pattern matching interception is not viable for me. I will look more into Stereotypes.

                InvocationContext regex seems to be a good option. Could you please point me to some sample code for implementing the regex on InvocationContext.

                Thanks !

                • 5. Re: Intercepting Seam components/Support for AspectJ

                  You can retrieve the method name by calling getMethod() on the InvocationContext. The method name can be used for pattern matching. Pattern and Matcher provided by the java.util.regex library will do the trick. This library is provided with your Java distribution. There are a variety of available tutorials on this library. Hope that helps.

                  • 6. Re: Intercepting Seam components/Support for AspectJ
                    b.reeve

                    Thank you for the briefing... it really helped.

                    Is it possible to annotate just the super interface and its concrete implementations will be intercepted. (I dont want to annotate all my classes for logging interception)

                    Is there any drawbacks in using this kind of interception for logging purpose? ( i think that AspectJ would have been a best choice if Seam had any support for it)

                    Would there be any performance hit in using seam interception as opposed to normal logging.


                    Thanks!