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.lang.reflect.Field ; 30 import java.util.List ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 34 import javax.ejb.MessageDriven ; 35 import javax.ejb.Stateful ; 36 import javax.ejb.Stateless ; 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 58 public abstract class AbstractHandler implements AnnotationHandler { 59 protected static LocalStringManagerImpl localStrings = 60 new LocalStringManagerImpl(AbstractHandler.class); 61 protected Logger logger = AnnotationUtils.getLogger(); 62 63 68 public Class <? extends Annotation >[] getTypeDependencies() { 69 return null; 70 } 71 72 74 77 protected HandlerProcessingResult getDefaultProcessedResult() { 78 return HandlerProcessingResultImpl.getDefaultResult( 79 getAnnotationType(), ResultType.PROCESSED); 80 } 81 82 85 protected HandlerProcessingResult getDefaultFailedResult() { 86 return HandlerProcessingResultImpl.getDefaultResult( 87 getAnnotationType(), ResultType.FAILED); 88 } 89 90 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 level, AnnotationInfo ainfo, 113 String 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 getInjectionMethodPropertyName(Method method, 132 AnnotationInfo ainfo) throws AnnotationProcessorException 133 { 134 String methodName = method.getName(); 135 String propertyName = methodName; 136 137 if( (methodName.length() > 3) && 138 methodName.startsWith("set") ) { 139 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 159 protected void validateInjectionMethod(Method 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 <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 195 protected Class <? extends Annotation >[] getEjbAnnotationTypes() { 196 return new Class [] { 197 MessageDriven .class, Stateful .class, Stateless .class }; 198 } 199 } 200 | Popular Tags |