1 22 package org.jboss.aop.microcontainer.integration; 23 24 import java.lang.annotation.Annotation ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 import org.jboss.aop.Advised; 30 import org.jboss.aop.Advisor; 31 import org.jboss.aop.proxy.container.AspectManaged; 32 import org.jboss.aop.util.ClassInfoMethodHashing; 33 import org.jboss.beans.info.spi.PropertyInfo; 34 import org.jboss.beans.metadata.spi.AnnotationMetaData; 35 import org.jboss.kernel.spi.metadata.MutableMetaDataContext; 36 import org.jboss.metadata.plugins.loader.memory.MemoryMetaDataLoader; 37 import org.jboss.metadata.spi.repository.MetaDataRepository; 38 import org.jboss.metadata.spi.repository.MutableMetaDataRepository; 39 import org.jboss.metadata.spi.retrieval.AnnotationItem; 40 import org.jboss.metadata.spi.retrieval.AnnotationsItem; 41 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval; 42 import org.jboss.metadata.spi.scope.CommonLevels; 43 import org.jboss.metadata.spi.scope.Scope; 44 import org.jboss.metadata.spi.scope.ScopeKey; 45 import org.jboss.reflect.spi.MethodInfo; 46 import org.jboss.repository.spi.MetaDataContext; 47 48 53 public class AOPMetaDataContext implements MutableMetaDataContext 54 { 55 final static List <Annotation > EMPTY_ANNOTATIONS = new ArrayList <Annotation >(); 56 Scope scope; 57 Object target; 58 MutableMetaDataRepository repository; 59 String beanName; 60 ScopeKey instanceKey; 61 62 public AOPMetaDataContext(MutableMetaDataRepository repository, String beanName) 63 { 64 this.repository = repository; 65 this.beanName = beanName; 66 scope = new Scope(CommonLevels.INSTANCE, beanName); 68 instanceKey = new ScopeKey(scope); 69 } 70 71 public <T extends Annotation > boolean hasAnnotation(Class <T> ann) 72 { 73 return getAnnotation(ann) != null; 74 } 75 76 public <T extends Annotation > Annotation getAnnotation(Class <T> ann) 77 { 78 return getAnnotation(instanceKey, ann); 79 } 80 81 public <T extends Annotation > boolean hasAnnotationForMethod(long methodHash, Class <T> ann) 82 { 83 return getAnnotationForMethod(methodHash, ann) != null; 84 } 85 86 public <T extends Annotation > Annotation getAnnotationForMethod(long methodHash, Class <T> ann) 87 { 88 ScopeKey joinpointKey = createHashedJoinpointKey(methodHash); 89 return getAnnotation(joinpointKey, ann); 90 } 91 92 public List <Annotation > getAnnotations() 93 { 94 return getAnnotations(instanceKey); 95 } 96 97 public List <Annotation > getAnnotationsForMethod(long methodHash) 98 { 99 ScopeKey joinpointKey = createHashedJoinpointKey(methodHash); 100 return getAnnotations(joinpointKey); 101 } 102 103 public List <Annotation > getAnnotationsForMethods(long[] methodHashes) 104 { 105 ArrayList <Annotation > annotations = new ArrayList <Annotation >(); 106 for (long hash : methodHashes) 107 { 108 ScopeKey joinpointKey = createHashedJoinpointKey(hash); 109 List <Annotation > methodAnnotations = getAnnotations(joinpointKey); 110 annotations.addAll(methodAnnotations); 111 } 112 return annotations; 113 } 114 115 public MetaDataRepository getRepository() 116 { 117 return repository; 118 } 119 120 124 public void addAnnotations(Set <AnnotationMetaData> annotations) 125 { 126 if (annotations.size() == 0) 127 { 128 return; 129 } 130 131 MemoryMetaDataLoader retrieval = new MemoryMetaDataLoader(instanceKey); 132 for (AnnotationMetaData annotationMetaData : annotations) 133 { 134 Annotation annotation = annotationMetaData.getAnnotationInstance(); 135 retrieval.addAnnotation(annotation); 136 } 137 repository.addMetaDataRetrieval(retrieval); 138 } 139 140 public void addPropertyAnnotations(String propertyName, Set <PropertyInfo> propertyInfos, Set <AnnotationMetaData> annotations) 141 { 142 for (PropertyInfo info : propertyInfos) 143 { 144 if (propertyName.equals(info.getName())) 145 { 146 MemoryMetaDataLoader getterRetrieval = createGetterMetaDataRetrieval(info); 147 MemoryMetaDataLoader setterRetrieval = createSetterMetaDataRetrieval(info); 148 149 if (getterRetrieval == null && setterRetrieval == null) 150 { 151 continue; 152 } 153 154 for (AnnotationMetaData annotation : annotations) 155 { 156 if (getterRetrieval != null) 157 { 158 getterRetrieval.addAnnotation(annotation.getAnnotationInstance()); 159 } 160 if (setterRetrieval != null) 161 { 162 setterRetrieval.addAnnotation(annotation.getAnnotationInstance()); 163 } 164 } 165 166 if (getterRetrieval != null) 167 { 168 repository.addMetaDataRetrieval(getterRetrieval); 169 } 170 if (setterRetrieval != null) 171 { 172 repository.addMetaDataRetrieval(setterRetrieval); 173 } 174 } 175 } 176 } 177 178 public void setTarget(Object tgt) 179 { 180 if (tgt == null) 181 { 182 return; 183 } 184 185 target = tgt; 186 Advisor advisor = null; 187 if (tgt instanceof AspectManaged) 188 { 189 advisor = (Advisor)((AspectManaged)tgt).getInstanceAdvisor(); 190 } 191 else if (tgt instanceof Advised) 192 { 193 advisor = (Advisor)((Advised)tgt)._getInstanceAdvisor(); 194 } 195 196 if (advisor != null) 197 { 198 MetaDataContext advCtx = advisor.getMetadataContext(); 199 if (advCtx != null && advCtx != this) 200 { 201 throw new RuntimeException ("Different context being set in constructed advisor"); 202 } 203 if (advCtx == null) 204 { 205 advisor.setMetadataContext(this); 206 } 207 } 208 } 209 210 private MemoryMetaDataLoader createGetterMetaDataRetrieval(PropertyInfo propertyInfo) 211 { 212 MethodInfo getter = propertyInfo.getGetter(); 213 return createMethodMetaDataRetrieval(getter); 214 } 215 216 private MemoryMetaDataLoader createSetterMetaDataRetrieval(PropertyInfo propertyInfo) 217 { 218 MethodInfo setter = propertyInfo.getSetter(); 219 return createMethodMetaDataRetrieval(setter); 220 } 221 222 private MemoryMetaDataLoader createMethodMetaDataRetrieval(MethodInfo accessor) 223 { 224 if (accessor == null) 225 { 226 return null; 227 } 228 long hash = ClassInfoMethodHashing.methodHash(accessor); 229 ScopeKey joinpointKey = createHashedJoinpointKey(hash); 230 MemoryMetaDataLoader retrieval = new MemoryMetaDataLoader(joinpointKey); 231 return retrieval; 232 } 233 234 private ScopeKey createHashedJoinpointKey(long hash) 235 { 236 ScopeKey joinpointKey = new ScopeKey(instanceKey.getScopes()); 237 joinpointKey.addScope(CommonLevels.JOINPOINT, String.valueOf(hash)); 238 return joinpointKey; 239 } 240 241 private <T extends Annotation > Annotation getAnnotation(ScopeKey key, Class <T> ann) 242 { 243 MetaDataRetrieval retrieval = repository.getMetaDataRetrieval(key); 244 245 if (retrieval != null) 246 { 247 AnnotationItem item = retrieval.retrieveAnnotation(ann); 248 if (item != null) 249 { 250 return item.getAnnotation(); 251 } 252 } 253 return null; 254 } 255 256 private List <Annotation > getAnnotations(ScopeKey key) 257 { 258 MetaDataRetrieval retrieval = repository.getMetaDataRetrieval(key); 259 260 if (retrieval != null) 261 { 262 AnnotationsItem item = retrieval.retrieveAnnotations(); 263 if (item != null) 264 { 265 AnnotationItem[] items = item.getAnnotations(); 266 List <Annotation > annotations = new ArrayList <Annotation >(); 267 for (AnnotationItem aitem : items) 268 { 269 annotations.add(aitem.getAnnotation()); 270 } 271 return annotations; 272 } 273 } 274 return EMPTY_ANNOTATIONS; 275 } 276 } 277 | Popular Tags |