View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.aop;
17  
18  import java.lang.reflect.Method;
19  
20  import org.aopalliance.intercept.MethodInterceptor;
21  import org.aopalliance.intercept.MethodInvocation;
22  import org.jdiagnose.remote.Agent;
23  import org.jdiagnose.remote.RemoteResultListener;
24  import org.jdiagnose.remote.template.DiagnosticCallback;
25  import org.jdiagnose.remote.template.DiagnosticServiceSupport;
26  
27  /***
28   * @author jmccrindle
29   */
30  public class DiagnosticInterceptor extends DiagnosticServiceSupport implements MethodInterceptor {
31      
32      private String name = null;
33      private Agent remoteResultFactory;
34  
35      public DiagnosticInterceptor(Agent agent, String name, RemoteResultListener listener, boolean filterSuccesses) {
36          super(agent, listener, filterSuccesses);
37          this.name = name;
38          this.remoteResultFactory = agent;
39      }
40  
41      public DiagnosticInterceptor(Agent agent, RemoteResultListener listener) {
42          super(agent, listener);
43          this.remoteResultFactory = agent;
44      }
45  
46      /* (non-Javadoc)
47       * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
48       */
49      public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
50          Method method = methodInvocation.getMethod();
51          if(name == null) {
52              name = method.getDeclaringClass().getName();
53          }
54          return getDiagnosticTemplate().execute(name + "." + method.toString(), new DiagnosticCallback() {
55              public Object invoke() throws Throwable {
56                  return methodInvocation.proceed();
57              }
58          });
59      }
60  
61  }