package org.jboss.remoting.network;
import org.jboss.logging.Logger;
import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.InvokerRegistry;
import org.jboss.remoting.ident.Identity;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServer;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class NetworkRegistry implements NetworkRegistryMBean
{
private static final Logger log = Logger.getLogger(NetworkRegistry.class);
private MBeanServer mBeanServer;
private ObjectName objectName;
private final JBossNotificationBroadcasterSupport broadcaster=new JBossNotificationBroadcasterSupport();
private final Map servers=new HashMap();
private static NetworkRegistry singleton;
public NetworkRegistry ()
{
super();
singleton=this;
}
public static final NetworkRegistry getInstance ()
{
if (singleton==null)
{
new NetworkRegistry ();
}
return singleton;
}
public void addServer (final Identity identity, final InvokerLocator invokers[])
{
boolean found = false;
synchronized(servers)
{
if (servers.containsKey(identity)==false)
{
servers.put(identity,new NetworkInstance(identity,invokers));
found=true;
}
}
if (found)
{
if (log.isDebugEnabled())
{
log.debug("addServer - "+identity);
}
new Thread()
{
public void run ()
{
broadcaster.sendNotification(new NetworkNotification(objectName,NetworkNotification.SERVER_ADDED,identity,invokers));
}
}.start();
}
}
public void updateServer ( final Identity identity, final InvokerLocator invokers[] )
{
boolean found = false;
synchronized (servers)
{
if (servers.containsKey(identity))
{
servers.put(identity,new NetworkInstance(identity,invokers));
found = true;
}
}
if (found)
{
new Thread ()
{
public void run ()
{
broadcaster.sendNotification(new NetworkNotification(objectName,NetworkNotification.SERVER_UPDATED,identity,invokers));
}
}.start();
}
}
public NetworkInstance[] getServers ()
{
synchronized(servers)
{
return (NetworkInstance[])servers.values().toArray(new NetworkInstance[servers.size()]);
}
}
public boolean hasServer (Identity identity)
{
synchronized(servers)
{
return servers.containsKey(identity);
}
}
public NetworkInstance[] queryServers (NetworkFilter filter)
{
NetworkInstance servers[] = getServers();
if (servers==null || servers.length<=0)
{
return new NetworkInstance[0];
}
Set result = new HashSet();
for (int c=0;c<servers.length;c++)
{
NetworkInstance instance=(NetworkInstance)this.servers.get(servers[c]);
if (filter==null ||
filter.filter(servers[c].getIdentity(),instance.getLocators()))
{
if (result.contains(servers[c])==false)
{
result.add(servers[c]);
}
}
}
return (NetworkInstance[])result.toArray(new NetworkInstance[result.size()]);
}
public void removeServer (final Identity identity)
{
NetworkInstance instance = null;
synchronized(servers)
{
instance = (NetworkInstance)servers.remove(identity);
}
if (instance!=null)
{
if (log.isDebugEnabled())
{
log.debug("removeServer - "+identity);
}
final InvokerLocator il[] = instance.getLocators();
new Thread()
{
public void run ()
{
broadcaster.sendNotification(new NetworkNotification(objectName,NetworkNotification.SERVER_REMOVED,identity,il));
}
}.start ();
}
}
public void addNotificationListener (NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws IllegalArgumentException
{
broadcaster.addNotificationListener(notificationListener,notificationFilter,o);
}
public MBeanNotificationInfo[] getNotificationInfo ()
{
MBeanNotificationInfo info[]=new MBeanNotificationInfo[3];
info[0]=new MBeanNotificationInfo(new String[]{NetworkNotification.SERVER_ADDED},NetworkNotification.class.getName(),"Fired when Server is added");
info[1]=new MBeanNotificationInfo(new String[]{NetworkNotification.SERVER_UPDATED},NetworkNotification.class.getName(),"Fired when Server is updated");
info[2]=new MBeanNotificationInfo(new String[]{NetworkNotification.SERVER_REMOVED},NetworkNotification.class.getName(),"Fired when Server is removed");
return info;
}
public void removeNotificationListener (NotificationListener notificationListener) throws ListenerNotFoundException
{
broadcaster.removeNotificationListener(notificationListener);
}
public void postDeregister ()
{
}
public void postRegister (Boolean aBoolean)
{
}
public void preDeregister () throws Exception
{
}
public ObjectName preRegister (MBeanServer mBeanServer, ObjectName objectName) throws Exception
{
this.mBeanServer = mBeanServer;
this.objectName = objectName;
Identity identity = Identity.get(this.mBeanServer);
System.setProperty("jboss.remoting.jmxid",identity.getJMXId());
System.setProperty("jboss.remoting.instanceid",identity.getInstanceId());
System.setProperty("jboss.remoting.domain",identity.getDomain());
return objectName;
}
public synchronized void changeDomain (String newDomain)
{
System.setProperty("jboss.remoting.domain",newDomain);
NetworkInstance servers[] = getServers();
if (servers==null || servers.length<=0)
{
return;
}
for (int c=0;c<servers.length;c++)
{
NetworkInstance instance=(NetworkInstance)this.servers.get(servers[c]);
if (newDomain.equals(instance.getIdentity().getDomain())==false)
{
this.servers.remove(servers[c]);
}
}
new Thread()
{
public void run ()
{
broadcaster.sendNotification(new NetworkNotification(objectName,NetworkNotification.DOMAIN_CHANGED,Identity.get(mBeanServer),InvokerRegistry.getRegisteredServerLocators()));
}
}.start();
}
}