4 Replies Latest reply on Oct 29, 2009 4:59 AM by Marius Bogoevici

    Interceptors on Tomcat

    Fabio Wang Newbie

      Hi there,


      I'm trying to create an interceptor in the numberguess example app of weld CR1. Here's what I've got so far:


      Game.java


      @Logging
      public class Game implements Serializable {
      ...
      }




      beans.xml:



      <beans>
              <interceptors>
                      <class>org.jboss.weld.examples.numberguess.LoggingInterceptor</class>
              </interceptors>
      </beans>




      LoggingInterceptor.java



      @Logging
      @Interceptor
      public class LoggingInterceptor {
      
              @AroundInvoke
              public Object around(InvocationContext ic) throws Exception {
                      Object result;
                      try {
                              System.out.println("Starting execution of " + ic.getMethod().getName());
                              result = ic.proceed();
                              System.out.println("Ending execution of " + ic.getMethod().getName());
                              System.out.println("Returning " + (result != null ? result.toString() : null));
                              return result;
                      } catch (Exception e) {
                              System.out.println("Throwing exception " + e.toString());
                              throw e;
                      }
              }
      }




      Logging.java



      @Inherited
      @InterceptorBinding
      @Target( { ElementType.TYPE, ElementType.METHOD })
      @Retention(RetentionPolicy.RUNTIME)
      public @interface Logging {
      }



      I'm running it on tomcat 6.0.20. It's not working as expected, though (well, at least as I expected :-P). The instance of Game which has its reset method invoked is not one with injected resources, and then I get a NullPointerException (randomNumber is null).


      Is this something that should work at all? Or this works only on application servers? I've read a topic about it which was posted some time ago, but I thought CR1 would come up with this feature...