1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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>>();
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
121
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 }