1 package com.bm.ejb3metadata.annotations.metadata;
2
3 import static javax.ejb.TransactionAttributeType.REQUIRED;
4 import static javax.ejb.TransactionManagementType.CONTAINER;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Map;
12
13 import javax.ejb.ApplicationException;
14 import javax.ejb.TransactionAttributeType;
15 import javax.ejb.TransactionManagementType;
16
17 import com.bm.ejb3metadata.annotations.ClassType;
18 import com.bm.ejb3metadata.annotations.InterceptorType;
19 import com.bm.ejb3metadata.annotations.JClassInterceptor;
20 import com.bm.ejb3metadata.annotations.JField;
21 import com.bm.ejb3metadata.annotations.JMethod;
22 import com.bm.ejb3metadata.annotations.exceptions.InterceptorsValidationException;
23 import com.bm.ejb3metadata.annotations.impl.JAnnotationResource;
24 import com.bm.ejb3metadata.annotations.impl.JCommonBean;
25 import com.bm.ejb3metadata.annotations.impl.JEjbEJB;
26 import com.bm.ejb3metadata.annotations.impl.JInterceptors;
27 import com.bm.ejb3metadata.annotations.impl.JLocal;
28 import com.bm.ejb3metadata.annotations.impl.JMessageDriven;
29 import com.bm.ejb3metadata.annotations.impl.JRemote;
30 import com.bm.ejb3metadata.annotations.impl.JService;
31 import com.bm.ejb3metadata.annotations.impl.JStateful;
32 import com.bm.ejb3metadata.annotations.impl.JStateless;
33 import com.bm.ejb3metadata.annotations.impl.JavaxPersistenceContext;
34 import com.bm.ejb3metadata.annotations.impl.JavaxPersistenceUnit;
35 import com.bm.ejb3metadata.annotations.metadata.interfaces.IEJBInterceptors;
36 import com.bm.ejb3metadata.annotations.metadata.interfaces.ITransactionAttribute;
37
38
39
40
41
42
43
44
45 public class ClassAnnotationMetadata extends CommonAnnotationMetadata implements
46 ITransactionAttribute, IEJBInterceptors {
47
48 private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory
49 .getLogger(ClassAnnotationMetadata.class);
50
51
52
53
54 private Map<JMethod, MethodAnnotationMetadata> methodsAnnotationMetadata = null;
55
56
57
58
59 private Map<JField, FieldAnnotationMetadata> fieldsAnnotationMetadata = null;
60
61
62
63
64 private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null;
65
66
67
68
69 private JLocal jLocal = null;
70
71
72
73
74 private JRemote jRemote = null;
75
76
77
78
79 private JCommonBean jCommonBean = null;
80
81
82
83
84 private JMessageDriven jMessageDriven = null;
85
86
87
88
89 private JStateless jStateless = null;
90
91
92
93
94 private JStateful jStateful = null;
95
96
97
98
99 private JService jService = null;
100
101
102
103
104 private String localHome = null;
105
106
107
108
109 private String remoteHome = null;
110
111
112
113
114 private JInterceptors annotationInterceptors = null;
115
116
117
118
119
120
121
122
123
124 private Map<InterceptorType, List<JClassInterceptor>> externalUserInterceptors = null;
125
126
127
128
129 private TransactionManagementType transactionManagementType = CONTAINER;
130
131
132
133
134 private TransactionAttributeType transactionAttributeType = REQUIRED;
135
136
137
138
139 private ApplicationException applicationException = null;
140
141
142
143
144 private String superName = null;
145
146
147
148
149 private String[] interfaces = null;
150
151
152
153
154
155
156 private ClassType classType = null;
157
158
159
160
161 private String className = null;
162
163
164
165
166
167 private List<MethodAnnotationMetadata> aroundInvokeMethodsMetadata = null;
168
169
170
171
172 private List<JEjbEJB> jEjbEJBs = null;
173
174
175
176
177 private List<JAnnotationResource> jAnnotationResources = null;
178
179
180
181
182
183 private List<JavaxPersistenceContext> javaxPersistencePersistenceContexts = null;
184
185
186
187
188
189 private List<JavaxPersistenceUnit> javaxPersistencePersistenceUnits = null;
190
191
192
193
194
195 private LinkedList<MethodAnnotationMetadata> postConstructMethodsMetadata = null;
196
197
198
199
200
201 private LinkedList<MethodAnnotationMetadata> preDestroyMethodsMetadata = null;
202
203
204
205
206
207 private LinkedList<MethodAnnotationMetadata> postActivateMethodsMetadata = null;
208
209
210
211
212
213 private LinkedList<MethodAnnotationMetadata> prePassivateMethodsMetadata = null;
214
215
216
217
218 private boolean modified = false;
219
220
221
222
223
224
225
226
227
228 public ClassAnnotationMetadata(final String className,
229 final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
230 this.className = className;
231 this.methodsAnnotationMetadata = new HashMap<JMethod, MethodAnnotationMetadata>();
232 this.fieldsAnnotationMetadata = new HashMap<JField, FieldAnnotationMetadata>();
233 this.ejbJarAnnotationMetadata = ejbJarAnnotationMetadata;
234 this.postConstructMethodsMetadata = new LinkedList<MethodAnnotationMetadata>();
235 this.preDestroyMethodsMetadata = new LinkedList<MethodAnnotationMetadata>();
236 this.postActivateMethodsMetadata = new LinkedList<MethodAnnotationMetadata>();
237 this.prePassivateMethodsMetadata = new LinkedList<MethodAnnotationMetadata>();
238 }
239
240
241
242
243
244
245 public String getClassName() {
246 return className;
247 }
248
249
250
251
252
253
254
255 public void addMethodAnnotationMetadata(
256 final MethodAnnotationMetadata methodAnnotationMetadata) {
257 JMethod key = methodAnnotationMetadata.getJMethod();
258
259 if (methodsAnnotationMetadata.containsKey(key)) {
260 String msg = "BeanAnnotationMetadata.addMethodAnnotationMetadata.alreadyPresent";
261 logger.debug(msg);
262 throw new IllegalStateException(msg);
263 }
264 methodsAnnotationMetadata.put(key, methodAnnotationMetadata);
265 }
266
267
268
269
270
271
272
273
274 public MethodAnnotationMetadata getMethodAnnotationMetadata(
275 final JMethod jMethod) {
276 return methodsAnnotationMetadata.get(jMethod);
277 }
278
279
280
281
282
283
284 public Collection<MethodAnnotationMetadata> getMethodAnnotationMetadataCollection() {
285 return methodsAnnotationMetadata.values();
286 }
287
288
289
290
291
292
293
294 public void addFieldAnnotationMetadata(
295 final FieldAnnotationMetadata fieldAnnotationMetadata) {
296 JField key = fieldAnnotationMetadata.getJField();
297
298 if (fieldsAnnotationMetadata.containsKey(key)) {
299 String msg = "BeanAnnotationMetadata.addFieldAnnotationMetadata.alreadyPresent";
300 logger.debug(msg);
301 throw new IllegalStateException(msg);
302 }
303 fieldsAnnotationMetadata.put(key, fieldAnnotationMetadata);
304 }
305
306
307
308
309
310
311
312
313 public FieldAnnotationMetadata getFieldAnnotationMetadata(
314 final JField jField) {
315 return fieldsAnnotationMetadata.get(jField);
316 }
317
318
319
320
321
322
323 public Collection<FieldAnnotationMetadata> getFieldAnnotationMetadataCollection() {
324 return fieldsAnnotationMetadata.values();
325 }
326
327
328
329
330
331
332
333 public void setLocalInterfaces(final JLocal jLocal) {
334 this.jLocal = jLocal;
335 }
336
337
338
339
340
341
342
343 public void setRemoteInterfaces(final JRemote jRemote) {
344 this.jRemote = jRemote;
345 }
346
347
348
349
350
351
352 public JLocal getLocalInterfaces() {
353 return jLocal;
354 }
355
356
357
358
359
360
361 public JRemote getRemoteInterfaces() {
362 return jRemote;
363 }
364
365
366
367
368
369
370 public boolean isStateless() {
371 return (classType != null && classType == ClassType.STATELESS);
372 }
373
374
375
376
377
378 public boolean isStateful() {
379 return (classType != null && classType == ClassType.STATEFUL);
380 }
381
382
383
384
385
386 public boolean isSession() {
387 return (classType != null && (classType == ClassType.STATELESS || classType == ClassType.STATEFUL));
388 }
389
390
391
392
393
394 public boolean isMdb() {
395 return (classType != null && classType == ClassType.MDB);
396 }
397
398
399
400
401
402 public boolean isService() {
403 return (classType != null && classType == ClassType.SERVICE);
404 }
405
406
407
408
409
410
411
412
413 public void setClassType(final ClassType cType) {
414 this.classType = cType;
415 }
416
417
418
419
420
421 public JMessageDriven getJMessageDriven() {
422 return jMessageDriven;
423 }
424
425
426
427
428
429
430
431 public void setJMessageDriven(final JMessageDriven messageDriven) {
432 jMessageDriven = messageDriven;
433 }
434
435
436
437
438
439 @Override
440 public String toString() {
441 StringBuilder sb = new StringBuilder();
442
443 sb.append(this.getClass().getName().substring(
444 this.getClass().getPackage().getName().length() + 1));
445 sb.append("[\n");
446
447
448 sb.append(super.toString());
449
450
451 concatStringBuilder("className", className, sb);
452
453
454 concatStringBuilder("superName", superName, sb);
455
456
457 concatStringBuilder("interfaces", interfaces, sb);
458
459
460 concatStringBuilder("classType", classType, sb);
461
462
463 concatStringBuilder("jLocal", jLocal, sb);
464
465
466 concatStringBuilder("aroundInvokeMethodsMetadata",
467 aroundInvokeMethodsMetadata, sb);
468
469
470 concatStringBuilder("jRemote", jRemote, sb);
471
472
473 concatStringBuilder("jMessageDriven", jMessageDriven, sb);
474
475
476 concatStringBuilder("remoteHome", remoteHome, sb);
477
478
479 concatStringBuilder("localHome", localHome, sb);
480
481
482 concatStringBuilder("transactionManagementType",
483 transactionManagementType, sb);
484
485
486 concatStringBuilder("transactionAttributeType",
487 transactionAttributeType, sb);
488
489
490 concatStringBuilder("annotationInterceptors", annotationInterceptors,
491 sb);
492
493
494 concatStringBuilder("jAnnotationEJBs", jEjbEJBs, sb);
495
496
497 concatStringBuilder("jAnnotationResources", jAnnotationResources, sb);
498
499
500 concatStringBuilder("javaxPersistencePersistenceContexts",
501 javaxPersistencePersistenceContexts, sb);
502
503
504 concatStringBuilder("javaxPersistencePersistenceUnits",
505 javaxPersistencePersistenceUnits, sb);
506
507
508 for (MethodAnnotationMetadata methodAnnotationMetadata : getMethodAnnotationMetadataCollection()) {
509 concatStringBuilder("methods", methodAnnotationMetadata, sb);
510 }
511
512 sb.append("]");
513 return sb.toString();
514 }
515
516
517
518
519
520 public String getRemoteHome() {
521 return remoteHome;
522 }
523
524
525
526
527
528
529
530 public void setRemoteHome(final String remoteHome) {
531 this.remoteHome = remoteHome;
532 }
533
534
535
536
537
538 public String getLocalHome() {
539 return localHome;
540 }
541
542
543
544
545
546
547
548 public void setLocalHome(final String localHome) {
549 this.localHome = localHome;
550 }
551
552
553
554
555
556
557 public TransactionManagementType getTransactionManagementType() {
558 return transactionManagementType;
559 }
560
561
562
563
564
565
566
567
568 public void setTransactionManagementType(
569 final TransactionManagementType transactionManagementType) {
570 this.transactionManagementType = transactionManagementType;
571 }
572
573
574
575
576
577
578 public TransactionAttributeType getTransactionAttributeType() {
579 return transactionAttributeType;
580 }
581
582
583
584
585
586
587
588
589 public void setTransactionAttributeType(
590 final TransactionAttributeType transactionAttributeType) {
591 this.transactionAttributeType = transactionAttributeType;
592 }
593
594
595
596
597
598 public JInterceptors getAnnotationInterceptors() {
599 return annotationInterceptors;
600 }
601
602
603
604
605
606
607
608
609 public void setAnnotationsInterceptors(
610 final JInterceptors annotationInterceptors) {
611 this.annotationInterceptors = annotationInterceptors;
612 }
613
614
615
616
617
618 public ApplicationException getApplicationException() {
619 return applicationException;
620 }
621
622
623
624
625
626
627
628
629 public void setApplicationException(
630 final ApplicationException applicationException) {
631 this.applicationException = applicationException;
632 }
633
634
635
636
637
638 public boolean isBean() {
639 return isStateless() || isStateful() || isMdb() || isService();
640 }
641
642
643
644
645
646 public String[] getInterfaces() {
647 return interfaces;
648 }
649
650
651
652
653
654
655
656 public void setInterfaces(final String[] interfaces) {
657 this.interfaces = interfaces;
658 }
659
660
661
662
663
664 public String getSuperName() {
665 return superName;
666 }
667
668
669
670
671
672
673
674 public void setSuperName(final String superName) {
675 this.superName = superName;
676 }
677
678
679
680
681
682 public EjbJarAnnotationMetadata getEjbJarAnnotationMetadata() {
683 return ejbJarAnnotationMetadata;
684 }
685
686
687
688
689
690
691
692
693
694 public Map<InterceptorType, List<JClassInterceptor>> getExternalUserEasyBeansInterceptors() {
695 return externalUserInterceptors;
696 }
697
698
699
700
701
702
703
704
705
706 public void setExternalUserInterceptors(
707 final Map<InterceptorType, List<JClassInterceptor>> externalUserInterceptors) {
708 this.externalUserInterceptors = externalUserInterceptors;
709 }
710
711
712
713
714
715
716 public boolean isAroundInvokeMethodMetadata() {
717 return (aroundInvokeMethodsMetadata != null);
718 }
719
720
721
722
723
724 public List<MethodAnnotationMetadata> getAroundInvokeMethodMetadatas() {
725 return aroundInvokeMethodsMetadata;
726 }
727
728
729
730
731
732
733 public void addAroundInvokeMethodMetadata(
734 final MethodAnnotationMetadata aroundInvokeMethodMetadata) {
735 if (aroundInvokeMethodsMetadata == null) {
736 this.aroundInvokeMethodsMetadata = new ArrayList<MethodAnnotationMetadata>();
737 }
738 aroundInvokeMethodsMetadata.add(aroundInvokeMethodMetadata);
739 }
740
741
742
743
744
745 public LinkedList<MethodAnnotationMetadata> getPostConstructMethodsMetadata() {
746 return postConstructMethodsMetadata;
747 }
748
749
750
751
752
753
754
755 public void addPostConstructMethodMetadata(
756 final MethodAnnotationMetadata postConstructMethodMetadata) {
757 checkLifeCycleDuplicate(postConstructMethodMetadata,
758 InterceptorType.POST_CONSTRUCT,
759 getPostConstructMethodsMetadata());
760 this.postConstructMethodsMetadata.addFirst(postConstructMethodMetadata);
761 }
762
763
764
765
766
767
768
769
770
771
772
773 private void checkLifeCycleDuplicate(
774 final MethodAnnotationMetadata postConstructMethodMetadata,
775 final InterceptorType itcType,
776 final List<MethodAnnotationMetadata> existingList) {
777
778 ClassAnnotationMetadata wantToAddClassMetadata = postConstructMethodMetadata
779 .getClassAnnotationMetadata();
780 if (postConstructMethodMetadata.isInherited()) {
781 wantToAddClassMetadata = postConstructMethodMetadata
782 .getOriginalClassAnnotationMetadata();
783 }
784 for (MethodAnnotationMetadata method : existingList) {
785 ClassAnnotationMetadata compareMetaData;
786 if (method.isInherited()) {
787 compareMetaData = method.getOriginalClassAnnotationMetadata();
788 } else {
789 compareMetaData = method.getClassAnnotationMetadata();
790 }
791 if (compareMetaData.equals(wantToAddClassMetadata)) {
792 throw new InterceptorsValidationException("Class "
793 + getClassName() + " has already a " + itcType
794 + " method which is " + method.getMethodName()
795 + ", cannot set new method "
796 + postConstructMethodMetadata.getMethodName());
797 }
798 }
799 }
800
801
802
803
804
805 public LinkedList<MethodAnnotationMetadata> getPreDestroyMethodsMetadata() {
806 return preDestroyMethodsMetadata;
807 }
808
809
810
811
812
813
814
815 public void addPreDestroyMethodMetadata(
816 final MethodAnnotationMetadata preDestroyMethodMetadata) {
817 checkLifeCycleDuplicate(preDestroyMethodMetadata,
818 InterceptorType.PRE_DESTROY, getPreDestroyMethodsMetadata());
819 this.preDestroyMethodsMetadata.addFirst(preDestroyMethodMetadata);
820 }
821
822
823
824
825
826 public LinkedList<MethodAnnotationMetadata> getPostActivateMethodsMetadata() {
827 return postActivateMethodsMetadata;
828 }
829
830
831
832
833
834
835
836 public void addPostActivateMethodMetadata(
837 final MethodAnnotationMetadata postActivateMethodMetadata) {
838 checkLifeCycleDuplicate(postActivateMethodMetadata,
839 InterceptorType.POST_ACTIVATE, getPostActivateMethodsMetadata());
840 this.postActivateMethodsMetadata.addFirst(postActivateMethodMetadata);
841 }
842
843
844
845
846
847 public LinkedList<MethodAnnotationMetadata> getPrePassivateMethodsMetadata() {
848 return prePassivateMethodsMetadata;
849 }
850
851
852
853
854
855
856
857 public void addPrePassivateMethodMetadata(
858 final MethodAnnotationMetadata prePassivateMethodMetadata) {
859 checkLifeCycleDuplicate(prePassivateMethodMetadata,
860 InterceptorType.PRE_PASSIVATE, getPrePassivateMethodsMetadata());
861 this.prePassivateMethodsMetadata.addFirst(prePassivateMethodMetadata);
862 }
863
864
865
866
867
868
869 public boolean isInterceptor() {
870 return (aroundInvokeMethodsMetadata != null && aroundInvokeMethodsMetadata
871 .size() > 0)
872 || (postConstructMethodsMetadata != null && postConstructMethodsMetadata
873 .size() > 0)
874 || (preDestroyMethodsMetadata != null && preDestroyMethodsMetadata
875 .size() > 0)
876 || (prePassivateMethodsMetadata != null && prePassivateMethodsMetadata
877 .size() > 0)
878 || (postActivateMethodsMetadata != null && postActivateMethodsMetadata
879 .size() > 0);
880 }
881
882
883
884
885
886
887
888 public List<JEjbEJB> getJEjbEJBs() {
889 return jEjbEJBs;
890 }
891
892
893
894
895
896
897
898 public void setJEjbEJBs(final List<JEjbEJB> jEjbEJBs) {
899 this.jEjbEJBs = jEjbEJBs;
900 }
901
902
903
904
905
906
907
908 public List<JAnnotationResource> getJAnnotationResources() {
909 return jAnnotationResources;
910 }
911
912
913
914
915
916
917
918 public void setJAnnotationResources(
919 final List<JAnnotationResource> jAnnotationResources) {
920 this.jAnnotationResources = jAnnotationResources;
921 }
922
923
924
925
926
927
928
929 public List<JavaxPersistenceContext> getJavaxPersistencePersistenceContexts() {
930 return javaxPersistencePersistenceContexts;
931 }
932
933
934
935
936
937
938
939
940 public void setJavaxPersistencePersistenceContexts(
941 final List<JavaxPersistenceContext> javaxPersistencePersistenceContexts) {
942 this.javaxPersistencePersistenceContexts = javaxPersistencePersistenceContexts;
943 }
944
945
946
947
948
949
950
951 public List<JavaxPersistenceUnit> getJavaxPersistencePersistenceUnits() {
952 return javaxPersistencePersistenceUnits;
953 }
954
955
956
957
958
959
960
961
962 public void setJavaxPersistencePersistenceUnits(
963 final List<JavaxPersistenceUnit> javaxPersistencePersistenceUnits) {
964 this.javaxPersistencePersistenceUnits = javaxPersistencePersistenceUnits;
965 }
966
967
968
969
970
971 public JCommonBean getJCommonBean() {
972 return jCommonBean;
973 }
974
975
976
977
978
979
980
981 public void setJCommonBean(final JCommonBean commonBean) {
982 jCommonBean = commonBean;
983 }
984
985
986
987
988
989 public JStateful getJStateful() {
990 return jStateful;
991 }
992
993
994
995
996
997
998
999 public void setJStateful(final JStateful jStateful) {
1000 this.jStateful = jStateful;
1001 }
1002
1003
1004
1005
1006
1007 public JStateless getJStateless() {
1008 return jStateless;
1009 }
1010
1011
1012
1013
1014
1015
1016
1017 public void setJStateless(final JStateless jStateless) {
1018 this.jStateless = jStateless;
1019 }
1020
1021
1022
1023
1024
1025 public JService getJService() {
1026 return jService;
1027 }
1028
1029
1030
1031
1032
1033
1034
1035 public void setJService(final JService service) {
1036 jService = service;
1037 }
1038
1039
1040
1041
1042
1043
1044 public boolean wasModified() {
1045 return modified;
1046 }
1047
1048
1049
1050
1051 public void setModified() {
1052 this.modified = true;
1053 }
1054
1055 }