1 8 9 package mx4j.remote.rmi; 10 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.Proxy ; 13 import java.security.AccessController ; 14 import java.security.PrivilegedAction ; 15 16 import javax.management.MBeanServerConnection ; 17 18 import mx4j.remote.ClientProxy; 19 20 28 public class ClientUnmarshaller extends ClientProxy 29 { 30 private final ClassLoader classLoader; 31 32 private ClientUnmarshaller(MBeanServerConnection target, ClassLoader loader) 33 { 34 super(target); 35 this.classLoader = loader; 36 } 37 38 public static MBeanServerConnection newInstance(MBeanServerConnection target, ClassLoader loader) 39 { 40 ClientUnmarshaller handler = new ClientUnmarshaller(target, loader); 41 return (MBeanServerConnection )Proxy.newProxyInstance(handler.getClass().getClassLoader(), new Class []{MBeanServerConnection .class}, handler); 42 } 43 44 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 45 { 46 if (classLoader == null) 47 { 48 return chain(proxy, method, args); 49 } 50 else 51 { 52 ClassLoader old = Thread.currentThread().getContextClassLoader(); 53 try 54 { 55 setContextClassLoader(classLoader); 56 return chain(proxy, method, args); 57 } 58 finally 59 { 60 setContextClassLoader(old); 61 } 62 } 63 } 64 65 private Object chain(Object proxy, Method method, Object [] args) throws Throwable 66 { 67 return super.invoke(proxy, method, args); 68 } 69 70 private void setContextClassLoader(final ClassLoader loader) 71 { 72 AccessController.doPrivileged(new PrivilegedAction () 73 { 74 public Object run() 75 { 76 Thread.currentThread().setContextClassLoader(loader); 77 return null; 78 } 79 }); 80 } 81 } 82 | Popular Tags |