KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.logging.Level JavaDoc;
34
35 import com.sun.enterprise.deployment.EjbBundleDescriptor;
36 import com.sun.enterprise.deployment.EjbDescriptor;
37 import com.sun.enterprise.deployment.MethodDescriptor;
38
39 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
40 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
41 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
42 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
43 import com.sun.enterprise.deployment.annotation.context.ComponentContext;
44 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext;
45 import com.sun.enterprise.deployment.annotation.context.EjbContext;
46 import com.sun.enterprise.deployment.annotation.context.EjbsContext;
47 import com.sun.enterprise.deployment.annotation.context.EjbInterceptorContext;
48
49 import com.sun.enterprise.util.TypeUtil;
50
51 /**
52  * This is an abstract class encapsulate generic behaviour of annotation
53  * handler applying on Ejb Class. It will get the corresponding
54  * EjbDescriptors associated to the annotation on the given Ejb Class
55  * and then pass it to underlying processAnnotation method.
56  * Concrete subclass handlers need to implement the following:
57  * public Class<? extends Annotation> getAnnotationType();
58  * protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
59  * EjbContext[] ejbContexts) throws AnnotationProcessorException;
60  * It may also need to override the following:
61  * a) if other annotations need to be processed prior to given annotation:
62  * public Class<? extends Annotation>[] getTypeDependencies();
63  * b) if the given annotation can be processed while processing another
64  * annotation
65  * protected boolean isDelegatee();
66  * c) if we need to process for interceptor
67  * protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
68  * EjbInterceptorContext ejbInterceptorContext)
69  * throws AnnotationProcessorException;
70  * d) indicate the annotation support type inheritance
71  * protected boolean supportTypeIneritance();
72  *
73  * @author Shing Wai Chan
74  */

75 abstract class AbstractAttributeHandler extends AbstractHandler {
76     /**
77      * Process Annotation with given EjbContexts.
78      * @param ainfo
79      * @param ejbContexts
80      * @return HandlerProcessingResult
81      * @exception AnnotationProcessorException
82      */

83     protected abstract HandlerProcessingResult processAnnotation(
84             AnnotationInfo ainfo, EjbContext[] ejbContexts)
85             throws AnnotationProcessorException;
86
87     /**
88      * Process Annotation with given InteceptorContext.
89      * @param ainfo
90      * @param ejbInterceptorContext
91      * @return HandlerProcessingResult
92      * @exception AnnotationProcessorException
93      */

94     protected HandlerProcessingResult processAnnotation(
95             AnnotationInfo ainfo, EjbInterceptorContext ejbInterceptorContext)
96             throws AnnotationProcessorException {
97         if (!isDelegatee()) {
98             throw new UnsupportedOperationException JavaDoc();
99         }
100         return getDefaultProcessedResult();
101     }
102
103     /**
104      * Process a particular annotation which type is the same as the
105      * one returned by @see getAnnotationType(). All information
106      * pertinent to the annotation and its context is encapsulated
107      * in the passed AnnotationInfo instance.
108      * This is a method in interface AnnotationHandler.
109      *
110      * @param ainfo the annotation information
111      */

112     public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo)
113             throws AnnotationProcessorException {
114         
115         AnnotatedElement JavaDoc ae = ainfo.getAnnotatedElement();
116         Annotation JavaDoc annotation = ainfo.getAnnotation();
117
118         if (logger.isLoggable(Level.FINER)) {
119             logger.finer("@process annotation " + annotation + " in " + ae);
120         }
121
122         AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
123
124         if (aeHandler instanceof EjbBundleContext) {
125             EjbBundleContext ejbBundleContext = (EjbBundleContext)aeHandler;
126             AnnotatedElementHandler aeh = ejbBundleContext.createContextForEjb();
127             if (aeh != null) {
128                 aeHandler = aeh;
129             } else {
130                 if (isDelegatee()) {
131                     aeHandler = ejbBundleContext.createContextForEjbInterceptor();
132                 }
133                 if (aeHandler == null) {
134                     return getInvalidAnnotatedElementHandlerResult(aeHandler, ainfo);
135                 }
136             }
137         }
138
139         if (!supportTypeInheritance() &&
140                 ElementType.TYPE.equals(ainfo.getElementType()) &&
141                 aeHandler instanceof ComponentContext) {
142             ComponentContext context = (ComponentContext)aeHandler;
143             Class JavaDoc clazz = (Class JavaDoc)ainfo.getAnnotatedElement();
144             if (!clazz.getName().equals(context.getComponentClassName())) {
145                 if (logger.isLoggable(Level.WARNING)) {
146                     log(Level.WARNING, ainfo,
147                         localStrings.getLocalString(
148                         "enterprise.deployment.annotation.handlers.typeinhernotsupp",
149                         "The annotation symbol inheritance is not supported."));
150                 }
151                 return getDefaultProcessedResult();
152             }
153         }
154                 
155         EjbContext[] ejbContexts = null;
156         EjbInterceptorContext ejbInterceptorContext = null;
157         if (aeHandler instanceof EjbContext) {
158             EjbContext ejbContext = (EjbContext)aeHandler;
159             ejbContexts = new EjbContext[] { ejbContext };
160         } else if (aeHandler instanceof EjbsContext) {
161             ejbContexts = ((EjbsContext)aeHandler).getEjbContexts();
162         } else if (isDelegatee() && aeHandler instanceof EjbInterceptorContext) {
163             ejbInterceptorContext = (EjbInterceptorContext)aeHandler;
164         } else {
165             return getInvalidAnnotatedElementHandlerResult(aeHandler, ainfo);
166         }
167
168         HandlerProcessingResult procResult = null;
169
170         if (ejbInterceptorContext != null) {
171             procResult = processAnnotation(ainfo, ejbInterceptorContext);
172         } else {
173             procResult = processAnnotation(ainfo, ejbContexts);
174         }
175
176         if (logger.isLoggable(Level.FINER)) {
177             logger.finer("New annotation for " + annotation);
178         }
179         return procResult;
180     }
181
182     /**
183      * This indicates whether the annotation can be processed by delegation
184      * from the another annotation.
185      */

