FileDocCategorySizeDatePackage
EJBHandler.javaAPI DocJBoss 4.2.114796Fri Jul 13 20:53:46 BST 2007org.jboss.injection

EJBHandler

public class EJBHandler extends Object implements InjectionHandler
Searches bean class for all @Inject and create Injectors
author
Bill Burke
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
Constructors Summary
Methods Summary
protected voidaddDependency(java.lang.String refName, org.jboss.ejb3.EJBContainer refcon, InjectionContainer container)


          
   
      // Do not depend on myself
      if(!container.equals(refcon))
         container.getDependencyPolicy().addDependency(refcon.getObjectName().getCanonicalName());
   
protected voidejbRefDependency(java.lang.String link, InjectionContainer container, java.lang.Class refClass, java.lang.String errorType, java.lang.String encName)

      EJBContainer refcon = null;

      if (refClass != null && (refClass.equals(Object.class) || refClass.equals(void.class))) refClass = null;

      if (refClass != null)
      {
         if (link != null && !link.trim().equals(""))
         {
            refcon = (EJBContainer) container.resolveEjbContainer(link, refClass);
            if (refcon == null)
            {
               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";
               log.warn(msg);
            }
         }
         else
         {
            try
            {
               refcon = (EJBContainer) container.resolveEjbContainer(refClass);
               if (refcon == null)
               {
                  String msg = "IGNORING DEPENDENCY: unable to find " + errorType + " from interface only " + refClass.getName() + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier();
                  log.warn(msg);
               }
            }
            catch (NameNotFoundException e)
            {
               String msg = "IGNORING DEPENDENCY: unable to find " + errorType + " from interface only " + refClass.getName() + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier() + e.getMessage();
               log.warn(msg);
            }
         }
      }
      else
      {
         String msg = "IGNORING DEPENDENCY: unable to resolve dependency of EJB, there is too little information";
         log.warn(msg);
      }

      if (refcon != null)
      {
         addDependency(encName, refcon, container);
      }
   
protected voidejbRefEncInjector(java.lang.String mappedName, java.lang.String encName, java.lang.Class refClass, java.lang.String link, java.lang.String errorType, InjectionContainer container)

      if (refClass != null && (refClass.equals(Object.class) || refClass.equals(void.class))) refClass = null;
      if (mappedName != null && mappedName.trim().equals("")) mappedName = null;

      EncInjector injector = null;

      if (mappedName == null)
      {
         injector = new EjbEncInjector(encName, refClass, link, errorType);
      }
      else
      {
         injector = new EjbEncInjector(encName, mappedName, errorType);
      }

      container.getEncInjectors().put(encName, injector);
   
protected voidejbRefXml(org.jboss.metamodel.descriptor.BaseEjbRef ref, java.lang.String interfaceName, InjectionContainer container, java.lang.String errorType)

      String encName = "env/" + ref.getEjbRefName();
      InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
      if (container.getEncInjectors().containsKey(encName))
         return;

      String mappedName = ref.getMappedName();
      if (mappedName != null && mappedName.equals("")) mappedName = null;

      String link = ref.getEjbLink();
      if (link != null && link.trim().equals("")) link = null;

      Class refClass = null;

      if (interfaceName != null)
      {
         try
         {
            refClass = container.getClassloader().loadClass(interfaceName);
         }
         catch (ClassNotFoundException e)
         {
            throw new RuntimeException("could not find " + errorType + "'s local interface " + interfaceName + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier());
         }
      }
      
      //----- injectors

      if (mappedName == null && refClass == null && link == null)
      {
         // must be jboss.xml only with @EJB used to define reference.  jboss.xml used to tag for ignore dependency
         // i think it is ok to assume this because the ejb-jar.xml schema should handle any missing elements
      }
      else
      {
         ejbRefEncInjector(mappedName, encName, refClass, link, errorType, container);
         if (ref.isIgnoreDependency())
         {
            log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
            return;
         }

         ejbRefDependency(link, container, refClass, errorType, encName);
      }
   
public static org.jboss.ejb3.EJBContainergetEjbContainer(javax.ejb.EJB ref, InjectionContainer container, java.lang.Class memberType)

      EJBContainer rtn = null;

      if (ref.mappedName() != null && !"".equals(ref.mappedName()))
      {
         return null;
      }

      if (ref.beanName().equals("") && memberType == null)
         throw new RuntimeException("For deployment " + container.getIdentifier() + "not enough information for @EJB.  Please fill out the beanName and/or businessInterface attributes");

      Class businessInterface = memberType;
      if (!ref.beanInterface().getName().equals(Object.class.getName()))
      {
         businessInterface = ref.beanInterface();
      }

      if (ref.beanName().equals(""))
      {
         try
         {
            rtn = (EJBContainer) container.resolveEjbContainer(businessInterface);
         }
         catch (NameNotFoundException e)
         {
            log.warn("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ") " + e.getMessage());
         }
      }
      else
      {
         rtn = (EJBContainer) container.resolveEjbContainer(ref.beanName(), businessInterface);
      }

      return rtn;
   
