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

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/InvokerRegistry.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;
24
 
 
25
 
 
26
 
import org.jboss.logging.Logger;
27
 
import org.jboss.remoting.serialization.ClassLoaderUtility;
28
 
import org.jboss.remoting.transport.ClientFactory;
29
 
import org.jboss.remoting.transport.ClientInvoker;
30
 
import org.jboss.remoting.transport.ServerFactory;
31
 
import org.jboss.remoting.transport.local.LocalClientInvoker;
32
 
import org.jboss.remoting.util.SecurityUtility;
33
 
 
34
 
import java.lang.reflect.InvocationTargetException;
35
 
import java.lang.reflect.Method;
36
 
import java.security.AccessController;
37
 
import java.security.PrivilegedActionException;
38
 
import java.security.PrivilegedExceptionAction;
39
 
import java.util.ArrayList;
40
 
import java.util.Collection;
41
 
import java.util.HashMap;
42
 
import java.util.HashSet;
43
 
import java.util.Iterator;
44
 
import java.util.List;
45
 
import java.util.Map;
46
 
import java.util.Set;
47
 
 
48
 
/**
49
 
 * InvokerRegistry is a simple registery for creating client and server side Invoker implementations,
50
 
 * getting information about the invokers and register as a invoker creator for one or more specific
51
 
 * transports.
52
 
 *
53
 
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
54
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
55
 
 * @version $Revision: 4994 $
56
 
 */
57
 
public class InvokerRegistry
58
 
