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

    Interceptors on Tomcat

    fabiowg

      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...

        • 1. Re: Interceptors on Tomcat
          gavin.king

          Well, you've left out the most critical piece of code ... how do you obtain the instance of Game?

          • 2. Re: Interceptors on Tomcat
            fabiowg

            The instance of the game is supposed to be obtained by the EL evaluation of this line of home.xhtml:


            <h:outputText id="Higher" value="Higher!" rendered="#{game.number gt game.guess and game.guess ne 0}"/>



            And the reset method was called because it was marked with @PostConstruct:



            @PostConstruct
            public void reset() {
                this.smallest = 0;
                this.guess = 0;
                this.remainingGuesses = 10;
                this.biggest = maxNumber;
                this.number = randomNumber.get();
            }



            I noticed that inside the body of InterceptMethodHandler.invoke method there's a reference to a proxy of the Game with everything injected (variable self), but the field target of this handler references an empty instance of Game, and that's the instance which has the methods invoked.

            • 3. Re: Interceptors on Tomcat
              gavin.king

              Well, that definitely looks buggy. Please submit your test code to JIRA.

              • 4. Re: Interceptors on Tomcat
                mbogoevici

                There was an issue with field-based injection in CR1, which is fixed in latest trunk ... or CR2 ;).