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