View Javadoc
1   package org.apache.fulcrum.yaafi.interceptor.javasimon;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Method;
23  
24  import org.apache.fulcrum.yaafi.interceptor.util.MethodToStringBuilderImpl;
25  import org.javasimon.SimonManager;
26  import org.javasimon.Split;
27  import org.javasimon.Stopwatch;
28  
29  /**
30   * Encapsulating the JAMon 2.x related API calls. JAMon 2.x allows for a much
31   * more powerful integration with Avalon services :
32   * <ul>
33   *  <li>custom ranges/units</li>
34   *  <li>exception monitoring</li>
35   *  <li>smooth web interface</li>
36   * </ul>
37   *
38   * @since 1.0.7
39   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
40   */
41  
42  public class JavaSimon4PerformanceMonitorImpl implements JavaSimonPerformanceMonitor
43  {
44      /** is monitoring enabled */
45      private boolean isActive;
46  
47      /** the method currently monitored */
48      private Method method;
49  
50      /** the split for this invocation */
51      private Split split;
52  
53      /**
54       * Constructor.
55       *
56       * @param serviceName the service name of the service being monitored
57       * @param method the method to be monitored
58       * @param isActive is this an active monitor
59       */
60      public JavaSimon4PerformanceMonitorImpl(String serviceName, Method method, Boolean isActive)
61      {
62          this.method = method;
63          this.isActive = isActive.booleanValue();
64      }
65  
66      /**
67       * Start the monitor.
68       */
69      public void start()
70      {
71          if(this.isActive)
72          {
73              String methodSignature = createMethodSignature(this.method, 0);
74              Stopwatch stopwatch = SimonManager.getStopwatch(methodSignature);
75              this.split = stopwatch.start();
76          }
77      }
78  
79      /**
80       * Stop the monitor
81       */
82      public void stop()
83      {
84          if(this.isActive) 
85          {
86              this.split.stop();
87          }
88      }
89  
90      /**
91       * Stop the monitor
92       */
93      public void stop(Throwable throwable)
94      {
95          if(this.isActive)
96          {
97              this.split.stop();
98          }
99      }
100 
101     /**
102      * Create a method signature - JavaSimon does not look names with '#' in it.
103      */
104     private String createMethodSignature(Method method, int mode)
105     {
106         MethodToStringBuilderImpl methodToStringBuilder = new MethodToStringBuilderImpl(method, mode);
107         return methodToStringBuilder.toString().replace("#", "[]");
108     }
109 }