View Javadoc

1   package org.apache.fulcrum.yaafi.interceptor.jamon;
2   
3   import com.jamonapi.Monitor;
4   import com.jamonapi.MonitorFactory;
5   import com.jamonapi.RangeHolder;
6   import org.apache.fulcrum.yaafi.interceptor.util.MethodToStringBuilderImpl;
7   
8   import java.lang.reflect.Method;
9   
10  /**
11   * Ecapsulating the JAMon 2.x related API calls. JAMon 2.x allows for a much
12   * more powerful integration with Avalon services :
13   * <ul>
14   *  <li>custom ranges/units</li>
15   *  <li>exception monitoring</li>
16   *  <li>smooth web interface</li>
17   * </ul>
18   *
19   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
20   */
21  
22  public class Jamon2PerformanceMonitorImpl implements JamonPerformanceMonitor
23  {
24      /** the default label being used */
25      private static final String MONITOR_LABEL = "ms.services";
26  
27      /** is monitoring enabled */
28      private boolean isActive;
29  
30      /** the method currenty monitored */
31      private Method method;
32  
33      /** the global JAMON monitor */
34      private Monitor monitor;
35  
36      /** the time the monitoring was started */
37      private long startTime;
38  
39      static
40      {
41          // configure the unit/ranges only once
42          MonitorFactory.setRangeDefault(MONITOR_LABEL, createMSHolder());
43      }
44    
45      /**
46       * Constructor.
47       *
48       * @param serviceName the service name of the service being monitored
49       * @param method the method to be monitored
50       * @param isActive is this an active monitor
51       */
52      public Jamon2PerformanceMonitorImpl(String serviceName, Method method, Boolean isActive)
53      {
54          this.method = method;
55          this.isActive = isActive.booleanValue();
56      }
57  
58      /**
59       * Start the monitor.
60       */
61      public void start()
62      {
63          if(this.isActive)
64          {
65              this.startTime = System.currentTimeMillis();
66              MethodToStringBuilderImpl methodToStringBuilder = new MethodToStringBuilderImpl(this.method, 0);
67              String methodSignature = methodToStringBuilder.toString();
68              this.monitor = MonitorFactory.getMonitor(methodSignature, MONITOR_LABEL);
69              this.monitor.start();
70          }
71      }
72  
73      /**
74       * Stop the monitor
75       */
76      public void stop()
77      {
78          if(this.isActive) 
79          {
80              long duration = System.currentTimeMillis() - this.startTime;
81              this.monitor.add(duration);
82              this.monitor.stop();
83          }
84      }
85  
86      /**
87       * Stop the monitor
88       */
89      public void stop(Throwable throwable)
90      {
91          if(this.isActive)
92          {
93              // use a negative execution time to mark an exception for an affiliate
94              this.monitor.add(-1);
95              this.monitor.stop();  
96          }
97      }
98  
99  
100     /**
101      * Create a performance report.
102      *
103      * @return the textual performance report
104      * @throws Exception generating the report failed
105      */
106     public String createReport() throws Exception
107     {
108         return MonitorFactory.getRootMonitor().getReport();
109     }
110 
111     /**
112      * @return a customized range holder for measuring the execution time for services.
113      */
114     private static RangeHolder createMSHolder() {
115       RangeHolder result = new RangeHolder("<");
116       result.add("Exceptions",0);
117       result.add("0_10ms",10);
118       result.add("10_20ms",20);
119       result.add("20_40ms",40);
120       result.add("40_80ms",80);
121       result.add("80_160ms",160);
122       result.add("160_320ms",320);
123       result.add("320_640ms",640);
124       result.add("640_1280ms",1280);
125       result.add("1280_2560ms",2560);
126       result.add("2560_5120ms",5120);
127       result.add("5120_10240ms",10240);
128       result.add("10240_20480ms",20480);
129       result.addLastHeader("20480ms_");
130       // note last range is always called lastRange and is added automatically
131       return result;
132     }
133 }