View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.annotation;
15  
16  import java.lang.reflect.Field;
17  import java.lang.reflect.Member;
18  import java.lang.reflect.Method;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.eclipse.jetty.util.log.Log;
26  
27  /**
28   * InjectionCollection
29   *
30   *
31   */
32  public class InjectionCollection
33  {
34      public static final String INJECTION_COLLECTION = "org.eclipse.jetty.injectionCollection";
35      private HashMap<Class<?>, List<Injection>> fieldInjectionsMap = new HashMap<Class<?>, List<Injection>>();//map of classname to field injections
36      private HashMap<Class<?>, List<Injection>> methodInjectionsMap = new HashMap<Class<?>, List<Injection>>();//map of classname to method injections
37      
38      
39      public void add (Injection injection)
40      {
41          if ((injection==null) || (injection.getTarget()==null) || (injection.getTargetClass()==null)) 
42              return;
43          
44          if (Log.isDebugEnabled())
45              Log.debug("Adding injection for class="+injection.getTargetClass()+ " on a "+injection.getTarget());
46          Map<Class<?>, List<Injection>> injectionsMap = null;
47          if (injection.getTarget() instanceof Field)
48              injectionsMap = fieldInjectionsMap;
49          if (injection.getTarget() instanceof Method)
50              injectionsMap = methodInjectionsMap;
51          
52          List<Injection> injections = (List<Injection>)injectionsMap.get(injection.getTargetClass());
53          if (injections==null)
54          {
55              injections = new ArrayList<Injection>();
56              injectionsMap.put(injection.getTargetClass(), injections);
57          }
58          
59          injections.add(injection);
60      }
61  
62      public List<Injection> getFieldInjections (Class<?> clazz)
63      {
64          if (clazz==null)
65              return null;
66          List<Injection> list = (List<Injection>)fieldInjectionsMap.get(clazz);
67          if (list == null)
68              list = Collections.emptyList();
69          return list;
70      }
71      
72      public List<Injection>  getMethodInjections (Class<?> clazz)
73      {
74          if (clazz==null)
75              return null;
76          List<Injection> list = (List<Injection>)methodInjectionsMap.get(clazz);
77          if (list == null)
78              list = Collections.emptyList();
79          return list;
80      }
81   
82      public List<Injection>  getInjections (Class<?> clazz)
83      {
84          if (clazz==null)
85              return null;
86          
87          List<Injection>  results = new ArrayList<Injection> ();
88          results.addAll(getFieldInjections(clazz));
89          results.addAll(getMethodInjections(clazz));
90          return results;
91      }
92      
93      public Injection getInjection (Class<?> clazz, Member member)
94      {
95          if (clazz==null)
96              return null;
97          if (member==null)
98              return null;
99          Map<Class<?>, List<Injection>> map = null;
100         if (member instanceof Field)
101             map = fieldInjectionsMap;
102         else if (member instanceof Method)
103             map = methodInjectionsMap;
104         
105         if (map==null)
106             return null;
107 
108         List<Injection>  injections = (List<Injection>)map.get(clazz);
109         Injection injection = null;
110         for (int i=0;injections!=null && i<injections.size() && injection==null;i++)
111         {
112             Injection candidate = (Injection)injections.get(i);
113             if (candidate.getTarget().equals(member))
114                 injection = candidate;
115         }
116         return injection;
117     }
118     
119     
120     public void inject (Object injectable)
121     throws Exception
122     {
123         if (injectable==null)
124             return;
125         
126         //Get all injections pertinent to the Object by
127         //looking at it's class hierarchy
128         Class<?> clazz = injectable.getClass();
129         
130        
131         while (clazz != null)
132         {
133             //Do field injections
134             List<Injection> injections = getFieldInjections(clazz);
135             for (Injection i : injections)
136             {
137                 i.inject(injectable);
138             }
139 
140             //Do method injections
141             injections = getMethodInjections(clazz);
142             for (Injection i : injections)
143             {
144                 i.inject(injectable);
145             }
146             
147             clazz = clazz.getSuperclass();
148         }
149     }
150 }