1 22 package org.jboss.injection; 23 24 import org.jboss.annotation.IgnoreDependency; 25 import org.jboss.ejb3.EJBContainer; 26 import org.jboss.logging.Logger; 27 import org.jboss.metamodel.descriptor.BaseEjbRef; 28 import org.jboss.metamodel.descriptor.EjbLocalRef; 29 import org.jboss.metamodel.descriptor.EjbRef; 30 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 31 32 import javax.ejb.EJB ; 33 import javax.ejb.EJBs ; 34 import javax.naming.NameNotFoundException ; 35 import java.lang.reflect.AccessibleObject ; 36 import java.lang.reflect.Field ; 37 import java.lang.reflect.Method ; 38 import java.util.Collection ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 42 48 public class EJBHandler implements InjectionHandler 49 { 50 private static final Logger log = Logger.getLogger(EJBHandler.class); 51 52 protected void addDependency(String refName, EJBContainer refcon, InjectionContainer container) 53 { 54 if(!container.equals(refcon)) 56 container.getDependencyPolicy().addDependency(refcon.getObjectName().getCanonicalName()); 57 } 58 59 public void loadXml(EnvironmentRefGroup xml, InjectionContainer container) 60 { 61 if (xml != null) 62 { 63 if (xml.getEjbLocalRefs() != null) loadEjbLocalXml(xml.getEjbLocalRefs(), container); 64 if (xml.getEjbRefs() != null) loadEjbRefXml(xml.getEjbRefs(), container); 65 } 66 } 67 68 protected void loadEjbLocalXml(Collection <EjbLocalRef> refs, InjectionContainer container) 69 { 70 for (EjbLocalRef ref : refs) 71 { 72 String interfaceName = ref.getLocal(); 73 String errorType = "<ejb-local-ref>"; 74 75 ejbRefXml(ref, interfaceName, container, errorType); 76 } 77 } 78 79 protected void loadEjbRefXml(Collection <EjbRef> refs, InjectionContainer container) 80 { 81 for (EjbRef ref : refs) 82 { 83 String interfaceName = ref.getRemote(); 84 String errorType = "<ejb-ref>"; 85 86 ejbRefXml(ref, interfaceName, container, errorType); 87 } 88 } 89 90 protected void ejbRefXml(BaseEjbRef ref, String interfaceName, InjectionContainer container, String errorType) 91 { 92 String encName = "env/" + ref.getEjbRefName(); 93 InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections()); 94 if (container.getEncInjectors().containsKey(encName)) 95 return; 96 97 String mappedName = ref.getMappedName(); 98 if (mappedName != null && mappedName.equals("")) mappedName = null; 99 100 String link = ref.getEjbLink(); 101 if (link != null && link.trim().equals("")) link = null; 102 103 Class refClass = null; 104 105 if (interfaceName != null) 106 { 107 try 108 { 109 refClass = container.getClassloader().loadClass(interfaceName); 110 } 111 catch (ClassNotFoundException e) 112 { 113 throw new RuntimeException ("could not find " + errorType + "'s local interface " + interfaceName + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier()); 114 } 115 } 116 117 119 if (mappedName == null && refClass == null && link == null) 120 { 121 } 124 else 125 { 126 ejbRefEncInjector(mappedName, encName, refClass, link, errorType, container); 127 if (ref.isIgnoreDependency()) 128 { 129 log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName); 130 return; 131 } 132 133 ejbRefDependency(link, container, refClass, errorType, encName); 134 } 135 } 136 137 protected void ejbRefDependency(String link, InjectionContainer container, Class refClass, String errorType, String encName) 138 { 139 EJBContainer refcon = null; 140 141 if (refClass != null && (refClass.equals(Object .class) || refClass.equals(void.class))) refClass = null; 142 143 if (refClass != null) 144 { 145 if (link != null && !link.trim().equals("")) 146 { 147 refcon = (EJBContainer) container.resolveEjbContainer(link, refClass); 148 if (refcon == null) 149 { 150 String msg = "IGNORING DEPENDENCY: unable to find " + errorType + " of interface " + refClass.getName() + " and ejbLink of " + link + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier() + " it might not be deployed yet"; 151 log.warn(msg); 152 } 153 } 154 else 155 { 156 try 157 { 158 refcon = (EJBContainer) container.resolveEjbContainer(refClass); 159 if (refcon == null) 160 { 161 String msg = "IGNORING DEPENDENCY: unable to find " + errorType + " from interface only " + refClass.getName() + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier(); 162 log.warn(msg); 163 } 164 } 165 catch (NameNotFoundException e) 166 { 167 String msg = "IGNORING DEPENDENCY: unable to find " + errorType + " from interface only " + refClass.getName() + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier() + e.getMessage(); 168 log.warn(msg); 169 } 170 } 171 } 172 else 173 { 174 String msg = "IGNORING DEPENDENCY: unable to resolve dependency of EJB, there is too little information"; 175 log.warn(msg); 176 } 177 178 if (refcon != null) 179 { 180 addDependency(encName, refcon, container); 181 } 182 } 183 184 protected void ejbRefEncInjector(String mappedName, String encName, Class refClass, String link, String errorType, InjectionContainer container) 185 { 186 if (refClass != null && (refClass.equals(Object .class) || refClass.equals(void.class))) refClass = null; 187 if (mappedName != null && mappedName.trim().equals("")) mappedName = null; 188 189 EncInjector injector = null; 190 191 if (mappedName == null) 192 { 193 injector = new EjbEncInjector(encName, refClass, link, errorType); 194 } 195 else 196 { 197 injector = new EjbEncInjector(encName, mappedName, errorType); 198 } 199 200 container.getEncInjectors().put(encName, injector); 201 } 202 203 public static EJBContainer getEjbContainer(EJB ref, InjectionContainer container, Class memberType) 204 { 205 EJBContainer rtn = null; 206 207 if (ref.mappedName() != null && !"".equals(ref.mappedName())) 208 { 209 return null; 210 } 211 212 if (ref.beanName().equals("") && memberType == null) 213 throw new RuntimeException ("For deployment " + container.getIdentifier() + "not enough information for @EJB. Please fill out the beanName and/or businessInterface attributes"); 214 215 Class businessInterface = memberType; 216 if (!ref.beanInterface().getName().equals(Object .class.getName())) 217 { 218 businessInterface = ref.beanInterface(); 219 } 220 221 if (ref.beanName().equals("")) 222 { 223 try 224 { 225 rtn = (EJBContainer) container.resolveEjbContainer(businessInterface); 226 } 227 catch (NameNotFoundException e) 228 { 229 log.warn("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ") " + e.getMessage()); 230 } 231 } 232 else 233 { 234 rtn = (EJBContainer) container.resolveEjbContainer(ref.beanName(), businessInterface); 235 } 236 237 return rtn; 238 } 239 240 public static String getJndiName(EJB ref, InjectionContainer container, Class memberType) 241 { 242 String jndiName; 243 244 if (ref.mappedName() != null && !"".equals(ref.mappedName())) 245 { 246 return ref.mappedName(); 247 } 248 249 if (ref.beanName().equals("") && memberType == null) 250 throw new RuntimeException ("For deployment " + container.getIdentifier() + "not enough information for @EJB. Please fill out the beanName and/or businessInterface attributes"); 251 252 Class businessInterface = memberType; 253 if (!ref.beanInterface().getName().equals(Object .class.getName())) 254 { 255 businessInterface = ref.beanInterface(); 256 } 257 258 if (ref.beanName().equals("")) 259 { 260 try 261 { 262 jndiName = container.getEjbJndiName(businessInterface); 263 } 264 catch (NameNotFoundException e) 265 { 266 throw new RuntimeException ("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ") " + e.getMessage()); 267 } 268 if (jndiName == null) 269 { 270 throw new RuntimeException ("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ")"); 271 } 272 } 273 else 274 { 275 jndiName = container.getEjbJndiName(ref.beanName(), businessInterface); 276 if (jndiName == null) 277 { 278 throw new RuntimeException ("For EJB " + container.getIdentifier() + "could not find jndi binding based on beanName and business interface for @EJB(" + ref.beanName() + ", " + businessInterface.getName() + ")"); 279 } 280 } 281 282 return jndiName; 283 } 284 285 public void handleClassAnnotations(Class clazz, InjectionContainer container) 286 { 287 EJBs ref = container.getAnnotation(EJBs .class, clazz); 288 if (ref != null) 289 { 290 EJB [] ejbs = ref.value(); 291 292 for (EJB ejb : ejbs) 293 { 294 handleClassAnnotation(ejb, clazz, container); 295 } 296 } 297 EJB ejbref = container.getAnnotation(EJB .class, clazz); 298 if (ejbref != null) handleClassAnnotation(ejbref, clazz, container); 299 } 300 301 protected void handleClassAnnotation(EJB ejb, Class clazz, InjectionContainer container) 302 { 303 String encName = ejb.name(); 304 if (encName == null || encName.equals("")) 305 { 306 throw new RuntimeException ("JBoss requires the name of the @EJB in the @EJBs: " + clazz); 307 } 308 encName = "env/" + encName; 309 310 if (container.getEncInjectors().containsKey(encName)) return; 311 ejbRefEncInjector(ejb.mappedName(), encName, ejb.beanInterface(), ejb.beanName(), "@EJB", container); 312 313 315 if (isIgnoreDependency(container, ejb)) 316 log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName); 317 else 318 ejbRefDependency(ejb.beanName(), container, ejb.beanInterface(), "@EJB", encName); 319 } 320 321 public void handleMethodAnnotations(Method method, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 322 { 323 324 EJB ref = method.getAnnotation(EJB .class); 325 if (ref != null) 326 { 327 if (!method.getName().startsWith("set")) 328 throw new RuntimeException ("@EJB can only be used with a set method: " + method); 329 String encName = ref.name(); 330 if (encName == null || encName.equals("")) 331 { 332 encName = InjectionUtil.getEncName(method); 333 } 334 else 335 { 336 encName = "env/" + encName; 337 } 338 if (!container.getEncInjectors().containsKey(encName)) 339 { 340 ejbRefEncInjector(ref.mappedName(), encName, method.getParameterTypes()[0], ref.beanName(), "@EJB", container); 341 342 if (container.getAnnotation(IgnoreDependency.class, method) == null) 343 { 344 if (isIgnoreDependency(container, ref)) 345 log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName); 346 else 347 ejbRefDependency(ref.beanName(), container, method.getParameterTypes()[0], "@EJB", encName); 348 } 349 } 350 351 injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc())); 352 } 353 } 354 355 public void handleFieldAnnotations(Field field, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 356 { 357 EJB ref = field.getAnnotation(EJB .class); 358 if (ref != null) 359 { 360 String encName = ref.name(); 361 if (encName == null || encName.equals("")) 362 { 363 encName = InjectionUtil.getEncName(field); 364 } 365 else 366 { 367 encName = "env/" + encName; 368 } 369 if (!container.getEncInjectors().containsKey(encName)) 370 { 371 if (container.getAnnotation(IgnoreDependency.class, field) == null) 372 { 373 if (isIgnoreDependency(container, ref)) 374 log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName); 375 else 376 ejbRefDependency(ref.beanName(), container, field.getType(), "@EJB", encName); 377 } 378 ejbRefEncInjector(ref.mappedName(), encName, field.getType(), ref.beanName(), "@EJB", container); 379 } 380 injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc())); 381 382 } 383 } 384 385 protected boolean isIgnoreDependency(InjectionContainer container, EJB ref) 386 { 387 EnvironmentRefGroup refGroup = (EnvironmentRefGroup)container.getEnvironmentRefGroup(); 388 389 if (refGroup != null) 390 { 391 Iterator <EjbRef> ejbRefs = refGroup.getEjbRefs().iterator(); 392 while (ejbRefs.hasNext()) 393 { 394 EjbRef ejbRef = ejbRefs.next(); 395 if (ejbRef.getEjbRefName().equals(ref.name())) 396 { 397 if (ejbRef.isIgnoreDependency()) 398 return true; 399 else 400 return false; 401 } 402 } 403 } 404 405 return false; 406 } 407 } 408 | Popular Tags |