1 23 package com.sun.enterprise.deployment.util; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.LinkedList ; 29 import java.util.Map ; 30 import java.util.HashMap ; 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 ; 43 44 47 public class InterceptorBindingTranslator { 48 49 private List <InterceptorBindingDescriptor> interceptorBindings; 50 private EjbBundleDescriptor ejbBundle; 51 52 private List <String > defaultInterceptorChain = 53 new LinkedList <String >(); 54 55 private List <String > classInterceptorChain = 56 new LinkedList <String >(); 57 58 private boolean hasTotalClassLevelOrdering = false; 59 private List <String > totalClassLevelOrdering = 60 new LinkedList <String >(); 61 62 private Map <MethodDescriptor, LinkedList <String >> methodInterceptorsMap = 63 new HashMap <MethodDescriptor, LinkedList <String >>(); 64 65 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 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 for(InterceptorBindingDescriptor binding : interceptorBindings) { 102 103 if( binding.getBindingType() == BindingType.DEFAULT ) { 104 defaultInterceptorChain.addAll 105 (binding.getInterceptorClasses()); 106 } 107 108 } 109 110 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 124 Map <MethodDescriptor, List <InterceptorBindingDescriptor>> 125 methodBindings = new HashMap <MethodDescriptor, 126 List <InterceptorBindingDescriptor>>(); 127 128 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 <InterceptorBindingDescriptor> methodBindingDescs = 139 methodBindings.get(method); 140 if( methodBindingDescs == null ) { 141 methodBindingDescs = 142 new LinkedList <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 for(String 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 <InterceptorBindingDescriptor> bindings) { 194 195 LinkedList <String > tempDefaultInterceptorChain = 196 new LinkedList <String >(); 197 198 LinkedList <String > tempClassInterceptorChain = 199 new LinkedList <String >(); 200 201 LinkedList <String > tempMethodInterceptorChain = 202 new LinkedList <String >(); 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 <String > methodInterceptors = new LinkedList <String >(); 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 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 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 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 <String > interceptorClassChain = (List <String >) 286 methodInterceptorsMap.get(nextMethod); 287 288 List <EjbInterceptor> interceptorChain = 289 new LinkedList <EjbInterceptor>(); 290 291 for(String 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 for(InterceptorBindingDescriptor binding : interceptorBindings) { 312 313 for(String interceptor : binding.getInterceptorClasses()) { 314 315 if(ejbBundle.getInterceptorByClassName(interceptor) == null) { 316 throw new IllegalStateException 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 <EjbInterceptor> allInterceptorClasses; 329 330 public List <EjbInterceptor> classInterceptorChain; 331 332 public Map <MethodDescriptor, List <EjbInterceptor>> 333 methodInterceptorsMap; 334 335 public TranslationResults() { 336 allInterceptorClasses = new HashSet <EjbInterceptor>(); 337 classInterceptorChain = new LinkedList <EjbInterceptor>(); 338 methodInterceptorsMap = 339 new HashMap <MethodDescriptor, List <EjbInterceptor>>(); 340 } 341 342 } 343 344 } 345 346 | Popular Tags |