KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > annotation > handlers > InterceptorsHandler


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.annotation.handlers;
24
25 import java.lang.annotation.Annotation JavaDoc;
26 import java.lang.annotation.ElementType JavaDoc;
27 import java.lang.reflect.AnnotatedElement JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Set JavaDoc;
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 JavaDoc;
39 import javax.ejb.PrePassivate JavaDoc;
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 /**
58  * This handler is responsible for handling javax.ejb.Interceptors
59  *
60  */

61 public class InterceptorsHandler extends AbstractAttributeHandler {
62     
63     public InterceptorsHandler() {
64     }
65     
66     /**
67      * @return the annoation type this annotation handler is handling
68      */

69     public Class JavaDoc<? extends Annotation JavaDoc> 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         // Process each of the interceptor classes.
84
for(Class JavaDoc interceptor : interceptors.value()) {
85             processInterceptorClass(interceptor, ejbBundle, ainfo);
86         }
87
88         for(EjbContext next : ejbContexts) {
89
90             EjbDescriptor ejbDescriptor = (EjbDescriptor) next.getDescriptor();
91
92             // Create binding information.
93
InterceptorBindingDescriptor binding =
94                 new InterceptorBindingDescriptor();
95
96             binding.setEjbName(ejbDescriptor.getName());
97
98             for(Class JavaDoc interceptor : interceptors.value()) {
99                 binding.appendInterceptorClass(interceptor.getName());
100             }
101             
102             if(ElementType.METHOD.equals(ainfo.getElementType())) {
103                 Method JavaDoc m = (Method JavaDoc) ainfo.getAnnotatedElement();
104                 MethodDescriptor md =
105                     new MethodDescriptor(m, MethodDescriptor.EJB_BEAN);
106                 binding.setBusinessMethod(md);
107             }
108
109             // All binding information processed from annotations should go
110
// before the binding information processed from the descriptors.
111
// Since descriptors are processed first, always place the binding
112
// info at the front. The binding information from the descriptor
113
// is ordered, but there is no prescribed order in which the
114
// annotations are processed, so all that matters is that it's
115
// before the descriptor bindings and that the descriptor binding
116
// order is preserved.
117
ejbBundle.prependInterceptorBinding(binding);
118         }
119
120         return getDefaultProcessedResult();
121     }
122
123     private void processInterceptorClass(Class JavaDoc interceptorClass,
124             EjbBundleDescriptor ejbBundle, AnnotationInfo ainfo)
125         throws AnnotationProcessorException {
126                 
127         Set JavaDoc<LifecycleCallbackDescriptor> aroundInvokeDescriptors =
128             new HashSet JavaDoc<LifecycleCallbackDescriptor>();
129         Set JavaDoc<LifecycleCallbackDescriptor> postActivateDescriptors =
130             new HashSet JavaDoc<LifecycleCallbackDescriptor>();
131         Set JavaDoc<LifecycleCallbackDescriptor> prePassivateDescriptors =
132             new HashSet JavaDoc<LifecycleCallbackDescriptor>();
133         
134         ComponentDefinition cdef = new ComponentDefinition(interceptorClass);
135         for(Method JavaDoc m : cdef.getMethods()) {
136             if( m.getAnnotation(AroundInvoke.class) != null ) {
137                 aroundInvokeDescriptors.add(getLifecycleCallbackDescriptor(m));
138             }
139             if( m.getAnnotation(PostActivate JavaDoc.class) != null ) {
140                 postActivateDescriptors.add(getLifecycleCallbackDescriptor(m));
141             }
142             if( m.getAnnotation(PrePassivate JavaDoc.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             // Add interceptor to the set of all interceptors in the ejb-jar
153
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         // process resource related annotations
171
EjbInterceptorContext ejbInterceptorContext =
172             new EjbInterceptorContext(interceptor);
173         ProcessingContext procContext = ainfo.getProcessingContext();
174         procContext.pushHandler(ejbInterceptorContext);
175         procContext.getProcessor().process(
176             procContext, new Class JavaDoc[] { interceptorClass });
177         return;
178     }
179
180     /**
181      * @return an array of annotation types this annotation handler would
182      * require to be processed (if present) before it processes it's own
183      * annotation type.
184      */

185     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
186         return getEjbAnnotationTypes();
187     }
188
189     private LifecycleCallbackDescriptor getLifecycleCallbackDescriptor(Method JavaDoc 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