1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
package org.apache.tapestry.services.impl; |
15 |
|
|
16 |
|
import org.apache.hivemind.ClassResolver; |
17 |
|
import org.apache.hivemind.PoolManageable; |
18 |
|
import org.apache.hivemind.Resource; |
19 |
|
import org.apache.hivemind.util.ClasspathResource; |
20 |
|
import org.apache.tapestry.*; |
21 |
|
import org.apache.tapestry.dojo.IWidget; |
22 |
|
import org.apache.tapestry.engine.DirectEventServiceParameter; |
23 |
|
import org.apache.tapestry.engine.IEngineService; |
24 |
|
import org.apache.tapestry.engine.IScriptSource; |
25 |
|
import org.apache.tapestry.html.Body; |
26 |
|
import org.apache.tapestry.internal.Component; |
27 |
|
import org.apache.tapestry.internal.event.ComponentEventProperty; |
28 |
|
import org.apache.tapestry.internal.event.EventBoundListener; |
29 |
|
import org.apache.tapestry.internal.event.IComponentEventInvoker; |
30 |
|
import org.apache.tapestry.services.ComponentRenderWorker; |
31 |
|
import org.apache.tapestry.util.ScriptUtils; |
32 |
|
|
33 |
|
import java.util.*; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
7 |
public class ComponentEventConnectionWorker implements ComponentRenderWorker, PoolManageable |
43 |
|
{ |
44 |
|
|
45 |
|
public static final String FORM_NAME_LIST = "org.apache.tapestry.services.impl.ComponentEventConnectionFormNames-"; |
46 |
|
|
47 |
|
|
48 |
|
private IComponentEventInvoker _invoker; |
49 |
|
|
50 |
|
|
51 |
|
private IEngineService _eventEngine; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private IScriptSource _scriptSource; |
56 |
|
|
57 |
|
|
58 |
|
private String _componentScript; |
59 |
|
private String _widgetScript; |
60 |
|
private String _elementScript; |
61 |
|
|
62 |
|
|
63 |
|
private ClassResolver _resolver; |
64 |
|
|
65 |
|
|
66 |
|
private ClasspathResource _componentResource; |
67 |
|
private ClasspathResource _widgetResource; |
68 |
|
private ClasspathResource _elementResource; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
7 |
private Map _deferredFormConnections = new HashMap(24); |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
7 |
class DeferredFormConnection { |
80 |
|
|
81 |
|
String _formId; |
82 |
|
Map _scriptParms; |
83 |
|
Boolean _async; |
84 |
|
Boolean _validate; |
85 |
|
String _uniqueHash; |
86 |
|
|
87 |
|
public DeferredFormConnection(String formId, Map scriptParms, Boolean async, |
88 |
|
Boolean validate, String uniqueHash) |
89 |
6 |
{ |
90 |
6 |
_formId = formId; |
91 |
6 |
_scriptParms = scriptParms; |
92 |
6 |
_async = async; |
93 |
6 |
_validate = validate; |
94 |
6 |
_uniqueHash = uniqueHash; |
95 |
6 |
} |
96 |
|
|
97 |
|
public boolean equals(Object o) |
98 |
|
{ |
99 |
5 |
if (this == o) return true; |
100 |
5 |
if (o == null || getClass() != o.getClass()) return false; |
101 |
|
|
102 |
5 |
DeferredFormConnection that = (DeferredFormConnection) o; |
103 |
|
|
104 |
5 |
if (_uniqueHash != null ? !_uniqueHash.equals(that._uniqueHash) : that._uniqueHash != null) |
105 |
4 |
return false; |
106 |
|
|
107 |
1 |
return true; |
108 |
|
} |
109 |
|
|
110 |
|
public int hashCode() |
111 |
|
{ |
112 |
0 |
return (_uniqueHash != null ? _uniqueHash.hashCode() : 0); |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
|
public void activateService() |
117 |
|
{ |
118 |
0 |
_deferredFormConnections.clear(); |
119 |
0 |
} |
120 |
|
|
121 |
|
public void passivateService() |
122 |
|
{ |
123 |
0 |
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
public void renderComponent(IRequestCycle cycle, IComponent component) |
129 |
|
{ |
130 |
13 |
if (cycle.isRewinding()) |
131 |
1 |
return; |
132 |
|
|
133 |
15 |
if (Component.class.isInstance(component) && !((Component)component).hasEvents() && !IForm.class.isInstance(component)) |
134 |
0 |
return; |
135 |
|
|
136 |
12 |
if (TapestryUtils.getOptionalPageRenderSupport(cycle) == null) |
137 |
1 |
return; |
138 |
|
|
139 |
|
|
140 |
11 |
IComponent field = (IComponent)cycle.getAttribute(TapestryUtils.FIELD_PRERENDER); |
141 |
11 |
if (field != null && field == component) |
142 |
1 |
return; |
143 |
|
|
144 |
10 |
linkComponentEvents(cycle, component); |
145 |
|
|
146 |
10 |
linkElementEvents(cycle, component); |
147 |
|
|
148 |
10 |
if (IForm.class.isInstance(component)) |
149 |
1 |
mapFormNames(cycle, (IForm)component); |
150 |
|
|
151 |
10 |
if (isDeferredForm(component)) |
152 |
1 |
linkDeferredForm(cycle, (IForm)component); |
153 |
10 |
} |
154 |
|
|
155 |
|
void linkComponentEvents(IRequestCycle cycle, IComponent component) |
156 |
|
{ |
157 |
10 |
ComponentEventProperty[] props = _invoker.getEventPropertyListeners(component.getExtendedId()); |
158 |
10 |
if (props == null) |
159 |
0 |
return; |
160 |
|
|
161 |
17 |
for (int i=0; i < props.length; i++) |
162 |
|
{ |
163 |
7 |
String clientId = component.getClientId(); |
164 |
|
|
165 |
7 |
Map parms = new HashMap(); |
166 |
7 |
parms.put("clientId", clientId); |
167 |
7 |
parms.put("component", component); |
168 |
|
|
169 |
7 |
Object[][] events = getEvents(props[i], clientId); |
170 |
7 |
Object[][] formEvents = filterFormEvents(props[i], parms, cycle); |
171 |
|
|
172 |
7 |
if (events.length < 1 && formEvents.length < 1) |
173 |
5 |
continue; |
174 |
|
|
175 |
2 |
DirectEventServiceParameter dsp = |
176 |
|
new DirectEventServiceParameter((IDirectEvent)component, new Object[] {}, new String[] {}, false); |
177 |
|
|
178 |
2 |
parms.put("url", _eventEngine.getLink(false, dsp).getURL()); |
179 |
2 |
parms.put("events", events); |
180 |
2 |
parms.put("formEvents", formEvents); |
181 |
|
|
182 |
2 |
PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, component); |
183 |
2 |
Resource resource = getScript(component); |
184 |
|
|
185 |
2 |
_scriptSource.getScript(resource).execute(component, cycle, prs, parms); |
186 |
|
} |
187 |
10 |
} |
188 |
|
|
189 |
|
void linkElementEvents(IRequestCycle cycle, IComponent component) |
190 |
|
{ |
191 |
12 |
if (!component.getSpecification().hasElementEvents()) |
192 |
11 |
return; |
193 |
|
|
194 |
1 |
DirectEventServiceParameter dsp = |
195 |
|
new DirectEventServiceParameter((IDirectEvent)component, new Object[] {}, new String[] {}, false); |
196 |
|
|
197 |
1 |
String url = _eventEngine.getLink(false, dsp).getURL(); |
198 |
|
|
199 |
1 |
PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, component); |
200 |
1 |
Resource resource = getElementScript(); |
201 |
|
|
202 |
1 |
Map elements = component.getSpecification().getElementEvents(); |
203 |
1 |
Iterator keys = elements.keySet().iterator(); |
204 |
|
|
205 |
|
|
206 |
2 |
while (keys.hasNext()) |
207 |
|
{ |
208 |
1 |
Map parms = new HashMap(); |
209 |
|
|
210 |
1 |
String target = (String)keys.next(); |
211 |
|
|
212 |
1 |
ComponentEventProperty prop = (ComponentEventProperty)elements.get(target); |
213 |
|
|
214 |
1 |
parms.put("component", component); |
215 |
1 |
parms.put("target", target); |
216 |
1 |
parms.put("url", url); |
217 |
1 |
parms.put("events", getEvents(prop, target)); |
218 |
1 |
parms.put("formEvents", filterFormEvents(prop, parms, cycle)); |
219 |
|
|
220 |
1 |
_scriptSource.getScript(resource).execute(component, cycle, prs, parms); |
221 |
1 |
} |
222 |
1 |
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
public void renderBody(IRequestCycle cycle, Body component) |
228 |
|
{ |
229 |
0 |
if (cycle.isRewinding()) |
230 |
0 |
return; |
231 |
|
|
232 |
0 |
renderComponent(cycle, component); |
233 |
|
|
234 |
|
|
235 |
0 |
_deferredFormConnections.clear(); |
236 |
0 |
} |
237 |
|
|
238 |
|
void mapFormNames(IRequestCycle cycle, IForm form) |
239 |
|
{ |
240 |
1 |
List names = (List)cycle.getAttribute(FORM_NAME_LIST + form.getExtendedId()); |
241 |
|
|
242 |
1 |
if (names == null) |
243 |
|
{ |
244 |
1 |
names = new ArrayList(); |
245 |
1 |
cycle.setAttribute(FORM_NAME_LIST + form.getExtendedId(), names); |
246 |
|
} |
247 |
|
|
248 |
1 |
names.add(form.getName()); |
249 |
1 |
} |
250 |
|
|
251 |
|
void linkDeferredForm(IRequestCycle cycle, IForm form) |
252 |
|
{ |
253 |
1 |
List deferred = (List)_deferredFormConnections.remove(form.getExtendedId()); |
254 |
|
|
255 |
3 |
for (int i=0; i < deferred.size(); i++) |
256 |
|
{ |
257 |
2 |
DeferredFormConnection fConn = (DeferredFormConnection)deferred.get(i); |
258 |
2 |
Map scriptParms = fConn._scriptParms; |
259 |
|
|
260 |
|
|
261 |
2 |
scriptParms.remove("events"); |
262 |
|
|
263 |
2 |
IComponent component = (IComponent)scriptParms.get("component"); |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
2 |
linkElementEvents(cycle, component); |
268 |
|
|
269 |
2 |
ComponentEventProperty[] props = _invoker.getEventPropertyListeners(component.getExtendedId()); |
270 |
2 |
if (props == null) |
271 |
0 |
continue; |
272 |
|
|
273 |
4 |
for (int e=0; e < props.length; e++) |
274 |
|
{ |
275 |
2 |
Object[][] formEvents = buildFormEvents(cycle, form.getExtendedId(), |
276 |
|
props[e].getFormEvents(), fConn._async, |
277 |
|
fConn._validate, fConn._uniqueHash); |
278 |
|
|
279 |
2 |
scriptParms.put("formEvents", formEvents); |
280 |
|
|
281 |
|
|
282 |
|
|
283 |
2 |
PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, component); |
284 |
2 |
Resource resource = getScript(component); |
285 |
|
|
286 |
2 |
_scriptSource.getScript(resource).execute(form, cycle, prs, scriptParms); |
287 |
|
} |
288 |
|
} |
289 |
1 |
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
Object[][] getEvents(ComponentEventProperty prop, String clientId) |
299 |
|
{ |
300 |
9 |
Set events = prop.getEvents(); |
301 |
9 |
List ret = new ArrayList(); |
302 |
|
|
303 |
9 |
Iterator it = events.iterator(); |
304 |
13 |
while (it.hasNext()) |
305 |
|
{ |
306 |
4 |
String event = (String)it.next(); |
307 |
|
|
308 |
4 |
int hash = 0; |
309 |
4 |
List listeners = prop.getEventListeners(event); |
310 |
|
|
311 |
8 |
for (int i=0; i < listeners.size(); i++) |
312 |
4 |
hash += listeners.get(i).hashCode(); |
313 |
|
|
314 |
4 |
ret.add(new Object[]{ event, ScriptUtils.functionHash(event + hash + clientId) }); |
315 |
4 |
} |
316 |
|
|
317 |
9 |
return (Object[][])ret.toArray(new Object[ret.size()][2]); |
318 |
|
} |
319 |
|
|
320 |
|
Object[][] buildFormEvents(IRequestCycle cycle, String formId, Set events, |
321 |
|
Boolean async, Boolean validate, Object uniqueHash) |
322 |
|
{ |
323 |
2 |
List formNames = (List)cycle.getAttribute(FORM_NAME_LIST + formId); |
324 |
2 |
List retval = new ArrayList(); |
325 |
|
|
326 |
2 |
Iterator it = events.iterator(); |
327 |
|
|
328 |
4 |
while (it.hasNext()) |
329 |
|
{ |
330 |
2 |
String event = (String)it.next(); |
331 |
|
|
332 |
2 |
retval.add(new Object[]{event, formNames, async, validate, |
333 |
|
ScriptUtils.functionHash(new String(uniqueHash + event)) }); |
334 |
2 |
} |
335 |
|
|
336 |
2 |
return (Object[][])retval.toArray(new Object[retval.size()][5]); |
337 |
|
} |
338 |
|
|
339 |
|
Resource getScript(IComponent component) |
340 |
|
{ |
341 |
14 |
if (IWidget.class.isInstance(component)) { |
342 |
|
|
343 |
5 |
if (_widgetResource == null) |
344 |
2 |
_widgetResource = new ClasspathResource(_resolver, _widgetScript); |
345 |
|
|
346 |
5 |
return _widgetResource; |
347 |
|
} |
348 |
|
|
349 |
9 |
if (_componentResource == null) |
350 |
3 |
_componentResource = new ClasspathResource(_resolver, _componentScript); |
351 |
|
|
352 |
9 |
return _componentResource; |
353 |
|
} |
354 |
|
|
355 |
|
Resource getElementScript() |
356 |
|
{ |
357 |
3 |
if (_elementResource == null) |
358 |
1 |
_elementResource = new ClasspathResource(_resolver, _elementScript); |
359 |
|
|
360 |
3 |
return _elementResource; |
361 |
|
} |
362 |
|
|
363 |
|
boolean isDeferredForm(IComponent component) |
364 |
|
{ |
365 |
10 |
if (IForm.class.isInstance(component) |
366 |
|
&& _deferredFormConnections.get(component.getExtendedId()) != null) |
367 |
1 |
return true; |
368 |
|
|
369 |
9 |
return false; |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
|
|
387 |
|
Object[][] filterFormEvents(ComponentEventProperty prop, Map scriptParms, IRequestCycle cycle) |
388 |
|
{ |
389 |
8 |
Set events = prop.getFormEvents(); |
390 |
|
|
391 |
8 |
if (events.size() < 1) |
392 |
3 |
return new Object[0][0]; |
393 |
|
|
394 |
5 |
List retval = new ArrayList(); |
395 |
|
|
396 |
5 |
Iterator it = events.iterator(); |
397 |
11 |
while (it.hasNext()) |
398 |
|
{ |
399 |
6 |
String event = (String)it.next(); |
400 |
6 |
Iterator lit = prop.getFormEventListeners(event).iterator(); |
401 |
|
|
402 |
6 |
while (lit.hasNext()) |
403 |
|
{ |
404 |
6 |
EventBoundListener listener = (EventBoundListener)lit.next(); |
405 |
|
|
406 |
6 |
String formId = listener.getFormId(); |
407 |
6 |
List formNames = (List)cycle.getAttribute(FORM_NAME_LIST + formId); |
408 |
|
|
409 |
|
|
410 |
6 |
if (formNames == null) |
411 |
|
{ |
412 |
6 |
deferFormConnection(formId, scriptParms, |
413 |
|
listener.isAsync(), |
414 |
|
listener.isValidateForm(), |
415 |
|
ScriptUtils.functionHash(listener.hashCode() + (String) scriptParms.get("clientId"))); |
416 |
|
|
417 |
|
|
418 |
|
|
419 |
6 |
break; |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
0 |
retval.add(new Object[] { |
424 |
|
event, formNames, |
425 |
|
Boolean.valueOf(listener.isAsync()), |
426 |
|
Boolean.valueOf(listener.isValidateForm()), |
427 |
|
ScriptUtils.functionHash(listener) |
428 |
|
}); |
429 |
0 |
} |
430 |
6 |
} |
431 |
|
|
432 |
5 |
return (Object[][])retval.toArray(new Object[retval.size()][5]); |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
|
454 |
|
void deferFormConnection(String formId, Map scriptParms, |
455 |
|
boolean async, boolean validate, String uniqueHash) |
456 |
|
{ |
457 |
6 |
List deferred = (List)_deferredFormConnections.get(formId); |
458 |
6 |
if (deferred == null) |
459 |
|
{ |
460 |
2 |
deferred = new ArrayList(); |
461 |
2 |
_deferredFormConnections.put(formId, deferred); |
462 |
|
} |
463 |
|
|
464 |
6 |
DeferredFormConnection connection = new DeferredFormConnection(formId, scriptParms, Boolean.valueOf(async), |
465 |
|
Boolean.valueOf(validate), uniqueHash); |
466 |
|
|
467 |
6 |
if (!deferred.contains(connection)) |
468 |
5 |
deferred.add(connection); |
469 |
6 |
} |
470 |
|
|
471 |
|
|
472 |
|
Map getDefferedFormConnections() |
473 |
|
{ |
474 |
5 |
return _deferredFormConnections; |
475 |
|
} |
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
|
481 |
|
public void setEventInvoker(IComponentEventInvoker invoker) |
482 |
|
{ |
483 |
5 |
_invoker = invoker; |
484 |
5 |
} |
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
|
489 |
|
|
490 |
|
|
491 |
|
|
492 |
|
public void setEventEngine(IEngineService eventEngine) |
493 |
|
{ |
494 |
4 |
_eventEngine = eventEngine; |
495 |
4 |
} |
496 |
|
|
497 |
|
|
498 |
|
|
499 |
|
|
500 |
|
|
501 |
|
|
502 |
|
public void setComponentScript(String script) |
503 |
|
{ |
504 |
3 |
_componentScript = script; |
505 |
3 |
} |
506 |
|
|
507 |
|
|
508 |
|
|
509 |
|
|
510 |
|
|
511 |
|
|
512 |
|
public void setWidgetScript(String script) |
513 |
|
{ |
514 |
2 |
_widgetScript = script; |
515 |
2 |
} |
516 |
|
|
517 |
|
|
518 |
|
|
519 |
|
|
520 |
|
|
521 |
|
|
522 |
|
public void setElementScript(String script) |
523 |
|
{ |
524 |
2 |
_elementScript = script; |
525 |
2 |
} |
526 |
|
|
527 |
|
|
528 |
|
|
529 |
|
|
530 |
|
|
531 |
|
public void setScriptSource(IScriptSource scriptSource) |
532 |
|
{ |
533 |
3 |
_scriptSource = scriptSource; |
534 |
3 |
} |
535 |
|
|
536 |
|
public void setClassResolver(ClassResolver resolver) |
537 |
|
{ |
538 |
4 |
_resolver = resolver; |
539 |
4 |
} |
540 |
|
} |