9 Replies Latest reply on Aug 30, 2007 9:29 PM by chlol

    the intercept component don't work!

    chlol

      my test intercept:
      @Name("interceptsTest")
      @Interceptor
      public class InterceptsTest {
      @Logger
      Log log;


      @AroundInvoke
      public Object doLogs(InvocationContext inv) throws Exception {
      int methodModify = inv.getMethod().getModifiers();
      String methodName = inv.getMethod().getName();

      if (methodModify == Modifier.PUBLIC
      && (methodName.startsWith("create")
      || methodName.startsWith("update")
      || methodName.startsWith("persist")
      || methodName.startsWith("remove"))) {

      Map data = inv.getContextData();
      Object target = inv.getTarget();
      log.debug("ContextData:" + data);
      log.debug("Target:" + target);
      }

      return inv.proceed();
      }

      }


      my invoke intercept class:

      @Name("useIntercept")
      @Interceptors(InterceptsTest.class)
      public class UseIntercept {

      public void persist() {
      System.out.println("&&&&&&&&&&&& do persist() &&&&&&&&&&&&");
      }

      }

      but when the persist() is executed,the intercept don't be invoked

      can you help me?

        • 1. Re: the intercept component don't work!
          pmuir

          Use [ code ] tags

          • 2. Re: the intercept component don't work!
            chlol

            my test intercept:

            @Name("interceptsTest")
            @Interceptor
            public class InterceptsTest {
             @Logger
             Log log;
            
            
             @AroundInvoke
             public Object doLogs(InvocationContext inv) throws Exception {
            
             int methodModify = inv.getMethod().getModifiers();
             String methodName = inv.getMethod().getName();
            
             if (methodModify == Modifier.PUBLIC
             && (methodName.startsWith("create")
             || methodName.startsWith("update")
             || methodName.startsWith("persist")
             || methodName.startsWith("remove"))) {
            
             Map data = inv.getContextData();
             Object target = inv.getTarget();
             log.debug("ContextData:" + data);
             log.debug("Target:" + target);
             }
            
             return inv.proceed();
             }
            
            }

            my invoke intercept class:

            @Name("useIntercept")
            @Interceptors(InterceptsTest.class)
            public class UseIntercept {
            
             public void persist() {
             System.out.println("&&&&&&&&&&&& do persist() &&&&&&&&&&&&");
             }
            
            }


            help me,please
            thank you

            • 3. Re: the intercept component don't work!
              matt.drees

              What's the code that calls persist()?

              • 4. Re: the intercept component don't work!
                chlol

                thank your reply!
                i use a test case to call the persist method,the following is code:

                @Test
                 public void testIntercept() throws Exception {
                 new ComponentTest() {
                 protected void testComponents() throws Exception {
                 invokeMethod("#{useIntercept.persist}");
                 }
                 }.run();
                 }


                • 5. Re: the intercept component don't work!
                  matt.drees

                  I think because your component is a javabean component, you can't use @Interceptors directly on it. Instead, you do something like:

                  
                  @Name("useIntercept")
                  @LogCrudOperations
                  public class UseIntercept {
                  
                   public void persist() {
                   System.out.println("&&&&&&&&&&&& do persist() &&&&&&&&&&&&");
                   }
                  
                  }
                  
                  @Interceptors(InterceptsTests.class)
                  @Retention(RetentionPolicy.RUNTIME)
                  @Target(ElementType.TYPE)
                  public @interface LogCrudOperations {
                  }
                  


                  • 6. Re: the intercept component don't work!
                    matt.drees

                    Also, your interceptor (InterceptsTest) doesn't get to be a Seam component. So @Logger won't work.

                    • 7. Re: the intercept component don't work!
                      chlol

                      that's ok,thank you!

                      but how to define a seam component? Is not it to use @Name to define?

                      pardon my prolixity!

                      • 8. Re: the intercept component don't work!
                        matt.drees

                        Yes, you use @Name to define a seam component. So, if you called Component.getInstance("interceptsTest"), the @Logger field would be filled.

                        But when Seam instantiates an interceptor from the class you specify, it just treats it like a normal object. It doesn't inject @Loggers or give it interceptors for bijection, etc.

                        • 9. Re: the intercept component don't work!
                          chlol

                          i understand
                          thank you very much again!