01: package org.directwebremoting.proxy.openajax;
02:
03: import java.util.List;
04:
05: import org.directwebremoting.ScriptBuffer;
06: import org.directwebremoting.ScriptSession;
07: import org.directwebremoting.event.PublishEvent;
08: import org.directwebremoting.event.PublishListener;
09:
10: /**
11: * OpenAjaxSynchronizingPublishListener is responsible for taking events
12: * from a server-side {@link PubSubHub} and passing them on to a client side
13: * OpenAjax hub.
14: * <p>TODO: Currently we are passing every message that the server hub knows of
15: * to all the relevant clients, but we should perhaps maintain a list of
16: * @author Joe Walker [joe at getahead dot ltd dot uk]
17: */
18: public class OpenAjaxSynchronizingPublishListener implements
19: PublishListener {
20: /**
21: * {@link OpenAjaxSynchronizingPublishListener}s need to reference an
22: * {@link OpenAjaxSynchronizer} to know who to publish to
23: * @param openAjaxSynchronizer the source of enrolled {@link ScriptSession}s
24: */
25: public OpenAjaxSynchronizingPublishListener(
26: OpenAjaxSynchronizer openAjaxSynchronizer) {
27: this .openAjaxSynchronizer = openAjaxSynchronizer;
28: }
29:
30: /* (non-Javadoc)
31: * @see org.directwebremoting.proxy.openajax.PublishListener#eventHappened(org.directwebremoting.proxy.openajax.PublishEvent)
32: */
33: public void publishHappened(PublishEvent ev) {
34: ScriptBuffer script = new ScriptBuffer();
35: script.appendScript("OpenAjax.publish(").appendData(
36: ev.getPrefix()).appendScript(",").appendData(
37: ev.getName()).appendScript(");");
38:
39: List<ScriptSession> scriptSessions = openAjaxSynchronizer
40: .getEnrolledScriptSessions();
41:
42: if (ev.getScriptSessionId()
43: .equals(PubSubHub.ANY_SCRIPT_SESSION)) {
44: for (ScriptSession scriptSession : scriptSessions) {
45: scriptSession.addScript(script);
46: }
47: } else {
48: for (ScriptSession scriptSession : scriptSessions) {
49: if (scriptSession.getId().equals(
50: ev.getScriptSessionId())) {
51: scriptSession.addScript(script);
52: }
53: }
54: }
55: }
56:
57: /**
58: * the source of enrolled {@link ScriptSession}s
59: */
60: private OpenAjaxSynchronizer openAjaxSynchronizer;
61: }
|