7 Replies Latest reply on Jan 12, 2011 3:37 AM by jaikiran

    Interceptor sub classing

    blep

      Hello,

       

      Suppose I have an abstract generified base class to be deployed as an EJB interceptor:

       

      {code}

      public abstract class AbstractServiceInitializer<T> {

      @PostConstruct

          public void init(InvocationContext ctx) {

                Object o = ctx.getTarget();

              try {

                  T service = (T) o;

                  init(service);

              } catch (ClassCastException e) {

                  log.warn("Class {} is not supported... doing nothing", o.getClass().getName());

                  return;

              }

          }

           public abstract void init(T service);

      }

      {code}

       

      And a subclass :

       

      {code}

      public class MyEjbImplInitializer extends AbstractServiceInitializer<MyEjbImpl>{

           @Override

          public void init(MyEjbImpl service) {

              // specific init stuff

          }

      }

      {code}

       

      The problem I face is that during the PostConstruct phase of the MyEjbImpl lifecycle, the init(InvocationContext ctx) method from the  superclass is not invoked (of course the @Interceptors directive is correctly declared). The only way to make it work is to override that method in the subclass with a simple :

      {code}

          @PostConstruct

          @Override

          public void init(InvocationContext ctx) {

              super.init(ctx);

          }

      {code}

       

      is it normal or is it related to https://issues.jboss.org/browse/JBMDR-73 ?

       

      Regards

        • 1. Interceptor sub classing
          jaikiran

          The spec doesn't allow that:

           

          Interceptors spec, section "Multiple Callback Interceptor Methods for a Life Cycle Callback Event" :

           

          If a lifecycle callback interceptor method is overridden by another method (regardless of whether that method is itself a lifecycle callback interceptor method (of the same or different type)), it will not be invoked.

          • 2. Re: Interceptor sub classing
            blep

            Ok maybe I was not clear, but the override is a workaround to make the superclass init method beeing invoked.

             

            If the init method is not called by the subclass, then the annotated init method in the superclass is not invoked. So with the following code, the PostConstruct method is not invoked:

             

             

            {code}public abstract class AbstractServiceInitializer<T> {
            @PostConstruct
                public void init(InvocationContext ctx) {
                      Object o = ctx.getTarget();
                    try {
                        T service = (T) o;
                        init(service);
                    } catch (ClassCastException e) {
                        log.warn("Class {} is not supported... doing nothing", o.getClass().getName());
                        return;
                    }
                }
                 public abstract void init(T service);
            }{code}

             

             

             

            {code}public class MyEjbImplInitializer extends AbstractServiceInitializer<MyEjbImpl>{
                 @Override
                public void init(MyEjbImpl service) {
                    // specific init stuff
                }
            }{code}

             

             

            Is this still uncompatible with the spec?

            • 3. Re: Interceptor sub classing
              jaikiran

              This one looks OK, assuming you have configured the interceptor for the bean. Does it work if you leave out the generics part?

              • 4. Re: Interceptor sub classing
                blep

                Yes it works without the generic stuff.

                 

                I joined the POC to the post to show you with the generified initializer.

                 

                Regards

                • 5. Re: Interceptor sub classing
                  jaikiran

                  Just to be sure - this is JBoss AS 6.0.0.Final, right?

                  • 6. Re: Interceptor sub classing
                    blep

                    Yes

                    • 7. Interceptor sub classing
                      jaikiran