~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/network/NetworkRegistry.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
 
 
23
package org.jboss.remoting.network;
 
24
 
 
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;
 
30
import java.util.Map;
 
31
import java.util.Set;
 
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;
 
44
 
 
45
/**
 
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.
 
50
 *
 
51
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 
52
 * @version $Revision: 5004 $
 
53
 */
 
54
public class NetworkRegistry implements NetworkRegistryMBean
 
55
{
 
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;
 
62
 
 
63
   public NetworkRegistry()
 
64
   {
 
65
      super();
 
66
      singleton = this;
 
67
   }
 
68
 
 
69
   /**
 
70
    * return the singleton instance
 
71
    *
 
72
    * @return
 
73
    */
 
74
   public static final NetworkRegistry getInstance()
 
75
   {
 
76
      if(singleton == null)
 
77
      {
 
78
         new NetworkRegistry();
 
79
      }
 
80
      return singleton;
 
81
   }
 
82
 
 
83
   /**
 
84
    * add a server for a given identity that is available on the network
 
85
    *
 
86
    * @param identity
 
87
    * @param invokers
 
88
    */
 
89
   public void addServer(final Identity identity, final ServerInvokerMetadata invokers[])
 
90
   {
 
91
      boolean found = false;
 
92
      synchronized(servers)
 
93
      {
 
94
         if(servers.containsKey(identity) == false)
 
95
         {
 
96
            servers.put(identity, new NetworkInstance(identity, invokers));
 
97
            found = true;
 
98
         }
 
99
      }
 
100
      if(found)
 
101
      {
 
102
         log.debug(this + " addServer - " + identity);
 
103
 
 
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
 
106
         new Thread()
 
107
         {
 
108
            public void run()
 
109
            {
 
110
               broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_ADDED, identity, invokers));
 
111
            }
 
112
         }.start();
 
113
      }
 
114
   }
 
115
 
 
116
   /**
 
117
    * update the invokers for a given server
 
118
    *
 
119
    * @param identity
 
120
    * @param invokers
 
121
    */
 
122
   public void updateServer(final Identity identity, final ServerInvokerMetadata invokers[])
 
123
   {
 
124
      boolean found = false;
 
125
 
 
126
      synchronized(servers)
 
127
      {
 
128
         if(servers.containsKey(identity))
 
129
         {
 
130
            servers.put(identity, new NetworkInstance(identity, invokers));
 
131
            found = true;
 
132
         }
 
133
      }
 
134
      if(found)
 
135
      {
 
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 ...
 
138
         new Thread()
 
139
         {
 
140
            public void run()
 
141
            {
 
142
               broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_UPDATED, identity, invokers));
 
143
            }
 
144
         }.start();
 
145
      }
 
146
   }
 
147
 
 
148
   /**
 
149
    * return the servers on the network
 
150
    *
 
151
    * @return
 
152
    */
 
153
   public NetworkInstance[] getServers()
 
154
   {
 
155
      synchronized(servers)
 
156
      {
 
157
         return (NetworkInstance[]) servers.values().toArray(new NetworkInstance[servers.size()]);
 
158
      }
 
159
   }
 
160
 
 
161
   /**
 
162
    * returns true if the server with the identity is available
 
163
    *
 
164
    * @param identity
 
165
    * @return
 
166
    */
 
167
   public boolean hasServer(Identity identity)
 
168
   {
 
169
      synchronized(servers)
 
170
      {
 
171
         return servers.containsKey(identity);
 
172
      }
 
173
   }
 
174
 
 
175
   /**
 
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.
 
178
    *
 
179
    * @param filter
 
180
    * @return
 
181
    */
 
182
   public NetworkInstance[] queryServers(NetworkFilter filter)
 
183
   {
 
184
      NetworkInstance servers[] = getServers();
 
185
      if(servers == null || servers.length <= 0)
 
186
      {
 
187
         return new NetworkInstance[0];
 
188
      }
 
189
      Set result = new HashSet();
 
190
      for(int c = 0; c < servers.length; c++)
 
191
      {
 
192
         NetworkInstance instance = (NetworkInstance) this.servers.get(servers[c]);
 
193
         if(filter == null ||
 
194
            filter.filter(servers[c].getIdentity(), instance.getLocators()))
 
195
         {
 
196
            if(result.contains(servers[c]) == false)
 
197
            {
 
198
               // the filter passed, add it
 
199
               result.add(servers[c]);
 
200
            }
 
201
         }
 
202
      }
 
203
      return (NetworkInstance[]) result.toArray(new NetworkInstance[result.size()]);
 
204
   }
 
