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