2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.network;
25
import java.security.AccessController;
26
import java.security.PrivilegedActionException;
27
import java.security.PrivilegedExceptionAction;
28
import java.util.HashMap;
29
import java.util.HashSet;
32
import javax.management.ListenerNotFoundException;
33
import javax.management.MBeanNotificationInfo;
34
import javax.management.MBeanServer;
35
import javax.management.NotificationFilter;
36
import javax.management.NotificationListener;
37
import javax.management.ObjectName;
38
import org.jboss.logging.Logger;
39
import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
40
import org.jboss.remoting.InvokerRegistry;
41
import org.jboss.remoting.detection.ServerInvokerMetadata;
42
import org.jboss.remoting.ident.Identity;
43
import org.jboss.remoting.util.SecurityUtility;
46
* NetworkRegistry is a concrete implemenation of the NetworkRegistryMBean
47
* interface. The NetworkRegistry will keep a list of all the detected
48
* JBoss servers on the network and provide a local facility for querying
49
* for different servers.
51
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
52
* @version $Revision: 5004 $
54
public class NetworkRegistry implements NetworkRegistryMBean
56
private static final Logger log = Logger.getLogger(NetworkRegistry.class);
57
private MBeanServer mBeanServer;
58
private ObjectName objectName;
59
private final JBossNotificationBroadcasterSupport broadcaster = new JBossNotificationBroadcasterSupport();
60
private final Map servers = new HashMap();
61
private static NetworkRegistry singleton;
63
public NetworkRegistry()
70
* return the singleton instance
74
public static final NetworkRegistry getInstance()
78
new NetworkRegistry();
84
* add a server for a given identity that is available on the network
89
public void addServer(final Identity identity, final ServerInvokerMetadata invokers[])
91
boolean found = false;
94
if(servers.containsKey(identity) == false)
96
servers.put(identity, new NetworkInstance(identity, invokers));
102
log.debug(this + " addServer - " + identity);
104
// put this on a separate thread so we don't block further detection ...
105
// TODO: this needs to go into a thread pool thread -JGH
110
broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_ADDED, identity, invokers));
117
* update the invokers for a given server
122
public void updateServer(final Identity identity, final ServerInvokerMetadata invokers[])
124
boolean found = false;
126
synchronized(servers)
128
if(servers.containsKey(identity))
130
servers.put(identity, new NetworkInstance(identity, invokers));
136
// TODO: let's put this in a thread pool thread -JGH
137
// put this on a separate thread so we don't block further detection ...
142
broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_UPDATED, identity, invokers));
149
* return the servers on the network
153
public NetworkInstance[] getServers()
155
synchronized(servers)
157
return (NetworkInstance[]) servers.values().toArray(new NetworkInstance[servers.size()]);
162
* returns true if the server with the identity is available
167
public boolean hasServer(Identity identity)
169
synchronized(servers)
171
return servers.containsKey(identity);
176
* query the network registry for <tt>0..*</tt> of servers based on a
177
* filter. if the filter is null, it is considered a wildcard.
182
public NetworkInstance[] queryServers(NetworkFilter filter)
184
NetworkInstance servers[] = getServers();
185
if(servers == null || servers.length <= 0)
187
return new NetworkInstance[0];
189
Set result = new HashSet();
190
for(int c = 0; c < servers.length; c++)
192
NetworkInstance instance = (NetworkInstance) this.servers.get(servers[c]);
194
filter.filter(servers[c].getIdentity(), instance.getLocators()))
196
if(result.contains(servers[c]) == false)
198
// the filter passed, add it
199
result.add(servers[c]);
203
return (NetworkInstance[]) result.toArray(new NetworkInstance[result.size()]);
207
* remove a server no longer available on the network
211
public void removeServer(final Identity identity)
213
NetworkInstance instance = null;
215
synchronized(servers)
217
instance = (NetworkInstance) servers.remove(identity);
221
log.debug(this + " removeServer - " + identity);
223
final ServerInvokerMetadata il[] = instance.getServerInvokers();
224
// put this on a separate thread so we don't block further detection ...
225
// TODO: let's put this is a thread pool thread -JGH
230
broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_REMOVED, identity, il));
236
public void addNotificationListener(NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws IllegalArgumentException
238
broadcaster.addNotificationListener(notificationListener, notificationFilter, o);
241
public MBeanNotificationInfo[] getNotificationInfo()
243
MBeanNotificationInfo info[] = new MBeanNotificationInfo[3];
244
info[0] = new MBeanNotificationInfo(new String[]{NetworkNotification.SERVER_ADDED}, NetworkNotification.class.getName(), "Fired when Server is added");
245
info[1] = new MBeanNotificationInfo(new String[]{NetworkNotification.SERVER_UPDATED}, NetworkNotification.class.getName(), "Fired when Server is updated");
246
info[2] = new MBeanNotificationInfo(new String[]{NetworkNotification.SERVER_REMOVED}, NetworkNotification.class.getName(), "Fired when Server is removed");
250
public void removeNotificationListener(NotificationListener notificationListener) throws ListenerNotFoundException
252
broadcaster.removeNotificationListener(notificationListener);
255
public void postDeregister()
259
public void postRegister(Boolean aBoolean)
263
public void preDeregister() throws Exception
267
public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) throws Exception
269
this.mBeanServer = mBeanServer;
270
this.objectName = objectName;
271
// make sure our identity system property is properly set
272
Identity identity = Identity.get(this.mBeanServer);
273
// this is a slight hack, but we have to have some way to know the main
274
// JBoss MBeanServer data
275
setSystemProperty("jboss.remoting.jmxid", identity.getJMXId());
276
setSystemProperty("jboss.remoting.instanceid", identity.getInstanceId());
277
setSystemProperty("jboss.remoting.domain", identity.getDomain());
282
* change the main domain of the local server
286
public synchronized void changeDomain(String newDomain)
288
setSystemProperty("jboss.remoting.domain", newDomain);
289
NetworkInstance servers[] = getServers();
290
if(servers == null || servers.length <= 0)
294
// remove entries that don't match out new domain
295
for(int c = 0; c < servers.length; c++)
297
NetworkInstance instance = (NetworkInstance) this.servers.get(servers[c]);
298
if(newDomain.equals(instance.getIdentity().getDomain()) == false)
300
this.servers.remove(servers[c]);
307
broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.DOMAIN_CHANGED, Identity.get(mBeanServer), InvokerRegistry.getRegisteredServerLocators()));
312
static private void setSystemProperty(final String name, final String value)
314
if (SecurityUtility.skipAccessControl())
316
System.setProperty(name, value);
322
AccessController.doPrivileged( new PrivilegedExceptionAction()
324
public Object run() throws Exception
326
return System.setProperty(name, value);
330
catch (PrivilegedActionException e)
332
throw (RuntimeException) e.getCause();