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.remote.template;
17  
18  import java.lang.reflect.InvocationHandler;
19  import java.lang.reflect.Method;
20  
21  import org.jdiagnose.remote.Agent;
22  import org.jdiagnose.remote.RemoteResultListener;
23  
24  
25  /***
26   * @author jmccrindle
27   */
28  public class DiagnosticProxy extends DiagnosticServiceSupport implements InvocationHandler {
29      
30      private Object delegate = null;
31      private String name = null;
32      
33      public DiagnosticProxy() {
34          super();
35      }
36  
37      public DiagnosticProxy(Agent agent, String name, Object delegate, RemoteResultListener listener, boolean filterSuccesses) {
38          super(agent, listener, filterSuccesses);
39          this.delegate = delegate;
40          this.name = name;
41      }
42  
43      /* (non-Javadoc)
44       * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
45       */
46      public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
47          return getDiagnosticTemplate().execute(name + "." + method.toString(), new DiagnosticCallback() {
48              public Object invoke() throws Throwable {
49                Method delegateMethod = delegate.getClass().getMethod(method.getName(), method.getParameterTypes());
50                return delegateMethod.invoke(delegate, args);
51              }
52          });
53      }
54  
55      /***
56       * @param remoteResult The remoteResult to set.
57       */
58      public void setDelegate(Object delegate) {
59          this.delegate = delegate;
60      }
61      /***
62       * @param name The name to set.
63       */
64      public void setName(String name) {
65          this.name = name;
66      }
67  }