001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.impl;
017:
018: import java.util.HashMap;
019: import java.util.HashSet;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import org.directwebremoting.Hub;
024: import org.directwebremoting.event.MessageEvent;
025: import org.directwebremoting.event.MessageListener;
026:
027: /**
028: * DWR's default implementation of {@link Hub}
029: * @author Joe Walker [joe at getahead dot ltd dot uk]
030: */
031: public class DefaultHub implements Hub {
032: /* (non-Javadoc)
033: * @see org.directwebremoting.Hub#subscribe(java.lang.String, org.directwebremoting.event.MessageListener)
034: */
035: public void subscribe(String topicName, MessageListener listener) {
036: synchronized (subscriptions) {
037: Set<MessageListener> listeners = subscriptions
038: .get(topicName);
039: if (listeners == null) {
040: listeners = new HashSet<MessageListener>();
041: subscriptions.put(topicName, listeners);
042: }
043:
044: listeners.add(listener);
045: }
046: }
047:
048: /* (non-Javadoc)
049: * @see org.directwebremoting.Hub#unsubscribe(java.lang.String, org.directwebremoting.event.MessageListener)
050: */
051: public boolean unsubscribe(String topicName,
052: MessageListener listener) {
053: synchronized (subscriptions) {
054: boolean unsubscribed = false;
055:
056: Set<MessageListener> listeners = subscriptions
057: .get(topicName);
058: if (listeners != null) {
059: unsubscribed = listeners.remove(listener);
060: if (listeners.isEmpty()) {
061: subscriptions.remove(topicName);
062: }
063: }
064:
065: return unsubscribed;
066: }
067: }
068:
069: /* (non-Javadoc)
070: * @see org.directwebremoting.Hub#publish(java.lang.String, java.lang.Object)
071: */
072: public void publish(String topicName, Object data) {
073: // First clone the list of subscriptions
074: Set<MessageListener> listeners;
075: synchronized (subscriptions) {
076: Set<MessageListener> current = subscriptions.get(topicName);
077: if (current == null) {
078: return;
079: }
080:
081: listeners = new HashSet<MessageListener>();
082: listeners.addAll(current);
083: }
084:
085: // Then tell everyone about the message
086: for (MessageListener listener : listeners) {
087: MessageEvent event;
088: if (data instanceof MessageEvent) {
089: event = (MessageEvent) data;
090: } else {
091: event = new MessageEvent(this , data);
092: }
093: listener.onMessage(event);
094: }
095: }
096:
097: /**
098: * The cache of current subscriptions
099: */
100: private final Map<String, Set<MessageListener>> subscriptions = new HashMap<String, Set<MessageListener>>();
101: }
|