public static java.lang.StringgetJndiName(javax.ejb.EJB ref, InjectionContainer container, java.lang.Class memberType)

      String jndiName;

      if (ref.mappedName() != null && !"".equals(ref.mappedName()))
      {
         return ref.mappedName();
      }

      if (ref.beanName().equals("") && memberType == null)
         throw new RuntimeException("For deployment " + container.getIdentifier() + "not enough information for @EJB.  Please fill out the beanName and/or businessInterface attributes");

      Class businessInterface = memberType;
      if (!ref.beanInterface().getName().equals(Object.class.getName()))
      {
         businessInterface = ref.beanInterface();
      }

      if (ref.beanName().equals(""))
      {
         try
         {
            jndiName = container.getEjbJndiName(businessInterface);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ") " + e.getMessage());
         }
         if (jndiName == null)
         {
            throw new RuntimeException("For deployment " + container.getIdentifier() + " could not find jndi binding based on interface only for @EJB(" + businessInterface.getName() + ")");
         }
      }
      else
      {
         jndiName = container.getEjbJndiName(ref.beanName(), businessInterface);
         if (jndiName == null)
         {
            throw new RuntimeException("For EJB " + container.getIdentifier() + "could not find jndi binding based on beanName and business interface for @EJB(" + ref.beanName() + ", " + businessInterface.getName() + ")");
         }
      }

      return jndiName;
   
protected voidhandleClassAnnotation(javax.ejb.EJB ejb, java.lang.Class clazz, InjectionContainer container)

      String encName = ejb.name();
      if (encName == null || encName.equals(""))
      {
         throw new RuntimeException("JBoss requires the name of the @EJB in the @EJBs: " + clazz);
      }
      encName = "env/" + encName;

      if (container.getEncInjectors().containsKey(encName)) return;
      ejbRefEncInjector(ejb.mappedName(), encName, ejb.beanInterface(), ejb.beanName(), "@EJB", container);

      // handle dependencies

      if (isIgnoreDependency(container, ejb))
         log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
      else
         ejbRefDependency(ejb.beanName(), container, ejb.beanInterface(), "@EJB", encName);
   
public voidhandleClassAnnotations(java.lang.Class clazz, InjectionContainer container)

      EJBs ref = container.getAnnotation(EJBs.class, clazz);
      if (ref != null)
      {
         EJB[] ejbs = ref.value();

         for (EJB ejb : ejbs)
         {
            handleClassAnnotation(ejb, clazz, container);
         }
      }
      EJB ejbref = container.getAnnotation(EJB.class, clazz);
      if (ejbref != null) handleClassAnnotation(ejbref, clazz, container);
   
public voidhandleFieldAnnotations(java.lang.reflect.Field field, InjectionContainer container, java.util.Map injectors)

      EJB ref = field.getAnnotation(EJB.class);
      if (ref != null)
      {
         String encName = ref.name();
         if (encName == null || encName.equals(""))
         {
            encName = InjectionUtil.getEncName(field);
         }
         else
         {
            encName = "env/" + encName;
         }
         if (!container.getEncInjectors().containsKey(encName))
         {
            if (container.getAnnotation(IgnoreDependency.class, field) == null)
            {
               if (isIgnoreDependency(container, ref))
                  log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
               else
                  ejbRefDependency(ref.beanName(), container, field.getType(), "@EJB", encName);
            }
            ejbRefEncInjector(ref.mappedName(), encName, field.getType(), ref.beanName(), "@EJB", container);
         }
         injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));

      }
   
public voidhandleMethodAnnotations(java.lang.reflect.Method method, InjectionContainer container, java.util.Map injectors)

      
      EJB ref = method.getAnnotation(EJB.class);
      if (ref != null)
      {
         if (!method.getName().startsWith("set"))
            throw new RuntimeException("@EJB can only be used with a set method: " + method);
         String encName = ref.name();
         if (encName == null || encName.equals(""))
         {
            encName = InjectionUtil.getEncName(method);
         }
         else
         {
            encName = "env/" + encName;
         }
         if (!container.getEncInjectors().containsKey(encName))
         {
            ejbRefEncInjector(ref.mappedName(), encName, method.getParameterTypes()[0], ref.beanName(), "@EJB", container);
            
            if (container.getAnnotation(IgnoreDependency.class, method) == null)
            {
               if (isIgnoreDependency(container, ref))
                  log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
               else
                  ejbRefDependency(ref.beanName(), container, method.getParameterTypes()[0], "@EJB", encName);
            }
         }

         injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
      }
   
protected booleanisIgnoreDependency(InjectionContainer container, javax.ejb.EJB ref)

      EnvironmentRefGroup refGroup =  (EnvironmentRefGroup)container.getEnvironmentRefGroup();
      
      if (refGroup != null)
      {
         Iterator<EjbRef> ejbRefs = refGroup.getEjbRefs().iterator();
         while (ejbRefs.hasNext())
         {
            EjbRef ejbRef = ejbRefs.next();
            if (ejbRef.getEjbRefName().equals(ref.name()))
            {
               if (ejbRef.isIgnoreDependency())
                  return true;
               else
                  return false;
            }
         }
      }
      
      return false;
   
protected voidloadEjbLocalXml(java.util.Collection refs, InjectionContainer container)

      for (EjbLocalRef ref : refs)
      {
         String interfaceName = ref.getLocal();
         String errorType = "<ejb-local-ref>";

         ejbRefXml(ref, interfaceName, container, errorType);
      }
   
protected voidloadEjbRefXml(java.util.Collection refs, InjectionContainer container)

      for (EjbRef ref : refs)
      {
         String interfaceName = ref.getRemote();
         String errorType = "<ejb-ref>";

         ejbRefXml(ref, interfaceName, container, errorType);
      }
   
public voidloadXml(org.jboss.metamodel.descriptor.EnvironmentRefGroup xml, InjectionContainer container)

      if (xml != null)
      {
         if (xml.getEjbLocalRefs() != null) loadEjbLocalXml(xml.getEjbLocalRefs(), container);
         log.trace("ejbRefs = " + xml.getEjbRefs());
         if (xml.getEjbRefs() != null) loadEjbRefXml(xml.getEjbRefs(), container);
      }