{
59
 
   private static final Logger log = Logger.getLogger(InvokerRegistry.class);
60
 
 
61
 
   private static boolean trace = log.isTraceEnabled();
62
 
 
63
 
   private static final Map clientLocators = new HashMap();
64
 
   private static final Map serverLocators = new HashMap();
65
 
 
66
 
   private static final Set registeredLocators = new HashSet();
67
 
   private static final Object serverLock = new Object();
68
 
   private static final Object clientLock = new Object();
69
 
 
70
 
   private static final Map transportClientFactoryClasses = new HashMap();
71
 
   private static final Map transportServerFactoryClasses = new HashMap();
72
 
 
73
 
   /**
74
 
    * return an array of InvokerLocators that are local to this VM (server invokers)
75
 
    */
76
 
   public static final InvokerLocator[] getRegisteredServerLocators()
77
 
   {
78
 
      synchronized (serverLock)
79
 
      {
80
 
         return (InvokerLocator[]) registeredLocators.toArray(new InvokerLocator[registeredLocators.size()]);
81
 
      }
82
 
   }
83
 
 
84
 
   /**
85
 
    * return a suitable local server invoker that can service the remote invoker locator based on
86
 
    * a compatible transport
87
 
    *
88
 
    * @param remote
89
 
    */
90
 
   public static InvokerLocator getSuitableServerLocatorForRemote(InvokerLocator remote)
91
 
   {
92
 
      synchronized (serverLock)
93
 
      {
94
 
         Iterator iter = registeredLocators.iterator();
95
 
         while(iter.hasNext())
96
 
         {
97
 
            InvokerLocator l = (InvokerLocator) iter.next();
98
 
            if(l.getProtocol().equals(remote.getProtocol()))
99
 
            {
100
 
               // we found a valid transport match
101
 
               return l;
102
 
            }
103
 
         }
104
 
         return null;
105
 
      }
106
 
   }
107
 
 
108
 
   /**
109
 
    * return an array of String of the registered transports
110
 
    */
111
 
   public static final String[] getRegisteredInvokerTransports()
112
 
   {
113
 
      synchronized(clientLock)
114
 
      {
115
 
         Set set = transportClientFactoryClasses.keySet();
116
 
         String transports[] = new String[set.size()];
117
 
         return (String[]) set.toArray(transports);
118
 
      }
119
 
   }
120
 
 
121
 
   /**
122
 
    * return an array of ClientInvokers that are connected
123
 
    */
124
 
   public static final ClientInvoker[] getClientInvokers()
125
 
   {
126
 
      synchronized(clientLock)
127
 
      {
128
 
         if(clientLocators.isEmpty())
129
 
         {
130
 
            return new ClientInvoker[0];
131
 
         }
132
 
         List clientInvokerList = new ArrayList();
133
 
         Collection collection = clientLocators.values();
134
 
         Iterator itr = collection.iterator();
135
 
         while(itr.hasNext())
136
 
         {
137
 
            List holderList = (List)itr.next();
138
 
            if(holderList != null)
139
 
            {
140
 
               for(int x = 0; x < holderList.size(); x++)
141
 
               {
142
 
                  ClientInvokerHolder holder = (ClientInvokerHolder)holderList.get(x);
143
 
                  clientInvokerList.add(holder.getClientInvoker());
144
 
               }
145
 
            }
146
 
         }
147
 
 
148
 
         return (ClientInvoker[]) clientInvokerList.toArray(new ClientInvoker[clientInvokerList.size()]);
149
 
      }
150
 
   }
151
 
 
152
 
   /**
153
 
    * return an array of ServerInvokers that are connected
154
 
    */
155
 
   public static final ServerInvoker[] getServerInvokers()
156
 
   {
157
 
      synchronized(serverLock)
158
 
      {
159
 
         if(serverLocators.isEmpty())
160
 
         {
161
 
            return new ServerInvoker[0];
162
 
         }
163
 
         Collection collection = serverLocators.values();
164
 
         return (ServerInvoker[]) collection.toArray(new ServerInvoker[collection.size()]);
165
 
      }
166
 
   }
167
 
 
168
 
   /**
169
 
    * register a client/server invoker factory Class pair for a given transport
170
 
    *
171
 
    * @param transport
172
 
    * @param clientFactory implementation of org.jboss.remoting.transport.ClientFactory
173
 
    * @param serverFactory implementation of org.jboss.remoting.transport.ServerFactory
174
 
    */
175
 
   public static void registerInvokerFactories(String transport, Class clientFactory, Class serverFactory)
176
 
   {
177
 
      synchronized (clientLock)
178
 
      {
179
 
         transportClientFactoryClasses.put(transport, clientFactory);
180
 
      }
181
 
      synchronized (serverLock)
182
 
      {
183
 
         transportServerFactoryClasses.put(transport, serverFactory);
184
 
      }
185
 
   }
186
 
 
187
 
   /**
188
 
    * unregister a client/server invoker factory pair for the given transport
189
 
    *
190
 
    * @param transport
191
 
    */
192
 
   public static void unregisterInvokerFactories(String transport)
193
 
   {
194
 
      synchronized (clientLock)
195
 
      {
196
 
         transportClientFactoryClasses.remove(transport);
197
 
      }
198
 
      synchronized (serverLock)
199
 
      {
200
 
         transportServerFactoryClasses.remove(transport);
201
 
      }
202
 
   }
203
 
 
204
 
   public static void unregisterLocator(InvokerLocator locator)
205
 
   {
206
 
      synchronized (serverLock)
207
 
      {
208
 
         serverLocators.remove(locator);
209
 
         registeredLocators.remove(locator);
210
 
      }
211
 
   }
212
 
 
213
 
   /**
214
 
    * returns true if the client invoker is registered in the local JVM for a given locator
215
 
    *
216
 
    * @param locator
217
 
    */
218
 
   public static boolean isClientInvokerRegistered(InvokerLocator locator)
219
 
   {
220
 
      synchronized(clientLock)
221
 
      {
222
 
         return clientLocators.containsKey(locator);
223
 
      }
224
 
   }
225
 
 
226
 
   /**
227
 
    * Called to destroy any cached RemoteClientInvoker copies inside the registry. This method
228
 
    * must be called when it is determined that a remote server (via the locator) is no
229
 
    * longer available.
230
 
    */
231
 
   public static void destroyClientInvoker(InvokerLocator locator, Map configuration)
232
 
   {
233
 
      synchronized(clientLock)
234
 
      {
235
 
         if (trace)
236
 
         {
237
 
            log.trace("destroying client invoker " + locator + ", config " + configuration);
238
 
         }
239
 
 
240
 
         ClientInvoker invoker = decrementClientInvokerCounter(locator, configuration);
241
 
 
242
 
         if(invoker != null)
243
 
         {
244
 
            if (trace)
245
 
            {
246
 
               log.trace("disconnecting " + invoker);
247
 
            }
248
 
            invoker.disconnect();
249
 
            invoker = null;
250
 
         }
251
 
      }
252
 
   }
253
 
 
254
 
   /**
255
 
     * create a ClientInvoker instance, using the specific InvokerLocator, which is just a client-side
256
 
    * invoker to a remote server.  Will use the default configuration values for the transport.
257
 
    *
258
 
    * @param locator
259
 
    * @return
260
 
    * @throws Exception
261
 
    */
262
 
   public static ClientInvoker createClientInvoker(InvokerLocator locator)
263
 
         throws Exception
264
 
   {
265
 
      return createClientInvoker(locator, null);
266
 
   }
267
 
 
268
 
   /**
269
 
    * create a ClientInvoker instance, using the specific InvokerLocator, which is just a client-side
270
 
    * invoker to a remote server
271
 
    *
272
 
    * @param locator
273
 
    * @return
274
 
    * @throws Exception
275
 
    */
276
 
   public static ClientInvoker createClientInvoker(InvokerLocator locator, Map configuration)
277
 
         throws Exception
278
 
   {
279
 
      if(locator == null)
280
 
      {
281
 
         throw new NullPointerException("locator cannot be null");
282
 
      }
283
 
      synchronized(clientLock)
284
 
      {
285
 
         ClientInvoker invoker = getRegisteredClientInvoker(locator, configuration);
286
 
         if(invoker != null)
287
 
         {
288
 
            if(trace) { log.trace("Found and returning cached client invoker (" + invoker + ")"); }
289
 
            return invoker;
290
 
         }
291
 
 
292
 
         boolean isForceRemote = false;
293
 
         boolean isPassByValue = false;
294
 
         Map parameters = locator.getParameters();
295
 
         if(parameters != null)
296
 
         {
297
 
            String value = (String) parameters.get(InvokerLocator.BYVALUE);
298
 
            if(value != null && Boolean.valueOf(value).booleanValue())
299
 
            {
300
 
               isPassByValue = true;
301
 
            }
302
 
            value = (String) parameters.get(InvokerLocator.FORCE_REMOTE);
303
 
            if(value != null && Boolean.valueOf(value).booleanValue())
304
 
            {
305
 
               isForceRemote = true;
306
 
            }
307
 
         }
308
 
         // configuration map will override locator params
309
 
         if(configuration != null)
310
 
         {
311
 
            String value = (String) configuration.get(InvokerLocator.BYVALUE);
312
 
            if(value != null && Boolean.valueOf(value).booleanValue())
313
 
            {
314
 
               isPassByValue = true;
315
 
            }
316
 
            value = (String) configuration.get(InvokerLocator.FORCE_REMOTE);
317
 
            if(value != null && Boolean.valueOf(value).booleanValue())
318
 
            {
319
 
               isForceRemote = true;
320
 
            }
321
 
         }
322
 
 
323
 
         // Check to see if server invoker is local
324
 
         // If in server locators map, then created locally by this class
325
 
         ServerInvoker svrInvoker = null;
326
 
         if (!isForceRemote)
327
 
         {
328
 
            synchronized (serverLock)
329
 
            {
330
 
               svrInvoker = (ServerInvoker) serverLocators.get(locator);
331
 
            }
332
 
            if(svrInvoker != null)
333
 
            {
334
 
               LocalClientInvoker localInvoker = new LocalClientInvoker(locator, configuration, isPassByValue);
335
 
               // have to set reference to local server invoker so client in invoke directly
336
 
               localInvoker.setServerInvoker(svrInvoker);
337
 
               invoker = localInvoker;
338
 
               InvokerLocator l = invoker.getLocator();
339
 
 
340
 
               addRegisteredClientInvoker(invoker, l, configuration);
341
 
            }
342
 
         }
343
 
         
344
 
         if (svrInvoker == null) //not local
345
 
         {
346
 
            String protocol = locator.getProtocol();
347
 
            if(protocol == null)
348
 
            {
349
 
               throw new NullPointerException("protocol cannot be null for the locator");
350
 
            }
351
 
 
352
 
            invoker = loadClientInvoker(protocol, locator, configuration);
353
 
 
354
 
            InvokerLocator l = invoker.getLocator();
355
 
 
356
 
            addRegisteredClientInvoker(invoker, l, configuration);
357
 
         }
358
 
         return invoker;
359
 
      }
360
 
   }
361
 
 
362
 
   private static void addRegisteredClientInvoker(ClientInvoker invoker, InvokerLocator locator, Map configuration)
363
 
   {
364
 
      ClientInvokerHolder holder = new ClientInvokerHolder(invoker, configuration);
365
 
      List holderList = (List) clientLocators.get(locator);
366
 
      if (holderList != null)
367
 
      {
368
 
         if(holderList.contains(holder))
369
 
         {
370
 
            throw new RuntimeException("Registering new ClientInvoker (" + invoker + "), but it already exists.");
371
 
         }
372
 
         else
373
 
         {
374
 
            holderList.add(holder);
375
 
         }
376
 
      }
377
 
      else
378
 
      {
379
 
         holderList = new ArrayList();
380
 
         holderList.add(holder);
381
 
         clientLocators.put(locator, holderList);
382
 
      }
383
 
 
384
 
      incrementClientInvokerCounter(holder);
385
 
 
386
 
   }
387
 
 
388
 
   /**
389
 
    * This will check the internal client invoker registry to see if there is a client invoker for
390
 
    * the specified locator that also has the same config map entries.  Will return it if found, null otherwise.
391
 
    * Note, this will also increment the internal reference count for the invoker
392
 
    * @param locator
393
 
    * @param configuration
394
 
    */
395
 
   private static ClientInvoker getRegisteredClientInvoker(InvokerLocator locator, Map configuration)
396
 
   {
397
 
      ClientInvoker invoker = null;
398
 
 
399
 
      List holderList = (List) clientLocators.get(locator);
400
 
      if (holderList != null)
401
 
      {
402
 
         for (int x = 0; x < holderList.size(); x++)
403
 
         {
404
 
            ClientInvokerHolder holder = (ClientInvokerHolder) holderList.get(x);
405
 
            if (sameInvoker(holder, configuration))
406
 
            {
407
 
               incrementClientInvokerCounter(holder);
408
 
               invoker = holder.getClientInvoker();
409
 
            }
410
 
         }
411
 
      }
412
 
 
413
 
      return invoker;
414
 
 
415
 
   }
416
 
 
417
 
   private static boolean sameInvoker(ClientInvokerHolder holder, Map configuration)
418
 
   {
419
 
      boolean isSame = false;
420
 
 
421
 
      if(holder != null && holder.getClientInvoker() != null)
422
 
      {
423
 
         Map config = holder.getConfig();
424
 
         if(config == null && configuration == null)
425
 
         {
426
 
            isSame = true;
427
 
         }
428
 
         else if(config != null && configuration != null)
429
 
         {
430
 
            isSame = config.equals(configuration);
431
 
         }
432
 
      }
433
 
 
434
 
      return isSame;
435
 
   }
436
 
 
437
 
   private static void incrementClientInvokerCounter(ClientInvokerHolder holder)
438
 
   {
439
 
      holder.incrementCount();
440
 
   }
441
 
 
442
 
   private static ClientInvoker loadClientInvoker(String protocol, InvokerLocator locator, Map configuration) throws Exception
443
 
   {
444
 
      ClientInvoker clientInvoker = null;
445
 
 
446
 
      Class transportFactoryClass = getTransportClientFactory(protocol);
447
 
      if(transportFactoryClass != null)
448
 
      {
449
 
         ClientFactory transportFactory = (ClientFactory)transportFactoryClass.newInstance();
450
 
         Method getClientInvokerMethod = getMethod(transportFactoryClass,
451
 
                                                   "createClientInvoker",
452
 
                                                   new Class[] {InvokerLocator.class, Map.class});
453
 
         clientInvoker = (ClientInvoker)getClientInvokerMethod.invoke(transportFactory, new Object[] {locator, configuration});
454
 
      }
455
 
      else
456
 
      {
457
 
         throw new ClassNotFoundException("Could not find class " + transportFactoryClass);
458
 
      }
459
 
 
460
 
      return clientInvoker;
461
 
   }
462
 
 
463
 
   private static ServerInvoker loadServerInvoker(String protocol, InvokerLocator locator, Map configuration) throws Exception
464
 
   {
465
 
      ServerInvoker serverInvoker = null;
466
 
 
467
 
      Class transportFactoryClass = getTransportServerFactory(protocol);
468
 
      if(transportFactoryClass != null)
469
 
      {
470
 
         ServerFactory transportFactory = (ServerFactory)transportFactoryClass.newInstance();
471
 
         Method getServerInvokerMethod = getMethod(transportFactoryClass,
472
 
                                                   "createServerInvoker",
473
 
                                                   new Class[] {InvokerLocator.class, Map.class});         
474
 
         serverInvoker = (ServerInvoker)getServerInvokerMethod.invoke(transportFactory, new Object[] {locator, configuration});
475
 
      }
476
 
      else
477
 
      {
478
 
         throw new ClassNotFoundException("Could not find class " + transportFactoryClass);
479
 
      }
480
 
 
481
 
      return serverInvoker;
482
 
   }
483
 
 
484
 
   private static Class getTransportClientFactory(String protocol)
485
 
         throws ClassNotFoundException
486
 
   {
487
 
      Class transportFactoryClass = (Class)transportClientFactoryClasses.get(protocol);
488
 
      if(transportFactoryClass == null)
489
 
      {
490
 
         String transportFactoryClassName = "org.jboss.remoting.transport." + protocol + ".TransportClientFactory";
491
 
         transportFactoryClass = ClassLoaderUtility.loadClass(InvokerRegistry.class, transportFactoryClassName);
492
 
         transportClientFactoryClasses.put(protocol, transportFactoryClass);
493
 
      }
494
 
      return transportFactoryClass;
495
 
   }
496
 
 
497
 
   private static Class getTransportServerFactory(String protocol)
498
 
         throws ClassNotFoundException
499
 
   {
500
 
      Class transportFactoryClass = (Class)transportServerFactoryClasses.get(protocol);
501
 
      if(transportFactoryClass == null)
502
 
      {
503
 
         String transportFactoryClassName = "org.jboss.remoting.transport." + protocol + ".TransportServerFactory";
504
 
         transportFactoryClass = ClassLoaderUtility.loadClass(transportFactoryClassName, InvokerRegistry.class);
505
 
         transportServerFactoryClasses.put(protocol, transportFactoryClass);
506
 
      }
507
 
      return transportFactoryClass;
508
 
   }
509
 
 
510
 
   /**
511
 
    * returns true if the server invoker is registered in the local JVM for a given locator/handler pair
512
 
    *
513
 
    * @param locator
514
 
    */
515
 
   public static boolean isServerInvokerRegistered(InvokerLocator locator)
516
 
   {
517
 
      synchronized(serverLock)
518
 
      {
519
 
         return serverLocators.containsKey(locator);
520
 
      }
521
 
   }
522
 
 
523
 
   /**
524
 
    * create a ServerInvoker instance, using the specific Invoker locator data and an implementation of the
525
 
    * ServerInvocationHandler interface.  Will use the default configuration values for the transport.
526
 
    *
527
 
    * @param locator
528
 
    * @return
529
 
    * @throws Exception
530
 
    */
531
 
   public static ServerInvoker createServerInvoker(InvokerLocator locator)
532
 
         throws Exception
533
 
   {
534
 
      return createServerInvoker(locator, null);
535
 
   }
536
 
 
537
 
   /**
538
 
    * create a ServerInvoker instance, using the specific Invoker locator data and an implementation of the
539
 
    * ServerInvocationHandler interface along with
540
 
    *
541
 
    * @param locator
542
 
    * @return
543
 
    * @throws Exception
544
 
    */
545
 
   public static ServerInvoker createServerInvoker(InvokerLocator locator, Map configuration)
546
 
         throws Exception
547
 
   {
548
 
 
549
 
      ServerInvoker invoker = null;
550
 
      synchronized(serverLock)
551
 
      {
552
 
         invoker = (ServerInvoker) serverLocators.get(locator);
553
 
         if(invoker != null)
554
 
         {
555
 
            throw new InvalidConfigurationException("The invoker for locator (" + locator + ") is already " +
556
 
                                                    "in use by another Connector.  Either change the locator or " +
557
 
                                                    "add new handlers to existing Connector.");
558
 
         }
559
 
         String protocol = locator.getProtocol();
560
 
 
561
 
         invoker = loadServerInvoker(protocol, locator, configuration);
562
 
 
563
 
         serverLocators.put(locator, invoker);
564
 
         registeredLocators.add(invoker.getLocator());
565
 
      }
566
 
      return invoker;
567
 
   }
568
 
 
569
 
   public static void destroyServerInvoker(ServerInvoker invoker)
570
 
   {
571
 
      if(invoker != null)
572
 
      {
573
 
         InvokerLocator locator = invoker.getLocator();
574
 
         unregisterLocator(locator);
575
 
      }
576
 
   }
577
 
 
578
 
   private static ClientInvoker decrementClientInvokerCounter(InvokerLocator locator, Map configuration)
579
 
   {
580
 
      List holderList = (List)clientLocators.get(locator);
581
 
 
582
 
      if (holderList == null)
583
 
      {
584
 
         log.debug("Could not decrement client invoker counter for locator " + locator +
585
 
            " as does not exist in invoker registry.");
586
 
         return null;
587
 
      }
588
 
 
589
 
      ClientInvokerHolder holder = null;
590
 
 
591
 
      // now look for specific invoker by configuration map
592
 
      for(int x = 0; x < holderList.size(); x++)
593
 
      {
594
 
         holder = (ClientInvokerHolder)holderList.get(x);
595
 
         if(holder != null)
596
 
         {
597
 
            Map config = holder.getConfig();
598
 
            if(config == null && configuration == null)
599
 
            {
600
 
               break;
601
 
            }
602
 
            else if(config != null && configuration != null)
603
 
            {
604
 
               if(config.equals(configuration))
605
 
               {
606
 
                  break;
607
 
               }
608
 
            }
609
 
         }
610
 
      }
611
 
 
612
 
      if (holder == null)
613
 
      {
614
 
         log.debug("Could not decrement client invoker counter for locator " + locator +
615
 
                   "as does not exist in invoker registry with matching configuraion map.");
616
 
         return null;
617
 
      }
618
 
 
619
 
      ClientInvoker clientInvoker =  null;
620
 
      holder.decrementCount();
621
 
 
622
 
      if(holder.getCount() == 0)
623
 
      {
624
 
         clientInvoker = holder.getClientInvoker();
625
 
         holderList.remove(holder);
626
 
         if(holderList.isEmpty())
627
 
         {
628
 
            clientLocators.remove(locator);
629
 
         }
630
 
 
631
 
         log.debug("removed " + clientInvoker + " from registry");
632
 
      }
633
 
      else
634
 
      {
635
 
         log.debug("decremented " + holder.getClientInvoker() +
636
 
            "'s count, current count " + holder.getCount());
637
 
      }
638
 
 
639
 
      return clientInvoker;
640
 
   }
641
 
 
642
 
   /**
643
 
    * This is needed by the ServerInvoker since it may change the port being used (if port specified was <= 0) to
644
 
    * next available port.
645
 
    *
646
 
    * @param locator
647
 
    * @param newLocator
648
 
    */
649
 
   public static void updateServerInvokerLocator(InvokerLocator locator, InvokerLocator newLocator)
650
 
   {
651
 
      synchronized (serverLock)
652
 
      {
653
 
         Object si = serverLocators.get(locator);
654
 
         serverLocators.remove(locator);
655
 
         registeredLocators.remove(locator);
656
 
         serverLocators.put(newLocator, si);
657
 
         registeredLocators.add(newLocator);
658
 
      }
659
 
   }
660
 
 
661
 
   /**
662
 
    * Indicates if a specific transport protocol type (e.g. socket, sslsocket, rmi, https)
663
 
    * supports ssl.  Note: almost all transports are able to support ssl if socket/serversocket
664
 
    * factory is set with an ssl version, so this is really just a hint from the invoker implementation.
665
 
    *
666
 
    * @param transport
667
 
    * @return
668
 
    * @throws Exception
669
 
    */
670
 
   public static boolean isSSLSupported(String transport) throws Exception
671
 
   {
672
 
      boolean isSSLSupported = false;
673
 
      Class transportFactoryClass = null;
674
 
      try
675
 
      {
676
 
         synchronized (clientLock)
677
 
         {
678
 
            transportFactoryClass = getTransportClientFactory(transport);
679
 
         }
680
 
         ClientFactory clientFactory = (ClientFactory)transportFactoryClass.newInstance();
681
 
         Method meth = getMethod(transportFactoryClass, "supportsSSL", new Class[]{});         
682
 
         Boolean boolVal = (Boolean)meth.invoke(clientFactory, null);
683
 
         isSSLSupported = boolVal.booleanValue();
684
 
      }
685
 
      catch (ClassNotFoundException e)
686
 
      {
687
 
         Exception ex = new Exception("Can not verify transport (" + transport + ") supports SSL because can not find invoker implementation matching transport.");
688
 
         ex.initCause(e);
689
 
         throw ex;
690
 
      }
691
 
      catch (NoSuchMethodException e)
692
 
      {
693
 
         Exception ex = new Exception("Can not call supportsSSL method on client factory class (" + transportFactoryClass + ") as there is no such method.");
694
 
         ex.initCause(e);
695
 
         throw ex;
696
 
      }
697
 
      catch (IllegalAccessException e)
698
 
      {
699
 
         Exception ex = new Exception("Can not call create instance of client factory class (" + transportFactoryClass + ").");
700
 
         ex.initCause(e);
701
 
         throw ex;
702
 
      }
703
 
      catch (InvocationTargetException e)
704
 
      {
705
 
         Exception ex = new Exception("Can not call supportsSSL method on client factory class (" + transportFactoryClass + ").");
706
 
         ex.initCause(e);
707
 
         throw ex;
708
 
      }
709
 
      catch (InstantiationException e)
710
 
      {
711
 
         Exception ex = new Exception("Can not call supportsSSL method on client factory class (" + transportFactoryClass + ").");
712
 
         ex.initCause(e);
713
 
         throw ex;
714
 
      }
715
 
 
716
 
      return isSSLSupported;
717
 
   }
718
 
 
719
 
   public String toString()
720
 
   {
721
 
      return "InvokerRegistry[" + Integer.toHexString(hashCode()) + "]";
722
 
   }
723
 
 
724
 
   private static class ClientInvokerHolder
725
 
   {
726
 
      private ClientInvoker invoker = null;
727
 
      private Map config = null;
728
 
      private int counter = 0;
729
 
 
730
 
      public ClientInvokerHolder(ClientInvoker invoker, Map config)
731
 
      {
732
 
         this.invoker = invoker;
733
 
         this.config = config;
734
 
      }
735
 
 
736
 
      public void incrementCount()
737
 
      {
738
 
         counter++;
739
 
      }
740
 
 
741
 
      public void decrementCount()
742
 
      {
743
 
         counter--;
744
 
         if(counter < 0)
745
 
         {
746
 
            throw new RuntimeException("ClientInvokerHolder decremented to negative number for client invoker " + invoker);
747
 
         }
748
 
      }
749
 
 
750
 
      public int getCount()
751
 
      {
752
 
         return counter;
753
 
      }
754
 
 
755
 
      public ClientInvoker getClientInvoker()
756
 
      {
757
 
         return invoker;
758
 
      }
759
 
 
760
 
      public Map getConfig()
761
 
      {
762
 
         return config;
763
 
      }
764
 
 
765
 
      public boolean equals(Object o)
766
 
      {
767
 
         boolean isEqual = false;
768
 
 
769
 
         if(o instanceof ClientInvokerHolder)
770
 
         {
771
 
            ClientInvokerHolder h = (ClientInvokerHolder)o;
772
 
            if(invoker.equals(h.getClientInvoker()))
773
 
            {
774
 
               Map configuration = h.getConfig();
775
 
               if(config == null && configuration == null)
776
 
               {
777
 
                  isEqual = true;
778
 
               }
779
 
               else if(config != null && configuration != null)
780
 
               {
781
 
                  isEqual = config.equals(configuration);
782
 
               }
783
 
            }
784
 
         }
785
 
         return isEqual;
786
 
      }
787
 
 
788
 
   }
789
 
   
790
 
   static private Method getMethod(final Class c, final String name, final Class[] parameterTypes)
791
 
   throws NoSuchMethodException
792
 
   {
793
 
      if (SecurityUtility.skipAccessControl())
794
 
      {
795
 
         return c.getMethod(name, parameterTypes);
796
 
      }
797
 
 
798
 
      try
799
 
      {
800
 
         return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
801
 
         {
802
 
            public Object run() throws NoSuchMethodException
803
 
            {
804
 
               return c.getMethod(name, parameterTypes);
805
 
            }
806
 
         });
807
 
      }
808
 
      catch (PrivilegedActionException e)
809
 
      {
810
 
         throw (NoSuchMethodException) e.getCause();
811
 
      }
812
 
   }
813
 
}