205
 
 
206
   /**
 
207
    * remove a server no longer available on the network
 
208
    *
 
209
    * @param identity
 
210
    */
 
211
   public void removeServer(final Identity identity)
 
212
   {
 
213
      NetworkInstance instance = null;
 
214
 
 
215
      synchronized(servers)
 
216
      {
 
217
         instance = (NetworkInstance) servers.remove(identity);
 
218
      }
 
219
      if(instance != null)
 
220
      {
 
221
         log.debug(this + " removeServer - " + identity);
 
222
 
 
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
 
226
         new Thread()
 
227
         {
 
228
            public void run()
 
229
            {
 
230
               broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.SERVER_REMOVED, identity, il));
 
231
            }
 
232
         }.start();
 
233
      }
 
234
   }
 
235
 
 
236
   public void addNotificationListener(NotificationListener notificationListener, NotificationFilter notificationFilter, Object o) throws IllegalArgumentException
 
237
   {
 
238
      broadcaster.addNotificationListener(notificationListener, notificationFilter, o);
 
239
   }
 
240
 
 
241
   public MBeanNotificationInfo[] getNotificationInfo()
 
242
   {
 
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");
 
247
      return info;
 
248
   }
 
249
 
 
250
   public void removeNotificationListener(NotificationListener notificationListener) throws ListenerNotFoundException
 
251
   {
 
252
      broadcaster.removeNotificationListener(notificationListener);
 
253
   }
 
254
 
 
255
   public void postDeregister()
 
256
   {
 
257
   }
 
258
 
 
259
   public void postRegister(Boolean aBoolean)
 
260
   {
 
261
   }
 
262
 
 
263
   public void preDeregister() throws Exception
 
264
   {
 
265
   }
 
266
 
 
267
   public ObjectName preRegister(MBeanServer mBeanServer, ObjectName objectName) throws Exception
 
268
   {
 
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());
 
278
      return objectName;
 
279
   }
 
280
 
 
281
   /**
 
282
    * change the main domain of the local server
 
283
    *
 
284
    * @param newDomain
 
285
    */
 
286
   public synchronized void changeDomain(String newDomain)
 
287
   {
 
288
      setSystemProperty("jboss.remoting.domain", newDomain);
 
289
      NetworkInstance servers[] = getServers();
 
290
      if(servers == null || servers.length <= 0)
 
291
      {
 
292
         return;
 
293
      }
 
294
      // remove entries that don't match out new domain
 
295
      for(int c = 0; c < servers.length; c++)
 
296
      {
 
297
         NetworkInstance instance = (NetworkInstance) this.servers.get(servers[c]);
 
298
         if(newDomain.equals(instance.getIdentity().getDomain()) == false)
 
299
         {
 
300
            this.servers.remove(servers[c]);
 
301
         }
 
302
      }
 
303
      new Thread()
 
304
      {
 
305
         public void run()
 
306
         {
 
307
            broadcaster.sendNotification(new NetworkNotification(objectName, NetworkNotification.DOMAIN_CHANGED, Identity.get(mBeanServer), InvokerRegistry.getRegisteredServerLocators()));
 
308
         }
 
309
      }.start();
 
310
   }
 
311
   
 
312
   static private void setSystemProperty(final String name, final String value)
 
313
   {
 
314
      if (SecurityUtility.skipAccessControl())
 
315
      {
 
316
         System.setProperty(name, value);
 
317
         return;
 
318
      }
 
319
      
 
320
      try
 
321
      {
 
322
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
323
         {
 
324
            public Object run() throws Exception
 
325
            {
 
326
               return System.setProperty(name, value);
 
327
            }
 
328
         });
 
329
      }
 
330
      catch (PrivilegedActionException e)
 
331
      {
 
332
         throw (RuntimeException) e.getCause();
 
333
      }
 
334
   }
 
335
}