1 package org.apache.ojb.broker.core.proxy; 2 3 17 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.InvocationHandler ; 21 import java.lang.reflect.Proxy ; 22 import java.util.HashMap ; 23 24 25 26 30 public class ProxyFactoryJDKImpl extends AbstractProxyFactory { 31 32 35 private HashMap foundInterfaces = new HashMap (); 36 37 38 public Class getDefaultIndirectionHandlerClass() 39 { 40 return IndirectionHandlerJDKImpl.class; 41 } 42 43 44 48 public Class getIndirectionHandlerBaseClass() 49 { 50 return IndirectionHandlerJDK.class; 51 } 52 53 54 55 public OJBProxy createProxy(Class baseClass, IndirectionHandler handler) throws Exception { 56 Class proxyClass = getDynamicProxyClass(baseClass); 57 Constructor constructor = proxyClass.getDeclaredConstructor(new Class [] { InvocationHandler .class }); 58 return (OJBProxy)constructor.newInstance(new Object [] { handler }); 59 } 60 61 public IndirectionHandler getDynamicIndirectionHandler(Object obj) { 62 return (IndirectionHandler)Proxy.getInvocationHandler(obj); 63 } 64 65 public boolean isNormalOjbProxy(Object proxyOrObject) { 66 return super.isNormalOjbProxy(proxyOrObject) && (proxyOrObject instanceof Proxy ) && Proxy.isProxyClass(proxyOrObject.getClass()); 67 } 68 69 75 private Class getDynamicProxyClass(Class baseClass) { 76 Class [] m_dynamicProxyClassInterfaces; 77 if (foundInterfaces.containsKey(baseClass)) { 78 m_dynamicProxyClassInterfaces = (Class [])foundInterfaces.get(baseClass); 79 } else { 80 m_dynamicProxyClassInterfaces = getInterfaces(baseClass); 81 foundInterfaces.put(baseClass, m_dynamicProxyClassInterfaces); 82 } 83 84 Class proxyClazz = Proxy.getProxyClass(baseClass.getClassLoader(), m_dynamicProxyClassInterfaces); 86 return proxyClazz; 87 } 88 89 95 private Class [] getInterfaces(Class clazz) { 96 Class superClazz = clazz; 97 Class [] interfaces = clazz.getInterfaces(); 98 99 if (clazz.isInterface()) { 103 Class [] tempInterfaces = new Class [interfaces.length + 1]; 104 tempInterfaces[0] = clazz; 105 106 System.arraycopy(interfaces, 0, tempInterfaces, 1, interfaces.length); 107 interfaces = tempInterfaces; 108 } 109 110 while ((superClazz = superClazz.getSuperclass()) != null) { 112 Class [] superInterfaces = superClazz.getInterfaces(); 113 Class [] combInterfaces = new Class [interfaces.length + superInterfaces.length]; 114 System.arraycopy(interfaces, 0, combInterfaces, 0, interfaces.length); 115 System.arraycopy(superInterfaces, 0, combInterfaces, interfaces.length, superInterfaces.length); 116 interfaces = combInterfaces; 117 } 118 119 124 HashMap unique = new HashMap (); 125 for (int i = 0; i < interfaces.length; i++) { 126 unique.put(interfaces[i].getName(), interfaces[i]); 127 } 128 129 unique.put(OJBProxy.class.getName(), OJBProxy.class); 130 131 interfaces = (Class [])unique.values().toArray(new Class [unique.size()]); 132 133 return interfaces; 134 } 135 136 public boolean interfaceRequiredForProxyGeneration() { 137 return true; 138 } 139 140 } 141 | Popular Tags |