KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > util > InterceptorBindingTranslator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.deployment.util;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31
32 import com.sun.enterprise.deployment.EjbBundleDescriptor;
33 import com.sun.enterprise.deployment.EjbInterceptor;
34 import com.sun.enterprise.deployment.InterceptorBindingDescriptor;
35
36 import com.sun.enterprise.deployment.InterceptorBindingDescriptor.BindingType;
37
38 import com.sun.enterprise.deployment.EjbDescriptor;
39 import com.sun.enterprise.deployment.MethodDescriptor;
40
41 import java.util.*;
42 import java.util.logging.Level JavaDoc;
43
44 /**
45  *
46  */

47 public class InterceptorBindingTranslator {
48
49     private List JavaDoc<InterceptorBindingDescriptor> interceptorBindings;
50     private EjbBundleDescriptor ejbBundle;
51
52     private List JavaDoc<String JavaDoc> defaultInterceptorChain =
53         new LinkedList JavaDoc<String JavaDoc>();
54
55     private List JavaDoc<String JavaDoc> classInterceptorChain =
56         new LinkedList JavaDoc<String JavaDoc>();
57
58     private boolean hasTotalClassLevelOrdering = false;
59     private List JavaDoc<String JavaDoc> totalClassLevelOrdering =
60         new LinkedList JavaDoc<String JavaDoc>();
61
62     private Map JavaDoc<MethodDescriptor, LinkedList JavaDoc<String JavaDoc>> methodInterceptorsMap =
63         new HashMap JavaDoc<MethodDescriptor, LinkedList JavaDoc<String JavaDoc>>();
64
65     // true if there are 0 bindings.
66
private boolean isEmpty;
67
68     public InterceptorBindingTranslator(EjbBundleDescriptor bundle) {
69
70         ejbBundle = bundle;
71         interceptorBindings = ejbBundle.getInterceptorBindings();
72
73         if( interceptorBindings.isEmpty() ) {
74
75             isEmpty = true;
76
77         } else {
78
79             validateInterceptors();
80
81         }
82
83         
84     }
85
86     public TranslationResults apply(String JavaDoc ejbName) {
87
88         if( isEmpty ) {
89             return new TranslationResults();
90         }
91
92         defaultInterceptorChain.clear();
93         classInterceptorChain.clear();
94
95         hasTotalClassLevelOrdering = false;
96         totalClassLevelOrdering.clear();
97
98         methodInterceptorsMap.clear();
99
100         // Do a pass through default interceptor bindings.
101
for(InterceptorBindingDescriptor binding : interceptorBindings) {
102
103             if( binding.getBindingType() == BindingType.DEFAULT ) {
104                 defaultInterceptorChain.addAll
105                     (binding.getInterceptorClasses());
106             }
107
108         }
109
110         // Do a pass through Class level bindings.
111
for(InterceptorBindingDescriptor binding : interceptorBindings) {
112
113             if( binding.getBindingType() == BindingType.CLASS ) {
114
115                 if( binding.getEjbName().equals(ejbName) ) {
116                     processClassLevelBinding(binding);
117                 }
118             }
119
120         }
121
122         // Now do method-level bindings.
123

124         Map JavaDoc<MethodDescriptor, List JavaDoc<InterceptorBindingDescriptor>>
125             methodBindings = new HashMap JavaDoc<MethodDescriptor,
126                                          List JavaDoc<InterceptorBindingDescriptor>>();
127
128         // First build a map of all business methods for the current
129
// ejb that have binding information, and their associated
130
// bindings.
131
for(InterceptorBindingDescriptor binding : interceptorBindings) {
132
133             if( (binding.getEjbName().equals(ejbName)) &&
134                 (binding.getBindingType() == BindingType.METHOD) ) {
135
136                 MethodDescriptor method = binding.getBusinessMethod();
137
138                 List JavaDoc<InterceptorBindingDescriptor> methodBindingDescs =
139                     methodBindings.get(method);
140                 if( methodBindingDescs == null ) {
141                     methodBindingDescs =
142                         new LinkedList JavaDoc<InterceptorBindingDescriptor>();
143                 }
144
145                 methodBindingDescs.add(binding);
146
147                 methodBindings.put(method, methodBindingDescs);
148             }
149
150         }
151
152         for(MethodDescriptor next : methodBindings.keySet()) {
153             processMethod(next, methodBindings.get(next));
154         }
155
156         TranslationResults results = buildResults();
157         
158         return results;
159
160     }
161
162     private void processClassLevelBinding(InterceptorBindingDescriptor
163                                           binding) {
164
165         if( binding.getExcludeDefaultInterceptors() ) {
166             defaultInterceptorChain.clear();
167         }
168             
169         if( binding.getIsTotalOrdering() ) {
170
171             hasTotalClassLevelOrdering = true;
172             totalClassLevelOrdering.clear();
173             totalClassLevelOrdering.addAll(binding.getInterceptorClasses());
174             
175             // totalClassLevelOrdering will take precedence, but keep
176
// classInterceptorChain updated to contain class-level, but not
177
// default-level, interceptors. These might be needed during
178
// method-level exclude-class-interceptors processing.
179
for(String JavaDoc next : binding.getInterceptorClasses()) {
180                 if( !defaultInterceptorChain.contains(next) ) {
181                     if( !classInterceptorChain.contains(next) ) {
182                         classInterceptorChain.add(next);
183                     }
184                 }
185             }
186         } else {
187             classInterceptorChain.addAll(binding.getInterceptorClasses());
188         }
189
190     }
191
192     private void processMethod(MethodDescriptor businessMethod,
193                                List JavaDoc<InterceptorBindingDescriptor> bindings) {
194
195         LinkedList JavaDoc<String JavaDoc> tempDefaultInterceptorChain =
196             new LinkedList JavaDoc<String JavaDoc>();
197
198         LinkedList JavaDoc<String JavaDoc> tempClassInterceptorChain =
199             new LinkedList JavaDoc<String JavaDoc>();
200
201         LinkedList JavaDoc<String JavaDoc> tempMethodInterceptorChain =
202             new LinkedList JavaDoc<String JavaDoc>();
203
204         if( hasTotalClassLevelOrdering ) {
205             tempClassInterceptorChain.addAll(totalClassLevelOrdering);
206         } else {
207             tempDefaultInterceptorChain.addAll(defaultInterceptorChain);
208             tempClassInterceptorChain.addAll(classInterceptorChain);
209         }
210
211         for(InterceptorBindingDescriptor nextBinding : bindings) {
212
213             if( nextBinding.getExcludeDefaultInterceptors() ) {
214                 if( hasTotalClassLevelOrdering ) {
215                     tempClassInterceptorChain.removeAll
216                         (defaultInterceptorChain);
217                 } else {
218                     tempDefaultInterceptorChain.clear();
219                 }
220             }
221
222             if( nextBinding.getExcludeClassInterceptors() ) {
223                 if( hasTotalClassLevelOrdering ) {
224                     tempClassInterceptorChain.removeAll
225                         (classInterceptorChain);
226                 } else {
227                     tempClassInterceptorChain.clear();
228                 }
229             }
230             
231             if( nextBinding.getIsTotalOrdering() ) {
232                 tempDefaultInterceptorChain.clear();
233                 tempClassInterceptorChain.clear();
234                 tempMethodInterceptorChain.clear();
235             }
236
237             tempMethodInterceptorChain.addAll
238                 (nextBinding.getInterceptorClasses());
239
240         }
241
242         LinkedList JavaDoc<String JavaDoc> methodInterceptors = new LinkedList JavaDoc<String JavaDoc>();
243         methodInterceptors.addAll(tempDefaultInterceptorChain);
244         methodInterceptors.addAll(tempClassInterceptorChain);
245         methodInterceptors.addAll(tempMethodInterceptorChain);
246
247         methodInterceptorsMap.put(businessMethod, methodInterceptors);
248         
249     }
250
251     private TranslationResults buildResults() {
252
253         TranslationResults results = new TranslationResults();
254
255         if( hasTotalClassLevelOrdering ) {
256
257             for(String JavaDoc next : totalClassLevelOrdering ) {
258                 EjbInterceptor interceptor =
259                     ejbBundle.getInterceptorByClassName(next);
260                 results.allInterceptorClasses.add(interceptor);
261                 results.classInterceptorChain.add(interceptor);
262             }
263
264         } else {
265
266             for(String JavaDoc next : defaultInterceptorChain) {
267                 EjbInterceptor interceptor =
268                     ejbBundle.getInterceptorByClassName(next);
269
270                 results.allInterceptorClasses.add(interceptor);
271                 results.classInterceptorChain.add(interceptor);
272             }
273
274             for(String JavaDoc next : classInterceptorChain) {
275                 EjbInterceptor interceptor =
276                     ejbBundle.getInterceptorByClassName(next);
277
278                 results.allInterceptorClasses.add(interceptor);
279                 results.classInterceptorChain.add(interceptor);
280             }
281         }
282
283         for(MethodDescriptor nextMethod : methodInterceptorsMap.keySet()) {
284
285             List JavaDoc<String JavaDoc> interceptorClassChain = (List JavaDoc<String JavaDoc>)
286                 methodInterceptorsMap.get(nextMethod);
287             
288             List JavaDoc<EjbInterceptor> interceptorChain =
289                 new LinkedList JavaDoc<EjbInterceptor>();
290             
291             for(String JavaDoc nextClass : interceptorClassChain) {
292                 EjbInterceptor interceptor =
293                     ejbBundle.getInterceptorByClassName(nextClass);
294
295                 results.allInterceptorClasses.add(interceptor);
296                 interceptorChain.add(interceptor);
297
298             }
299             
300             results.methodInterceptorsMap.put(nextMethod, interceptorChain);
301
302         }
303
304         return results;
305     }
306
307     private void validateInterceptors() {
308
309         // Make sure there's an interceptor defined for every interceptor
310
// class name listed in the bindings.
311
for(InterceptorBindingDescriptor binding : interceptorBindings) {
312
313             for(String JavaDoc interceptor : binding.getInterceptorClasses()) {
314
315                 if(ejbBundle.getInterceptorByClassName(interceptor) == null) {
316                     throw new IllegalStateException JavaDoc
317                         ("Interceptor binding contains an interceptor class " +
318                          " name = " + interceptor +
319                          " that is not defined as an interceptor");
320                 }
321             }
322         }
323
324     }
325
326     public static class TranslationResults {
327         
328         public Set JavaDoc<EjbInterceptor> allInterceptorClasses;
329
330         public List JavaDoc<EjbInterceptor> classInterceptorChain;
331
332         public Map JavaDoc<MethodDescriptor, List JavaDoc<EjbInterceptor>>
333             methodInterceptorsMap;
334
335         public TranslationResults() {
336             allInterceptorClasses = new HashSet JavaDoc<EjbInterceptor>();
337             classInterceptorChain = new LinkedList JavaDoc<EjbInterceptor>();
338             methodInterceptorsMap =
339                 new HashMap JavaDoc<MethodDescriptor, List JavaDoc<EjbInterceptor>>();
340         }
341
342     }
343
344 }
345
346
Popular Tags