KickJava   Java API By Example, From Geeks To Geeks.

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


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.ElementType JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import com.sun.enterprise.deployment.EjbDescriptor;
29 import com.sun.enterprise.deployment.WebComponentDescriptor;
30 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
31 import com.sun.enterprise.deployment.annotation.AnnotationInfo;
32 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
33 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult;
34 import com.sun.enterprise.deployment.annotation.context.ComponentContext;
35 import com.sun.enterprise.deployment.annotation.context.EjbBundleContext;
36 import com.sun.enterprise.deployment.annotation.context.EjbContext;
37 import com.sun.enterprise.deployment.annotation.context.EjbsContext;
38 import com.sun.enterprise.deployment.annotation.context.WebBundleContext;
39 import com.sun.enterprise.deployment.annotation.context.WebComponentContext;
40 import com.sun.enterprise.deployment.annotation.context.WebComponentsContext;
41
42 /**
43  * This is an abstract class encapsulate generic behaviour of annotation
44  * handler applying on Ejb and WebComponent Class. It will get the corresponding
45  * EjbDescriptors or WebComponentDescriptor associated to the annotation on
46  * the given Class and then pass it to underlying processAnnotation method.
47  * Concrete subclass handlers need to implement the following:
48  * public Class<? extends Annotation> getAnnotationType();
49  * protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
50  * EjbContext[] ejbContexts) throws AnnotationProcessorException;
51  * protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
52  * WebComponentContext[] webCompContexts)
53  * throws AnnotationProcessorException;
54  * protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo,
55  * WebBundleContext webBundleContext)
56  * throws AnnotationProcessorException;
57  * It may also need to override the following if other annotations
58  * need to be processed prior to given annotation:
59  * public Class<? extends Annotation>[] getTypeDependencies();
60  *
61  * @author Shing Wai Chan
62  */

63 abstract class AbstractCommonAttributeHandler extends AbstractHandler {
64     /**
65      * Process Annotation with given EjbContexts.
66      * @param ainfo
67      * @param ejbContexts
68      * @return HandlerProcessingResult
69      */

70     protected abstract HandlerProcessingResult processAnnotation(
71             AnnotationInfo ainfo, EjbContext[] ejbContexts)
72             throws AnnotationProcessorException;
73
74     /**
75      * Process Annotation with given WebCompContexts.
76      * @param ainfo
77      * @param webCompContexts
78      * @return HandlerProcessingResult
79      */

80     protected abstract HandlerProcessingResult processAnnotation(
81             AnnotationInfo ainfo, WebComponentContext[] webCompContexts)
82             throws AnnotationProcessorException;
83
84     /**
85      * Process Annotation with given WebBundleContext.
86      * @param ainfo
87      * @param webBundleContext
88      * @return HandlerProcessingResult
89      */

90     protected abstract HandlerProcessingResult processAnnotation(
91             AnnotationInfo ainfo, WebBundleContext webBundleContext)
92             throws AnnotationProcessorException;
93
94     /**
95      * Process a particular annotation which type is the same as the
96      * one returned by @see getAnnotationType(). All information
97      * pertinent to the annotation and its context is encapsulated
98      * in the passed AnnotationInfo instance.
99      *
100      * @param ainfo the annotation information
101      */

102     public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo)
103             throws AnnotationProcessorException {
104
105         AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
106         if (aeHandler instanceof EjbBundleContext) {
107             EjbBundleContext ejbBundleContext = (EjbBundleContext)aeHandler;
108             aeHandler = ejbBundleContext.createContextForEjb();
109         } else if (aeHandler instanceof WebBundleContext) {
110             WebBundleContext webBundleContext = (WebBundleContext)aeHandler;
111             aeHandler = webBundleContext.createContextForWeb();
112             if (aeHandler == null) {
113                 // no such web comp, use webBundleContext
114
aeHandler = ainfo.getProcessingContext().getHandler();
115             }
116         }
117
118         if (aeHandler == null) {
119             // no such ejb
120
return getInvalidAnnotatedElementHandlerResult(
121                 ainfo.getProcessingContext().getHandler(), ainfo);
122         }
123
124         if (!supportTypeInheritance() &&
125                 ElementType.TYPE.equals(ainfo.getElementType()) &&
126                 aeHandler instanceof ComponentContext) {
127             ComponentContext context = (ComponentContext)aeHandler;
128             Class JavaDoc clazz = (Class JavaDoc)ainfo.getAnnotatedElement();
129             if (!clazz.getName().equals(context.getComponentClassName())) {
130                 if (logger.isLoggable(Level.WARNING)) {
131                     log(Level.WARNING, ainfo,
132                         localStrings.getLocalString(
133                         "enterprise.deployment.annotation.handlers.typeinhernotsupp",
134                         "The annotation symbol inheritance is not supported."));
135                 }
136                 return getDefaultProcessedResult();
137             }
138         }
139
140         HandlerProcessingResult procResult = null;
141         if (aeHandler instanceof EjbContext) {
142             procResult = processAnnotation(ainfo, new EjbContext[] { (EjbContext)aeHandler });
143         } else if (aeHandler instanceof EjbsContext) {
144             EjbsContext ejbsContext = (EjbsContext)aeHandler;
145             procResult = processAnnotation(ainfo, ejbsContext.getEjbContexts());
146         } else if (aeHandler instanceof WebComponentContext) {
147             procResult = processAnnotation(ainfo,
148                 new WebComponentContext[] { (WebComponentContext)aeHandler });
149         } else if (aeHandler instanceof WebComponentsContext) {
150             WebComponentsContext webCompsContext = (WebComponentsContext)aeHandler;
151             procResult = processAnnotation(ainfo, webCompsContext.getWebComponentContexts());
152         } else if (aeHandler instanceof WebBundleContext) {
153             WebBundleContext webBundleContext = (WebBundleContext)aeHandler;
154             procResult = processAnnotation(ainfo, webBundleContext);
155         } else {
156             return getInvalidAnnotatedElementHandlerResult(aeHandler, ainfo);
157         }
158
159         return procResult;
160     }
161
162     /**
163      * This indicates whether the annotation type should be processed for
164      * type level in super-class.
165      */

166     protected boolean supportTypeInheritance() {
167         return false;
168     }
169 }
170
Popular Tags