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.Field ; 29 import java.lang.reflect.Method ; 30 31 import java.util.logging.Level ; 32 33 import javax.ejb.EJB ; 34 import javax.ejb.EJBHome ; 35 import javax.ejb.EJBLocalHome ; 36 import javax.ejb.EJBObject ; 37 import javax.ejb.Local ; 38 import javax.ejb.Remote ; 39 40 import com.sun.enterprise.deployment.EjbDescriptor; 41 import com.sun.enterprise.deployment.EjbEntityDescriptor; 42 import com.sun.enterprise.deployment.EjbReferenceDescriptor; 43 import com.sun.enterprise.deployment.EjbSessionDescriptor; 44 import com.sun.enterprise.deployment.MethodDescriptor; 45 import com.sun.enterprise.deployment.InjectionTarget; 46 import com.sun.enterprise.deployment.types.EjbReferenceContainer; 47 import com.sun.enterprise.deployment.annotation.AnnotatedElementHandler; 48 import com.sun.enterprise.deployment.annotation.AnnotationInfo; 49 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 50 import com.sun.enterprise.deployment.annotation.HandlerProcessingResult; 51 import com.sun.enterprise.deployment.annotation.context.ResourceContainerContext; 52 53 58 public class EJBHandler extends AbstractResourceHandler { 59 60 public EJBHandler() { 61 } 62 63 66 public Class <? extends Annotation > getAnnotationType() { 67 return EJB .class; 68 } 69 70 80 protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, 81 ResourceContainerContext[] rcContexts) 82 throws AnnotationProcessorException { 83 84 EJB ejbAn = (EJB )ainfo.getAnnotation(); 85 return processEJB(ainfo, rcContexts, ejbAn); 86 } 87 88 89 100 protected HandlerProcessingResult processEJB(AnnotationInfo ainfo, 101 ResourceContainerContext[] rcContexts, EJB ejbAn) 102 throws AnnotationProcessorException { 103 EjbReferenceDescriptor ejbRefs[] = null; 104 105 if (ElementType.FIELD.equals(ainfo.getElementType())) { 106 Field f = (Field )ainfo.getAnnotatedElement(); 107 String targetClassName = f.getDeclaringClass().getName(); 108 109 String logicalName = ejbAn.name(); 110 111 if (logicalName.equals("")) { 113 logicalName = targetClassName + "/" + f.getName(); 114 } 115 116 Class beanInterface = (ejbAn.beanInterface() == Object .class) ? 119 f.getType() : ejbAn.beanInterface(); 120 121 InjectionTarget target = new InjectionTarget(); 122 target.setClassName(targetClassName); 123 target.setFieldName(f.getName()); 124 125 ejbRefs = getEjbReferenceDescriptors(logicalName, rcContexts); 126 for (EjbReferenceDescriptor ejbRef : ejbRefs) { 127 ejbRef.addInjectionTarget(target); 128 129 if (ejbRef.getName().length() == 0) { processNewEJBAnnotation(ejbRef, beanInterface, 131 logicalName, ejbAn); 132 } 133 } 134 } else if (ElementType.METHOD.equals(ainfo.getElementType())) { 135 136 Method m = (Method )ainfo.getAnnotatedElement(); 137 String targetClassName = m.getDeclaringClass().getName(); 138 139 String logicalName = ejbAn.name(); 140 if( logicalName.equals("") ) { 141 String propertyName = 143 getInjectionMethodPropertyName(m, ainfo); 144 145 logicalName = targetClassName + "/" + propertyName; 147 } 148 149 validateInjectionMethod(m, ainfo); 150 151 Class [] params = m.getParameterTypes(); 152 Class beanInterface = (ejbAn.beanInterface() == Object .class) ? 155 params[0] : ejbAn.beanInterface(); 156 157 InjectionTarget target = new InjectionTarget(); 158 target.setClassName(targetClassName); 159 target.setMethodName(m.getName()); 160 161 ejbRefs = getEjbReferenceDescriptors(logicalName, rcContexts); 162 for (EjbReferenceDescriptor ejbRef : ejbRefs) { 163 164 ejbRef.addInjectionTarget(target); 165 166 if (ejbRef.getName().length() == 0) { 168 processNewEJBAnnotation(ejbRef, beanInterface, 169 logicalName, ejbAn); 170 } 171 } 172 } else if( ElementType.TYPE.equals(ainfo.getElementType()) ) { 173 String logicalName = ejbAn.name(); 176 Class beanInterface = ejbAn.beanInterface(); 177 178 if( "".equals(logicalName) || beanInterface == Object .class ) { 179 Class c = (Class ) ainfo.getAnnotatedElement(); 180 log(Level.SEVERE, ainfo, 181 localStrings.getLocalString( 182 "enterprise.deployment.annotation.handlers.invalidtypelevelejb", 183 "Invalid TYPE-level @EJB with name() = [{0}] and beanInterface = [{1}] in {2}. Each TYPE-level @EJB must specify both name() and beanInterface().", 184 new Object [] { logicalName, beanInterface, c })); 185 return getDefaultFailedResult(); 186 } 187 188 ejbRefs = getEjbReferenceDescriptors(logicalName, rcContexts); 189 for (EjbReferenceDescriptor ejbRef : ejbRefs) { 190 if (ejbRef.getName().length() == 0) { 192 processNewEJBAnnotation(ejbRef, beanInterface, 193 logicalName, ejbAn); 194 } 195 } 196 } 197 198 return getDefaultProcessedResult(); 199 } 200 201 208 private EjbReferenceDescriptor[] getEjbReferenceDescriptors( 209 String logicalName, ResourceContainerContext[] rcContexts) { 210 EjbReferenceDescriptor ejbRefs[] = 211 new EjbReferenceDescriptor[rcContexts.length]; 212 for (int i = 0; i < rcContexts.length; i++) { 213 EjbReferenceDescriptor ejbRef = 214 (EjbReferenceDescriptor)rcContexts[i].getEjbReference(logicalName); 215 if (ejbRef == null) { 216 ejbRef = new EjbReferenceDescriptor(); 217 rcContexts[i].addEjbReferenceDescriptor(ejbRef); 218 } 219 ejbRefs[i] = ejbRef; 220 } 221 222 return ejbRefs; 223 } 224 225 private void processNewEJBAnnotation(EjbReferenceDescriptor ejbRef, 226 Class beanInterface, 227 String logicalName, EJB annotation) { 228 229 ejbRef.setName(logicalName); 230 231 String targetBeanType = EjbSessionDescriptor.TYPE; 232 if (EJBHome .class.isAssignableFrom(beanInterface) || 233 EJBLocalHome .class.isAssignableFrom(beanInterface)) { 234 targetBeanType = processForHomeInterface(ejbRef, beanInterface); 235 } else { 236 ejbRef.setEjbInterface(beanInterface.getName()); 238 239 if( beanInterface.getAnnotation(Local .class) != null ) { 240 ejbRef.setLocal(true); 241 } else if( beanInterface.getAnnotation(Remote .class) 242 != null ) { 243 ejbRef.setLocal(false); 244 } else { 245 ejbRef.setLocal(false); 253 } 254 } 255 256 String ejbAnBeanName = annotation.beanName(); 257 if (ejbAnBeanName != null && ejbAnBeanName.length() > 0) { 258 ejbRef.setLinkName(ejbAnBeanName); 259 } 260 261 ejbRef.setType(targetBeanType); 262 ejbRef.setMappedName(annotation.mappedName()); 263 264 } 265 266 269 private String processForHomeInterface(EjbReferenceDescriptor ejbRef, 270 Class beanInterface) { 271 272 String targetBeanType = EjbSessionDescriptor.TYPE; 274 ejbRef.setHomeClassName(beanInterface.getName()); 275 276 try { 277 Method [] methods = beanInterface.getMethods(); 281 for (Method m : methods) { 282 if (m.getName().equals("create")) { 283 ejbRef.setEjbInterface(m.getReturnType().getName()); 284 break; 285 } 286 } 287 for (Method m : methods) { 290 if (m.getName().equals("findByPrimaryKey")) { 291 targetBeanType = EjbEntityDescriptor.TYPE; 292 break; 293 } 294 } 295 } catch(Exception e) { 296 if (logger.isLoggable(Level.FINE)) { 297 logger.log(Level.FINE, 298 "component intf / ejb type annotation processing error", e); 299 } 300 } 301 302 ejbRef.setLocal(EJBLocalHome .class.isAssignableFrom(beanInterface)); 303 return targetBeanType; 304 } 305 } 306 | Popular Tags |