186     protected boolean isDelegatee() {
187         return false;
188     }
189
190     /**
191      * This indicates whether the annotation type should be processed for
192      * type level in super-class.
193      */

194     protected boolean supportTypeInheritance() {
195         return false;
196     }
197
198     /**
199      * Returns MethodDescriptors representing All for a given EjbDescriptor.
200      * @param ejbDesc
201      * @return resulting MethodDescriptor
202      */

203     protected Set JavaDoc<MethodDescriptor> getMethodAllDescriptors(
204             EjbDescriptor ejbDesc) {
205         Set JavaDoc methodAlls = new HashSet JavaDoc();
206         if (ejbDesc.isRemoteInterfacesSupported() ||
207             ejbDesc.isRemoteBusinessInterfacesSupported()) {
208             methodAlls.add(
209                     new MethodDescriptor(MethodDescriptor.ALL_METHODS,
210                     "", MethodDescriptor.EJB_REMOTE));
211             if (ejbDesc.isRemoteInterfacesSupported()) {
212                 methodAlls.add(
213                     new MethodDescriptor(MethodDescriptor.ALL_METHODS,
214                     "", MethodDescriptor.EJB_HOME));
215             }
216         }
217
218         if (ejbDesc.isLocalInterfacesSupported() ||
219                 ejbDesc.isLocalBusinessInterfacesSupported()) {
220             methodAlls.add(
221                     new MethodDescriptor(MethodDescriptor.ALL_METHODS,
222                     "", MethodDescriptor.EJB_LOCAL));
223             if (ejbDesc.isLocalInterfacesSupported()) {
224                 methodAlls.add(
225                     new MethodDescriptor(MethodDescriptor.ALL_METHODS,
226                     "", MethodDescriptor.EJB_LOCALHOME));
227             }
228         }
229
230         if (ejbDesc.hasWebServiceEndpointInterface()) {
231             methodAlls.add(
232                     new MethodDescriptor(MethodDescriptor.ALL_METHODS,
233                     "", MethodDescriptor.EJB_WEB_SERVICE));
234         }
235
236         return methodAlls;
237     }
238
239     protected boolean hasMethodPermissionsFromDD(MethodDescriptor methodDesc,
240             EjbDescriptor ejbDesc) {
241         HashMap JavaDoc methodPermissionsFromDD = ejbDesc.getMethodPermissionsFromDD();
242         if (methodPermissionsFromDD != null) {
243             Set JavaDoc allMethods = ejbDesc.getMethodDescriptors();
244             String JavaDoc ejbClassSymbol = methodDesc.getEjbClassSymbol();
245             for (Object JavaDoc mdObjsObj : methodPermissionsFromDD.values()) {
246                 List JavaDoc mdObjs = (List JavaDoc)mdObjsObj;
247                 for (Object JavaDoc mdObj : mdObjs) {
248                     MethodDescriptor md = (MethodDescriptor)mdObj;
249                     for (Object JavaDoc style3MdObj :
250                             md.doStyleConversion(ejbDesc, allMethods)) {
251                         MethodDescriptor style3Md = (MethodDescriptor)style3MdObj;
252                         if (methodDesc.equals(style3Md)) {
253                             return true;
254                         }
255                     }
256                 }
257             }
258         }
259         return false;
260     }
261 }
262
Popular Tags