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.aop;
18  
19  import org.apache.commons.monitoring.configuration.Configuration;
20  import org.apache.commons.monitoring.util.ClassLoaders;
21  import org.apache.commons.proxy.Invoker;
22  import org.apache.commons.proxy.ProxyFactory;
23  
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  
27  public final class MonitoringProxyFactory {
28      private static final ProxyFactory PROXY_FACTORY = Configuration.newInstance(ProxyFactory.class);
29  
30      public static <T> T monitor(final Class<T> clazz, final Object instance) {
31          return clazz.cast(PROXY_FACTORY.createInvokerProxy(ClassLoaders.current(), new MonitoringHandler(instance), new Class<?>[]{clazz}));
32      }
33  
34      private MonitoringProxyFactory() {
35          // no-op
36      }
37  
38      private static class MonitoringHandler extends AbstractPerformanceInterceptor<Invocation> implements Invoker {
39          private final Object instance;
40  
41          public MonitoringHandler(final Object instance) {
42              this.instance = instance;
43          }
44  
45          @Override
46          public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
47              return doInvoke(new Invocation(instance, method, args));
48          }
49  
50          @Override
51          protected Object proceed(final Invocation invocation) throws Throwable {
52              try {
53                  return invocation.method.invoke(invocation.target, invocation.args);
54              } catch (final InvocationTargetException ite) {
55                  throw ite.getCause();
56              }
57          }
58  
59          @Override
60          protected String getCounterName(final Invocation invocation) {
61              return getCounterName(invocation.target, invocation.method);
62          }
63      }
64  
65      private static class Invocation {
66          private final Object target;
67          private final Method method;
68          private final Object[] args;
69  
70          private Invocation(final Object target, final Method method, final Object[] args) {
71              this.target = target;
72              this.method = method;
73              this.args = args;
74          }
75      }
76  }