1 23 package com.sun.enterprise.deployment.annotation.handlers; 24 25 import java.lang.annotation.Annotation ; 26 import java.lang.annotation.ElementType ; 27 import java.lang.reflect.AnnotatedElement ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.HashSet ; 32 import java.util.Set ; 33 34 import javax.annotation.PostConstruct; 35 import javax.annotation.PreDestroy; 36 import javax.interceptor.AroundInvoke; 37 import javax.interceptor.Interceptors; 38 import javax.ejb.PostActivate ; 39 import javax.ejb.PrePassivate ; 40 41 import com.sun.enterprise.deployment.EjbDescriptor; 42 import com.sun.enterprise.deployment.EjbBundleDescriptor; 43 import com.sun.enterprise.deployment.InterceptorBindingDescriptor; 44 import com.sun.enterprise.deployment.LifecycleCallbackDescriptor; 45 import com.sun.enterprise.deployment.EjbInterceptor; 46 import static com.sun.enterprise.deployment.LifecycleCallbackDescriptor.CallbackType; 47 import com.sun.enterprise.deployment.EjbSessionDescriptor; 48 import com.sun.enterprise.deployment.MethodDescriptor; 49 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 50 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 51 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 52 import com.sun.enterprise.deployment.annotation.ProcessingContext; 53 import com.sun.enterprise.deployment.annotation.context.EjbContext; 54 import com.sun.enterprise.deployment.annotation.context.EjbInterceptorContext; 55 import com.sun.enterprise.deployment.annotation.impl.ComponentDefinition; 56 57 61 public class InterceptorsHandler extends AbstractAttributeHandler { 62 63 public InterceptorsHandler() { 64 } 65 66 69 public Class <? extends Annotation > getAnnotationType() { 70 return Interceptors.class; 71 } 72 73 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 74 EjbContext[] ejbContexts) throws AnnotationProcessorException { 75 76 Interceptors interceptors = (Interceptors) ainfo.getAnnotation(); 77 78 79 EjbBundleDescriptor ejbBundle = 80 ((EjbDescriptor)ejbContexts[0].getDescriptor()). 81 getEjbBundleDescriptor(); 82 83 for(Class interceptor : interceptors.value()) { 85 processInterceptorClass(interceptor, ejbBundle, ainfo); 86 } 87 88 for(EjbContext next : ejbContexts) { 89 90 EjbDescriptor ejbDescriptor = (EjbDescriptor) next.getDescriptor(); 91 92 InterceptorBindingDescriptor binding = 94 new InterceptorBindingDescriptor(); 95 96 binding.setEjbName(ejbDescriptor.getName()); 97 98 for(Class interceptor : interceptors.value()) { 99 binding.appendInterceptorClass(interceptor.getName()); 100 } 101 102 if(ElementType.METHOD.equals(ainfo.getElementType())) { 103 Method m = (Method ) ainfo.getAnnotatedElement(); 104 MethodDescriptor md = 105 new MethodDescriptor(m, MethodDescriptor.EJB_BEAN); 106 binding.setBusinessMethod(md); 107 } 108 109 ejbBundle.prependInterceptorBinding(binding); 118 } 119 120 return getDefaultProcessedResult(); 121 } 122 123 private void processInterceptorClass(Class interceptorClass, 124 EjbBundleDescriptor ejbBundle, AnnotationInfo ainfo) 125 throws AnnotationProcessorException { 126 127 Set <LifecycleCallbackDescriptor> aroundInvokeDescriptors = 128 new HashSet <LifecycleCallbackDescriptor>(); 129 Set <LifecycleCallbackDescriptor> postActivateDescriptors = 130 new HashSet <LifecycleCallbackDescriptor>(); 131 Set <LifecycleCallbackDescriptor> prePassivateDescriptors = 132 new HashSet <LifecycleCallbackDescriptor>(); 133 134 ComponentDefinition cdef = new ComponentDefinition(interceptorClass); 135 for(Method m : cdef.getMethods()) { 136 if( m.getAnnotation(AroundInvoke.class) != null ) { 137 aroundInvokeDescriptors.add(getLifecycleCallbackDescriptor(m)); 138 } 139 if( m.getAnnotation(PostActivate .class) != null ) { 140 postActivateDescriptors.add(getLifecycleCallbackDescriptor(m)); 141 } 142 if( m.getAnnotation(PrePassivate .class) != null ) { 143 prePassivateDescriptors.add(getLifecycleCallbackDescriptor(m)); 144 } 145 } 146 147 EjbInterceptor interceptor = 148 ejbBundle.getInterceptorByClassName(interceptorClass.getName()); 149 if (interceptor == null) { 150 interceptor = new EjbInterceptor(); 151 interceptor.setInterceptorClassName(interceptorClass.getName()); 152 ejbBundle.addInterceptor(interceptor); 154 } 155 156 if (aroundInvokeDescriptors.size() > 0) { 157 interceptor.addAroundInvokeDescriptors(aroundInvokeDescriptors); 158 } 159 160 if (postActivateDescriptors.size() > 0) { 161 interceptor.addCallbackDescriptors(CallbackType.POST_ACTIVATE, 162 postActivateDescriptors); 163 } 164 165 if (prePassivateDescriptors.size() > 0) { 166 interceptor.addCallbackDescriptors(CallbackType.PRE_PASSIVATE, 167 prePassivateDescriptors); 168 } 169 170 EjbInterceptorContext ejbInterceptorContext = 172 new EjbInterceptorContext(interceptor); 173 ProcessingContext procContext = ainfo.getProcessingContext(); 174 procContext.pushHandler(ejbInterceptorContext); 175 procContext.getProcessor().process( 176 procContext, new Class [] { interceptorClass }); 177 return; 178 } 179 180 185 public Class <? extends Annotation >[] getTypeDependencies() { 186 return getEjbAnnotationTypes(); 187 } 188 189 private LifecycleCallbackDescriptor getLifecycleCallbackDescriptor(Method m) { 190 LifecycleCallbackDescriptor lccDesc = new LifecycleCallbackDescriptor(); 191 lccDesc.setLifecycleCallbackClass(m.getDeclaringClass().getName()); 192 lccDesc.setLifecycleCallbackMethod(m.getName()); 193 return lccDesc; 194 } 195 } 196 | Popular Tags |