View Javadoc

1   package com.bm.ejb3metadata.annotations.metadata;
2   
3   import java.util.List;
4   import java.util.Map;
5   
6   import javax.ejb.Remove;
7   import javax.ejb.TransactionAttributeType;
8   
9   import com.bm.ejb3metadata.annotations.InterceptorType;
10  import com.bm.ejb3metadata.annotations.JClassInterceptor;
11  import com.bm.ejb3metadata.annotations.JMethod;
12  import com.bm.ejb3metadata.annotations.impl.JInterceptors;
13  import com.bm.ejb3metadata.annotations.metadata.interfaces.IEJBInterceptors;
14  import com.bm.ejb3metadata.annotations.metadata.interfaces.ITransactionAttribute;
15  
16  /**
17   * This class represents the annotation metadata of a method.
18   * @author Daniel Wiese
19   */
20  public class MethodAnnotationMetadata extends CommonAnnotationMetadata
21      implements Cloneable, ITransactionAttribute, IEJBInterceptors {
22  
23      /**
24       * Logger.
25       */
26      //private static JLog logger = JLogFactory.getLog(MethodAnnotationMetadata.class);
27  
28      /**
29       * Method on which we got metadata.
30       */
31      private JMethod jMethod = null;
32  
33      /**
34       * Parent metadata.
35       */
36      private ClassAnnotationMetadata classAnnotationMetadata = null;
37  
38  
39      /**
40       * Original parent metadata (if method is inherited).
41       */
42      private ClassAnnotationMetadata originalClassAnnotationMetadata = null;
43  
44      /**
45       * Type of transaction.
46       */
47      private TransactionAttributeType transactionAttributeType = null;
48  
49      /**
50       * @{@link javax.ejb.Remove} annotation.
51       */
52      private Remove remove = null;
53  
54      /**
55       * This method is a business method ?
56       */
57      private boolean businessMethod = false;
58  
59      /**
60       * PostConstruct method ?
61       */
62      private boolean postConstruct = false;
63  
64      /**
65       * PreDestroy method ?
66       */
67      private boolean preDestroy = false;
68  
69      /**
70       * PostActivate method ?
71       */
72      private boolean postActivate = false;
73  
74      /**
75       * PrePassivate method ?
76       */
77      private boolean prePassivate = false;
78  
79      /**
80       * @{@link javax.interceptor.AroundInvoke} method used by interceptors ?
81       */
82      private boolean aroundInvoke = false;
83  
84      /**
85       *This method is a method from a super class ?<br>
86       * This flag is used by enhancers
87       */
88      private boolean inherited = false;
89  
90      /**
91       * &#64;{@link javax.interceptor.ExcludeClassInterceptors} method ?
92       */
93      private boolean excludeClassInterceptors = false;
94  
95      /**
96       * &#64;{@link javax.ejb.Timeout} method ?
97       */
98      private boolean timeout = false;
99  
100     /**
101      * EasyBeans global interceptors.<br>
102      * These interceptors correspond to a list of interceptors
103      * that need to be present first on this current method.
104      */
105     private List<JClassInterceptor> globalEasyBeansInterceptors = null;
106 
107 
108     /**
109      * EasyBeans method interceptors. These interceptors correspond to a list of Interceptors like security or transaction.
110      */
111     private List<JClassInterceptor> interceptors = null;
112 
113     /**
114      * List of annotation interceptors.
115      */
116     private JInterceptors annotationInterceptors = null;
117 
118 
119     /**
120      * User interceptors. These interceptors correspond to a list of Interceptor that user has specified in its bean class.
121      * Map&lt;interceptor type &lt;--&gt; List of methods/class corresponding to the interceptor&gt;
122      */
123     private Map<InterceptorType, List<JClassInterceptor>> userInterceptors = null;
124 
125     /**
126      * Constructor.
127      * @param jMethod the method on which we will set/add metadata
128      * @param classAnnotationMetadata the parent metadata.
129      */
130     public MethodAnnotationMetadata(final JMethod jMethod, final ClassAnnotationMetadata classAnnotationMetadata) {
131         this.jMethod = jMethod;
132         this.classAnnotationMetadata = classAnnotationMetadata;
133     }
134 
135     /**
136      * @return name of the method
137      */
138     public String getMethodName() {
139         return this.jMethod.getName();
140     }
141 
142     /**
143      * @return JMethod object
144      */
145     public JMethod getJMethod() {
146         return this.jMethod;
147     }
148 
149     /**
150      * @return transaction Attribute type.
151      * @see javax.ejb.TransactionAttributeType
152      */
153     public TransactionAttributeType getTransactionAttributeType() {
154         return transactionAttributeType;
155     }
156 
157     /**
158      * Sets Transaction Attribute Type.
159      * @see javax.ejb.TransactionAttributeType
160      * @param transactionAttributeType the type of transaction.
161      */
162     public void setTransactionAttributeType(final TransactionAttributeType transactionAttributeType) {
163         this.transactionAttributeType = transactionAttributeType;
164     }
165 
166     /**
167      * @return true if the method is a business method.
168      */
169     public boolean isBusinessMethod() {
170         return businessMethod;
171     }
172 
173     /**
174      * This method is a business method.
175      * @param flag true/false if method is a business method.
176      */
177     public void setBusinessMethod(final boolean flag) {
178         this.businessMethod = flag;
179     }
180 
181     /**
182      * @return true if the method is a lifecycle method.
183      */
184     public boolean isLifeCycleMethod() {
185         return  isPostActivate() || isPostConstruct() || isPreDestroy() || isPrePassivate();
186     }
187 
188     /**
189      * @return remove attributes  &#64;{@link javax.ejb.Remove} attributes
190      */
191     public Remove getJRemove() {
192         return this.remove;
193     }
194 
195     /**
196      * Sets &#64;{@link javax.ejb.Remove} attribute.
197      * @param remove contains the attribute with retainIfException.
198      */
199     public void setRemove(final Remove remove) {
200         this.remove = remove;
201     }
202 
203 
204 
205     /**
206      * @return string representation
207      */
208     @Override
209     public String toString() {
210         StringBuilder sb = new StringBuilder();
211         String titleIndent = " ";
212 
213         // classname
214         sb.append(titleIndent);
215         sb.append(this.getClass().getName().substring(this.getClass().getPackage().getName().length() + 1));
216         sb.append("[\n");
217 
218         // Add super class toString()
219         sb.append(super.toString());
220 
221         // Method
222         concatStringBuilder("jMethod", jMethod, sb);
223 
224         // inherited
225         concatStringBuilder("inherited", Boolean.valueOf(inherited), sb);
226 
227         // transactionAttributeType
228         concatStringBuilder("transactionAttributeType", transactionAttributeType, sb);
229 
230         // remove
231         concatStringBuilder("remove", remove, sb);
232 
233         // businessMethod
234         concatStringBuilder("businessMethod", Boolean.valueOf(businessMethod), sb);
235 
236         // aroundInvoke
237         concatStringBuilder("aroundInvoke", Boolean.valueOf(aroundInvoke), sb);
238 
239         // postConstruct
240         concatStringBuilder("postConstruct", Boolean.valueOf(postConstruct), sb);
241 
242         // preDestroy
243         concatStringBuilder("preDestroy", Boolean.valueOf(preDestroy), sb);
244 
245         // postActivate
246         concatStringBuilder("postActivate", Boolean.valueOf(postActivate), sb);
247 
248         // prePassivate
249         concatStringBuilder("prePassivate", Boolean.valueOf(prePassivate), sb);
250 
251         // timeout
252         concatStringBuilder("timeout", Boolean.valueOf(timeout), sb);
253 
254         // annotation Interceptors
255         concatStringBuilder("annotationInterceptors", annotationInterceptors, sb);
256 
257         // interceptors
258         concatStringBuilder("interceptors", interceptors, sb);
259 
260         sb.append(titleIndent);
261         sb.append("]\n");
262         return sb.toString();
263     }
264 
265     /**
266      * @return true if method has  &#64;{@link javax.ejb.PostActivate}
267      */
268     public boolean isPostActivate() {
269         return postActivate;
270     }
271 
272     /**
273      * Sets true if method has &#64;{@link javax.ejb.PostActivate}.
274      * @param postActivate true/false.
275      */
276     public void setPostActivate(final boolean postActivate) {
277         this.postActivate = postActivate;
278     }
279 
280     /**
281      * @return true if method has &#64;{@link javax.annotation.PostConstruct}.
282      */
283     public boolean isPostConstruct() {
284         return postConstruct;
285     }
286 
287     /**
288      * Sets true if method has &#64;{@link javax.annotation.PostConstruct}.
289      * @param postConstruct true/false.
290      */
291     public void setPostConstruct(final boolean postConstruct) {
292         this.postConstruct = postConstruct;
293     }
294 
295     /**
296      * @return true if method has &#64;{@link javax.annotation.PreDestroy}.
297      */
298 
299     public boolean isPreDestroy() {
300         return preDestroy;
301     }
302 
303     /**
304      * Sets true if method has &#64;{@link javax.annotation.PreDestroy}.
305      * @param preDestroy true/false.
306      */
307     public void setPreDestroy(final boolean preDestroy) {
308         this.preDestroy = preDestroy;
309     }
310 
311     /**
312      * @return true if method has &#64;{@link javax.ejb.PrePassivate}.
313      */
314 
315     public boolean isPrePassivate() {
316         return prePassivate;
317     }
318 
319     /**
320      * Sets true if method has &#64;{@link javax.ejb.PrePassivate}.
321      * @param prePassivate true/false.
322      */
323     public void setPrePassivate(final boolean prePassivate) {
324         this.prePassivate = prePassivate;
325     }
326 
327     /**
328      * @return true if method has &#64;{@link javax.ejb.Timeout}.
329      */
330     public boolean isTimeout() {
331         return timeout;
332     }
333 
334     /**
335      * Sets true if method has &#64;{@link javax.ejb.Timeout}.
336      * @param timeout true/false.
337      */
338     public void setTimeout(final boolean timeout) {
339         this.timeout = timeout;
340     }
341 
342     /**
343      * @return true if method has &#64;{@link javax.interceptor.AroundInvoke}.
344      */
345     public boolean isAroundInvoke() {
346         return aroundInvoke;
347     }
348 
349     /**
350      * Sets true if method has &#64;{@link javax.interceptor.AroundInvoke}.
351      * @param aroundInvoke true/false
352      */
353     public void setAroundInvoke(final boolean aroundInvoke) {
354         this.aroundInvoke = aroundInvoke;
355     }
356 
357     /**
358      * @return true if this method is inherited from a super class
359      */
360     public boolean isInherited() {
361         return inherited;
362     }
363 
364     /**
365      * Sets the inheritance of this method.
366      * @param inherited true if method is from a super class
367      * @param originalClassAnnotationMetadata the metadata of the original class (not inherited)
368      */
369     public void setInherited(final boolean inherited, final ClassAnnotationMetadata originalClassAnnotationMetadata) {
370         this.inherited = inherited;
371         this.originalClassAnnotationMetadata = originalClassAnnotationMetadata;
372     }
373 
374     /**
375      * @return true if this method won't use user interceptors.
376      */
377     public boolean isExcludedClassInterceptors() {
378         return excludeClassInterceptors;
379     }
380 
381     /**
382      * Flag this method as a method which exclude user interceptors.
383      * @param excludeClassInterceptors true if this method is a method which exclude user interceptors.
384      */
385     public void setExcludeClassInterceptors(final boolean excludeClassInterceptors) {
386         this.excludeClassInterceptors = excludeClassInterceptors;
387     }
388 
389 
390 
391     /**
392      * @return parent metadata (class)
393      */
394     public ClassAnnotationMetadata getClassAnnotationMetadata() {
395         return classAnnotationMetadata;
396     }
397 
398     /**
399      * @return original parent metadata (class) if inherited.
400      */
401     public ClassAnnotationMetadata getOriginalClassAnnotationMetadata() {
402         return originalClassAnnotationMetadata;
403     }
404 
405     /**
406      * @return list of interceptors that enhancer will use. (ie : security/transaction)
407      */
408     public List<JClassInterceptor> getInterceptors() {
409         return interceptors;
410     }
411 
412     /**
413      * Sets the list of interceptors(tx, security, etc) that enhancers will use.<br>
414      * These interceptors are defined per methods.
415      * @param interceptors list of interceptors that enhancer will use.
416      */
417     public void setInterceptors(final List<JClassInterceptor> interceptors) {
418         this.interceptors = interceptors;
419     }
420 
421     /**
422      * @return object representing list of &#64;{@link javax.interceptor.Interceptors}.
423      */
424     public JInterceptors getAnnotationInterceptors() {
425         return annotationInterceptors;
426     }
427 
428     /**
429      * Sets the object representing the &#64;{@link javax.interceptor.Interceptors} annotation.
430      * @param annotationInterceptors list of classes
431      */
432     public void setAnnotationsInterceptors(final JInterceptors annotationInterceptors) {
433         this.annotationInterceptors = annotationInterceptors;
434     }
435 
436     /**
437      * @return Map&lt;interceptor type &lt;--&gt; List of methods/class corresponding to the interceptor&gt;
438      * of user interceptors that enhancer will use.
439      */
440     public Map<InterceptorType, List<JClassInterceptor>> getUserEasyBeansInterceptors() {
441         return userInterceptors;
442     }
443 
444     /**
445      * Sets the list of user interceptors that enhancers will use.<br>
446      * These interceptors are defined in bean class.
447      * @param userInterceptors list of interceptors that enhancer will use.
448      */
449     public void setUserInterceptors(final Map<InterceptorType, List<JClassInterceptor>> userInterceptors) {
450         this.userInterceptors = userInterceptors;
451     }
452 
453     /**
454      * @return list of global interceptors that enhancer will use. (ie : Remove interceptor)
455      */
456     public List<JClassInterceptor> getGlobalEasyBeansInterceptors() {
457         return globalEasyBeansInterceptors;
458     }
459 
460     /**
461      * Sets the list of global interceptors that enhancers will use.
462      * @param globalEasyBeansInterceptors list of interceptors that enhancer will use.
463      */
464     public void setGlobalEasyBeansInterceptors(final List<JClassInterceptor> globalEasyBeansInterceptors) {
465         this.globalEasyBeansInterceptors = globalEasyBeansInterceptors;
466     }
467 
468     /**
469      * @return a clone object.
470      */
471     @Override
472     public Object clone() {
473         MethodAnnotationMetadata newMethodAnnotationMetadata = new MethodAnnotationMetadata(jMethod, classAnnotationMetadata);
474         newMethodAnnotationMetadata.setAnnotationsInterceptors(annotationInterceptors);
475         newMethodAnnotationMetadata.setAroundInvoke(aroundInvoke);
476         newMethodAnnotationMetadata.setBusinessMethod(businessMethod);
477         newMethodAnnotationMetadata.setExcludeClassInterceptors(excludeClassInterceptors);
478         newMethodAnnotationMetadata.setInherited(inherited, originalClassAnnotationMetadata);
479         newMethodAnnotationMetadata.setInterceptors(interceptors);
480         newMethodAnnotationMetadata.setJAnnotationResource(getJAnnotationResource());
481         newMethodAnnotationMetadata.setJEjbEJB(getJEjbEJB());
482         newMethodAnnotationMetadata.setJavaxPersistenceContext(getJavaxPersistenceContext());
483         newMethodAnnotationMetadata.setJavaxPersistenceUnit(getJavaxPersistenceUnit());
484         newMethodAnnotationMetadata.setPostActivate(postActivate);
485         newMethodAnnotationMetadata.setPostConstruct(postConstruct);
486         newMethodAnnotationMetadata.setPreDestroy(preDestroy);
487         newMethodAnnotationMetadata.setPrePassivate(prePassivate);
488         newMethodAnnotationMetadata.setRemove(remove);
489         newMethodAnnotationMetadata.setTimeout(timeout);
490         newMethodAnnotationMetadata.setTransactionAttributeType(transactionAttributeType);
491         newMethodAnnotationMetadata.setUserInterceptors(userInterceptors);
492         newMethodAnnotationMetadata.setGlobalEasyBeansInterceptors(globalEasyBeansInterceptors);
493         return  newMethodAnnotationMetadata;
494     }
495 
496 
497     /**
498      * Replace the link to the classannotation metadata.
499      * @param classAnnotationMetadata new object for the link.
500      */
501     public void setClassAnnotationMetadata(final ClassAnnotationMetadata classAnnotationMetadata) {
502         this.classAnnotationMetadata = classAnnotationMetadata;
503     }
504 }