KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > network > NetworkRegistry


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.remoting.network;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.management.ListenerNotFoundException JavaDoc;
16 import javax.management.MBeanNotificationInfo JavaDoc;
17 import javax.management.MBeanServer JavaDoc;
18 import javax.management.NotificationFilter JavaDoc;
19 import javax.management.NotificationListener JavaDoc;
20 import javax.management.ObjectName JavaDoc;
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 /**
28  * NetworkRegistry is a concrete implemenation of the NetworkRegistryMBean
29  * interface. The NetworkRegistry will keep a list of all the detected
30  * JBoss servers on the network and provide a local facility for querying
31  * for different servers.
32  *
33  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
34  * @version $Revision: 1.4 $
35  */

36 public class NetworkRegistry implements NetworkRegistryMBean
37 {
38    private static final Logger log = Logger.getLogger(NetworkRegistry.class);
39    private MBeanServer JavaDoc mBeanServer;
40    private ObjectName JavaDoc objectName;
41    private final JBossNotificationBroadcasterSupport broadcaster = new JBossNotificationBroadcasterSupport();
42    private final Map JavaDoc servers = new HashMap JavaDoc();
43    private static NetworkRegistry singleton;
44
45    public NetworkRegistry()
46    {
47       super();
48       singleton = this;
49    }
50
51    /**
52     * return the singleton instance
53     *
54     * @return
55     */

56    public static final NetworkRegistry getInstance()
57    {
58       if(singleton == null)
59       {
60          new NetworkRegistry();
61       }
62       return singleton;
63    }
64
65    /**
66     * add a server for a given identity that is available on the network
67     *
68     * @param identity
69     * @param invokers
70     */

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          // put this on a separate thread so we don't block further detection ...
87
// TODO: this needs to go into a thread pool thread -JGH
88
new Thread JavaDoc()
89          {
90             public void run()
91             {
92                broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_ADDED, identity, invokers));
93             }
94          }.start();
95       }
96    }
97
98    /**
99     * update the invokers for a given server
100     *
101     * @param identity
102     * @param invokers
103     */

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          // TODO: let's put this in a thread pool thread -JGH
119
// put this on a separate thread so we don't block further detection ...
120
new Thread JavaDoc()
121          {
122             public void run()
123             {
124                broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_UPDATED, identity, invokers));
125             }
126          }.start();
127       }
128    }
129
130    /**
131     * return the servers on the network
132     *
133     * @return
134     */

135    public NetworkInstance[] getServers()
136    {
137       synchronized(servers)
138       {
139          return (NetworkInstance[]) servers.values().toArray(new NetworkInstance[servers.size()]);
140       }
141    }
142
143    /**
144     * returns true if the server with the identity is available
145     *
146     * @param identity
147     * @return
148     */

149    public boolean hasServer(Identity identity)
150    {
151       synchronized(servers)
152       {
153          return servers.containsKey(identity);
154       }
155    }
156
157    /**
158     * query the network registry for <tt>0..*</tt> of servers based on a
159     * filter. if the filter is null, it is considered a wildcard.
160     *
161     * @param filter
162     * @return
163     */

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 JavaDoc result = new HashSet JavaDoc();
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                // the filter passed, add it
181
result.add(servers[c]);
182             }
183          }
184       }
185       return (NetworkInstance[]) result.toArray(new NetworkInstance[result.size()]);
186    }
187
188    /**
189     * remove a server no longer available on the network
190     *
191     * @param identity
192     */

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          // put this on a separate thread so we don't block further detection ...
207
// TODO: let's put this is a thread pool thread -JGH
208
new Thread JavaDoc()
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 JavaDoc notificationListener, NotificationFilter JavaDoc notificationFilter, Object JavaDoc o) throws IllegalArgumentException JavaDoc
219    {
220       broadcaster.addNotificationListener(notificationListener, notificationFilter, o);
221    }
222
223    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
224    {
225       MBeanNotificationInfo JavaDoc info[] = new MBeanNotificationInfo JavaDoc[3];
226       info[0] = new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{NetworkNotification.SERVER_ADDED}, NetworkNotification.class.getName(), "Fired when Server is added");
227       info[1] = new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{NetworkNotification.SERVER_UPDATED}, NetworkNotification.class.getName(), "Fired when Server is updated");
228       info[2] = new MBeanNotificationInfo JavaDoc(new String JavaDoc[]{NetworkNotification.SERVER_REMOVED}, NetworkNotification.class.getName(), "Fired when Server is removed");
229       return info;
230    }
231
232    public void removeNotificationListener(NotificationListener JavaDoc notificationListener) throws ListenerNotFoundException JavaDoc
233    {
234       broadcaster.removeNotificationListener(notificationListener);
235    }
236
237    public void postDeregister()
238    {
239    }
240
241    public void postRegister(Boolean JavaDoc aBoolean)
242    {
243    }
244
245    public void preDeregister() throws Exception JavaDoc
246    {
247    }
248
249    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc mBeanServer, ObjectName JavaDoc objectName) throws Exception JavaDoc
250    {
251       this.mBeanServer = mBeanServer;
252       this.objectName = objectName;
253       // make sure our identity system property is properly set
254
Identity identity = Identity.get(this.mBeanServer);
255       // this is a slight hack, but we have to have some way to know the main
256
// JBoss MBeanServer data
257
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    /**
264     * change the main domain of the local server
265     *
266     * @param newDomain
267     */

268    public synchronized void changeDomain(String JavaDoc 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       // remove entries that don't match out new domain
277
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 JavaDoc()
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