KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > microcontainer > integration > AOPMetaDataContext


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.aop.microcontainer.integration;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
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 /**
49  *
50  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
51  * @version $Revision: 58072 $
52  */

53 public class AOPMetaDataContext implements MutableMetaDataContext
54 {
55    final static List JavaDoc<Annotation JavaDoc> EMPTY_ANNOTATIONS = new ArrayList JavaDoc<Annotation JavaDoc>();
56    Scope scope;
57    Object JavaDoc target;
58    MutableMetaDataRepository repository;
59    String JavaDoc beanName;
60    ScopeKey instanceKey;
61    
62    public AOPMetaDataContext(MutableMetaDataRepository repository, String JavaDoc beanName)
63    {
64       this.repository = repository;
65       this.beanName = beanName;
66       //TODO: This needs linking up with the parent scopes somehow - where will that info come from?
67
scope = new Scope(CommonLevels.INSTANCE, beanName);
68       instanceKey = new ScopeKey(scope);
69    }
70    
71    public <T extends Annotation JavaDoc> boolean hasAnnotation(Class JavaDoc<T> ann)
72    {
73       return getAnnotation(ann) != null;
74    }
75
76    public <T extends Annotation JavaDoc> Annotation JavaDoc getAnnotation(Class JavaDoc<T> ann)
77    {
78       return getAnnotation(instanceKey, ann);
79    }
80
81    public <T extends Annotation JavaDoc> boolean hasAnnotationForMethod(long methodHash, Class JavaDoc<T> ann)
82    {
83       return getAnnotationForMethod(methodHash, ann) != null;
84    }
85
86    public <T extends Annotation JavaDoc> Annotation JavaDoc getAnnotationForMethod(long methodHash, Class JavaDoc<T> ann)
87    {
88       ScopeKey joinpointKey = createHashedJoinpointKey(methodHash);
89       return getAnnotation(joinpointKey, ann);
90    }
91
92    public List JavaDoc<Annotation JavaDoc> getAnnotations()
93    {
94       return getAnnotations(instanceKey);
95    }
96    
97    public List JavaDoc<Annotation JavaDoc> getAnnotationsForMethod(long methodHash)
98    {
99       ScopeKey joinpointKey = createHashedJoinpointKey(methodHash);
100       return getAnnotations(joinpointKey);
101    }
102    
103    public List JavaDoc<Annotation JavaDoc> getAnnotationsForMethods(long[] methodHashes)
104    {
105       ArrayList JavaDoc<Annotation JavaDoc> annotations = new ArrayList JavaDoc<Annotation JavaDoc>();
106       for (long hash : methodHashes)
107       {
108          ScopeKey joinpointKey = createHashedJoinpointKey(hash);
109          List JavaDoc<Annotation JavaDoc> methodAnnotations = getAnnotations(joinpointKey);
110          annotations.addAll(methodAnnotations);
111       }
112       return annotations;
113    }
114    
115    public MetaDataRepository getRepository()
116    {
117       return repository;
118    }
119
120    /**
121     * Add instance-level annotations
122     * @param annotations a Set<AnnotationMetaData>
123     */

124    public void addAnnotations(Set JavaDoc<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 JavaDoc annotation = annotationMetaData.getAnnotationInstance();
135          retrieval.addAnnotation(annotation);
136       }
137       repository.addMetaDataRetrieval(retrieval);
138    }
139    
140    public void addPropertyAnnotations(String JavaDoc propertyName, Set JavaDoc<PropertyInfo> propertyInfos, Set JavaDoc<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 JavaDoc 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 JavaDoc("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 JavaDoc> Annotation JavaDoc getAnnotation(ScopeKey key, Class JavaDoc<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 JavaDoc<Annotation JavaDoc> 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 JavaDoc<Annotation JavaDoc> annotations = new ArrayList JavaDoc<Annotation JavaDoc>();
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