001: package org.directwebremoting.proxy.openajax;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.directwebremoting.Container;
007: import org.directwebremoting.ScriptSession;
008: import org.directwebremoting.WebContext;
009: import org.directwebremoting.WebContextFactory;
010: import org.directwebremoting.event.ScriptSessionEvent;
011: import org.directwebremoting.event.ScriptSessionListener;
012: import org.directwebremoting.extend.ScriptSessionManager;
013:
014: /**
015: * This class is designed for export by DWR to enable the OpenAjax hub to
016: * exchange publish/subscribe data with the server.
017: * @author Joe Walker [joe at getahead dot ltd dot uk]
018: */
019: public class OpenAjaxSynchronizer {
020: /**
021: *
022: */
023: public OpenAjaxSynchronizer() {
024: Container container = WebContextFactory.get().getContainer();
025:
026: ScriptSessionManager scriptSessionManager = (ScriptSessionManager) container
027: .getBean(ScriptSessionManager.class.getName());
028: scriptSessionManager
029: .addScriptSessionListener(new CustomScriptSessionListener());
030:
031: pubSubHub = (PubSubHub) container.getBean(PubSubHub.class
032: .getName());
033: pubSubHub.subscribe(PubSubHub.ANY_PREFIX, PubSubHub.ANY_NAME,
034: new OpenAjaxSynchronizingPublishListener(this ));
035: pubSubHub
036: .addSubscriptionListener(new OpenAjaxSynchronizingSubscriptionListener(
037: this ));
038: }
039:
040: /**
041: * Called by clients that wish to be included in the whole hub sync thing.
042: */
043: public void enroll() {
044: WebContext webContext = WebContextFactory.get();
045: ScriptSession scriptSession = webContext.getScriptSession();
046: synchronized (enrolledScriptSessions) {
047: enrolledScriptSessions.add(scriptSession);
048: }
049: }
050:
051: /**
052: * This method allows the client side hub synchronizer to forward publish
053: * messages to the server hub
054: * @param prefix The prefix that was published to
055: * @param name The name that was published to
056: * @param data The published data
057: * @param hubsVisited The list of hubs that this message has been through
058: */
059: public void publish(String prefix, String name, String data,
060: List<String> hubsVisited) {
061: WebContext webContext = WebContextFactory.get();
062:
063: String httpSessionId = webContext.getSession(true).getId();
064: String scriptSessionId = webContext.getScriptSession().getId();
065:
066: pubSubHub.publish(httpSessionId, scriptSessionId, prefix, name,
067: data, hubsVisited);
068: }
069:
070: /**
071: * Get a list of the {@link ScriptSession}s that are part of this
072: * distributed hub.
073: * @return The current list of known enrolled {@link ScriptSession}s
074: */
075: protected List<ScriptSession> getEnrolledScriptSessions() {
076: List<ScriptSession> reply = new ArrayList<ScriptSession>();
077: synchronized (enrolledScriptSessions) {
078: reply.addAll(enrolledScriptSessions);
079: }
080: return reply;
081: }
082:
083: /**
084: * The current server side hub
085: */
086: private PubSubHub pubSubHub;
087:
088: /**
089: * The current list of known enrolled {@link ScriptSession}s
090: */
091: protected List<ScriptSession> enrolledScriptSessions = new ArrayList<ScriptSession>();
092:
093: /**
094: * We need to know when pages go away so we don't keep publishing changes
095: * to them.
096: */
097: protected final class CustomScriptSessionListener implements
098: ScriptSessionListener {
099: /* (non-Javadoc)
100: * @see org.directwebremoting.event.ScriptSessionListener#sessionCreated(org.directwebremoting.event.ScriptSessionEvent)
101: */
102: public void sessionCreated(ScriptSessionEvent ev) {
103: // The creation of a ScriptSession is not important, because they
104: // might not enroll. We care about the enrolled ones that have gone
105: // away.
106: }
107:
108: /* (non-Javadoc)
109: * @see org.directwebremoting.event.ScriptSessionListener#sessionDestroyed(org.directwebremoting.event.ScriptSessionEvent)
110: */
111: public void sessionDestroyed(ScriptSessionEvent ev) {
112: ScriptSession scriptSession = ev.getSession();
113: synchronized (enrolledScriptSessions) {
114: enrolledScriptSessions.add(scriptSession);
115: }
116: }
117: }
118: }
|