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  package org.apache.commons.monitoring.aspectj;
18  
19  import org.apache.commons.monitoring.aop.AbstractPerformanceInterceptor;
20  import org.aspectj.lang.ProceedingJoinPoint;
21  import org.aspectj.lang.Signature;
22  import org.aspectj.lang.annotation.Around;
23  import org.aspectj.lang.annotation.Aspect;
24  import org.aspectj.lang.annotation.Pointcut;
25  
26  import java.lang.reflect.Method;
27  
28  @Aspect
29  public abstract class CommonsMonitoringAspect extends AbstractPerformanceInterceptor<ProceedingJoinPoint> {
30      @Pointcut
31      protected abstract void pointcut();
32  
33      @Around("pointcut()")
34      public Object monitor(final ProceedingJoinPoint pjp) throws Throwable {
35          return doInvoke(pjp);
36      }
37  
38      @Override
39      protected Object proceed(final ProceedingJoinPoint invocation) throws Throwable {
40          return invocation.proceed();
41      }
42  
43      @Override
44      protected String getCounterName(final ProceedingJoinPoint invocation) {
45          final String monitorName = getCounterName(invocation.getTarget(), findMethod(invocation.getSignature()));
46          if (monitorName != null) {
47              return monitorName;
48          }
49          return invocation.getSignature().toLongString();
50      }
51  
52      private static Method findMethod(final Signature signature) {
53          if ("org.aspectj.runtime.reflect.MethodSignatureImpl".equals(signature.getClass().getName())) {
54              try {
55                  final Method mtd = signature.getClass().getMethod("getMethod");
56                  if (!mtd.isAccessible()) {
57                      mtd.setAccessible(true);
58                  }
59                  return Method.class.cast(mtd.invoke(signature));
60              } catch (final Exception e) {
61                  // no-op
62              }
63          }
64          return null;
65      }
66  }