4 Replies Latest reply on Apr 28, 2005 5:20 PM by grafpoo

    aop in jboss webapp - request for help

    grafpoo

      i want to add an aspect on creating and closing hibernate sessions.
      i am using java 5 with a webapp running jboss-4.0.1sp1.

      just for a feel-good, my first aspect is a log-everywhere one, but i
      cannot get it to work, so i am asking for help. i have a feeling that
      jboss is not even picking up on the fact that i have an aspect, so
      hopefully that is the lone hurdle. here's what i did.

      1) created a class JbossSessionMonitor:


      package foo.aop;

      import org.apache.log4j.Logger;
      import org.hibernate.Session;
      import org.jboss.aop.Aspect;
      import org.jboss.aop.Bind;
      import org.jboss.aop.PointcutDef;
      import org.jboss.aop.advice.Scope;
      import org.jboss.aop.joinpoint.MethodInvocation;
      import org.jboss.aop.pointcut.Pointcut;

      /**
      * @author john guthrie
      */
      @Aspect(scope = Scope.PER_VM)
      public class JbossSessionMonitor {
      private static Logger logger = Logger.getLogger(JbossSessionMonitor.class);

      @PointcutDef ("execution( public * org.hibernate.Session -> close(..) )")
      public static Pointcut closeSessionPointcut;

      @PointcutDef ("execution( public * org.hibernate.SessionFactory -> openSession(..) )")
      public static Pointcut openSessionPointcut;

      @PointcutDef ("execution( public * org.hibernate.Session -> getSession(..) )")
      public static Pointcut getSessionPointcut;

      @Bind (pointcut="foo.aop.JbossSessionMonitor.closeSessionPointcut")
      public Object closeSessionAdvice(MethodInvocation mi) throws Throwable {
      String msg = "closing session: " + sessionInfo(mi.getTargetObject());
      logger.error(msg);
      System.out.println(msg);
      return mi.invokeNext();
      }

      @Bind (pointcut="foo.aop.JbossSessionMonitor.openSessionPointcut")
      public Object openSessionAdvice(MethodInvocation mi) throws Throwable {
      Object o = mi.invokeNext();
      String msg = "opening session: " + sessionInfo(o);
      logger.error(msg);
      System.out.println(msg);
      return o;
      }

      @Bind (pointcut="foo.aop.JbossSessionMonitor.getSessionPointcut")
      public Object getSessionAdvice(MethodInvocation mi) throws Throwable {
      String msg = "getting session: " + sessionInfo(mi.getTargetObject());
      logger.error(msg);
      System.out.println(msg);
      return mi.invokeNext();
      }

      private String sessionInfo(Object o) {
      if (o instanceof Session) {
      return ((Session) o).toString();
      } else {
      return "NOT-A-SESSION (" + o.getClass().getName() + ")";
      }
      }

      }


      from what i could tell from the 'injbossaop' example that comes with jboss-aop,
      what i needed to do to get this working is/was:
      1) create a file foo.aop that has my class (jar-style), plus a jboss-aop.xml
      in META-INF in foo.aop that just has an empty aop root element (empty because
      the aspect info is all in annotations
      2) my regular foo.war
      3) a foo.sar that contains #1 and #2 (foo.aop and foo.war) and a jboss-service.xml
      with an empty server root element (since i am deploying no mbeans)

      with this setup, i got no logging.

      so i changed the jboss-aop.xml to include what i wanted, so it looks like this:
      <?xml version="1.0" encoding="UTF-8"?>
















      updated the jars and redeployed. still nothing.

      i next changed the log-level in the jboss log4j.xml to debug level for org.jboss.aop
      i got no logs, which is what has me thinking that i am not telling jboss the proper
      place(s) for the aop configuration.

        • 1. Re: aop in jboss webapp - request for help
          kabirkhan

          Are you using the 'all' configuration of jboss?
          Have you set EnableTransformations to true in the AspectManagerService?

          To paste your xml surround it with code tags...

          • 2. Re: aop in jboss webapp - request for help
            grafpoo

            i am not using 'all', is that a requirement? what services above default does aop require?

            and no, i did not set enableTransformations.

            i'll repost the xml with code tags (it actually showed up in preview, dunno why it disappeared) if the above two don't sort it out. but am i correct in thinking that with annotations i can just use an empty jboss-aop.xml and with no mbeans an empty jboss-service.xml?

            and thanks for the prompt and useful reply!

            john

            • 3. Re: aop in jboss webapp - request for help
              kabirkhan

              Just to make sure I had a little play with the injboss example, it works when using the 'all' configuration, but not using 'default'. I'm not able to look into why right now, are you OK to use all for now?

              EnableTransformer is necessary if you're doing loadtime transformations. If you're precompiling you should not need to use it.

              • 4. Re: aop in jboss webapp - request for help
                grafpoo

                yes, i got it working today, thanks to your help. i have a mixin problem i am going to search the archives for. if i can't find a solution, i'll be posting that as well.

                again, thanks.
                john