KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Field JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 import javax.ejb.MessageDriven JavaDoc;
35 import javax.ejb.Stateful JavaDoc;
36 import javax.ejb.Stateless JavaDoc;
37
38 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler;
39 import com.sun.enterprise.deployment.annotation.AnnotationHandler;
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.ResultType;
44 import com.sun.enterprise.deployment.annotation.impl.AnnotationUtils;
45 import com.sun.enterprise.deployment.annotation.impl.HandlerProcessingResultImpl;
46
47 import com.sun.enterprise.util.LocalStringManagerImpl;
48
49 /**
50  * This is an abstract base class for Handlers.
51  * Concrete subclass need to implements the following methods:
52  * public Class<? extends Annotation> getAnnotationType();
53  * public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo)
54  * throws AnnotationProcessorException;
55  *
56  * @author Shing Wai Chan
57  */

58 public abstract class AbstractHandler implements AnnotationHandler {
59     protected static LocalStringManagerImpl localStrings =
60             new LocalStringManagerImpl(AbstractHandler.class);
61     protected Logger JavaDoc logger = AnnotationUtils.getLogger();
62
63     /**
64      * @return an array of annotation types this annotation handler would
65      * require to be processed (if present) before it processes it's own
66      * annotation type.
67      */

68     public Class JavaDoc<? extends Annotation JavaDoc>[] getTypeDependencies() {
69         return null;
70     }
71
72     // ----- end of implements AnnotationHandler -----
73

74     /**
75      * @return a default processed result
76      */

77     protected HandlerProcessingResult getDefaultProcessedResult() {
78         return HandlerProcessingResultImpl.getDefaultResult(
79                 getAnnotationType(), ResultType.PROCESSED);
80     }
81
82     /**
83      * @return a default failed result
84      */

85     protected HandlerProcessingResult getDefaultFailedResult() {
86         return HandlerProcessingResultImpl.getDefaultResult(
87                 getAnnotationType(), ResultType.FAILED);
88     }
89
90     /**
91      * @param aeHandler
92      * @param ainfo
93      * @return a result for invalid AnnotatedElementHandler
94      */

95     protected HandlerProcessingResult getInvalidAnnotatedElementHandlerResult(
96             AnnotatedElementHandler aeHandler, AnnotationInfo ainfo)
97             throws AnnotationProcessorException {
98
99         if (logger.isLoggable(Level.FINE)) {
100             log(Level.FINE, ainfo,
101                 localStrings.getLocalString(
102                 "enterprise.deployment.annotation.handlers.invalidaehandler",
103                 "Invalid annotation symbol found for this type of class."));
104         }
105         if (logger.isLoggable(Level.FINER)) {
106             logger.finer("Invalid AnnotatedElementHandler: " + aeHandler);
107         }
108
109         return getDefaultProcessedResult();
110     }
111
112     protected void log(Level JavaDoc level, AnnotationInfo ainfo,
113             String JavaDoc localizedMessage) throws AnnotationProcessorException {
114         if (Level.SEVERE.equals(level)) {
115             ainfo.getProcessingContext().getErrorHandler().error(
116                 new AnnotationProcessorException(localizedMessage, ainfo));
117         } else if (Level.WARNING.equals(level)) {
118             ainfo.getProcessingContext().getErrorHandler().warning(
119                 new AnnotationProcessorException(localizedMessage, ainfo));
120         } else if (Level.FINE.equals(level)) {
121             ainfo.getProcessingContext().getErrorHandler().fine(
122                 new AnnotationProcessorException(localizedMessage, ainfo));
123         } else if (ainfo != null) {
124             ainfo.getProcessingContext().getProcessor().log(
125                 level, ainfo, localizedMessage);
126         } else {
127             logger.log(level, localizedMessage);
128         }
129     }
130
131     protected String JavaDoc getInjectionMethodPropertyName(Method JavaDoc method,
132             AnnotationInfo ainfo) throws AnnotationProcessorException
133     {
134         String JavaDoc methodName = method.getName();
135         String JavaDoc propertyName = methodName;
136
137         if( (methodName.length() > 3) &&
138             methodName.startsWith("set") ) {
139             // Derive javabean property name.
140
propertyName =
141                 methodName.substring(3, 4).toLowerCase() +
142                 methodName.substring(4);
143         } else {
144             throw new AnnotationProcessorException(
145                 localStrings.getLocalString(
146                 "enterprise.deployment.annotation.handlers.invalidinjectionmethodname",
147                 "Injection method name must start with \"set\""),
148                 ainfo);
149         }
150
151         return propertyName;
152     }
153
154     /**
155      * Check if given method is a valid injection method.
156      * Throw Exception if it is not.
157      * @exception AnnotationProcessorException
158      */

159     protected void validateInjectionMethod(Method JavaDoc method, AnnotationInfo ainfo)
160             throws AnnotationProcessorException {
161         if (method.getParameterTypes().length!=1){
162             throw new AnnotationProcessorException(
163                 localStrings.getLocalString(
164                 "enterprise.deployment.annotation.handlers.invalidinjectionmethod",
165                 "Injection on a method requires a JavaBeans setter method type with one parameter "),
166                 ainfo);
167                 
168         }
169         if (!void.class.equals(method.getReturnType())) {
170             throw new AnnotationProcessorException(
171                 localStrings.getLocalString(
172                 "enterprise.deployment.annotation.handlers.injectionmethodmustreturnvoid",
173                 "Injection on a method requires a void return type"),
174                 ainfo);
175         }
176     }
177
178     protected HandlerProcessingResult getOverallProcessingResult(
179             List JavaDoc<HandlerProcessingResult> resultList) {
180         HandlerProcessingResult overallProcessingResult = null;
181         for (HandlerProcessingResult result : resultList) {
182             if (overallProcessingResult == null ||
183                     (result.getOverallResult().compareTo(
184                     overallProcessingResult.getOverallResult()) > 0)) {
185                 overallProcessingResult = result;
186             }
187         }
188         return overallProcessingResult;
189     }
190
191     /**
192      * This is called by getTypeDependencies().
193      * @return an array of all ejb annotation types
194      */

195     protected Class JavaDoc<? extends Annotation JavaDoc>[] getEjbAnnotationTypes() {
196         return new Class JavaDoc[] {
197                 MessageDriven JavaDoc.class, Stateful JavaDoc.class, Stateless JavaDoc.class };
198     }
199 }
200
Popular Tags