KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > management > JMXInterfaceProxy


1 /**
2 * Copyright (c) 2004-2005 jManage.org
3 *
4 * This is a free software; you can redistribute it and/or
5 * modify it under the terms of the license at
6 * http://www.jmanage.org.
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */

14 package org.jmanage.core.management;
15
16 import java.lang.reflect.*;
17 import java.io.Serializable JavaDoc;
18
19 /**
20  * This is generic proxy object to handle incompatible JMX interfaces.
21  * This proxy object wraps the incompatible object, and uses reflection
22  * to invoke the same operation on the wrapped object.
23  * <p>
24  * This prevents the ClassCastException because of different classloaders.
25  *
26  * <p>
27  * Date: Sep 27, 2005
28  * @author Rakesh Kalra
29  * @see org.jmanage.core.config.ClassLoaderRepository
30  */

31 public class JMXInterfaceProxy implements InvocationHandler, Serializable JavaDoc {
32
33     public static Object JavaDoc newProxyInstance(Class JavaDoc clazz, Object JavaDoc object){
34         assert clazz.isInstance(object);
35         JMXInterfaceProxy proxy = new JMXInterfaceProxy(clazz, object);
36         return Proxy.newProxyInstance(
37                 JMXInterfaceProxy.class.getClassLoader(),
38                 new Class JavaDoc[]{clazz},
39                 proxy);
40     }
41
42     private Class JavaDoc clazz;
43     private Object JavaDoc wrappedObject;
44
45     private JMXInterfaceProxy(Class JavaDoc clazz, Object JavaDoc wrappedObject){
46         this.clazz = clazz;
47         this.wrappedObject = wrappedObject;
48     }
49
50     public Object JavaDoc invoke(Object JavaDoc proxy, Method method, Object JavaDoc[] args)
51             throws Throwable JavaDoc {
52
53         try {
54             Method wrappedMethod =
55                     clazz.getMethod(method.getName(),
56                             method.getParameterTypes());
57             return wrappedMethod.invoke(wrappedObject, args);
58         } catch (InvocationTargetException e) {
59             throw e.getCause();
60         }
61     }
62 }
63
Popular Tags