1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.listener; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.util.Defense; |
19 |
|
import org.apache.tapestry.IPage; |
20 |
|
import org.apache.tapestry.IRequestCycle; |
21 |
|
import org.apache.tapestry.engine.ILink; |
22 |
|
import org.apache.tapestry.event.BrowserEvent; |
23 |
|
|
24 |
|
import java.lang.reflect.InvocationTargetException; |
25 |
|
import java.lang.reflect.Method; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class ListenerMethodInvokerImpl implements ListenerMethodInvoker |
39 |
|
{ |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
private static final byte DEFAULT_BYTE = -1; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private static final short DEFAULT_SHORT = -1; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private final Method[] _methods; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private final String _name; |
63 |
|
|
64 |
|
public ListenerMethodInvokerImpl(String name, Method[] methods) |
65 |
255 |
{ |
66 |
255 |
Defense.notNull(name, "name"); |
67 |
255 |
Defense.notNull(methods, "methods"); |
68 |
|
|
69 |
255 |
_name = name; |
70 |
255 |
_methods = methods; |
71 |
255 |
} |
72 |
|
|
73 |
|
public void invokeListenerMethod(Object target, IRequestCycle cycle) |
74 |
|
{ |
75 |
15 |
Object[] listenerParameters = cycle.getListenerParameters(); |
76 |
|
|
77 |
15 |
if (listenerParameters == null) |
78 |
3 |
listenerParameters = new Object[0]; |
79 |
|
|
80 |
15 |
if (searchAndInvoke(target, cycle, listenerParameters)) |
81 |
12 |
return; |
82 |
|
|
83 |
0 |
throw new ApplicationRuntimeException(ListenerMessages.noListenerMethodFound(_name, listenerParameters, target), |
84 |
|
target, null, null); |
85 |
|
} |
86 |
|
|
87 |
|
private boolean searchAndInvoke(Object target, IRequestCycle cycle, Object[] listenerParameters) |
88 |
|
{ |
89 |
15 |
BrowserEvent event = null; |
90 |
15 |
if (listenerParameters.length > 0 |
91 |
2 |
&& BrowserEvent.class.isInstance(listenerParameters[listenerParameters.length - 1])) |
92 |
3 |
event = (BrowserEvent)listenerParameters[listenerParameters.length - 1]; |
93 |
|
|
94 |
15 |
List invokeParms = new ArrayList(); |
95 |
|
|
96 |
15 |
Method possibleMethod = null; |
97 |
|
|
98 |
|
methods: |
99 |
68 |
for (int i = 0; i < _methods.length; i++, invokeParms.clear()) { |
100 |
|
|
101 |
67 |
if (!_methods[i].getName().equals(_name)) |
102 |
52 |
continue; |
103 |
|
|
104 |
15 |
Class[] parms = _methods[i].getParameterTypes(); |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
15 |
if (parms.length > (listenerParameters.length + 1) ) { |
109 |
|
|
110 |
0 |
if (possibleMethod == null) |
111 |
0 |
possibleMethod = _methods[i]; |
112 |
0 |
else if (parms.length < possibleMethod.getParameterTypes().length) |
113 |
0 |
possibleMethod = _methods[i]; |
114 |
|
|
115 |
|
continue; |
116 |
|
} |
117 |
|
|
118 |
15 |
int listenerIndex = 0; |
119 |
33 |
for (int p = 0; p < parms.length && listenerIndex < (listenerParameters.length + 1); p++) { |
120 |
|
|
121 |
|
|
122 |
18 |
if (BrowserEvent.class.isAssignableFrom(parms[p])) { |
123 |
3 |
if (event == null) |
124 |
0 |
continue methods; |
125 |
|
|
126 |
3 |
if (!invokeParms.contains(event)) |
127 |
3 |
invokeParms.add(event); |
128 |
|
|
129 |
|
continue; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
15 |
if (IRequestCycle.class.isAssignableFrom(parms[p])) { |
134 |
5 |
invokeParms.add(cycle); |
135 |
5 |
continue; |
136 |
|
} |
137 |
|
|
138 |
10 |
if (event != null && listenerIndex < (listenerParameters.length + 1) |
139 |
|
|| listenerIndex < listenerParameters.length) { |
140 |
9 |
invokeParms.add(listenerParameters[listenerIndex]); |
141 |
9 |
listenerIndex++; |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
15 |
if (invokeParms.size() != parms.length) { |
146 |
|
|
147 |
|
|
148 |
|
|
149 |
1 |
if (possibleMethod == null) |
150 |
1 |
possibleMethod = _methods[i]; |
151 |
0 |
else if (parms.length < possibleMethod.getParameterTypes().length) |
152 |
0 |
possibleMethod = _methods[i]; |
153 |
|
|
154 |
|
continue; |
155 |
|
} |
156 |
|
|
157 |
14 |
invokeListenerMethod(_methods[i], target, cycle, invokeParms.toArray(new Object[invokeParms.size()])); |
158 |
|
|
159 |
11 |
return true; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
1 |
if (possibleMethod != null) { |
166 |
|
|
167 |
1 |
Class[] parms = possibleMethod.getParameterTypes(); |
168 |
1 |
Object[] args = new Object[parms.length]; |
169 |
|
|
170 |
2 |
for (int p=0; p < parms.length; p++) { |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
1 |
if (parms[p].isPrimitive()) { |
175 |
|
|
176 |
0 |
if (parms[p] == Boolean.TYPE) { |
177 |
|
|
178 |
0 |
args[p] = Boolean.FALSE; |
179 |
0 |
} else if (parms[p] == Byte.TYPE) { |
180 |
|
|
181 |
0 |
args[p] = new Byte(DEFAULT_BYTE); |
182 |
0 |
} else if (parms[p] == Short.TYPE) { |
183 |
|
|
184 |
0 |
args[p] = new Short(DEFAULT_SHORT); |
185 |
0 |
} else if (parms[p] == Integer.TYPE) { |
186 |
|
|
187 |
0 |
args[p] = new Integer(-1); |
188 |
0 |
} else if (parms[p] == Long.TYPE) { |
189 |
|
|
190 |
0 |
args[p] = new Long(-1); |
191 |
0 |
} else if (parms[p] == Float.TYPE) { |
192 |
|
|
193 |
0 |
args[p] = new Float(-1); |
194 |
0 |
} else if (parms[p] == Double.TYPE) { |
195 |
|
|
196 |
0 |
args[p] = new Double(-1); |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
1 |
if (IRequestCycle.class.isAssignableFrom(parms[p])) { |
201 |
0 |
args[p] = cycle; |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
1 |
invokeListenerMethod(possibleMethod, target, cycle, args); |
206 |
|
|
207 |
1 |
return true; |
208 |
|
} |
209 |
|
|
210 |
0 |
return false; |
211 |
|
} |
212 |
|
|
213 |
|
private void invokeListenerMethod(Method listenerMethod, Object target, |
214 |
|
IRequestCycle cycle, Object[] parameters) |
215 |
|
{ |
216 |
|
|
217 |
15 |
Object methodResult = null; |
218 |
|
|
219 |
|
try |
220 |
|
{ |
221 |
15 |
methodResult = invokeTargetMethod(target, listenerMethod, parameters); |
222 |
|
} |
223 |
2 |
catch (InvocationTargetException ex) |
224 |
|
{ |
225 |
2 |
Throwable targetException = ex.getTargetException(); |
226 |
|
|
227 |
2 |
if (targetException instanceof ApplicationRuntimeException) |
228 |
1 |
throw (ApplicationRuntimeException) targetException; |
229 |
|
|
230 |
1 |
throw new ApplicationRuntimeException(ListenerMessages.listenerMethodFailure(listenerMethod, target, |
231 |
|
targetException), target, null, targetException); |
232 |
|
} |
233 |
1 |
catch (Exception ex) |
234 |
|
{ |
235 |
1 |
throw new ApplicationRuntimeException(ListenerMessages.listenerMethodFailure(listenerMethod, target, ex), target, |
236 |
|
null, ex); |
237 |
|
|
238 |
12 |
} |
239 |
|
|
240 |
|
|
241 |
|
|
242 |
12 |
if (methodResult == null) return; |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
3 |
if (methodResult instanceof String) |
249 |
|
{ |
250 |
1 |
cycle.activate((String) methodResult); |
251 |
1 |
return; |
252 |
|
} |
253 |
|
|
254 |
2 |
if (methodResult instanceof ILink) |
255 |
|
{ |
256 |
1 |
ILink link = (ILink) methodResult; |
257 |
|
|
258 |
1 |
String url = link.getAbsoluteURL(); |
259 |
|
|
260 |
1 |
cycle.sendRedirect(url); |
261 |
1 |
return; |
262 |
|
} |
263 |
|
|
264 |
1 |
cycle.activate((IPage) methodResult); |
265 |
1 |
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
protected Object invokeTargetMethod(Object target, Method listenerMethod, |
273 |
|
Object[] parameters) |
274 |
|
throws IllegalAccessException, InvocationTargetException |
275 |
|
{ |
276 |
15 |
return listenerMethod.invoke(target, parameters); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
public String getMethodName() |
281 |
|
{ |
282 |
0 |
return _name; |
283 |
|
} |
284 |
|
|
285 |
|
public String toString() |
286 |
|
{ |
287 |
1 |
return "ListenerMethodInvokerImpl[" + |
288 |
|
"_name='" + _name + '\'' + |
289 |
|
']'; |
290 |
|
} |
291 |
|
} |