5 Replies Latest reply on Feb 1, 2010 11:52 AM by kabirkhan

    Tried everything to get Interceptor working, what am I missing?

      I am losing the will to live with aop. Just trying to get a simple hello world Interceptor working by intercepting public methods. Using JBoss 4.3.0, have already given up on using annotations and instead gone for the xml approach.

       

      My Interceptor:

       

      public class MyInterceptor implements Interceptor {
      
      
           @Override
           public Object invoke (Invocation invocation) throws Throwable {
               try {
                    System.out.println("Enter the joinpoint");
                 System.out.println(invocation.getClass());
                 return invocation.invokeNext ();
               } finally {
                 System.out.println("Leave the joinpoint");
               }
              }
      
           @Override
           public String getName() {
                return "my.packages.MyInterceptor";
           }
      }
      

       

       

      My *-aop.xml in which out of desperation I am attempting to intercept all public methods as this is the only thing that creates any output:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE aop PUBLIC
         "-//JBoss//DTD JBOSS AOP 1.0//EN"
         "http://labs.jboss.com/portal/jbossaop/dtd/jboss-aop_1_0.dtd">
      <aop>
      <interceptor class="my.packages.MyInterceptor" scope="PER_VM"/>
      
      
      <bind pointcut="execution(public * *->*(..))">
            <interceptor-ref name="my.packages.MyInterceptor"/>
      </bind>
      
      </aop>
      

       

      The only output I get is from Intercepting some jms classes:

       

      12:29:35,466 INFO  [STDOUT] Enter the joinpoint

      12:29:35,468 INFO  [STDOUT] class org.jboss.jms.client.delegate.ClientSessionDel

      egate$createObjectMessage_N7497440531089519617

      12:29:35,469 INFO  [STDOUT] Leave the joinpoint

      12:29:35,470 INFO  [STDOUT] Enter the joinpoint

      12:29:35,471 INFO  [STDOUT] class org.jboss.jms.client.delegate.ClientProducerDe

      legate$send_3961598017717988886

      12:29:35,472 INFO  [STDOUT] Enter the joinpoint

      12:29:35,473 INFO  [STDOUT] class org.jboss.jms.client.delegate.ClientSessionDel

      egate$send_6145266547759487588

      12:29:35,474 INFO  [STDOUT] Enter the joinpoint

      12:29:35,475 INFO  [STDOUT] class org.jboss.jms.server.endpoint.advised.SessionA

      dvised$send_7280680627620114891

      12:29:35,476 INFO  [STDOUT] Leave the joinpoint

      12:29:35,477 INFO  [STDOUT] Leave the joinpoint

       

      I am compiling my code with the aopc

       

       

      Any ideas? I am clearly missing something here.

        • 1. Re: Tried everything to get Interceptor working, what am I missing?
          kabirkhan
          • 2. Re: Tried everything to get Interceptor working, what am I missing?

            I have looked at the injboss example. It uses annotations which I had already given up on  after several hours without any luck.

             

            The link to the documentation is for loadtime instrumentation while I am attempting pre-compiled intstumentation. Anyhow the XML in the sample is identical to what I already had, it is the same as what is shipping with JBoss. But am I required to turn on EnableLoadtimeWeaving and edit the include / exclude elements? I tried this and I got several NullPointers and stack traces.

             

            In chapter 8 it says to:

             

            1. Delete jboss-aop.deployer file or directory from the existing JBoss Application Server distribution under server/<config-name>/deploy
            2. From the JBoss AOP distribution, from the jboss-40-install directory copy jboss-aop.deployer/ or jboss-aop-jdk50.deployer to the JBoss Application Server distribution under server/<config-name>/deploy depending on which JDK you are running with.

             

            I have tried this and again no difference. I had to download the same version of aop shipped with jboss 4.3.0 anyhow as otherwise there are many classes missing.

             

            The documentation is very confusing, there are 3 ways of implementing aspects (POJO with xml, annotations or a mixture), there are 3 ways of implementing instrumentation (pre-compile, loadtime, dynamic) and different instructions on how to build/install/deploy/command line options for aop depending on which combinations of the above you used and whether you are running jboss 4/5. Arrrrrggghh!

             

            1) If I am using annotations e.g @InterceptorDef  do I still need an *-aop.xml file at all?

            2) When I use the aopc ant task, obviously the aspect/interceptor classes need to be on the classpath, but do the target classes which I am intercepting also need to be on the classpath?

            • 3. Re: Tried everything to get Interceptor working, what am I missing?
              kabirkhan

              baronDodd wrote:

               

              The link to the documentation is for loadtime instrumentation while I am attempting pre-compiled intstumentation. Anyhow the XML in the sample is identical to what I already had, it is the same as what is shipping with JBoss. But am I required to turn on EnableLoadtimeWeaving and edit the include / exclude elements? I tried this and I got several NullPointers and stack traces.

              For compile-time weaving you don't need to turn on EnableLoadtimeWeaving, but you still need to deploy a jboss-aop.xml file (either standalone OR in an .aop archive in META-INF/jboss-aop.xml.

              baronDodd wrote:

               

              1) If I am using annotations e.g @InterceptorDef  do I still need an *-aop.xml file at all?

              To use annotations the classes need to be in a .aop archive, and in AS 4 for this to be recognized you need the META-INF/jboss-aop.xml file, but you don't need anything apart from <aop></aop>

              baronDodd wrote:

               

              2) When I use the aopc ant task, obviously the aspect/interceptor classes need to be on the classpath, but do the target classes which I am intercepting also need to be on the classpath?

              Yes

              1 of 1 people found this helpful
              • 4. Re: Tried everything to get Interceptor working, what am I missing?

                Ok thanks. Sorry for my naivety on this subject, going to learn all about this in a course later in the year but trying to implement it for a quick bug fix! My plan may have been flawed from the very start. What I ultimately want to do is intercept calls to all invocations of java.ejb.SessionContext.getCallerPrincipal() which could be any number of concrete classes at runtime in several jar files. So precompilation will not be an option.

                 

                I got my test working the same as before but this time with the annotations:

                 

                @InterceptorDef(scope=Scope.PER_VM)
                @Bind (pointcut="execution(public * *->*(..))")
                

                 

                Same result as before, only picking up the jms classes. If I am specifying my bindings in annotations, can I get away with an empty *-aop.xml? Also as this is not a war/ear deployment it can simply sit alongside my jar file? I am intercepting the jms classes even without xml.

                 

                Anyhow, I realized that I had not used the proper command line argument for loadtime:

                 

                -javaagent:$(JBossHome)/server/deploy/jboss-aop-jdk50.deployer/jboss-aop-jdk50.jar

                 

                Added this and suddenly lots of NoClassDefFounds. Added the jdk50.deployer jars to the classpath and suddenly it sparked into live and I got a stackoverflow from my interceptor intercepting itself to infinity! This is what I had been waiting for! Changed the pointcut expression to something more sensible, and now I get :

                 

                  java.lang.NoClassDefFoundError: org/jboss/dom4j/Element

                 

                Which appears to be in a ridiculously named dom4j-jarjar.jar in the 3rd party libs which JBoss is built against...

                 

                Think this is starting to suck through too many changes for a quick fix.

                • 5. Re: Tried everything to get Interceptor working, what am I missing?
                  kabirkhan

                  If I remember correctly the implementors of SessionContext might live in jars in the lib/ directory. AOP is not deployed until after that, when the contents of the deploy/ directory are deployed, meaning that you cannot intercept those classes.

                   

                  If the calls are in your application code, you should have more success with a pointcut like

                   

                  "call(* $instanceof{ava.ejb.SessionContext}->getCallerPrincipal() AND (within(class.where.call.is.Made) OR within (another.Class))"

                   

                  This weaves the caller and not the called class. The within is important since it avoids having to inspect all the calls made by every class, which is a performance hit