[jboss-cvs] jboss-ejb3/src/main/org/jboss/injection ...

Bill Burke bill at jboss.com
Tue Jul 25 22:59:15 EDT 2006


  User: bill    
  Date: 06/07/25 22:59:15

  Added:       src/main/org/jboss/injection                               
                        DependsFieldInjector.java DependsHandler.java
                        DependsMethodInjector.java
                        EJBContextFieldInjector.java
                        EJBContextMethodInjector.java EJBHandler.java
                        EjbEncInjector.java EncInjector.java
                        EntityManagerFactoryFieldInjector.java
                        EntityManagerFactoryMethodInjector.java
                        EnvEntryEncInjector.java
                        ExtendedPersistenceContextInjector.java
                        InjectionContainer.java InjectionHandler.java
                        InjectionUtil.java Injector.java
                        JndiFieldInjector.java JndiInjectHandler.java
                        JndiMethodInjector.java LinkRefEncInjector.java
                        PcEncInjector.java PersistenceContextHandler.java
                        PersistenceUnitHandler.java PojoInjector.java
                        PuEncInjector.java ResourceHandler.java
                        TimerServiceFieldInjector.java
                        TimerServiceMethodInjector.java
                        UserTransactionFieldInjector.java
                        UserTransactionMethodInjector.java
                        WebServiceHandler.java
  Log:
  untested refactoring of injection classes.  Not done yet.  committing to not lose it.
  
  Revision  Changes    Path
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/DependsFieldInjector.java
  
  Index: DependsFieldInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.mx.util.MBeanProxyExt;
  
  /**
   * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
   * @version $Revision: 1.1 $
   */
  public class DependsFieldInjector implements Injector
  {
     Field field;
     ObjectName on;
  
     public DependsFieldInjector(Field field, ObjectName on)
     {
        this.field = field;
        this.on = on;
        field.setAccessible(true);
     }
  
     public void inject(BeanContext ctx)
     {
        Object instance = ctx.getInstance();
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        Class clazz = field.getType();
        Object value = null;
  
        if (clazz == ObjectName.class)
        {
           value = on;
        }
        else
        {
           MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
           value = MBeanProxyExt.create(clazz, on, server);
        }
  
        try
        {
           field.set(instance, value);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);
        }
     }
  
     public Class getInjectionClass()
     {
        return field.getType();
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/DependsHandler.java
  
  Index: DependsHandler.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.AccessibleObject;
  import java.util.Map;
  import javax.management.ObjectName;
  import javax.management.MalformedObjectNameException;
  
  import org.jboss.annotation.ejb.Depends;
  import org.jboss.logging.Logger;
  
  /**
   * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
   * @version $Revision: 1.1 $
   */
  public class DependsHandler
  {
     private static final Logger log = Logger.getLogger(DependsHandler.class);
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        Depends dep = container.getAnnotation(Depends.class, method.getDeclaringClass(), method);
        if (dep != null)
        {
           if (!method.getName().startsWith("set"))
              throw new RuntimeException("@EJB can only be used with a set method: " + method);
           String[] names = dep.value();
           if (names.length != 1)
              throw new RuntimeException("@Depends on a field can only take one object name: " + method);
           ObjectName on = null;
           try
           {
              on = new ObjectName(names[0]);
           }
           catch (MalformedObjectNameException e)
           {
              throw new RuntimeException(e);
           }
           injectors.put(method, new DependsMethodInjector(method, on));
           container.getDependencyPolicy().addDependency(names[0]);
        }
     }
  
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        Depends dep = container.getAnnotation(Depends.class, field.getDeclaringClass(), field);
        if (dep != null)
        {
           String[] names = dep.value();
           if (names.length != 1)
              throw new RuntimeException("@Depends on a field can only take one object name: " + field);
           ObjectName on = null;
           try
           {
              on = new ObjectName(names[0]);
           }
           catch (MalformedObjectNameException e)
           {
              throw new RuntimeException(e);
           }
           injectors.put(field, new DependsFieldInjector(field, on));
           container.getDependencyPolicy().addDependency(names[0]);
        }
     }
  
     public void handleClassAnnotations(Class clazz, InjectionContainer container)
     {
        Depends dep = (Depends)container.getAnnotation(Depends.class, clazz);
        if (dep == null) return;
        for (String dependency : dep.value())
        {
           container.getDependencyPolicy().addDependency(dependency);
        }
     }
  
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/DependsMethodInjector.java
  
  Index: DependsMethodInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.mx.util.MBeanProxyExt;
  
  /**
   * @author <a href="mailto:kabir.khan at jboss.org">Kabir Khan</a>
   * @version $Revision: 1.1 $
   */
  public class DependsMethodInjector implements Injector
  {
     Method method;
     ObjectName on;
  
     public DependsMethodInjector(Method method, ObjectName on)
     {
        this.method = method;
        this.on = on;
        method.setAccessible(true);
     }
  
     public void inject(BeanContext ctx)
     {
        Object instance = ctx.getInstance();
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        Class clazz = method.getParameterTypes()[0];
        Object value = null;
  
        if (clazz == ObjectName.class)
        {
           value = on;
        }
        else
        {
           MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
           value = MBeanProxyExt.create(clazz, on, server);
        }
  
        try
        {
           method.invoke(instance, value);
        }
        catch (InvocationTargetException e)
        {
           throw new RuntimeException(e);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);
        }
     }
  
     public Class getInjectionClass()
     {
        return method.getParameterTypes()[0];
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EJBContextFieldInjector.java
  
  Index: EJBContextFieldInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import org.jboss.ejb3.BeanContext;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EJBContextFieldInjector implements Injector, PojoInjector
  {
     private Field field;
  
     public EJBContextFieldInjector(Field field)
     {
        this.field = field;
        field.setAccessible(true);
     }
  
     public void inject(BeanContext ctx)
     {
        inject(ctx, ctx.getInstance());
     }
     
     public void inject(BeanContext ctx, Object instance)
     {
        try
        {
           field.set(instance, ctx.getEJBContext());
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);
        }
     }
  
     public void inject(Object instance)
     {
        throw new RuntimeException("Illegal operation");
     }
  
     public Class getInjectionClass()
     {
        return field.getType();
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EJBContextMethodInjector.java
  
  Index: EJBContextMethodInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import org.jboss.ejb3.BeanContext;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EJBContextMethodInjector implements Injector, PojoInjector
  {
     private Method setMethod;
  
     public EJBContextMethodInjector(Method setMethod)
     {
        this.setMethod = setMethod;
        setMethod.setAccessible(true);
     }
  
     public void inject(BeanContext ctx)
     {
        inject(ctx, ctx.getInstance());
     }
     
     public void inject(BeanContext ctx, Object instance)
     {
  
        Object[] args = {ctx.getEJBContext()};
        try
        {
           setMethod.invoke(instance, args);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter method: " + setMethod.toString());
        }
        catch (InvocationTargetException e)
        {
           throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
        }
     }
  
     public void inject(Object instance)
     {
        throw new RuntimeException("Illegal operation");
     }
  
    
     public Class getInjectionClass()
     {
        return setMethod.getParameterTypes()[0];
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EJBHandler.java
  
  Index: EJBHandler.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import org.jboss.annotation.IgnoreDependency;
  import org.jboss.ejb3.EJBContainer;
  import org.jboss.logging.Logger;
  import org.jboss.metamodel.descriptor.BaseEjbRef;
  import org.jboss.metamodel.descriptor.EjbLocalRef;
  import org.jboss.metamodel.descriptor.EjbRef;
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  
  import javax.ejb.EJB;
  import javax.ejb.EJBs;
  import javax.naming.NameNotFoundException;
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Collection;
  import java.util.Map;
  
  /**
   * Searches bean class for all @Inject and create Injectors
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EJBHandler implements InjectionHandler
  {
     private static final Logger log = Logger.getLogger(EJBHandler.class);
  
     protected void addDependency(String refName, EJBContainer refcon, InjectionContainer container)
     {
        container.getDependencyPolicy().addDependency(refcon.getObjectName().getCanonicalName());
     }
  
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
     {
        if (xml != null)
        {
           if (xml.getEjbLocalRefs() != null) loadEjbLocalXml(xml.getEjbLocalRefs(), container);
           if (xml.getEjbRefs() != null) loadEjbRefXml(xml.getEjbRefs(), container);
        }
     }
  
     protected void loadEjbLocalXml(Collection<EjbLocalRef> refs, InjectionContainer container)
     {
        for (EjbLocalRef ref : refs)
        {
           String interfaceName = ref.getLocal();
           String errorType = "<ejb-local-ref>";
  
           ejbRefXml(ref, interfaceName, container, errorType);
        }
     }
  
     protected void loadEjbRefXml(Collection<EjbRef> refs, InjectionContainer container)
     {
        for (EjbRef ref : refs)
        {
           String interfaceName = ref.getRemote();
           String errorType = "<ejb-ref>";
  
           ejbRefXml(ref, interfaceName, container, errorType);
        }
     }
  
     protected void ejbRefXml(BaseEjbRef ref, String interfaceName, InjectionContainer container, 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
  
        ejbRefEncInjector(mappedName, encName, refClass, link, errorType, container);
  
        // handle dependencies
  
  
        if (ref.isIgnoreDependency())
        {
           log.debug("IGNORING <ejb-ref> DEPENDENCY: " + encName);
           return;
        }
        ejbRefDependency(link, container, refClass, errorType, encName);
     }
  
     protected void ejbRefDependency(String link, InjectionContainer container, Class refClass, String errorType, String encName)
     {
        EJBContainer refcon = null;
  
        if (refClass != null && (refClass.equals(Object.class) || refClass.equals(void.class))) refClass = null;
  
        if (refClass != null)
        {
           if (link != null)
           {
              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.debug(msg);
              }
           }
           else
           {
              try
              {
                 refcon = (EJBContainer) container.resolveEjbContainer(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();
                    log.debug(msg);
                 }
              }
              catch (NameNotFoundException e)
              {
                 String msg = "IGNORING DEPENDENCY: unable to find " + errorType + " of interface " + refClass.getName() + " and ejbLink of " + link + " in " + container.getDeploymentDescriptorType() + " of " + container.getIdentifier() + e.getMessage();
                 log.debug(msg);
              }
           }
        }
        else
        {
           String msg = "IGNORING DEPENDENCY: unable to resolve EJB";
           log.debug(msg);
        }
  
        if (refcon != null)
        {
           addDependency(encName, refcon, container);
        }
     }
  
     protected void ejbRefEncInjector(String mappedName, String encName, Class refClass, String link, 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);
     }
  
     public static EJBContainer getEjbContainer(EJB ref, InjectionContainer container, 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 String getJndiName(EJB ref, InjectionContainer container, 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;
     }
  
     public void handleClassAnnotations(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);
     }
  
     protected void handleClassAnnotation(EJB ejb, 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
  
        ejbRefDependency(ejb.beanName(), container, ejb.beanInterface(), "@EJB", encName);
     }
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> 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 (!method.isAnnotationPresent(IgnoreDependency.class)) ejbRefDependency(ref.beanName(), container, method.getParameterTypes()[0], "@EJB", encName);
           }
  
           injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
        }
     }
  
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> 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 (!field.isAnnotationPresent(IgnoreDependency.class)) 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()));
  
        }
     }
  
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EjbEncInjector.java
  
  Index: EjbEncInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.naming.Util;
  
  import javax.naming.NameNotFoundException;
  import javax.naming.LinkRef;
  import javax.naming.NamingException;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EjbEncInjector implements EncInjector
  {
     private String ejbLink;
     private Class refClass;
     private String jndiName;
     private String error;
     private String encName;
  
     public EjbEncInjector(String name, String mappedName, String error)
     {
        this.jndiName = mappedName;
        this.error = error;
        this.encName = name;
     }
  
     public EjbEncInjector(String name, Class refClass, String ejbLink, String error)
     {
        this.refClass = refClass;
        this.ejbLink = ejbLink;
        this.error = error;
        this.encName = name;
     }
  
  
     public void inject(InjectionContainer container)
     {
        if (jndiName == null || jndiName.equals(""))
        {
           if (ejbLink != null && !"".equals(ejbLink))
           {
              jndiName = container.getEjbJndiName(ejbLink, refClass);
           }
           else
           {
              try
              {
                 jndiName = container.getEjbJndiName(refClass);
              }
              catch (NameNotFoundException e)
              {
                 throw new RuntimeException("could not resolve global JNDI name for " + error + " for container " + container.getIdentifier() + ": reference class: " + refClass.getName() + " ejbLink: " + ejbLink + " " + e.getMessage());
              }
           }
        }
        try
        {
           Util.bind(container.getEnc(), encName, new LinkRef(jndiName));
        }
        catch (NamingException e)
        {
           throw new RuntimeException("could not bind enc name '" +  encName + "' for " + error + " for container " + container.getIdentifier() + ": reference class: " + refClass.getName() + " ejbLink: " + ejbLink + " " + e.getMessage());
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EncInjector.java
  
  Index: EncInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  /**
   * Responsible for populating ENC
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public interface EncInjector
  {
     void inject(InjectionContainer container);
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EntityManagerFactoryFieldInjector.java
  
  Index: EntityManagerFactoryFieldInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.logging.Logger;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EntityManagerFactoryFieldInjector implements Injector, PojoInjector
  {
     private static final Logger log = Logger.getLogger(EntityManagerFactoryFieldInjector.class);
     private Field field;
     private Object factory;
  
     public EntityManagerFactoryFieldInjector(Field field, Object factory)
     {
        this.field = field;
        this.field.setAccessible(true);
        this.factory = factory;
     }
  
     public void inject(BeanContext ctx)
     {
        inject(ctx, ctx.getInstance());
     }
     
     public void inject(BeanContext ctx, Object instance)
     {
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        try
        {
           field.set(instance, factory);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter field: " + field.toString());
        }
     }
  
     public Class getInjectionClass()
     {
        return field.getType();
     }
  
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EntityManagerFactoryMethodInjector.java
  
  Index: EntityManagerFactoryMethodInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.logging.Logger;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EntityManagerFactoryMethodInjector implements Injector, PojoInjector
  {
     private static final Logger log = Logger.getLogger(EntityManagerFactoryMethodInjector.class);
     private Method setMethod;
     private Object factory;
  
     public EntityManagerFactoryMethodInjector(Method setMethod, Object factory)
     {
        this.setMethod = setMethod;
        setMethod.setAccessible(true);
        this.factory = factory;
     }
  
     public void inject(BeanContext ctx)
     {
        inject(ctx, ctx.getInstance());
     }
     
     public void inject(BeanContext ctx, Object instance)
     {
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        try
        {
           Object[] args = {factory};
           setMethod.invoke(instance, args);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter method: " + setMethod.toString());
        }
        catch (InvocationTargetException e)
        {
           throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
        }
     }
  
     public Class getInjectionClass()
     {
        return setMethod.getParameterTypes()[0];
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/EnvEntryEncInjector.java
  
  Index: EnvEntryEncInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.logging.Logger;
  import org.jboss.naming.Util;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class EnvEntryEncInjector implements EncInjector
  {
     private static final Logger log = Logger.getLogger(EnvEntryEncInjector.class);
  
     private String name;
     private String entryType;
     private String value;
  
  
     public EnvEntryEncInjector(String encName, String entryType, String value)
     {
        this.name = encName;
        this.entryType = entryType;
        this.value = value;
     }
  
     public void inject(InjectionContainer container)
     {
        try
        {
           Util.bind(container.getEnc(),
                   name,
                   getEnvEntryValue());
        }
        catch (Exception e)
        {
           throw new RuntimeException("Invalid <env-entry> name: " + name, e);
        }
     }
  
  
     protected Object getEnvEntryValue() throws ClassNotFoundException
     {
        Class type = Thread.currentThread().getContextClassLoader().loadClass(entryType);
        if (type == String.class)
        {
           return value;
        }
        else if (type == Integer.class)
        {
           return new Integer(value);
        }
        else if (type == Long.class)
        {
           return new Long(value);
        }
        else if (type == Double.class)
        {
           return new Double(value);
        }
        else if (type == Float.class)
        {
           return new Float(value);
        }
        else if (type == Byte.class)
        {
           return new Byte(value);
        }
        else if (type == Character.class)
        {
           String input = value;
           if (input == null || input.length() == 0)
           {
              return new Character((char) 0);
           }
           else
           {
              if (input.length() > 1)
                 // TODO: Add deployment context
                 log.warn("Warning character env-entry is too long: binding="
                         + name + " value=" + input);
              return new Character(input.charAt(0));
           }
        }
        else if (type == Short.class)
        {
           return new Short(value);
        }
        else if (type == Boolean.class)
        {
           return new Boolean(value);
        }
        else
        {
           return value;
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/ExtendedPersistenceContextInjector.java
  
  Index: ExtendedPersistenceContextInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import javax.persistence.EntityManager;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
  import org.jboss.ejb3.stateful.StatefulBeanContext;
  import org.jboss.logging.Logger;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class ExtendedPersistenceContextInjector implements Injector, PojoInjector
  {
     protected static final Logger log = Logger.getLogger(ExtendedPersistenceContextInjector.class);
     protected ManagedEntityManagerFactory factory;
  
     protected ExtendedPersistenceContextInjector(ManagedEntityManagerFactory factory)
     {
        this.factory = factory;
     }
  
     public void inject(BeanContext ctx)
     {
        inject(ctx, ctx.getInstance());
     }
  
     public void inject(BeanContext beanContext, Object instance)
     {
        StatefulBeanContext ctx = (StatefulBeanContext)beanContext;
        EntityManager pc = ctx.getExtendedPersistenceContext(factory.getKernelName());
        if (pc == null)
        {
           pc = factory.createEntityManager();
           ctx.addExtendedPersistenceContext(factory.getKernelName(), pc);
        }
     }
  
     public void inject(Object instance)
     {
        throw new RuntimeException("Illegal operation");
     }
  
     public Class getInjectionClass()
     {
        return null;
     }
  
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/InjectionContainer.java
  
  Index: InjectionContainer.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.ejb3.Container;
  import org.jboss.ejb3.DependencyPolicy;
  import org.jboss.ejb3.entity.PersistenceUnitDeployment;
  
  import javax.naming.Context;
  import javax.naming.NameNotFoundException;
  import java.util.Map;
  import java.lang.reflect.Method;
  import java.lang.reflect.Field;
  import java.lang.reflect.AccessibleObject;
  
  /**
   * This is the container that manages all injections.  Could be an EJB Container
   * or a WAR.
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public interface InjectionContainer
  {
     /**
      * Some identifier that can be used in error messages
      *
      * @return
      */
     String getIdentifier();
  
     /**
      * For error messages
      *
      * @return  ejb-jar.xml, web.xml, etc..
      */
     String getDeploymentDescriptorType();
  
     ClassLoader getClassloader();
  
     Map<String, String> getEncLinkRefEntries();
  
     boolean hasEnvEntry(String name);
     void addEnvEntry(String name, String type, String value) throws ClassNotFoundException;
     boolean hasEncEntry(String name);
     //-----------------------
  
     Map<String, EncInjector> getEncInjectors();
     Map <String, Map<AccessibleObject, Injector>> getEncInjections();
  
     Context getEnc();
     Context getEncEnv();
  
  
     PersistenceUnitDeployment getPersistenceUnitDeployment(String unitName) throws NameNotFoundException;
     Map getExtendedPCs();
  
  
     Container resolveEjbContainer(String link, Class businessIntf);
     Container resolveEjbContainer(Class businessIntf) throws NameNotFoundException;
     String getEjbJndiName(Class businessInterface) throws NameNotFoundException;
     String getEjbJndiName(String link, Class businessInterface);
  
     /**
      * If class has container overridable annotations, this method will
      * discover those overriden annotations.
      *
      * @param annotationType
      * @param clazz
      * @return
      */
     <T> T getAnnotation(Class<T> annotationType, Class clazz);
  
     /**
      * If class has container overridable annotations, this method will
      * discover those overriden annotations.
  
      * @param annotationType
      * @param clazz
      * @param method
      * @return
      */
     <T> T getAnnotation(Class<T> annotationType, Class clazz, Method method);
  
     /**
      * If class has container overridable annotations, this method will
      * discover those overriden annotations.
  
      * @param annotationType
      * @param clazz
      * @param field
      * @return
      */
     <T> T getAnnotation(Class<T> annotationType, Class clazz, Field field);
  
     DependencyPolicy getDependencyPolicy();
  
  
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/InjectionHandler.java
  
  Index: InjectionHandler.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Map;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public interface InjectionHandler
  {
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container);
     public void handleClassAnnotations(Class clazz, InjectionContainer container);
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors);
     public void handleFieldAnnotations(Field Field, InjectionContainer container, Map<AccessibleObject, Injector> injectors);
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/InjectionUtil.java
  
  Index: InjectionUtil.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.Modifier;
  import java.util.HashSet;
  import java.util.Map;
  import java.util.HashMap;
  
  import org.jboss.ejb3.EJBContainer;
  import org.jboss.logging.Logger;
  
  import org.jboss.metamodel.descriptor.InjectionTarget;
  import org.jboss.metamodel.descriptor.Ref;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class InjectionUtil
  {
     private static final Logger log = Logger
     .getLogger(InjectionUtil.class);
  
  
     /**
      * This method will take a set of XML loaded injectors and collapse them based on spec inheritance rules
      * It will remove injectors that should not be used in the injection of the base component class.
      *
      * @param visitedMethods
      * @param clazz
      * @param xmlDefinedInjectors
      * @param classInjectors
      */
     public static void collapseXmlMethodInjectors(HashSet<String> visitedMethods, Class clazz, Map<String, Map<AccessibleObject, Injector>> xmlDefinedInjectors, Map<AccessibleObject, Injector> classInjectors)
     {
        if (clazz == null || clazz.equals(Object.class))
        {
           return;
        }
        Map<AccessibleObject, Injector> xmlInjectors = xmlDefinedInjectors.get(clazz.getName());
        Method[] methods = clazz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++)
        {
           if (methods[i].getParameterTypes().length != 1) continue;
  
           if (!Modifier.isPrivate(methods[i].getModifiers()))
           {
              if (visitedMethods.contains(methods[i].getName()))
              {
                 xmlInjectors.remove(methods[i]); // if not private then it has been overriden
                 continue;
              }
              visitedMethods.add(methods[i].getName());
           }
        }
        classInjectors.putAll(xmlInjectors);
        // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
        collapseXmlMethodInjectors(visitedMethods, clazz.getSuperclass(), xmlDefinedInjectors, classInjectors);
     }
  
  
     public static AccessibleObject findInjectionTarget(ClassLoader loader, InjectionTarget target)
     {
        Class clazz = null;
        try
        {
           clazz = loader.loadClass(target.getTargetClass());
        }
        catch (ClassNotFoundException e)
        {
           throw new RuntimeException("<injection-target> class: " + target.getTargetClass() + " was not found nin deployment");
        }
  
        for (Field field : clazz.getDeclaredFields())
        {
           if (target.getTargetName().equals(field.getName())) return field;
        }
  
        for (java.lang.reflect.Method method : clazz.getDeclaredMethods())
        {
           if (method.getName().equals(target.getTargetName())) return method;
        }
  
        throw new RuntimeException("<injection-target> could not be found: " + target.getTargetClass() + "." + target.getTargetName());
  
     }
  
     public static String getEncName(Method method)
     {
        String encName = method.getName().substring(3);
        if (encName.length() > 1)
        {
           encName = encName.substring(0, 1).toLowerCase() + encName.substring(1);
        }
        else
        {
           encName = encName.toLowerCase();
        }
  
        encName = "env/" + method.getDeclaringClass().getName() + "/" + encName;
        return encName;
     }
  
     public static String getEncName(Field field)
     {
        return "env/" + field.getDeclaringClass().getName() + "/" + field.getName();
     }
  
     public static Object getAnnotation(Class annotation, EJBContainer container, Class annotatedClass, boolean isContainer)
     {
        if (isContainer)
        {
           return container.resolveAnnotation(annotation);
        }
        else
        {
           return annotatedClass.getAnnotation(annotation);
        }
     }
  
     public static Object getAnnotation(Class annotation, EJBContainer container, Method method, boolean isContainer)
     {
        if (isContainer)
        {
           return container.resolveAnnotation(method, annotation);
        }
        else
        {
           return method.getAnnotation(annotation);
        }
     }
  
     public static Object getAnnotation(Class annotation, EJBContainer container, Field field, boolean isContainer)
     {
        if (isContainer)
        {
           return container.resolveAnnotation(field, annotation);
        }
        else
        {
           return field.getAnnotation(annotation);
        }
     }
  
     public static Class injectionTarget(String encName, Ref ref, InjectionContainer container, Map<String, Map<AccessibleObject, Injector>> classInjectors)
     {
        if (ref.getInjectionTarget() != null)
        {
           Class injectionType;
           // todo, get injection target class
           AccessibleObject ao = findInjectionTarget(container.getClassloader(), ref.getInjectionTarget());
           Map<AccessibleObject, Injector> injectors = classInjectors.get(ref.getInjectionTarget().getTargetClass());
           if (injectors == null)
           {
              injectors = new HashMap<AccessibleObject, Injector>();
              classInjectors.put(ref.getInjectionTarget().getTargetClass().trim(), injectors);
           }
           if (ao instanceof Field)
           {
              injectionType = ((Field) ao).getType();
              injectors.put(ao, new JndiFieldInjector((Field) ao, encName, container.getEnc()));
           }
           else
           {
              injectionType = ((Method) ao).getParameterTypes()[0];
              injectors.put(ao, new JndiMethodInjector((Method) ao, encName, container.getEnc()));
           }
           return injectionType;
        }
        else
        {
           return null;
        }
  
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/Injector.java
  
  Index: Injector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import org.jboss.ejb3.BeanContext;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public interface Injector
  {
     void inject(BeanContext ctx);
  
     void inject(Object instance);
     
     Class getInjectionClass();
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/JndiFieldInjector.java
  
  Index: JndiFieldInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import javax.naming.Context;
  import javax.naming.NamingException;
  
  import org.jboss.logging.Logger;
  
  import org.jboss.ejb3.BeanContext;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class JndiFieldInjector implements Injector, PojoInjector
  {
     private static final Logger log = Logger.getLogger(JndiFieldInjector.class);
     
     private Field field;
     private String jndiName;
     private Context ctx;
  
     public JndiFieldInjector(Field field, String jndiName, Context ctx)
     {
        this.field = field;
        this.field.setAccessible(true);
        this.jndiName = jndiName;
        this.ctx = ctx;
     }
  
     public JndiFieldInjector(Field field, Context ctx)
     {
        this(field, field.getName(), ctx);
     }
  
     public void inject(BeanContext bctx)
     {
        inject(bctx, bctx.getInstance());
     }
  
     public Class getInjectionClass()
     {
        return field.getType();
     }
  
     public Field getField()
     {
        return field;
     }
  
     protected Object lookup(String jndiName, Class field)
     {
        Object dependency = null;
  
        try
        {
           dependency = ctx.lookup(jndiName);
  
           if (dependency instanceof javax.xml.rpc.Service && !field.isAssignableFrom(javax.xml.rpc.Service.class))
           {
              javax.xml.rpc.Service service = (javax.xml.rpc.Service)dependency;
              dependency = service.getPort(field);
           }
        }
        catch (NamingException e)
        {
           e.printStackTrace();
           throw new RuntimeException("Unable to inject jndi dependency: " + jndiName + " into field " + field, e);
        }
        catch (javax.xml.rpc.ServiceException e)
        {
           e.printStackTrace();
           throw new RuntimeException("Unable to inject jndi webservice dependency: " + jndiName + " into field " + field, e);
        }
        
        return dependency;
     }
     
     public void inject(BeanContext bctx, Object instance)
     {
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        Object dependency = lookup(jndiName, field.getType());
  
        try
        {
           field.set(instance, dependency);
        }
        catch (IllegalArgumentException e)
        {
           String type = "UNKNOWN";
           String interfaces = "";
           if (dependency != null)
           {
              type = dependency.getClass().getName();
              Class[] intfs = dependency.getClass().getInterfaces();
              for (Class intf : intfs) interfaces += ", " + intf.getName();
           }
           throw new RuntimeException("Non matching type for inject of field: " + field + " for type: " + type + " of jndiName " + jndiName + "\nintfs: " + interfaces, e);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/JndiInjectHandler.java
  
  Index: JndiInjectHandler.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import org.jboss.annotation.JndiInject;
  import org.jboss.logging.Logger;
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  import org.jboss.metamodel.descriptor.JndiRef;
  
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Map;
  
  /**
   * Searches bean class for all @Inject and create Injectors
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class JndiInjectHandler implements InjectionHandler
  {
     private static final Logger log = Logger.getLogger(JndiInjectHandler.class);
     
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
     {
        if (xml.getJndiRefs() == null) return;
        for (JndiRef ref : xml.getJndiRefs())
        {
           if (ref.getMappedName() == null || ref.getMappedName().equals(""))
              throw new RuntimeException("mapped-name is required for " + ref.getJndiRefName() + " of container " + container.getIdentifier());
  
           String encName = "env/" + ref.getJndiRefName();
           if (!container.getEncInjectors().containsKey(encName))
           {
              container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.getMappedName(), "jndi ref"));
           }
           InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
        }
     }
  
     public void handleClassAnnotations(Class clazz, InjectionContainer container)
     {
        // complete
     }
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        JndiInject ref = method.getAnnotation(JndiInject.class);
        if (ref != null)
        {
           if (!method.getName().startsWith("set"))
              throw new RuntimeException("@EJB can only be used with a set method: " + method);
           String encName = InjectionUtil.getEncName(method);
           if (!container.getEncInjectors().containsKey(encName))
           {
              container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.jndiName(), "@JndiInject"));
           }
           injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
        }
     }
     
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        JndiInject ref = field.getAnnotation(JndiInject.class);
        if (ref != null)
        {
           String encName = InjectionUtil.getEncName(field);
           if (!container.getEncInjectors().containsKey(encName))
           {
              container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.jndiName(), "@JndiInject"));
           }
           injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/JndiMethodInjector.java
  
  Index: JndiMethodInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import javax.naming.Context;
  import javax.naming.NamingException;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.logging.Logger;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class JndiMethodInjector implements Injector, PojoInjector
  {
     private static final Logger log = Logger.getLogger(JndiMethodInjector.class);
     
     private Method setMethod;
     private String jndiName;
     private Context ctx;
  
     public JndiMethodInjector(Method setMethod, String jndiName, Context ctx)
     {
        this.setMethod = setMethod;
        setMethod.setAccessible(true);
        this.jndiName = jndiName;
        this.ctx = ctx;
     }
  
     public void inject(BeanContext bctx)
     {
        inject(bctx, bctx.getInstance());
     }
     
     public Class getInjectionClass()
     {
        return setMethod.getParameterTypes()[0];
     }
     
     protected Object lookup(String jndiName, Class param)
     {
        Object dependency = null;
        
        try
        {
           dependency = ctx.lookup(jndiName);
              
           if (dependency instanceof javax.xml.rpc.Service && !param.isAssignableFrom(javax.xml.rpc.Service.class))
           {
              javax.xml.rpc.Service service = (javax.xml.rpc.Service)dependency;
              dependency = service.getPort(param);
           }
        }
        catch (NamingException e)
        {
           e.printStackTrace();
           throw new RuntimeException("Unable to @Inject jndi dependency: " + jndiName + " into method " + setMethod, e);
        }
        catch (javax.xml.rpc.ServiceException e)
        {
           e.printStackTrace();
           throw new RuntimeException("Unable to @Inject webservice jndi dependency: " + jndiName + " into method " + setMethod, e);
        }
        
        return dependency;
     }
     
     public void inject(BeanContext bctx, Object instance)
     {
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        Object dependency = lookup(jndiName, setMethod.getParameterTypes()[0]);
  
        Object[] args = {dependency};
        try
        {
           setMethod.invoke(instance, args);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           String type = "UNKNOWN";
           if (dependency != null) type = dependency.getClass().getName();
           throw new RuntimeException("Non matching type for @Inject of setter: " + setMethod + " for type: " + type, e);  //To change body of catch statement use Options | File Templates.
        }
        catch (InvocationTargetException e)
        {
           throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/LinkRefEncInjector.java
  
  Index: LinkRefEncInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.naming.Util;
  
  import javax.naming.LinkRef;
  import javax.naming.NamingException;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class LinkRefEncInjector implements EncInjector
  {
     private String jndiName;
     private String error;
     private String encName;
  
     public LinkRefEncInjector(String name, String mappedName, String error)
     {
        this.jndiName = mappedName;
        this.error = error;
        this.encName = name;
     }
  
     public void inject(InjectionContainer container)
     {
        try
        {
           Util.bind(container.getEnc(), encName, new LinkRef(jndiName));
        }
        catch (NamingException e)
        {
           throw new RuntimeException(new StringBuilder().append("could not bind enc name '").append(encName).append("' for ").append(error).append(" for container ").append(container.getIdentifier()).append(e.getMessage()).toString());
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/PcEncInjector.java
  
  Index: PcEncInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
  import org.jboss.ejb3.entity.ExtendedEntityManager;
  import org.jboss.ejb3.entity.ExtendedHibernateSession;
  import org.jboss.ejb3.entity.TransactionScopedEntityManager;
  import org.jboss.ejb3.entity.TransactionScopedHibernateSession;
  import org.jboss.ejb3.stateful.StatefulContainer;
  import org.jboss.naming.Util;
  
  import javax.persistence.PersistenceContextType;
  import javax.persistence.EntityManager;
  import javax.naming.NameNotFoundException;
  import javax.naming.NamingException;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class PcEncInjector implements EncInjector
  {
     private String encName;
     private String unitName;
     private PersistenceContextType type;
     private Class injectionType;
     private String error;
  
     public PcEncInjector(String encName, String unitName, PersistenceContextType type, Class injectionType, String error)
     {
        this.encName = encName;
        this.unitName = unitName;
        this.type = type;
        this.injectionType = injectionType;
        this.error = error;
     }
  
     public void inject(InjectionContainer container)
     {
        String error1 = error;
        ManagedEntityManagerFactory factory = null;
        try
        {
           factory = PersistenceUnitHandler.getManagedEntityManagerFactory(
                   container, unitName);
        }
        catch (NameNotFoundException e)
        {
           error1 += " " + e.getMessage();
        }
        if (factory == null)
        {
           throw new RuntimeException(error1);
        }
        if (type == PersistenceContextType.EXTENDED)
        {
           if (!(container instanceof StatefulContainer))
              throw new RuntimeException("It is illegal to inject an EXTENDED PC into something other than a SFSB");
           container.getExtendedPCs().put(factory.getKernelName(),
                   new ExtendedPersistenceContextInjector(factory));
           Object extendedPc = null;
           if (injectionType == null
                   || injectionType.getName().equals(EntityManager.class.getName()))
           {
              extendedPc = new ExtendedEntityManager(factory.getKernelName());
           }
           else
           {
              extendedPc = new ExtendedHibernateSession(factory.getKernelName());
           }
           try
           {
              Util.bind(container.getEnc(), encName, extendedPc);
           }
           catch (NamingException e)
           {
              throw new RuntimeException(error1, e);
           }
        }
        else
        {
           Object entityManager = null;
           if (injectionType == null
                   || injectionType.getName().equals(EntityManager.class.getName()))
           {
              entityManager = new TransactionScopedEntityManager(factory);
           }
           else
           {
              entityManager = new TransactionScopedHibernateSession(factory);
           }
           try
           {
              Util.bind(container.getEnc(), encName, entityManager);
           }
           catch (NamingException e)
           {
              throw new RuntimeException(error1, e);
           }
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/PersistenceContextHandler.java
  
  Index: PersistenceContextHandler.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2005, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  package org.jboss.injection;
  
  import org.jboss.metamodel.descriptor.PersistenceContextRef;
  import org.jboss.logging.Logger;
  import org.jboss.annotation.IgnoreDependency;
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  
  import javax.naming.NameNotFoundException;
  import javax.persistence.PersistenceContext;
  import javax.persistence.PersistenceContextType;
  import javax.persistence.PersistenceContexts;
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Map;
  
  /**
   * Searches bean class for all
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   * @Inject and create Injectors
   */
  public class PersistenceContextHandler implements InjectionHandler
  {
     private static final Logger log = Logger
             .getLogger(PersistenceContextHandler.class);
  
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
     {
        if (xml.getPersistenceContextRefs() == null) return;
        for (PersistenceContextRef ref : xml.getPersistenceContextRefs())
        {
           String encName = "env/" + ref.getRefName();
           // we add injection target no matter what.  enc injection might be overridden but
           // XML injection cannot be overriden
           Class injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
  
           if (container.getEncInjectors().containsKey(encName))
              continue;
           // add it to list of
           String error = "unable to load <persistence-context-ref> for unitName: "
                   + ref.getUnitName() + " <ref-name>: " + ref.getRefName();
           PersistenceContextType type = ref.getPersistenceContextType();
           String unitName = ref.getUnitName();
           container.getEncInjectors().put(encName, new PcEncInjector(encName, unitName, type, injectionType, error));
           try
           {
              PersistenceUnitHandler.addPUDependency(ref.getUnitName(), container);
           }
           catch (NameNotFoundException e)
           {
              throw new RuntimeException("Illegal <persistence-context-ref> of " + ref.getRefName() + " :" + e.getMessage());
           }
        }
     }
  
     public void handleClassAnnotations(Class clazz, InjectionContainer container)
     {
        PersistenceContexts resources = container.getAnnotation(PersistenceContexts.class, clazz);
        if (resources != null)
        {
           for (PersistenceContext ref : resources.value())
           {
              loadPersistenceContextClassAnnotation(ref, container, clazz);
           }
        }
        PersistenceContext pc = container.getAnnotation(PersistenceContext.class, clazz);
  
        if (pc != null)
        {
           loadPersistenceContextClassAnnotation(pc, container, clazz);
        }
  
     }
  
     private static void loadPersistenceContextClassAnnotation(
             PersistenceContext ref, InjectionContainer container, Class clazz)
     {
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           throw new RuntimeException(
                   "JBoss requires name() for class level @PersistenceContext");
        }
        encName = "env/" + ref.name();
        if (container.getEncInjectors().containsKey(encName)) return;
  
        String error = "Unable to load class-level @PersistenceContext("
                + ref.unitName() + ") on " + container.getIdentifier();
        container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), null, error));
        try
        {
           PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
        }
        catch (NameNotFoundException e)
        {
           throw new RuntimeException("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
        }
     }
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        PersistenceContext ref = method.getAnnotation(PersistenceContext.class);
        if (ref == null) return;
        if (!method.getName().startsWith("set"))
           throw new RuntimeException("@PersistenceUnit 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/" + ref.name();
        }
        if (!container.getEncInjectors().containsKey(encName))
        {
           try
           {
              if (!method.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
           }
           catch (NameNotFoundException e)
           {
              throw new RuntimeException("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
           }
           String error = "@PersistenceContext(name='" + encName
                   + "',unitName='" + ref.unitName() + "') on EJB: "
                   + container.getIdentifier() + " failed to inject on method "
                   + method.toString();
           container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), method.getParameterTypes()[0], error));
        }
        injectors.put(method, new JndiMethodInjector(method,
                encName, container.getEnc()));
     }
  
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        PersistenceContext ref = field.getAnnotation(PersistenceContext.class);
        if (ref == null) return;
  
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           encName = InjectionUtil.getEncName(field);
        }
        else
        {
           encName = "env/" + ref.name();
        }
        if (!container.getEncInjectors().containsKey(encName))
        {
           try
           {
              if (!field.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
           }
           catch (NameNotFoundException e)
           {
              throw new RuntimeException("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
           }
           String error = "@PersistenceContext(name='" + encName
                   + "',unitName='" + ref.unitName() + "') on EJB: "
                   + container.getIdentifier() + " failed to inject on field "
                   + field.toString();
           container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), field.getType(), error));
        }
        injectors.put(field, new JndiFieldInjector(field,
                encName, container.getEnc()));
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/PersistenceUnitHandler.java
  
  Index: PersistenceUnitHandler.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import org.hibernate.SessionFactory;
  import org.jboss.annotation.IgnoreDependency;
  import org.jboss.ejb3.entity.InjectedEntityManagerFactory;
  import org.jboss.ejb3.entity.InjectedSessionFactory;
  import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
  import org.jboss.ejb3.entity.PersistenceUnitDeployment;
  import org.jboss.logging.Logger;
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  import org.jboss.metamodel.descriptor.PersistenceUnitRef;
  
  import javax.naming.NameNotFoundException;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.PersistenceUnit;
  import javax.persistence.PersistenceUnits;
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Map;
  
  /**
   * Searches bean class for all @Inject and create Injectors
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class PersistenceUnitHandler implements InjectionHandler
  {
     private static final Logger log = Logger
             .getLogger(PersistenceContextHandler.class);
  
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
     {
        if (xml.getPersistenceUnitRefs() == null) return;
  
        for (PersistenceUnitRef ref : xml.getPersistenceUnitRefs())
        {
           String encName = "env/" + ref.getRefName();
           // we add injection target no matter what.  enc injection might be overridden but
           // XML injection cannot be overriden
           Class injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
           if (container.getEncInjectors().containsKey(encName))
              return;
           container.getEncInjectors().put(encName, new PuEncInjector(encName, injectionType, ref.getUnitName(), "<persistence-unit-ref>"));
           try
           {
              addPUDependency(ref.getUnitName(), container);
           }
           catch (NameNotFoundException e)
           {
              throw new RuntimeException("Illegal <persistence-unit-ref> of " + ref.getRefName() + " :" + e.getMessage());
           }
        }
     }
  
  
     public void handleClassAnnotations(Class clazz, InjectionContainer container)
     {
        PersistenceUnits resources = container.getAnnotation(
                PersistenceUnits.class, clazz);
        if (resources != null)
        {
           for (PersistenceUnit ref : resources.value())
           {
              handleClassAnnotation(ref, container, clazz);
           }
        }
        PersistenceUnit pu = container.getAnnotation(PersistenceUnit.class, clazz);
        if (pu != null)
        {
           handleClassAnnotation(pu, container, clazz);
        }
     }
  
     private static void handleClassAnnotation(PersistenceUnit ref, InjectionContainer container, Class clazz)
     {
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           throw new RuntimeException("JBoss requires name() for class level @PersistenceUnit");
        }
        encName = "env/" + encName;
        if (container.getEncInjectors().containsKey(encName)) return;
        container.getEncInjectors().put(encName, new PuEncInjector(encName, null, ref.unitName(), "@PersistenceUnit"));
        try
        {
           addPUDependency(ref.unitName(), container);
        }
        catch (NameNotFoundException e)
        {
           throw new RuntimeException("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
        }
     }
  
     public static void addPUDependency(String unitName, InjectionContainer container) throws NameNotFoundException
     {
        PersistenceUnitDeployment deployment = null;
        // look in EAR first
        deployment = container.getPersistenceUnitDeployment(unitName);
        if (deployment != null)
        {
           container.getDependencyPolicy().addDependency(deployment.getKernelName());
           return;
        }
        // probably not deployed yet.
        // todo not sure if we should do this in JBoss 5
        container.getDependencyPolicy().addDependency(PersistenceUnitDeployment.getDefaultKernelName(unitName));
     }
  
     public static ManagedEntityManagerFactory getManagedEntityManagerFactory(InjectionContainer container, String unitName)
             throws NameNotFoundException
     {
        ManagedEntityManagerFactory factory;
        PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName);
        if (deployment != null)
        {
           factory = deployment.getManagedFactory();
        }
        else
        {
           throw new NameNotFoundException("Unable to find persistence unit: " + unitName + " for deployment: " + container.getIdentifier());
        }
        return factory;
     }
  
  
     public static EntityManagerFactory getEntityManagerFactory(PersistenceUnit ref, InjectionContainer container) throws NameNotFoundException
     {
        return getEntityManagerFactory(ref.unitName(), container);
     }
  
     public static Object getFactory(Class type, String unitName, InjectionContainer container) throws NameNotFoundException
     {
        if (type != null && type.getName().equals(SessionFactory.class.getName()))
           return getSessionFactory(unitName, container);
        return getEntityManagerFactory(unitName, container);
     }
  
     public static EntityManagerFactory getEntityManagerFactory(String unitName, InjectionContainer container) throws NameNotFoundException
     {
        ManagedEntityManagerFactory managedFactory;
        PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName);
        if (deployment != null)
        {
           managedFactory = deployment.getManagedFactory();
        }
        else
        {
           return null;
        }
        return new InjectedEntityManagerFactory(managedFactory);
     }
  
  
     private static SessionFactory getSessionFactory(String ref, InjectionContainer container) throws NameNotFoundException
     {
        ManagedEntityManagerFactory managedFactory;
        PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(ref);
        if (deployment != null)
        {
           managedFactory = deployment.getManagedFactory();
        }
        else
        {
           return null;
        }
        return new InjectedSessionFactory(managedFactory);
     }
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        PersistenceUnit ref = method.getAnnotation(PersistenceUnit.class);
        if (ref == null) return;
        if (!method.getName().startsWith("set"))
           throw new RuntimeException("@PersistenceUnit 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))
        {
           container.getEncInjectors().put(encName, new PuEncInjector(encName, method.getParameterTypes()[0], ref.unitName(), "@PersistenceUnit"));
           try
           {
              if (!method.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container);
           }
           catch (NameNotFoundException e)
           {
              throw new RuntimeException("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
           }
        }
  
        injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
     }
  
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        PersistenceUnit ref = field.getAnnotation(PersistenceUnit.class);
        if (ref == null) return;
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           encName = InjectionUtil.getEncName(field);
        }
        else
        {
           encName = "env/" + encName;
        }
        if (!container.getEncInjectors().containsKey(encName))
        {
           container.getEncInjectors().put(encName, new PuEncInjector(encName, field.getType(), ref.unitName(), "@PersistenceUnit"));
           try
           {
              if (!field.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container);
           }
           catch (NameNotFoundException e)
           {
              throw new RuntimeException("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
           }
        }
  
        injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/PojoInjector.java
  
  Index: PojoInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */ 
  package org.jboss.injection;
  
  import org.jboss.ejb3.BeanContext;
  
  /**
   * 
   * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
   * @version $Revision: 1.1 $
   */
  public interface PojoInjector
  {
     void inject(BeanContext ctx, Object instance);
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/PuEncInjector.java
  
  Index: PuEncInjector.java
  ===================================================================
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.injection;
  
  import org.jboss.naming.Util;
  
  import javax.naming.NameNotFoundException;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class PuEncInjector implements EncInjector
  {
     private String encName;
     private Class injectionType;
     private String unitName;
     private String error;
  
     public PuEncInjector(String encName, Class injectionType, String unitName, String error)
     {
        this.encName = encName;
        this.injectionType = injectionType;
        this.error = error;
        this.unitName = unitName;
     }
  
     public void inject(InjectionContainer container)
     {
        Object factory = null;
        try
        {
           factory = PersistenceUnitHandler.getFactory(injectionType, encName, container);
        }
        catch (NameNotFoundException e)
        {
           throw new RuntimeException(e);
        }
        if (factory == null)
        {
           throw new RuntimeException("Failed to locate " + error + " of unit name: " + unitName + " for " + container.getIdentifier());
        }
  
        try
        {
           Util.bind(container.getEnc(), encName, factory);
        }
        catch (Exception e)
        {
           throw new RuntimeException("Failed to bind " + error + " of unit name: " + unitName + " ref-name" + encName + " for container " + container.getIdentifier(), e);
        }
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/ResourceHandler.java
  
  Index: ResourceHandler.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import org.jboss.ejb3.Container;
  import org.jboss.logging.Logger;
  import org.jboss.metamodel.descriptor.EnvEntry;
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  import org.jboss.metamodel.descriptor.MessageDestinationRef;
  import org.jboss.metamodel.descriptor.ResourceEnvRef;
  import org.jboss.metamodel.descriptor.ResourceRef;
  
  import javax.annotation.Resource;
  import javax.annotation.Resources;
  import javax.ejb.EJBContext;
  import javax.ejb.TimerService;
  import javax.transaction.UserTransaction;
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Collection;
  import java.util.Map;
  
  /**
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class ResourceHandler implements InjectionHandler
  {
     private static final Logger log = Logger.getLogger(ResourceHandler.class);
  
     private static void loadEnvEntry(InjectionContainer container, Collection<EnvEntry> envEntries)
     {
        for (EnvEntry envEntry : envEntries)
        {
           String encName = "env/" + envEntry.getEnvEntryName();
           InjectionUtil.injectionTarget(encName, envEntry, container, container.getEncInjections());
           if (container.getEncInjectors().containsKey(encName)) continue;
           container.getEncInjectors().put(encName, new EnvEntryEncInjector(encName, envEntry.getEnvEntryType(), envEntry.getEnvEntryValue()));
        }
     }
  
     private static void loadXmlResourceRefs(InjectionContainer container, Collection<ResourceRef> refs)
     {
        for (ResourceRef envRef : refs)
        {
           String encName = "env/" + envRef.getResRefName();
           if (container.getEncInjectors().containsKey(encName)) continue;
           if (envRef.getMappedName() == null || envRef.getMappedName().equals(""))
           {
              throw new RuntimeException("mapped-name is required for " + envRef.getResRefName() + " of deployment " + container.getIdentifier());
           }
           container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, envRef.getMappedName(), "<resource-ref>"));
           InjectionUtil.injectionTarget(encName, envRef, container, container.getEncInjections());
        }
     }
  
     private static void loadXmlResourceEnvRefs(InjectionContainer container, Collection<ResourceEnvRef> refs)
     {
        for (ResourceEnvRef envRef : refs)
        {
           String encName = "env/" + envRef.getResRefName();
           if (container.getEncInjectors().containsKey(encName)) continue;
           if (envRef.getMappedName() == null || envRef.getMappedName().equals(""))
           {
              throw new RuntimeException("mapped-name is required for " + envRef.getResRefName() + " of deployment " + container.getIdentifier());
           }
           container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, envRef.getMappedName(), "<resource-ref>"));
           InjectionUtil.injectionTarget(encName, envRef, container, container.getEncInjections());
        }
     }
  
     private static void loadXmlMessageDestinationRefs(InjectionContainer container, Collection<MessageDestinationRef> refs)
     {
        for (MessageDestinationRef envRef : refs)
        {
           String encName = "env/" + envRef.getMessageDestinationRefName();
           if (container.getEncInjectors().containsKey(encName)) continue;
           if (envRef.getMappedName() == null || envRef.getMappedName().equals(""))
           {
              throw new RuntimeException("mapped-name is required for " + envRef.getMessageDestinationRefName() + " of deployment " + container.getIdentifier());
           }
           container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, envRef.getMappedName(), "<message-destination-ref>"));
           InjectionUtil.injectionTarget(encName, envRef, container, container.getEncInjections());
        }
     }
  
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
     {
        if (xml.getMessageDestinationRefs() != null) loadXmlMessageDestinationRefs(container, xml.getMessageDestinationRefs());
        if (xml.getResourceEnvRefs() != null) loadXmlResourceEnvRefs(container, xml.getResourceEnvRefs());
        if (xml.getResourceRefs() != null) loadXmlResourceRefs(container, xml.getResourceRefs());
        if (xml.getEnvEntries() != null) loadEnvEntry(container, xml.getEnvEntries());
     }
  
     public void handleClassAnnotations(Class clazz, InjectionContainer container)
     {
        Resources resources = container.getAnnotation(Resources.class, clazz);
        if (resources != null)
        {
        for (Resource ref : resources.value())
        {
           handleClassAnnotation(ref, container, clazz);
        }
        }
        Resource res = container.getAnnotation(Resource.class, clazz);
        if (res != null) handleClassAnnotation(res, container, clazz);
     }
  
     private void handleClassAnnotation(Resource ref, InjectionContainer container, Class clazz)
     {
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           throw new RuntimeException("JBoss requires name() for class level @Resource");
        }
        encName = "env/" + ref.name();
        if (container.getEncInjectors().containsKey(encName)) return;
  
        String mappedName = ref.mappedName();
        if (mappedName == null || mappedName.equals(""))
        {
           throw new RuntimeException("You did not specify a @Resource.mappedName() on " + clazz.getName() + " and there is no binding for that enc name in XML");
        }
        container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@Resource"));
     }
  
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        Resource ref = method.getAnnotation(Resource.class);
        if (ref == null) return;
  
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           encName = InjectionUtil.getEncName(method);
        }
        else
        {
           encName = "env/" + encName;
        }
  
        method.setAccessible(true);
  
        if (!method.getName().startsWith("set"))
           throw new RuntimeException("@Resource can only be used with a set method: " + method);
        if (method.getParameterTypes().length != 1)
           throw new RuntimeException("@Resource can only be used with a set method of one parameter: " + method);
  
        Class type = method.getParameterTypes()[0];
        if (!ref.type().equals(Object.class))
        {
           type = ref.type();
        }
        if (type.equals(UserTransaction.class))
        {
           injectors.put(method, new UserTransactionMethodInjector(method, container));
        }
        else if (type.equals(TimerService.class))
        {
           injectors.put(method, new TimerServiceMethodInjector(method, (Container) container)); // only EJBs
        }
        else if (EJBContext.class.isAssignableFrom(type))
        {
           injectors.put(method, new EJBContextMethodInjector(method));
        }
        else if (type.equals(String.class)
                || type.equals(Character.class)
                || type.equals(Byte.class)
                || type.equals(Short.class)
                || type.equals(Integer.class)
                || type.equals(Long.class)
                || type.equals(Boolean.class)
                || type.equals(Double.class)
                || type.equals(Float.class)
                || type.isPrimitive()
                )
        {
  
           // don't add an injector if no XML <env-entry is present as there will be no value to inject
           if (container.getEncInjectors().containsKey(encName))
           {
              injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
           }
        }
        else
        {
           if (!container.getEncInjectors().containsKey(encName))
           {
              String mappedName = ref.mappedName();
              if (mappedName == null || mappedName.equals(""))
              {
                throw new RuntimeException("You did not specify a @Resource.mappedName() on " + method + " and there is no binding for that enc name in XML");
              }
              container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@Resource"));
           }
           injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
        }
     }
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        Resource ref = field.getAnnotation(Resource.class);
        if (ref == null) return;
  
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           encName = InjectionUtil.getEncName(field);
        }
        else
        {
           encName = "env/" + encName;
        }
  
        field.setAccessible(true);
  
        Class type = field.getType();
        if (!ref.type().equals(Object.class))
        {
           type = ref.type();
        }
        if (type.equals(UserTransaction.class))
        {
           injectors.put(field, new UserTransactionFieldInjector(field, container));
        }
        else if (type.equals(TimerService.class))
        {
           injectors.put(field, new TimerServiceFieldInjector(field, (Container) container)); // only EJBs
        }
        else if (EJBContext.class.isAssignableFrom(type))
        {
           injectors.put(field, new EJBContextFieldInjector(field));
        }
        else if (type.equals(String.class)
                || type.equals(Character.class)
                || type.equals(Byte.class)
                || type.equals(Short.class)
                || type.equals(Integer.class)
                || type.equals(Long.class)
                || type.equals(Boolean.class)
                || type.equals(Double.class)
                || type.equals(Float.class)
                || type.isPrimitive()
                )
        {
  
           // don't add an injector if no XML <env-entry is present as there will be no value to inject
           if (container.getEncInjectors().containsKey(encName))
           {
              injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
           }
        }
        else
        {
           if (!container.getEncInjectors().containsKey(encName))
           {
              String mappedName = ref.mappedName();
              if (mappedName == null || mappedName.equals(""))
              {
                throw new RuntimeException("You did not specify a @Resource.mappedName() on " + field + " and there is no binding for that enc name in XML");
              }
              container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@Resource"));
           }
           injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
        }
     }
  
  
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/TimerServiceFieldInjector.java
  
  Index: TimerServiceFieldInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.ejb3.Container;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class TimerServiceFieldInjector implements Injector
  {
     private Field field;
     private Container container;
  
     public TimerServiceFieldInjector(Field field, Container container)
     {
        this.field = field;
        this.field.setAccessible(true);
        this.container = container;
     }
  
     public void inject(Object instance)
     {
        throw new RuntimeException("SHOULD NOT BE INVOKED");
     }
  
     public void inject(BeanContext ctx)
     {
        try
        {
           field.set(ctx.getInstance(), container.getTimerService());
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter field: " + field.toString());
        }
     }
     
     public Class getInjectionClass()
     {
        return field.getType();
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/TimerServiceMethodInjector.java
  
  Index: TimerServiceMethodInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.ejb3.Container;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class TimerServiceMethodInjector implements Injector
  {
     private Method setMethod;
     private Container container;
  
     public TimerServiceMethodInjector(Method setMethod, Container container)
     {
        this.setMethod = setMethod;
        setMethod.setAccessible(true);
        this.container = container;
     }
  
     public void inject(Object instance)
     {
        throw new RuntimeException("SHOULD NOT BE INVOKED");
     }
  
     public void inject(BeanContext ctx)
     {
  
        Object[] args = {container.getTimerService()};
        try
        {
           setMethod.invoke(ctx.getInstance(), args);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter method: " + setMethod.toString());
        }
        catch (InvocationTargetException e)
        {
           throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
        }
     }
     
     public Class getInjectionClass()
     {
        return setMethod.getParameterTypes()[0];
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/UserTransactionFieldInjector.java
  
  Index: UserTransactionFieldInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.Field;
  import javax.ejb.TransactionManagementType;
  import javax.transaction.UserTransaction;
  import org.jboss.aop.Advisor;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.ejb3.Container;
  import org.jboss.ejb3.tx.TxUtil;
  import org.jboss.ejb3.tx.UserTransactionImpl;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class UserTransactionFieldInjector implements Injector
  {
     private Field field;
  
     public UserTransactionFieldInjector(Field field, InjectionContainer container)
     {
        if (container instanceof Container)
        {
           TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) container));
           if (type != TransactionManagementType.BEAN)
              throw new IllegalStateException("Container " + ((Container) container).getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean");
        }
        this.field = field;
        this.field.setAccessible(true);
     }
  
     public void inject(BeanContext ctx)
     {
        Object instance = ctx.getInstance();
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        UserTransaction ut = new UserTransactionImpl();
        try
        {
           field.set(instance, ut);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter field: " + field.toString());
        }
     }
  
     public Class getInjectionClass()
     {
        return field.getType();
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/UserTransactionMethodInjector.java
  
  Index: UserTransactionMethodInjector.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import org.jboss.aop.Advisor;
  import org.jboss.ejb3.BeanContext;
  import org.jboss.ejb3.Container;
  import org.jboss.ejb3.tx.TxUtil;
  import org.jboss.ejb3.tx.UserTransactionImpl;
  
  import javax.ejb.TransactionManagementType;
  import javax.transaction.UserTransaction;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  
  /**
   * Comment
   *
   * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class UserTransactionMethodInjector implements Injector
  {
     private Method setMethod;
  
     public UserTransactionMethodInjector(Method setMethod, InjectionContainer container)
     {
        if (container instanceof Container)
        {
           TransactionManagementType type = TxUtil.getTransactionManagementType(((Advisor) container));
           if (type != TransactionManagementType.BEAN)
              throw new IllegalStateException("Container " + ((Container) container).getEjbName() + ": it is illegal to inject UserTransaction into a CMT bean");
        }
        this.setMethod = setMethod;
        setMethod.setAccessible(true);
     }
  
     public void inject(BeanContext ctx)
     {
        Object instance = ctx.getInstance();
        inject(instance);
     }
  
     public void inject(Object instance)
     {
        UserTransaction ut = new UserTransactionImpl();
        Object[] args = {ut};
        try
        {
           setMethod.invoke(instance, args);
        }
        catch (IllegalAccessException e)
        {
           throw new RuntimeException(e);  //To change body of catch statement use Options | File Templates.
        }
        catch (IllegalArgumentException e)
        {
           throw new RuntimeException("Failed in setting EntityManager on setter method: " + setMethod.toString());
        }
        catch (InvocationTargetException e)
        {
           throw new RuntimeException(e.getCause());  //To change body of catch statement use Options | File Templates.
        }
     }
  
     public Class getInjectionClass()
     {
        return setMethod.getParameterTypes()[0];
     }
  }
  
  
  
  1.1      date: 2006/07/26 02:59:14;  author: bill;  state: Exp;jboss-ejb3/src/main/org/jboss/injection/WebServiceHandler.java
  
  Index: WebServiceHandler.java
  ===================================================================
  /*
    * JBoss, Home of Professional Open Source
    * Copyright 2005, JBoss Inc., and individual contributors as indicated
    * by the @authors tag. See the copyright.txt in the distribution for a
    * full listing of individual contributors.
    *
    * This is free software; you can redistribute it and/or modify it
    * under the terms of the GNU Lesser General Public License as
    * published by the Free Software Foundation; either version 2.1 of
    * the License, or (at your option) any later version.
    *
    * This software is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public
    * License along with this software; if not, write to the Free
    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
    */
  package org.jboss.injection;
  
  import java.lang.reflect.AccessibleObject;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.Map;
  
  import org.jboss.metamodel.descriptor.WebServiceRef;
  import org.jboss.metamodel.descriptor.EnvironmentRefGroup;
  import org.jboss.logging.Logger;
  
  /**
   * @author <a href="mailto:bdecoste at jboss.com">William DeCoste</a>
   * @version <tt>$Revision: 1.1 $</tt>
   */
  public class WebServiceHandler implements InjectionHandler
  {
     private static final Logger log = Logger.getLogger(WebServiceHandler.class);
  
     public void loadXml(EnvironmentRefGroup xml, InjectionContainer container)
     {
        if (xml.getWebServiceRefs() == null) return;
        for (WebServiceRef wsRef : xml.getWebServiceRefs())
        {
           if (wsRef.getMappedName() == null || wsRef.getMappedName().equals(""))
              throw new RuntimeException("mapped-name is required for <service-ref> " + wsRef.getServiceRefName() + " of " + container.getIdentifier());
  
           String encName = "env/" + wsRef.getServiceRefName();
           if (!container.getEncInjectors().containsKey(encName))
           {
              container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, wsRef.getMappedName(), "jndi ref"));
           }
           InjectionUtil.injectionTarget(encName, wsRef, container, container.getEncInjections());
        }
     }
  
     public void handleClassAnnotations(Class clazz, InjectionContainer container)
     {
        javax.xml.ws.WebServiceRef ref = container.getAnnotation(javax.xml.ws.WebServiceRef.class, clazz);
  
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           throw new RuntimeException("JBoss requires name() for class level @WebServiceRef");
        }
        encName = "env/" + ref.name();
        if (container.getEncInjectors().containsKey(encName)) return;
  
        String mappedName = ref.mappedName();
        if (mappedName == null || mappedName.equals(""))
        {
           throw new RuntimeException("You did not specify a @WebServiceRef.mappedName() on " + clazz.getName() + " and there is no binding for that enc name in XML");
        }
        container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@WebServiceRef"));
     }
  
     public void handleMethodAnnotations(Method method, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        javax.xml.ws.WebServiceRef ref = method.getAnnotation(javax.xml.ws.WebServiceRef.class);
        if (ref == null) return;
        if (!method.getName().startsWith("set"))
           throw new RuntimeException("@ javax.xml.ws.WebServiceRef 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))
        {
           container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@ javax.xml.ws.WebServiceRef"));
        }
  
        injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
     }
  
     public void handleFieldAnnotations(Field field, InjectionContainer container, Map<AccessibleObject, Injector> injectors)
     {
        javax.xml.ws.WebServiceRef ref = field.getAnnotation(javax.xml.ws.WebServiceRef.class);
        if (ref == null) return;
        String encName = ref.name();
        if (encName == null || encName.equals(""))
        {
           encName = InjectionUtil.getEncName(field);
        }
        else
        {
           encName = "env/" + encName;
        }
        if (!container.getEncInjectors().containsKey(encName))
        {
           container.getEncInjectors().put(encName, new LinkRefEncInjector(encName, ref.mappedName(), "@ javax.xml.ws.WebServiceRef"));
        }
  
        injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list