1   //========================================================================
2   //$Id: InjectionCollection.java 3353 2008-07-22 10:39:41Z 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<Class<?>, List<Injection>> fieldInjectionsMap = new HashMap<Class<?>, List<Injection>>();//map of classname to field injections
38      private HashMap<Class<?>, List<Injection>> methodInjectionsMap = new HashMap<Class<?>, List<Injection>>();//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<Class<?>, List<Injection>> injectionsMap = null;
49          if (injection.getTarget() instanceof Field)
50              injectionsMap = fieldInjectionsMap;
51          if (injection.getTarget() instanceof Method)
52              injectionsMap = methodInjectionsMap;
53          
54          List<Injection> injections = (List<Injection>)injectionsMap.get(injection.getTargetClass());
55          if (injections==null)
56          {
57              injections = new ArrayList<Injection>();
58              injectionsMap.put(injection.getTargetClass(), injections);
59          }
60          
61          injections.add(injection);
62      }
63  
64      public List<Injection> getFieldInjections (Class<?> clazz)
65      {
66          if (clazz==null)
67              return null;
68          List<Injection> list = (List<Injection>)fieldInjectionsMap.get(clazz);
69          if (list == null)
70              list = Collections.emptyList();
71          return list;
72      }
73      
74      public List<Injection>  getMethodInjections (Class<?> clazz)
75      {
76          if (clazz==null)
77              return null;
78          List<Injection> list = (List<Injection>)methodInjectionsMap.get(clazz);
79          if (list == null)
80              list = Collections.emptyList();
81          return list;
82      }
83   
84      public List<Injection>  getInjections (Class<?> clazz)
85      {
86          if (clazz==null)
87              return null;
88          
89          List<Injection>  results = new ArrayList<Injection> ();
90          results.addAll(getFieldInjections(clazz));
91          results.addAll(getMethodInjections(clazz));
92          return results;
93      }
94      
95      public Injection getInjection (Class<?> clazz, Member member)
96      {
97          if (clazz==null)
98              return null;
99          if (member==null)
100             return null;
101         Map<Class<?>, List<Injection>> map = null;
102         if (member instanceof Field)
103             map = fieldInjectionsMap;
104         else if (member instanceof Method)
105             map = methodInjectionsMap;
106         
107         if (map==null)
108             return null;
109 
110         List<Injection>  injections = (List<Injection>)map.get(clazz);
111         Injection injection = null;
112         for (int i=0;injections!=null && i<injections.size() && injection==null;i++)
113         {
114             Injection candidate = (Injection)injections.get(i);
115             if (candidate.getTarget().equals(member))
116                 injection = candidate;
117         }
118         return injection;
119     }
120     
121     
122     public void inject (Object injectable)
123     throws Exception
124     {
125         if (injectable==null)
126             return;
127         
128         //Get all injections pertinent to the Object by
129         //looking at it's class hierarchy
130         Class<?> clazz = injectable.getClass();
131         
132         
133         if (injectable instanceof PojoWrapper)
134         { 
135             injectable = ((PojoWrapper)injectable).getPojo();
136             clazz = injectable.getClass();
137         }
138       
139         
140         while (clazz != null)
141         {
142             //Do field injections
143             List<Injection> injections = getFieldInjections(clazz);
144             for (Injection i : injections)
145                 i.inject(injectable);
146 
147             //Do method injections
148             injections = getMethodInjections(clazz);
149             for (Injection i : injections)
150                 i.inject(injectable);
151             
152             clazz = clazz.getSuperclass();
153         }
154     }
155 }