Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file 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.util.List;
 import java.util.Map;
 import java.util.Set;
 
Comment

Author(s):
Bill Burke
Version:
$Revision: 76876 $
 
 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.

Parameters:
visitedMethods
clazz
xmlDefinedInjectors
classInjectors
 
    public static void collapseXmlMethodInjectors(Set<StringvisitedMethodsClass clazzMap<StringMap<AccessibleObjectInjector>> xmlDefinedInjectorsMap<AccessibleObjectInjectorclassInjectors)
    {
       if (clazz == null || clazz.equals(Object.class))
       {
          return;
       }
       Map<AccessibleObjectInjectorxmlInjectors = xmlDefinedInjectors.get(clazz.getName());
       if (xmlInjectors != null)
       {
          Method[] methods = clazz.getDeclaredMethods();
          for (Method method : methods)
          {
             if (method.getParameterTypes().length != 1) continue;
 
             if (!Modifier.isPrivate(method.getModifiers()))
             {
                if (visitedMethods.contains(method.getName()))
                {
                   xmlInjectors.remove(method); // if not private then it has been overriden
                   continue;
                }
                visitedMethods.add(method.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(visitedMethodsclazz.getSuperclass(), xmlDefinedInjectorsclassInjectors);
    }

   
Create and add multiple injectors for injection targets.

Parameters:
injectors the list on which to add injectors
classLoader the class loader to resolve an injection target
factory the injector factory
injectionTargets the injection targets
   public static void createInjectors(List<InjectorinjectorsClassLoader classLoaderInjectorFactory<?> factoryCollection<ResourceInjectionTargetMetaDatainjectionTargets)
   {
      for(ResourceInjectionTargetMetaData injectionTarget : injectionTargets)
      {
         AccessibleObject ao = findInjectionTarget(classLoaderinjectionTarget);
         BeanProperty property = BeanPropertyFactory.create(ao);
         injectors.add(factory.create(property));
      }
   }
   
   public static <X extends RemoteEnvironmentvoid processMethodAnnotations(InjectionContainer containerCollection<InjectionHandler<X>> handlersSet<StringvisitedMethodsClass<?> clazzMap<AccessibleObjectInjectorclassInjectors)
   {
      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
      Method[] methods = clazz.getDeclaredMethods();
      for (Method method : methods)
      {
         if (method.getParameterTypes().length != 1) continue;
         if (!Modifier.isPrivate(method.getModifiers()))
         {
            if (visitedMethods.contains(method.getName()))
            {
               continue;
            }
            visitedMethods.add(method.getName());
         }
        
         if (handlers != null)
         {
            for (InjectionHandler<?> handler : handlers)
            {
               handler.handleMethodAnnotations(methodcontainerclassInjectors);
            }
         }
      }
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      processMethodAnnotations(containerhandlersvisitedMethodsclazz.getSuperclass(), classInjectors);
   }
   public static <X extends RemoteEnvironmentvoid processFieldAnnotations(InjectionContainer containerCollection<InjectionHandler<X>> handlersClass<?> clazzMap<AccessibleObjectInjectorclassInjectors)
   {
      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
 
      if (handlers != null)
      {
         Field[] fields = clazz.getDeclaredFields();
         for (Field field : fields)
         {
            .trace("process field annotation for " + field.toGenericString());
            for (InjectionHandler<?> handler : handlers)
            {
               handler.handleFieldAnnotations(fieldcontainerclassInjectors);
            }
         }
      }
      
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      processFieldAnnotations(containerhandlersclazz.getSuperclass(), classInjectors);
   }
   public static <X extends RemoteEnvironmentvoid processClassAnnotations(InjectionContainer containerCollection<InjectionHandler<X>> handlersClass<?> clazz)
   {
      if (clazz == null || clazz.equals(Object.class))
      {
         return;
      }
    
      if (handlers != null)
      {
         for (InjectionHandler<?> handler : handlers)
         {
            handler.handleClassAnnotations(clazzcontainer);
         }
      }
      
      // recursion needs to come last as the method could be overriden and we don't want the overriding method to be ignored
      processClassAnnotations(containerhandlersclazz.getSuperclass());
   }
   public static <X extends RemoteEnvironmentMap<AccessibleObjectInjectorprocessAnnotations(InjectionContainer containerCollection<InjectionHandler<X>> handlersClass<?> clazz)
   {
      Map<AccessibleObjectInjectorclassInjectors = new HashMap<AccessibleObjectInjector>();
      HashSet<StringvisitedMethods = new HashSet<String>();
      collapseXmlMethodInjectors(visitedMethodsclazzcontainer.getEncInjections(), classInjectors);
      processClassAnnotations(containerhandlersclazz);
      visitedMethods = new HashSet<String>();
      processMethodAnnotations(containerhandlersvisitedMethodsclazzclassInjectors);
      processFieldAnnotations(containerhandlersclazzclassInjectors);
      return classInjectors;
   }
   {
      Class<?> clazz = null;
      try
      {
         clazz = loader.loadClass(target.getInjectionTargetClass());
      }
      catch (ClassNotFoundException e)
      {
         throw new RuntimeException("<injection-target> class: " + target.getInjectionTargetClass() + " was not found in deployment");
      }
      for (Field field : clazz.getDeclaredFields())
      {
         if (target.getInjectionTargetName().equals(field.getName())) return field;
      }
      for (java.lang.reflect.Method method : clazz.getDeclaredMethods())
      {
         if (method.getName().equals(target.getInjectionTargetName())) return method;
      }
      throw new RuntimeException("<injection-target> could not be found: " + target.getInjectionTargetClass() + "." + target.getInjectionTargetName());
   }
   public static String getEncName(Class type)
   {
      return "env/" + type.getName();
   }
   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 = getEncName(method.getDeclaringClass()) + "/" + encName;
      return encName;
   }
   public static String getEncName(Field field)
   {
      return getEncName(field.getDeclaringClass()) + "/" + field.getName();
   }
   public static Object getAnnotation(Class<? extends AnnotationannotationEJBContainer containerClass<?> annotatedClassboolean isContainer)
   {
      if (isContainer)
      {
         return container.resolveAnnotation(annotation);
      }
      else
      {
         return annotatedClass.getAnnotation(annotation);
      }
   }
   public static Object getAnnotation(Class<? extends AnnotationannotationEJBContainer containerMethod methodboolean isContainer)
   {
      if (isContainer)
      {
         return container.resolveAnnotation(methodannotation);
      }
      else
      {
         return method.getAnnotation(annotation);
      }
   }
   public static Object getAnnotation(Class<? extends AnnotationannotationEJBContainer containerField fieldboolean isContainer)
   {
      if (isContainer)
      {
         return container.resolveAnnotation(fieldannotation);
      }
      else
      {
         return field.getAnnotation(annotation);
      }
   }
   public static Class<?> injectionTarget(String encNameResourceInjectionMetaData refInjectionContainer containerMap<StringMap<AccessibleObjectInjector>> classInjectors)
   {
      Class<?> injectionType = null;
      
      if(ref.getInjectionTargets() == null)
         return injectionType;
      
      for(ResourceInjectionTargetMetaData injectionTarget : ref.getInjectionTargets())
      {
         // todo, get injection target class
         AccessibleObject ao = findInjectionTarget(container.getClassloader(), injectionTarget);
         Map<AccessibleObjectInjectorinjectors = classInjectors.get(injectionTarget.getInjectionTargetClass());
         if (injectors == null)
         {
            injectors = new HashMap<AccessibleObjectInjector>();
            classInjectors.put(injectionTarget.getInjectionTargetClass(), injectors);
         }
         Class<?> type;
         if (ao instanceof Field)
         {
            type = ((Fieldao).getType();
            injectors.put(aonew JndiFieldInjector((FieldaoencNamecontainer.getEnc()));
         }
         else
         {
            type = ((Methodao).getParameterTypes()[0];
            injectors.put(aonew JndiMethodInjector((MethodaoencNamecontainer.getEnc()));
         }
         if(injectionType == null)
            injectionType = type;
         else
         {
            if(!injectionType.equals(type))
               throw new IllegalStateException("Found multiple injection targets with different types");
         }
      }
      
      return injectionType;
   }
New to GrepCode? Check out our FAQ X