|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.listener; |
|
16 |
| |
|
17 |
| import java.lang.reflect.InvocationTargetException; |
|
18 |
| import java.lang.reflect.Method; |
|
19 |
| |
|
20 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
21 |
| import org.apache.hivemind.util.Defense; |
|
22 |
| import org.apache.tapestry.IPage; |
|
23 |
| import org.apache.tapestry.IRequestCycle; |
|
24 |
| import org.apache.tapestry.Tapestry; |
|
25 |
| import org.apache.tapestry.engine.ILink; |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| public class ListenerMethodInvokerImpl implements ListenerMethodInvoker |
|
37 |
| { |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| private final Method[] _methods; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| private final String _name; |
|
50 |
| |
|
51 |
4158
| public ListenerMethodInvokerImpl(String name, Method[] methods)
|
|
52 |
| { |
|
53 |
4158
| Defense.notNull(name, "name");
|
|
54 |
4158
| Defense.notNull(methods, "methods");
|
|
55 |
| |
|
56 |
4158
| _name = name;
|
|
57 |
4158
| _methods = methods;
|
|
58 |
| } |
|
59 |
| |
|
60 |
156
| public void invokeListenerMethod(Object target, IRequestCycle cycle)
|
|
61 |
| { |
|
62 |
156
| Object[] listenerParameters = cycle.getListenerParameters();
|
|
63 |
| |
|
64 |
| |
|
65 |
156
| if (searchAndInvoke(target, false, true, cycle, listenerParameters))
|
|
66 |
12
| return;
|
|
67 |
| |
|
68 |
| |
|
69 |
141
| if (searchAndInvoke(target, true, true, cycle, listenerParameters))
|
|
70 |
108
| return;
|
|
71 |
| |
|
72 |
| |
|
73 |
27
| if (searchAndInvoke(target, false, false, cycle, listenerParameters))
|
|
74 |
3
| return;
|
|
75 |
| |
|
76 |
| |
|
77 |
18
| if (searchAndInvoke(target, true, false, cycle, listenerParameters))
|
|
78 |
15
| return;
|
|
79 |
| |
|
80 |
3
| throw new ApplicationRuntimeException(ListenerMessages.noListenerMethodFound(
|
|
81 |
| _name, |
|
82 |
| listenerParameters, |
|
83 |
| target), target, null, null); |
|
84 |
| } |
|
85 |
| |
|
86 |
342
| private boolean searchAndInvoke(Object target, boolean includeCycle, boolean includeParameters,
|
|
87 |
| IRequestCycle cycle, Object[] listenerParameters) |
|
88 |
| { |
|
89 |
342
| int listenerParameterCount = Tapestry.size(listenerParameters);
|
|
90 |
342
| int methodParameterCount = includeParameters ? listenerParameterCount : 0;
|
|
91 |
| |
|
92 |
342
| if (includeCycle)
|
|
93 |
159
| methodParameterCount++;
|
|
94 |
| |
|
95 |
342
| for (int i = 0; i < _methods.length; i++)
|
|
96 |
| { |
|
97 |
345
| Method m = _methods[i];
|
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
345
| Class[] parameterTypes = m.getParameterTypes();
|
|
103 |
| |
|
104 |
345
| if (parameterTypes.length < methodParameterCount)
|
|
105 |
48
| break;
|
|
106 |
| |
|
107 |
297
| if (parameterTypes.length != methodParameterCount)
|
|
108 |
141
| continue;
|
|
109 |
| |
|
110 |
156
| boolean firstIsCycle = parameterTypes.length > 0
|
|
111 |
| && parameterTypes[0] == IRequestCycle.class; |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
156
| if (includeCycle != firstIsCycle)
|
|
122 |
3
| continue;
|
|
123 |
| |
|
124 |
153
| invokeListenerMethod(
|
|
125 |
| m, |
|
126 |
| target, |
|
127 |
| includeCycle, |
|
128 |
| includeParameters, |
|
129 |
| cycle, |
|
130 |
| listenerParameters); |
|
131 |
| |
|
132 |
138
| return true;
|
|
133 |
| } |
|
134 |
| |
|
135 |
189
| return false;
|
|
136 |
| } |
|
137 |
| |
|
138 |
153
| private void invokeListenerMethod(Method listenerMethod, Object target, boolean includeCycle,
|
|
139 |
| boolean includeParameters, IRequestCycle cycle, Object[] listenerParameters) |
|
140 |
| { |
|
141 |
153
| Object[] parameters = new Object[listenerMethod.getParameterTypes().length];
|
|
142 |
153
| int cursor = 0;
|
|
143 |
| |
|
144 |
153
| if (includeCycle)
|
|
145 |
129
| parameters[cursor++] = cycle;
|
|
146 |
| |
|
147 |
153
| if (includeParameters)
|
|
148 |
129
| for (int i = 0; i < Tapestry.size(listenerParameters); i++)
|
|
149 |
78
| parameters[cursor++] = listenerParameters[i];
|
|
150 |
| |
|
151 |
153
| Object methodResult = null;
|
|
152 |
| |
|
153 |
153
| try
|
|
154 |
| { |
|
155 |
153
| methodResult = invokeTargetMethod(target, listenerMethod, parameters);
|
|
156 |
| } |
|
157 |
| catch (InvocationTargetException ex) |
|
158 |
| { |
|
159 |
12
| Throwable targetException = ex.getTargetException();
|
|
160 |
| |
|
161 |
12
| if (targetException instanceof ApplicationRuntimeException)
|
|
162 |
9
| throw (ApplicationRuntimeException) targetException;
|
|
163 |
| |
|
164 |
3
| throw new ApplicationRuntimeException(ListenerMessages.listenerMethodFailure(
|
|
165 |
| listenerMethod, |
|
166 |
| target, |
|
167 |
| targetException), target, null, targetException); |
|
168 |
| } |
|
169 |
| catch (Exception ex) |
|
170 |
| { |
|
171 |
3
| throw new ApplicationRuntimeException(ListenerMessages.listenerMethodFailure(
|
|
172 |
| listenerMethod, |
|
173 |
| target, |
|
174 |
| ex), target, null, ex); |
|
175 |
| |
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
138
| if (methodResult == null)
|
|
181 |
129
| return;
|
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
9
| if (methodResult instanceof String)
|
|
188 |
| { |
|
189 |
3
| cycle.activate((String) methodResult);
|
|
190 |
3
| return;
|
|
191 |
| } |
|
192 |
| |
|
193 |
6
| if (methodResult instanceof ILink)
|
|
194 |
| { |
|
195 |
3
| ILink link = (ILink) methodResult;
|
|
196 |
| |
|
197 |
3
| String url = link.getAbsoluteURL();
|
|
198 |
| |
|
199 |
3
| cycle.sendRedirect(url);
|
|
200 |
3
| return;
|
|
201 |
| } |
|
202 |
| |
|
203 |
3
| cycle.activate((IPage) methodResult);
|
|
204 |
| } |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
153
| protected Object invokeTargetMethod(Object target, Method listenerMethod, Object[] parameters)
|
|
212 |
| throws IllegalAccessException, InvocationTargetException |
|
213 |
| { |
|
214 |
153
| return listenerMethod.invoke(target, parameters);
|
|
215 |
| } |
|
216 |
| } |