1 16 package com.jdon.bussinessproxy.dyncproxy; 17 import java.lang.reflect.Proxy ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import com.jdon.aop.AopClient; 22 import com.jdon.bussinessproxy.TargetMetaDef; 23 import com.jdon.bussinessproxy.meta.DistributedTargetMetaDef; 24 import com.jdon.bussinessproxy.meta.EJBTargetMetaDef; 25 import com.jdon.container.access.TargetMetaRequest; 26 import com.jdon.container.visitor.Visitable; 27 import com.jdon.util.Debug; 28 29 38 public class ProxyInstanceFactoryVisitable implements Visitable{ 39 private final static String module = ProxyInstanceFactoryVisitable.class.getName(); 40 private AopClient aopClient; 41 42 45 public ProxyInstanceFactoryVisitable(AopClient aopClient) { 46 super(); 47 this.aopClient = aopClient; 48 } 49 50 53 public Object accept(TargetMetaRequest targetMetaRequest) { 54 Debug.logVerbose("[JdonFramework]enter Proxy.newProxyInstance ", module); 55 Object dynamicProxy = null; 56 try { 57 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 58 59 DynamicProxyWeaving dynamicProxyWeaving = new DynamicProxyWeaving(targetMetaRequest, aopClient); 60 dynamicProxy = 61 Proxy.newProxyInstance( 62 classLoader, 63 getInterfaces(targetMetaRequest.getTargetMetaDef()), 64 dynamicProxyWeaving); 65 } catch (Exception ex) { 66 Debug.logError("[JdonFramework] Proxy.newProxyInstance error:" + ex, module); 67 } catch (Throwable ex) { 68 Debug.logError("[JdonFramework] Proxy.newProxyInstance error:" + ex, module); 69 } 70 return dynamicProxy; 71 } 72 73 82 private Class [] getInterfaces(TargetMetaDef targetMetaDef){ 83 Class [] interfaces = targetMetaDef.getInterfaces(); 84 if (interfaces != null) return interfaces; 85 try { 86 if (targetMetaDef.isEJB()){ 87 if (targetMetaDef instanceof EJBTargetMetaDef) 88 interfaces = getEJB2Interfaces(targetMetaDef); 89 else if (targetMetaDef instanceof DistributedTargetMetaDef) 90 interfaces = getEJB3Interfaces(targetMetaDef); 91 }else{ 92 interfaces = getPOJOInterfaces(targetMetaDef); 93 } 94 } catch (Exception ex) { 95 Debug.logError("[JdonFramework] getInterfaces error:" + ex, module); 96 } catch (Throwable ex) { 97 Debug.logError("[JdonFramework] getInterfaces error:" + ex, module); 98 } 99 if ((interfaces == null) || (interfaces.length == 0)){ 100 Debug.logError("[JdonFramework] no find any interface for the service:" + targetMetaDef.getClassName(), module); 101 }else{ 102 targetMetaDef.setInterfaces(interfaces); Debug.logVerbose("[JdonFramework]found the the below interfaces for the service:"+ targetMetaDef.getClassName(), module); 104 for(int i=0; i<interfaces.length; i++){ 105 Debug.logVerbose(interfaces[i].getName() + ";", module); 106 } 107 } 108 return interfaces; 109 } 110 111 private Class [] getEJB3Interfaces(TargetMetaDef targetMetaDef) throws Exception { 112 Class [] interfaces = null; 113 try { 114 DistributedTargetMetaDef dtargetMetaDef = (DistributedTargetMetaDef)targetMetaDef; 115 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 116 Debug.logVerbose("[JdonFramework]getEJB3Interfaces interface="+ dtargetMetaDef.getInterfaceClass(), module); 117 interfaces = new Class [] {classLoader.loadClass(dtargetMetaDef.getInterfaceClass()) }; 118 } catch (Exception e) { 119 e.printStackTrace(); 120 throw new Exception (); 121 } 122 return interfaces; 123 } 124 125 126 private Class [] getEJB2Interfaces(TargetMetaDef targetMetaDef) throws Exception { 127 Class [] interfaces = null; 128 Debug.logVerbose("[JdonFramework]getEJB2Interfaces "+ targetMetaDef.getClassName(), module); 129 try { 130 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 131 Class configClasses = classLoader.loadClass(targetMetaDef.getClassName()); 132 EJBTargetMetaDef em = (EJBTargetMetaDef) targetMetaDef; 133 if ((em.getInterfaceClass() != null) 134 && (em.getInterfaceClass().length() != 0)) { 135 interfaces = new Class [] { configClasses, classLoader.loadClass(em.getInterfaceClass()) }; 136 } else 137 interfaces = new Class [] { configClasses }; 138 } catch (Exception e) { 139 e.printStackTrace(); 140 throw new Exception (); 141 } 142 return interfaces; 143 } 144 145 146 public Class [] getPOJOInterfaces(TargetMetaDef targetMetaDef) { 147 Class [] interfaces = null; 148 Debug.logVerbose("[JdonFramework]getPOJOInterfaces "+ targetMetaDef.getClassName(), module); 149 try { 150 List interfacesL = new ArrayList (); 151 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 152 Class pojoClass = classLoader.loadClass(targetMetaDef.getClassName()); 153 while (pojoClass != null) { 154 for (int i = 0; i < pojoClass.getInterfaces().length; i++) { 155 Class ifc = pojoClass.getInterfaces()[i]; 156 interfacesL.add(ifc); 157 } 158 pojoClass = pojoClass.getSuperclass(); 159 } 160 if (interfacesL.size() == 0){ 161 throw new Exception (); 162 } 163 interfaces = (Class []) interfacesL.toArray(new Class [interfacesL.size()]); 164 } catch (Exception e) { 165 Debug.logError("[JdonFramework] not found the interface of your pojoClass=" + 166 targetMetaDef.getClassName() +", error:" + e, module); 167 } 168 return interfaces; 169 } 170 171 172 173 } 174
| Popular Tags
|