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.Method;
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.eclipse.jetty.util.log.Log;
24  
25  /**
26   * InjectionCollection
27   *
28   *
29   */
30  public class InjectionCollection
31  { 
32      public static final String INJECTION_COLLECTION = "org.eclipse.jetty.injectionCollection";
33  
34      private HashMap<String, List<Injection>> _injectionMap = new HashMap<String, List<Injection>>();//map of classname to injections
35      
36      
37      public void add (Injection injection)
38      {
39          if ((injection==null) || injection.getTargetClass()==null) 
40              return;
41          
42          if (Log.isDebugEnabled())
43              Log.debug("Adding injection for class="+(injection.getTargetClass()+ " on a "+(injection.getTarget().getName())));
44     
45          
46          List<Injection> injections = (List<Injection>)_injectionMap.get(injection.getTargetClass().getCanonicalName());
47          if (injections==null)
48          {
49              injections = new ArrayList<Injection>();
50              _injectionMap.put(injection.getTargetClass().getCanonicalName(), injections);
51          }
52          injections.add(injection);
53      }
54  
55   
56      public List<Injection>  getInjections (String className)
57      {
58          if (className==null)
59              return null;
60  
61          return _injectionMap.get(className);
62      }
63    
64      
65      
66      public Injection getInjection (String jndiName, Class clazz, Field field)
67      {
68          if (field == null || clazz == null)
69              return null;
70          
71          List<Injection> injections = getInjections(clazz.getCanonicalName());
72          if (injections == null)
73              return null;
74          Iterator<Injection> itor = injections.iterator();
75          Injection injection = null;
76          while (itor.hasNext() && injection == null)
77          {
78              Injection i = itor.next();
79              if (i.isField() && field.getName().equals(i.getTarget().getName()))
80                  injection = i;
81          }
82          
83          return injection;
84      }
85      
86      public Injection getInjection (String jndiName, Class clazz, Method method, Class paramClass)
87      {
88          if (clazz == null || method == null || paramClass == null)
89              return null;
90          
91          List<Injection> injections = getInjections(clazz.getCanonicalName());
92          if (injections == null)
93              return null;
94          Iterator<Injection> itor = injections.iterator();
95          Injection injection = null;
96          while (itor.hasNext() && injection == null)
97          {
98              Injection i = itor.next();
99              if (i.isMethod() && i.getTarget().getName().equals(method.getName()) && paramClass.equals(i.getParamClass()))
100                 injection = i;
101         }
102         
103         return injection;
104     }
105     
106     
107     public void inject (Object injectable)
108     {
109         if (injectable==null)
110             return;
111         
112         //Get all injections pertinent to the Object by
113         //looking at it's class hierarchy
114         Class<?> clazz = injectable.getClass();
115         
116         while (clazz != null)
117         {
118             List<Injection> injections = _injectionMap.get(clazz.getCanonicalName());
119             if (injections != null)
120             {
121                 for (Injection i : injections)
122                     i.inject(injectable);
123             }
124             
125             clazz = clazz.getSuperclass();
126         }
127     }
128 }