View Javadoc

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