1 45 package org.openejb.client.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 59 public class Jdk13ProxyFactory implements ProxyFactory { 60 61 64 private final Class [] constructorParams = { java.lang.reflect.InvocationHandler .class}; 65 66 72 public void init(Properties props) { 73 String version = ""; 74 String badVersion = "1.3.0-"; 75 try { 76 version = System.getProperty("java.vm.version"); 77 } catch ( Exception e ) { 78 } 79 80 if ( version.indexOf(badVersion) != -1 ) { 81 String message = ""+ 82 "INCOMPATIBLE VM: \n\n"+ 83 "The Java Virtual Machine you are using contains a bug\n"+ 84 "in the proxy generation logic. This bug has been \n"+ 85 "documented by Sun and has been fixed in later VMs. \n"+ 86 "Please download the latest 1.3 Virtual Machine. \n"+ 87 "For more details see: \n"+ 88 "http://developer.java.sun.com/developer/bugParade/bugs/4346224.html\n "; 89 throw new RuntimeException (message); 90 } 91 } 92 93 96 public InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException { 97 98 Jdk13InvocationHandler handler = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy); 99 100 if ( handler == null ) return null; 101 102 return handler.getInvocationHandler(); 103 } 104 105 108 public Object setInvocationHandler(Object proxy, InvocationHandler handler) throws IllegalArgumentException { 109 110 Jdk13InvocationHandler jdk13 = (Jdk13InvocationHandler)Proxy.getInvocationHandler(proxy); 111 112 if ( jdk13 == null ) throw new IllegalArgumentException ("Proxy "+proxy+" unknown!"); 113 114 return jdk13.setInvocationHandler(handler); 115 } 116 117 125 public Class getProxyClass(Class interfce) throws IllegalArgumentException { 126 return Proxy.getProxyClass(interfce.getClassLoader(), new Class []{interfce}); 127 } 128 129 137 public Class getProxyClass(Class [] interfaces) throws IllegalArgumentException { 138 if ( interfaces.length < 1 ) { 139 throw new IllegalArgumentException ("There must be at least one interface to implement."); 140 } 141 142 return Proxy.getProxyClass(interfaces[0].getClassLoader(), interfaces); 143 } 144 145 153 public boolean isProxyClass(Class cl) { 154 return Proxy.isProxyClass(cl); 155 } 156 157 158 165 public Object newProxyInstance(Class proxyClass) throws IllegalArgumentException { 166 if ( !Proxy.isProxyClass(proxyClass) ) { 167 throw new IllegalArgumentException ("This class is not a proxy."); 168 } 169 170 try { 171 172 Constructor cons = proxyClass.getConstructor(constructorParams); 173 return cons.newInstance(new Object [] { new Jdk13InvocationHandler()}); 174 175 } catch ( NoSuchMethodException e ) { 176 throw new InternalError (e.toString()); 177 } catch ( IllegalAccessException e ) { 178 throw new InternalError (e.toString()); 179 } catch ( InstantiationException e ) { 180 throw new InternalError (e.toString()); 181 } catch ( InvocationTargetException e ) { 182 throw new InternalError (e.toString()); 183 } 184 } 185 186 195 public Object newProxyInstance(Class interfce, InvocationHandler h) throws IllegalArgumentException { 196 197 Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h); 198 199 return Proxy.newProxyInstance(interfce.getClassLoader(), new Class []{interfce}, handler); 200 } 201 202 211 public Object newProxyInstance(Class [] interfaces, InvocationHandler h) throws IllegalArgumentException { 212 if ( interfaces.length < 1 ) { 213 throw new IllegalArgumentException ("There must be at least one interface to implement."); 214 } 215 216 Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h); 217 218 return Proxy.newProxyInstance(interfaces[0].getClassLoader(), interfaces, handler); 219 } 220 } 221 | Popular Tags |