org.directwebremoting.impl.DefaultHub Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dwr Show documentation
Show all versions of dwr Show documentation
DWR is easy Ajax for Java. It makes it simple to call Java code directly from Javascript.
It gets rid of almost all the boiler plate code between the web browser and your Java code.
The newest version!
/*
* Copyright 2005 Joe Walker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.directwebremoting.impl;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.directwebremoting.Hub;
import org.directwebremoting.event.DefaultMessageEvent;
import org.directwebremoting.event.MessageEvent;
import org.directwebremoting.event.MessageListener;
/**
* DWR's default implementation of {@link Hub}
* @author Joe Walker [joe at getahead dot ltd dot uk]
*/
public class DefaultHub implements Hub
{
/* (non-Javadoc)
* @see org.directwebremoting.Hub#subscribe(java.lang.String, org.directwebremoting.event.MessageListener)
*/
public void subscribe(String topicName, MessageListener listener)
{
synchronized (subscriptions)
{
Set listeners = subscriptions.get(topicName);
if (listeners == null)
{
listeners = new HashSet();
subscriptions.put(topicName, listeners);
}
listeners.add(listener);
}
}
/* (non-Javadoc)
* @see org.directwebremoting.Hub#unsubscribe(java.lang.String, org.directwebremoting.event.MessageListener)
*/
public boolean unsubscribe(String topicName, MessageListener listener)
{
synchronized (subscriptions)
{
boolean unsubscribed = false;
Set listeners = subscriptions.get(topicName);
if (listeners != null)
{
unsubscribed = listeners.remove(listener);
if (listeners.isEmpty())
{
subscriptions.remove(topicName);
}
}
return unsubscribed;
}
}
/* (non-Javadoc)
* @see org.directwebremoting.Hub#publish(java.lang.String, java.lang.Object)
*/
public void publish(String topicName, Object data)
{
// First clone the list of subscriptions
Set listeners;
synchronized (subscriptions)
{
Set current = subscriptions.get(topicName);
if (current == null)
{
return;
}
listeners = new HashSet();
listeners.addAll(current);
}
// Then tell everyone about the message
for (MessageListener listener : listeners)
{
MessageEvent event;
if (data instanceof MessageEvent)
{
event = (MessageEvent) data;
}
else
{
event = new DefaultMessageEvent(this, data);
}
listener.onMessage(event);
}
}
/**
* The cache of current subscriptions
*/
private final Map> subscriptions = new HashMap>();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy