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
20 import javax.naming.InitialContext;
21 import javax.naming.NamingException;
22
23 import org.eclipse.jetty.util.IntrospectionUtil;
24 import org.eclipse.jetty.util.log.Log;
25
26
27
28
29
30
31
32
33
34 public class Injection
35 {
36 private Class _targetClass;
37 private String _jndiName;
38 private String _mappingName;
39 private Member _target;
40
41
42 public Injection ()
43 {}
44
45
46
47
48
49 public Class getTargetClass()
50 {
51 return _targetClass;
52 }
53
54
55
56
57
58 public void setTargetClass(Class clazz)
59 {
60 _targetClass = clazz;
61 }
62
63
64
65
66 public String getJndiName()
67 {
68 return _jndiName;
69 }
70
71
72
73 public void setJndiName(String jndiName)
74 {
75 this._jndiName = jndiName;
76 }
77
78
79
80 public String getMappingName()
81 {
82 return _mappingName;
83 }
84
85
86
87 public void setMappingName(String mappingName)
88 {
89 this._mappingName = mappingName;
90 }
91
92
93
94
95 public Member getTarget()
96 {
97 return _target;
98 }
99
100
101
102
103 public void setTarget(Member target)
104 {
105 this._target = target;
106 }
107
108
109
110 public void setTarget (Class clazz, String targetName, Class targetType)
111 {
112
113 String setter = "set"+targetName.substring(0,1).toUpperCase()+targetName.substring(1);
114 try
115 {
116 Log.debug("Looking for method for setter: "+setter+" with arg "+targetType);
117 _target = IntrospectionUtil.findMethod(clazz, setter, new Class[] {targetType}, true, false);
118 _targetClass = clazz;
119 }
120 catch (NoSuchMethodException me)
121 {
122
123 try
124 {
125 _target = IntrospectionUtil.findField(clazz, targetName, targetType, true, false);
126 _targetClass = clazz;
127 }
128 catch (NoSuchFieldException fe)
129 {
130 throw new IllegalArgumentException("No such field or method "+targetName+" on class "+_targetClass);
131 }
132 }
133 }
134
135
136
137
138
139
140
141 public void inject (Object injectable)
142 {
143 Member theTarget = getTarget();
144 if (theTarget instanceof Field)
145 {
146 injectField((Field)theTarget, injectable);
147 }
148 else if (theTarget instanceof Method)
149 {
150 injectMethod((Method)theTarget, injectable);
151 }
152 }
153
154
155
156
157
158
159
160 public Object lookupInjectedValue ()
161 throws NamingException
162 {
163 InitialContext context = new InitialContext();
164 return context.lookup("java:comp/env/"+getJndiName());
165 }
166
167
168
169
170
171
172
173
174 public void injectField (Field field, Object injectable)
175 {
176 try
177 {
178
179 boolean accessibility = field.isAccessible();
180 field.setAccessible(true);
181 field.set(injectable, lookupInjectedValue());
182 field.setAccessible(accessibility);
183 }
184 catch (Exception e)
185 {
186 Log.warn(e);
187 throw new IllegalStateException("Inject failed for field "+field.getName());
188 }
189 }
190
191
192
193
194
195
196 public void injectMethod (Method method, Object injectable)
197 {
198
199 try
200 {
201 boolean accessibility = method.isAccessible();
202 method.setAccessible(true);
203 method.invoke(injectable, new Object[] {lookupInjectedValue()});
204 method.setAccessible(accessibility);
205 }
206 catch (Exception e)
207 {
208 Log.warn(e);
209 throw new IllegalStateException("Inject failed for method "+method.getName());
210 }
211 }
212
213
214
215
216 private void validateInjection (Method method, Object injectable)
217 throws NoSuchMethodException
218 {
219 if ((injectable==null) || (method==null))
220 return;
221
222
223 injectable.getClass().getMethod(method.getName(), method.getParameterTypes());
224 }
225
226 private void validateInjection (Field field, Object injectable)
227 throws NoSuchFieldException
228 {
229 if ((field==null) || (injectable==null))
230 return;
231
232 Field f = injectable.getClass().getField(field.getName());
233 if (!f.getType().isAssignableFrom(field.getType()))
234 throw new NoSuchFieldException("Mismatching type of field: "+f.getType().getName()+" v "+field.getType().getName());
235 }
236 }