jvanzyl     02/01/22 05:47:58

  Added:       src/aspects/org/apache/turbine Trace.java TraceTurbine.java
  Log:
  - some very rudimentary tracing aspects that use log4j.
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-3/src/aspects/org/apache/turbine/Trace.java
  
  Index: Trace.java
  ===================================================================
  package org.apache.turbine;
  
  import java.io.PrintStream;
  import org.apache.log4j.Category;
  
  /**
   * This class provides support for printing trace messages into a
   * log4j category.
   * 
   * The messages are appended with the string representation of the objects
   * whose constructors and methods are being traced.
   * It defines one abstract pointcut for injecting that tracing functionality 
   * into any application classes.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: Trace.java,v 1.1 2002/01/22 13:47:58 jvanzyl Exp $
   */
  abstract aspect Trace 
  {
      /*
       * Functional part
       */
  
      /**
       * There are 3 trace levels (values of TRACELEVEL):
       * 
       * 0 - No messages are printed
       * 1 - Trace messages are printed, but there is no indentation 
       *     according to the call stack
       * 2 - Trace messages are printed, and they are indented
       *     according to the call stack
       */
      public static int TRACELEVEL = 2;
      
      /**
       * Tracks the call depth for indented traces made according
       * to the structure of the stack.
       */
      protected static int callDepth = 0;
      
      /**
       * Log4j category used for tracing.
       */
      private static Category log = Category.getInstance("trace");
      
      /**
       * Tracing method used in before advice.
       */
      protected static void traceEntry(String str, Object o) 
      {
          if (TRACELEVEL == 0) 
          {
              return;
          }            
          
          if (TRACELEVEL == 2) 
          {
              callDepth++;
          }            
          printEntering(str + ": " + o.toString());
      }
  
      /**
       * Tracing method used in after advice.
       */
      protected static void traceExit(String str, Object o) 
      {
          if (TRACELEVEL == 0) 
          {
              return;
          }            
          
          printExiting(str + ": " + o.toString());
          
          if (TRACELEVEL == 2) 
          {
              callDepth--;
          }            
      }
      
      private static void printEntering(String str) 
      {
          log.debug(indent() + "--> " + str);
      }
  
      private static void printExiting(String str) 
      {
          log.debug(indent() + "<-- " + str);
      }
  
      private static String indent() 
      {
          StringBuffer sb = new StringBuffer();
          
          for (int i = 0; i < callDepth; i++)
          {
              sb.append("  ");
          }            
      
          return sb.toString();
      }
  
      /*
       * Crosscut part
       */
  
      /**
       * Application classes - left unspecified.
       */
      abstract pointcut myClass(Object obj);
      
      /**
       * The constructors in those classes.
       */
      pointcut myConstructor(Object obj): myClass(obj) && execution(new(..));
      
      /**
       * The methods of those classes.
       */
      pointcut myMethod(Object obj): myClass(obj) && 
          execution(* *(..)) && !execution(String toString());
  
      /**
       * Before advice that will execute before a constructor
       * is invoked.
       */
      before(Object obj): myConstructor(obj) 
      {
          traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
      }
      
      /**
       * After advice that will execute after a constructor
       * a constructor has been invoked.
       */
      after(Object obj): myConstructor(obj) 
      {
          traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
      }
      
      /**
       * Before advice that will execute before a method
       * is invoked.
       */
      before(Object obj): myMethod(obj) 
      {
          traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
      }
      
      /**
       * After advice that will execute after a method
       * has been invoked.
       */
      after(Object obj): myMethod(obj) 
      {
          traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
      }
  }
  
  
  
  1.1                  
jakarta-turbine-3/src/aspects/org/apache/turbine/TraceTurbine.java
  
  Index: TraceTurbine.java
  ===================================================================
  package org.apache.turbine;
  
  /**
   * This class concretizes the abstract crosscut in Trace, 
   * applying the trace facility to these application classes.
   *
   * We will move toward having a set of aspects that define a
   * particular tracing behaviour and then we can control which
   * ones will be applied using an if() designator and some
   * run-time properties. This was originally made to watch
   * the pipeline in action but it will be fleshed out.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: TraceTurbine.java,v 1.1 2002/01/22 13:47:58 jvanzyl Exp $
   */
  public aspect TraceTurbine
      extends Trace 
  {
      /**
       * A simple selection of classes in the pipeline package.
       */
      pointcut myClass(Object obj): this(obj) && 
          (within(org.apache.turbine.pipeline.*));
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to