1 45 package org.openejb.util.proxy; 46 47 import java.lang.reflect.Constructor ; 48 import java.lang.reflect.InvocationTargetException ; 49 import java.lang.reflect.Proxy ; 50 import java.util.Properties ; 51 52 import org.openejb.OpenEJBException; 53 54 62 public class Jdk13ProxyFactory implements ProxyFactory { 63 public void init(Properties props) throws OpenEJBException { 64 String version = ""; 65 String badVersion = "1.3.0-"; 66 try{ 67 version = System.getProperty("java.vm.version"); 68 } catch(Exception e){ 69 } 70 if (version.indexOf(badVersion) != -1) { 71 String message = ""+ 72 "INCOMPATIBLE VM: \n\n"+ 73 "The Java Virtual Machine you are using contains a bug\n"+ 74 "in the proxy generation logic. This bug has been \n"+ 75 "documented by Sun and has been fixed in later VMs. \n"+ 76 "Please download the latest 1.3 Virtual Machine. \n"+ 77 "For more details see: \n"+ 78 "http://developer.java.sun.com/developer/bugParade/bugs/4346224.html\n "; 79 throw new OpenEJBException(message); 80 } 81 } 82 83 86 public org.openejb.util.proxy.InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException { 87 Jdk13InvocationHandler handler = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy); 88 if(handler == null) 89 return null; 90 return handler.getInvocationHandler(); 91 } 92 93 96 public Object setInvocationHandler(Object proxy, org.openejb.util.proxy.InvocationHandler handler) throws IllegalArgumentException { 97 Jdk13InvocationHandler jdk13 = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy); 98 if(jdk13 == null) 99 throw new IllegalArgumentException ("Proxy "+proxy+" unknown!"); 100 return jdk13.setInvocationHandler(handler); 101 } 102 103 106 public Class getProxyClass(Class interfce) throws IllegalArgumentException { 107 return Proxy.getProxyClass(interfce.getClassLoader(), new Class []{interfce}); 108 } 109 112 public Class getProxyClass(Class [] interfaces) throws IllegalArgumentException { 113 if(interfaces.length < 1) { 114 throw new IllegalArgumentException ("It's boring to implement 0 interfaces!"); 115 } 116 return Proxy.getProxyClass(interfaces[0].getClassLoader(), interfaces); 117 } 118 119 120 123 public boolean isProxyClass(Class cl) { 124 return Proxy.isProxyClass(cl); 125 } 126 127 private final static Class [] constructorParams = { java.lang.reflect.InvocationHandler .class }; 128 129 public Object newProxyInstance(Class proxyClass) throws IllegalArgumentException { 130 if(!Proxy.isProxyClass(proxyClass)) 131 throw new IllegalArgumentException (); 132 try { 133 Constructor cons = proxyClass.getConstructor(constructorParams); 134 return (Object ) cons.newInstance(new Object [] { new Jdk13InvocationHandler() }); 135 } catch (NoSuchMethodException e) { 136 throw new InternalError (e.toString()); 137 } catch (IllegalAccessException e) { 138 throw new InternalError (e.toString()); 139 } catch (InstantiationException e) { 140 throw new InternalError (e.toString()); 141 } catch (InvocationTargetException e) { 142 throw new InternalError (e.toString()); 143 } 144 } 145 146 150 public Object newProxyInstance(Class interfce, org.openejb.util.proxy.InvocationHandler h) throws IllegalArgumentException { 151 Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h); 152 return Proxy.newProxyInstance(interfce.getClassLoader(), new Class []{interfce}, handler); 153 } 154 155 159 public Object newProxyInstance(Class [] interfaces, org.openejb.util.proxy.InvocationHandler h) throws IllegalArgumentException { 160 if(interfaces.length < 1) { 161 throw new IllegalArgumentException ("It's boring to implement 0 interfaces!"); 162 } 163 Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h); 164 return Proxy.newProxyInstance(interfaces[0].getClassLoader(), interfaces, handler); 165 } 166 } 167 168 | Popular Tags |