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