|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package net.sf.crispy; |
|
6 |
| |
|
7 |
| import java.util.ArrayList; |
|
8 |
| import java.util.Iterator; |
|
9 |
| import java.util.List; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public final class InterceptorHandler { |
|
18 |
| |
|
19 |
| private List interceptors = new ArrayList(); |
|
20 |
| private Modifier modifier = null; |
|
21 |
| private List interceptorFilters = new ArrayList(); |
|
22 |
| |
|
23 |
1339
| public InterceptorHandler() { }
|
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
236
| public int addInterceptor(Interceptor pvProxyInterceptor) {
|
|
31 |
236
| interceptors.add(pvProxyInterceptor);
|
|
32 |
236
| return interceptors.indexOf(pvProxyInterceptor);
|
|
33 |
| } |
|
34 |
| |
|
35 |
2
| public Iterator getInterceptorIterator () { return interceptors.iterator(); }
|
|
36 |
| |
|
37 |
29
| public Interceptor getInterceptorByPos (int pvPos) { return (Interceptor) interceptors.get(pvPos); }
|
|
38 |
| |
|
39 |
479
| public int getInterceptorSize () { return interceptors.size(); }
|
|
40 |
| |
|
41 |
12
| public Interceptor removeInterceptorByPos (int pvPos) { return (Interceptor) interceptors.remove(pvPos); }
|
|
42 |
5
| public void removeAllInterceptors () { interceptors.clear(); }
|
|
43 |
| |
|
44 |
3
| public Modifier getModifier() { return modifier; }
|
|
45 |
44
| public void setModifier(Modifier pvModifier) { modifier = pvModifier; }
|
|
46 |
| |
|
47 |
| |
|
48 |
11
| public void addInterceptorFilter (InterceptorFilter pvFilter) {
|
|
49 |
11
| interceptorFilters.add(pvFilter);
|
|
50 |
| } |
|
51 |
| |
|
52 |
7
| public int getInterceptorFilterSize() { return interceptorFilters.size(); }
|
|
53 |
1
| public InterceptorFilter getInterceptorFilterByPos (int pvPos) { return (InterceptorFilter) interceptorFilters.get(pvPos); }
|
|
54 |
2
| public InterceptorFilter removeInterceptorFilter (int pvPos) { return (InterceptorFilter) interceptorFilters.remove(pvPos); }
|
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
475
| public void fireBeforeNewInstance (Class pvInterfaceClass) {
|
|
60 |
475
| if (checkInterceptorFilter(pvInterfaceClass)) {
|
|
61 |
468
| int size = interceptors.size();
|
|
62 |
468
| for (int i=0; i<size; i++) {
|
|
63 |
230
| Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
|
|
64 |
230
| lvProxyInterceptor.beforeNewInstance(pvInterfaceClass);
|
|
65 |
| } |
|
66 |
| } |
|
67 |
| } |
|
68 |
| |
|
69 |
475
| public void fireAfterNewInstance (Class pvInterfaceClass, Object pvProxyObject) {
|
|
70 |
475
| if (checkInterceptorFilter(pvInterfaceClass)) {
|
|
71 |
468
| int size = interceptors.size();
|
|
72 |
468
| for (int i=0; i<size; i++) {
|
|
73 |
230
| Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
|
|
74 |
230
| lvProxyInterceptor.afterNewInstance(pvInterfaceClass, pvProxyObject);
|
|
75 |
| } |
|
76 |
| } |
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
845
| public boolean fireBeforeMethodInvocation (InterceptorContext pvInterceptorContext) {
|
|
81 |
845
| boolean lvInterruptIncocation = false;
|
|
82 |
845
| if (checkInterceptorFilter(pvInterceptorContext)) {
|
|
83 |
840
| int size = interceptors.size();
|
|
84 |
840
| for (int i=0; i<size; i++) {
|
|
85 |
306
| Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
|
|
86 |
306
| lvProxyInterceptor.beforeMethodInvocation(pvInterceptorContext);
|
|
87 |
306
| if (pvInterceptorContext.getInterruptInvocation()) {
|
|
88 |
7
| lvInterruptIncocation = true;
|
|
89 |
| } |
|
90 |
| } |
|
91 |
| } |
|
92 |
845
| return lvInterruptIncocation;
|
|
93 |
| } |
|
94 |
| |
|
95 |
845
| public void fireAfterMethodInvocation (InterceptorContext pvInterceptorContext) {
|
|
96 |
845
| if (checkInterceptorFilter(pvInterceptorContext)) {
|
|
97 |
840
| int size = interceptors.size();
|
|
98 |
840
| for (int i=0; i<size; i++) {
|
|
99 |
306
| Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
|
|
100 |
306
| lvProxyInterceptor.afterMethodInvocation(pvInterceptorContext);
|
|
101 |
| } |
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
45
| public void fireOnError(Throwable pvThrowable) {
|
|
106 |
45
| if (checkInterceptorFilter(pvThrowable)) {
|
|
107 |
43
| int size = interceptors.size();
|
|
108 |
43
| for (int i=0; i<size; i++) {
|
|
109 |
5
| Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
|
|
110 |
5
| lvProxyInterceptor.onError(pvThrowable);
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| } |
|
114 |
| |
|
115 |
844
| public InterceptorContext fireBeforeModifyInvocation (InterceptorContext pvInterceptorContext) {
|
|
116 |
844
| InterceptorContext lvReturn = pvInterceptorContext;
|
|
117 |
844
| if ((modifier != null) && (checkInterceptorFilter(pvInterceptorContext))) {
|
|
118 |
42
| lvReturn = modifier.modifyBeforeInvocation(lvReturn);
|
|
119 |
| } |
|
120 |
844
| return lvReturn;
|
|
121 |
| } |
|
122 |
| |
|
123 |
844
| public InterceptorContext fireAfterModifyInvocation (InterceptorContext pvInterceptorContext) {
|
|
124 |
844
| InterceptorContext lvReturn = pvInterceptorContext;
|
|
125 |
844
| if ((modifier != null) && (checkInterceptorFilter(pvInterceptorContext))) {
|
|
126 |
42
| lvReturn = modifier.modifyAfterInvocation(lvReturn);
|
|
127 |
| } |
|
128 |
844
| return lvReturn;
|
|
129 |
| } |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
2769
| private boolean checkInterceptorFilter(InterceptorFilterContext pvFilterContext) {
|
|
139 |
2769
| for (int i=0; i<interceptorFilters.size(); i++) {
|
|
140 |
58
| InterceptorFilter lvFilter = (InterceptorFilter) interceptorFilters.get(i);
|
|
141 |
58
| if (lvFilter.accept(pvFilterContext) == false) {
|
|
142 |
26
| return false;
|
|
143 |
| } |
|
144 |
| } |
|
145 |
2743
| return true;
|
|
146 |
| } |
|
147 |
| |
|
148 |
1774
| private boolean checkInterceptorFilter(InterceptorContext pvContext) {
|
|
149 |
1774
| InterceptorFilterContext lvFilterContext = new InterceptorFilterContext();
|
|
150 |
1774
| lvFilterContext.setArgs(pvContext.getArgs());
|
|
151 |
1774
| lvFilterContext.setMethod(pvContext.getMethod());
|
|
152 |
1774
| return checkInterceptorFilter(lvFilterContext);
|
|
153 |
| } |
|
154 |
| |
|
155 |
950
| private boolean checkInterceptorFilter(Class pvClass) {
|
|
156 |
950
| InterceptorFilterContext lvFilterContext = new InterceptorFilterContext(pvClass);
|
|
157 |
950
| return checkInterceptorFilter(lvFilterContext);
|
|
158 |
| } |
|
159 |
| |
|
160 |
45
| private boolean checkInterceptorFilter(Throwable pvThrowable) {
|
|
161 |
45
| InterceptorFilterContext lvFilterContext = new InterceptorFilterContext(pvThrowable);
|
|
162 |
45
| return checkInterceptorFilter(lvFilterContext);
|
|
163 |
| } |
|
164 |
| |
|
165 |
| } |