1 22 package org.jboss.aop.microcontainer.integration; 23 24 import java.lang.reflect.Constructor ; 25 26 import org.jboss.aop.Advisor; 27 import org.jboss.aop.AspectManager; 28 import org.jboss.aop.advice.Interceptor; 29 import org.jboss.aop.joinpoint.ConstructorInvocation; 30 import org.jboss.aop.proxy.container.AOPProxyFactory; 31 import org.jboss.aop.proxy.container.AOPProxyFactoryParameters; 32 import org.jboss.aop.proxy.container.ContainerCache; 33 import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory; 34 import org.jboss.joinpoint.plugins.BasicConstructorJoinPoint; 35 import org.jboss.reflect.spi.ConstructorInfo; 36 import org.jboss.reflect.spi.TypeInfo; 37 import org.jboss.repository.spi.MetaDataContext; 38 39 48 public class AOPConstructorJoinpoint extends BasicConstructorJoinPoint 49 { 50 AOPProxyFactory proxyFactory = new GeneratedAOPProxyFactory(); 51 52 MetaDataContext metaDataContext; 53 54 59 public AOPConstructorJoinpoint(ConstructorInfo constructorInfo, MetaDataContext metaDataContext) 60 { 61 super(constructorInfo); 62 this.metaDataContext = metaDataContext; 63 } 64 65 public Object dispatch() throws Throwable 66 { 67 Class clazz = constructorInfo.getDeclaringClass().getType(); 68 AspectManager manager = AspectManager.instance(); 69 if (manager.isNonAdvisableClassName(clazz.getName())) 70 { 71 return super.dispatch(); 72 } 73 ContainerCache cache = ContainerCache.initialise(manager, clazz, metaDataContext); 74 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 75 Object target = createTarget(cache, params); 76 77 params.setProxiedClass(target.getClass()); 78 params.setMetaDataContext(metaDataContext); 79 params.setTarget(target); 80 params.setContainerCache(cache); 81 82 return proxyFactory.createAdvisedProxy(params); 83 } 84 85 private Object createTarget(ContainerCache cache, AOPProxyFactoryParameters params) throws Throwable 86 { 87 Advisor advisor = cache.getAdvisor(); 88 if (advisor != null) 89 { 90 org.jboss.aop.ConstructorInfo aopinfo = findAopConstructorInfo(advisor); 91 92 Interceptor[] interceptors = (aopinfo != null) ? aopinfo.getInterceptors() : null; 93 94 if (interceptors != null) 95 { 96 ConstructorInvocation inv = new ConstructorInvocation(aopinfo, aopinfo.getInterceptors()); 97 inv.setArguments(getArguments()); 98 return inv.invokeNext(); 99 } 100 101 if (getConstructorInfo().getParameterTypes().length > 0) 102 { 103 Constructor constructor = null; 104 if (aopinfo == null) 105 { 106 Class clazz = advisor.getClazz(); 108 Constructor [] ctors = clazz.getConstructors(); 109 for (Constructor ctor : ctors) 110 { 111 if (matchConstructor(ctor)) 112 { 113 constructor = ctor; 114 break; 115 } 116 } 117 } 118 else 119 { 120 constructor = aopinfo.getConstructor(); 121 } 122 params.setCtor(constructor.getParameterTypes(), getArguments()); 123 } 124 } 125 126 return super.dispatch(); 127 } 128 129 private org.jboss.aop.ConstructorInfo findAopConstructorInfo(Advisor advisor) 130 { 131 org.jboss.aop.ConstructorInfo[] infos = advisor.getConstructorInfos(); 132 for (int i = 0 ; i < infos.length ; i++) 133 { 134 if (matchConstructor(infos[i].getConstructor())) 135 { 136 return infos[i]; 137 } 138 } 139 return null; 140 } 141 142 private boolean matchConstructor(Constructor ctor) 143 { 144 TypeInfo[] params = constructorInfo.getParameterTypes(); 145 Class [] ctorParams = ctor.getParameterTypes(); 146 if (params.length == ctorParams.length) 147 { 148 boolean match = true; 149 for (int p = 0 ; p < params.length ; p++) 150 { 151 if (!params[p].getName().equals(ctorParams[p].getName())) 152 { 153 match = false; 154 break; 155 } 156 } 157 158 if (match) 159 { 160 return true; 161 } 162 } 163 return false; 164 } 165 166 } 167 | Popular Tags |