View Javadoc

    1   //========================================================================
    2   //$Id: InjectionCollection.java 1540 2007-01-19 12:24:10Z janb $
    3   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package org.mortbay.jetty.plus.annotation;
    17
    18  import java.lang.reflect.Field;
    19  import java.lang.reflect.Member;
    20  import java.lang.reflect.Method;
    21  import java.util.ArrayList;
    22  import java.util.Collections;
    23  import java.util.HashMap;
    24  import java.util.Iterator;
    25  import java.util.List;
    26  import java.util.Map;
    27
    28  import org.mortbay.log.Log;
    29
    30  /**
    31   * InjectionCollection
    32   *
    33   *
    34   */
    35  public class InjectionCollection
    36  {
    37      private HashMap fieldInjectionsMap = new HashMap();//map of classname to field injections
    38      private HashMap methodInjectionsMap = new HashMap();//map of classname to method injections
    39
    40
    41      public void add (Injection injection)
    42      {
    43          if ((injection==null) || (injection.getTarget()==null) || (injection.getTargetClass()==null))
    44              return;
    45
    46          if (Log.isDebugEnabled())
    47              Log.debug("Adding injection for class="+injection.getTargetClass()+ " on a "+injection.getTarget());
    48          Map injectionsMap = null;
    49          if (injection.getTarget() instanceof Field)
    50              injectionsMap = fieldInjectionsMap;
    51          if (injection.getTarget() instanceof Method)
    52              injectionsMap = methodInjectionsMap;
    53
    54          List injections = (List)injectionsMap.get(injection.getTargetClass());
    55          if (injections==null)
    56          {
    57              injections = new ArrayList();
    58              injectionsMap.put(injection.getTargetClass(), injections);
    59          }
    60
    61          injections.add(injection);
    62      }
    63
    64      public List getFieldInjections (Class clazz)
    65      {
    66          if (clazz==null)
    67              return null;
    68          List list = (List)fieldInjectionsMap.get(clazz);
    69          return (list==null?Collections.EMPTY_LIST:list);
    70      }
    71
    72      public List getMethodInjections (Class clazz)
    73      {
    74          if (clazz==null)
    75              return null;
    76          List list = (List)methodInjectionsMap.get(clazz);
    77          return (list==null?Collections.EMPTY_LIST:list);
    78      }
    79
    80      public List getInjections (Class clazz)
    81      {
    82          if (clazz==null)
    83              return null;
    84
    85          List results = new ArrayList();
    86          results.addAll(getFieldInjections(clazz));
    87          results.addAll(getMethodInjections(clazz));
    88          return results;
    89      }
    90
    91      public Injection getInjection (Class clazz, Member member)
    92      {
    93          if (clazz==null)
    94              return null;
    95          if (member==null)
    96              return null;
    97          Map map = null;
    98          if (member instanceof Field)
    99              map = fieldInjectionsMap;
    100         else if (member instanceof Method)
    101             map = methodInjectionsMap;
    102
    103         if (map==null)
    104             return null;
    105
    106         List injections = (List)map.get(clazz);
    107         Injection injection = null;
    108         for (int i=0;injections!=null && i<injections.size() && injection==null;i++)
    109         {
    110             Injection candidate = (Injection)injections.get(i);
    111             if (candidate.getTarget().equals(member))
    112                 injection = candidate;
    113         }
    114         return injection;
    115     }
    116
    117
    118     public void inject (Object injectable)
    119     throws Exception
    120     {
    121         if (injectable==null)
    122             return;
    123
    124         //Do field injections
    125         List list = getFieldInjections(injectable.getClass());
    126         for (int i=0; list!=null && i<list.size();i++)
    127             ((Injection)list.get(i)).inject(injectable);
    128
    129         //Do method injections
    130         list = getMethodInjections(injectable.getClass());
    131         for (int i=0; list!=null && i<list.size();i++)
    132             ((Injection)list.get(i)).inject(injectable);
    133     }
    134 }