1 9 package org.jboss.remoting.network; 10 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Map ; 14 import java.util.Set ; 15 import javax.management.ListenerNotFoundException ; 16 import javax.management.MBeanNotificationInfo ; 17 import javax.management.MBeanServer ; 18 import javax.management.NotificationFilter ; 19 import javax.management.NotificationListener ; 20 import javax.management.ObjectName ; 21 import org.jboss.logging.Logger; 22 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 23 import org.jboss.remoting.InvokerRegistry; 24 import org.jboss.remoting.detection.ServerInvokerMetadata; 25 import org.jboss.remoting.ident.Identity; 26 27 36 public class NetworkRegistry implements NetworkRegistryMBean 37 { 38 private static final Logger log = Logger.getLogger(NetworkRegistry.class); 39 private MBeanServer mBeanServer; 40 private ObjectName objectName; 41 private final JBossNotificationBroadcasterSupport broadcaster = new JBossNotificationBroadcasterSupport(); 42 private final Map servers = new HashMap (); 43 private static NetworkRegistry singleton; 44 45 public NetworkRegistry() 46 { 47 super(); 48 singleton = this; 49 } 50 51 56 public static final NetworkRegistry getInstance() 57 { 58 if(singleton == null) 59 { 60 new NetworkRegistry(); 61 } 62 return singleton; 63 } 64 65 71 public void addServer(final Identity identity, final ServerInvokerMetadata invokers[]) 72 { 73 boolean found = false; 74 synchronized(servers) 75 { 76 if(servers.containsKey(identity) == false) 77 { 78 servers.put(identity, new NetworkInstance(identity, invokers)); 79 found = true; 80 } 81 } 82 if(found) 83 { 84 log.debug("addServer - " + identity); 85 86 new Thread () 89 { 90 public void run() 91 { 92 broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_ADDED, identity, invokers)); 93 } 94 }.start(); 95 } 96 } 97 98 104 public void updateServer(final Identity identity, final ServerInvokerMetadata invokers[]) 105 { 106 boolean found = false; 107 108 synchronized(servers) 109 { 110 if(servers.containsKey(identity)) 111 { 112 servers.put(identity, new NetworkInstance(identity, invokers)); 113 found = true; 114 } 115 } 116 if(found) 117 { 118 new Thread () 121 { 122 public void run() 123 { 124 broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_UPDATED, identity, invokers)); 125 } 126 }.start(); 127 } 128 } 129 130 135 public NetworkInstance[] getServers() 136 { 137 synchronized(servers) 138 { 139 return (NetworkInstance[]) servers.values().toArray(new NetworkInstance[servers.size()]); 140 } 141 } 142 143 149 public boolean hasServer(Identity identity) 150 { 151 synchronized(servers) 152 { 153 return servers.containsKey(identity); 154 } 155 } 156 157 164 public NetworkInstance[] queryServers(NetworkFilter filter) 165 { 166 NetworkInstance servers[] = getServers(); 167 if(servers == null || servers.length <= 0) 168 { 169 return new NetworkInstance[0]; 170 } 171 Set result = new HashSet (); 172 for(int c = 0; c < servers.length; c++) 173 { 174 NetworkInstance instance = (NetworkInstance) this.servers.get(servers[c]); 175 if(filter == null || 176 filter.filter(servers[c].getIdentity(), instance.getLocators())) 177 { 178 if(result.contains(servers[c]) == false) 179 { 180 result.add(servers[c]); 182 } 183 } 184 } 185 return (NetworkInstance[]) result.toArray(new NetworkInstance[result.size()]); 186 } 187 188 193 public void removeServer(final Identity identity) 194 { 195 NetworkInstance instance = null; 196 197 synchronized(servers) 198 { 199 instance = (NetworkInstance) servers.remove(identity); 200 } 201 if(instance != null) 202 { 203 log.debug("removeServer - " + identity); 204 205 final ServerInvokerMetadata il[] = instance.getServerInvokers(); 206 new Thread () 209 { 210 public void run() 211 { 212 broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_REMOVED, identity, il)); 213 } 214 }.start(); 215 } 216 } 217 218 public void addNotificationListener(NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws IllegalArgumentException 219 { 220 broadcaster.addNotificationListener(notificationListener, notificationFilter, o); 221 } 222 223 public MBeanNotificationInfo [] getNotificationInfo() 224 { 225 MBeanNotificationInfo info[] = new MBeanNotificationInfo [3]; 226 info[0] = new MBeanNotificationInfo (new String []{NetworkNotification.SERVER_ADDED}, NetworkNotification.class.getName(), "Fired when Server is added"); 227 info[1] = new MBeanNotificationInfo (new String []{NetworkNotification.SERVER_UPDATED}, NetworkNotification.class.getName(), "Fired when Server is updated"); 228 info[2] = new MBeanNotificationInfo (new String []{NetworkNotification.SERVER_REMOVED}, NetworkNotification.class.getName(), "Fired when Server is removed"); 229 return info; 230 } 231 232 public void removeNotificationListener(NotificationListener notificationListener) throws ListenerNotFoundException 233 { 234 broadcaster.removeNotificationListener(notificationListener); 235 } 236 237 public void postDeregister() 238 { 239 } 240 241 public void postRegister(Boolean aBoolean) 242 { 243 } 244 245 public void preDeregister() throws Exception 246 { 247 } 248 249 public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) throws Exception 250 { 251 this.mBeanServer = mBeanServer; 252 this.objectName = objectName; 253 Identity identity = Identity.get(this.mBeanServer); 255 System.setProperty("jboss.remoting.jmxid", identity.getJMXId()); 258 System.setProperty("jboss.remoting.instanceid", identity.getInstanceId()); 259 System.setProperty("jboss.remoting.domain", identity.getDomain()); 260 return objectName; 261 } 262 263 268 public synchronized void changeDomain(String newDomain) 269 { 270 System.setProperty("jboss.remoting.domain", newDomain); 271 NetworkInstance servers[] = getServers(); 272 if(servers == null || servers.length <= 0) 273 { 274 return; 275 } 276 for(int c = 0; c < servers.length; c++) 278 { 279 NetworkInstance instance = (NetworkInstance) this.servers.get(servers[c]); 280 if(newDomain.equals(instance.getIdentity().getDomain()) == false) 281 { 282 this.servers.remove(servers[c]); 283 } 284 } 285 new Thread () 286 { 287 public void run() 288 { 289 broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.DOMAIN_CHANGED, Identity.get(mBeanServer), InvokerRegistry.getRegisteredServerLocators())); 290 } 291 }.start(); 292 } 293 294 } 295 | Popular Tags |