1
2
3
4
5
6
7
8
9
10
11
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
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>>();
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
110
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 }