View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.monitoring.aop;
19  
20  import org.apache.commons.monitoring.Role;
21  import org.apache.commons.monitoring.counters.Counter;
22  import org.apache.commons.monitoring.repositories.Repository;
23  import org.apache.commons.monitoring.stopwatches.StopWatch;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.PrintStream;
27  import java.lang.reflect.Method;
28  
29  /**
30   * A method interceptor that compute method invocation performances.
31   * <p/>
32   * Concrete implementation will adapt the method interception API to
33   * this class requirement.
34   *
35   * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
36   */
37  public abstract class AbstractPerformanceInterceptor<T> {
38  
39      protected MonitorNameExtractor monitorNameExtractor;
40  
41      public AbstractPerformanceInterceptor() {
42          setMonitorNameExtractor(DefaultMonitorNameExtractor.INSTANCE);
43      }
44  
45      /**
46       * API neutral method invocation
47       */
48      protected Object doInvoke(final T invocation) throws Throwable {
49          final String name = getCounterName(invocation);
50          if (name == null) {
51              return proceed(invocation);
52          }
53  
54          final Counter monitor = Repository.INSTANCE.getCounter(new Counter.Key(Role.PERFORMANCES, name));
55          final StopWatch stopwatch = Repository.INSTANCE.start(monitor);
56          Throwable error = null;
57          try {
58              return proceed(invocation);
59          } catch (final Throwable t) {
60              error = t;
61              throw t;
62          } finally {
63              stopwatch.stop();
64              if (error != null) {
65                  final ByteArrayOutputStream writer = new ByteArrayOutputStream();
66                  error.printStackTrace(new PrintStream(writer));
67                  Repository.INSTANCE.getCounter(new Counter.Key(Role.FAILURES, writer.toString())).add(stopwatch.getElapsedTime());
68              }
69          }
70      }
71  
72      protected abstract Object proceed(T invocation) throws Throwable;
73  
74      protected abstract String getCounterName(T invocation);
75  
76      /**
77       * Compute the counter name associated to this method invocation
78       *
79       * @param method method being invoked
80       * @return counter name. If <code>null</code>, nothing will be monitored
81       */
82      protected String getCounterName(final Object instance, final Method method) {
83          return monitorNameExtractor.getMonitorName(instance, method);
84      }
85  
86      public void setMonitorNameExtractor(final MonitorNameExtractor monitorNameExtractor) {
87          this.monitorNameExtractor = monitorNameExtractor;
88      }
89  }