1 7 package org.ejtools.jmx; 8 9 import java.lang.reflect.InvocationHandler ; 10 import java.lang.reflect.Method ; 11 import java.lang.reflect.Proxy ; 12 13 import javax.management.MBeanServer ; 14 15 21 public class MBeanServerProxy implements InvocationHandler 22 { 23 24 protected Class clazz = null; 25 26 protected Object object = null; 27 28 private final static Class [] INTERFACES = new Class []{MBeanServer .class}; 29 30 31 37 public MBeanServerProxy(Object object, Class clazz) 38 { 39 this.object = object; 40 this.clazz = clazz; 41 } 42 43 44 53 public Object invoke(Object obj, Method method, Object [] args) 54 throws Throwable 55 { 56 Method m = this.clazz.getMethod(method.getName(), method.getParameterTypes()); 57 if (m != null) 58 { 59 ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); 60 61 try 62 { 63 Thread.currentThread().setContextClassLoader(this.clazz.getClassLoader()); 64 return m.invoke(this.object, args); 65 } 66 finally 67 { 68 Thread.currentThread().setContextClassLoader(ctxLoader); 69 } 70 } 71 return null; 72 } 73 74 75 81 public static MBeanServer createMBeanProxy(Object object) 82 { 83 return MBeanServerProxy.createMBeanProxy(object, object.getClass()); 84 } 85 86 87 94 public static MBeanServer createMBeanProxy(Object object, Class clazz) 95 { 96 MBeanServerProxy proxy = new MBeanServerProxy(object, clazz); 97 return (MBeanServer ) Proxy.newProxyInstance(MBeanServerProxy.class.getClassLoader(), MBeanServerProxy.INTERFACES, proxy); 98 } 99 } 100 | Popular Tags |