1 22 package org.jboss.ejb3.stateless; 23 24 import java.io.ObjectStreamException ; 25 import java.io.Serializable ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.InvocationTargetException ; 28 29 import javassist.util.proxy.MethodHandler; 30 import javassist.util.proxy.ProxyFactory; 31 import javassist.util.proxy.ProxyObject; 32 33 39 public class JavassistProxy implements Serializable 40 { 41 private static final long serialVersionUID = 1L; 42 43 private MethodHandler handler; 44 private Class interfaces[]; 45 46 64 protected static void pokeInterfaces(JavassistProxy proxy, Class interfaces[]) { 65 proxy.interfaces = interfaces; 66 } 67 68 private Object readResolve() throws ObjectStreamException 69 { 70 System.err.println("JavassistProxy.readResolve"); 71 try { 72 ProxyFactory proxyFactory = new ProxyFactory() { 73 protected ClassLoader getClassLoader() 74 { 75 return Thread.currentThread().getContextClassLoader(); 76 } 77 }; 78 proxyFactory.setInterfaces(interfaces); 79 proxyFactory.setSuperclass(JavassistProxy.class); 80 Class proxyClass = proxyFactory.createClass(); 81 Constructor proxyConstructor = proxyClass.getConstructor((Class []) null); 82 JavassistProxy proxy = (JavassistProxy) proxyConstructor.newInstance((Object []) null); 83 proxy.setMethodHandler(this.handler); 84 proxy.interfaces = this.interfaces; 85 return proxy; 86 } 87 catch (NoSuchMethodException e) 88 { 89 throw new RuntimeException (e); 90 } 91 catch (InstantiationException e) 92 { 93 throw new RuntimeException (e); 94 } 95 catch (IllegalAccessException e) 96 { 97 throw new RuntimeException (e); 98 } 99 catch (InvocationTargetException e) 100 { 101 throw new RuntimeException (e.getTargetException()); 102 } 103 } 104 105 protected void setInterfaces(Class interfaces[]) 106 { 107 this.interfaces = interfaces; 109 } 110 111 protected void setMethodHandler(MethodHandler handler) 112 { 113 ((ProxyObject) this).setHandler(handler); 115 this.handler = handler; 116 } 117 118 private Object writeReplace() throws ObjectStreamException 119 { 120 System.err.println("JavassistProxy.writeReplace"); 121 JavassistProxy replacement = new JavassistProxyReplacement(); 122 replacement.handler = this.handler; 123 replacement.interfaces = this.interfaces; 124 return replacement; 125 } 126 } 127 | Popular Tags |