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

« back to all changes in this revision

Viewing changes to .pc/0001-convert-to-official-Java-concurrent-packages.patch/src/main/org/jboss/remoting/transport/bisocket/BisocketClientInvoker.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.transport.bisocket;
 
24
 
 
25
import java.io.IOException;
 
26
import java.io.OutputStream;
 
27
import java.lang.reflect.Method;
 
28
import java.net.Socket;
 
29
import java.security.AccessController;
 
30
import java.security.PrivilegedActionException;
 
31
import java.security.PrivilegedExceptionAction;
 
32
import java.util.Collections;
 
33
import java.util.HashMap;
 
34
import java.util.HashSet;
 
35
import java.util.Iterator;
 
36
import java.util.LinkedList;
 
37
import java.util.Map;
 
38
import java.util.Set;
 
39
import java.util.Timer;
 
40
import java.util.TimerTask;
 
41
 
 
42
import org.jboss.logging.Logger;
 
43
import org.jboss.remoting.Client;
 
44
import org.jboss.remoting.ConnectionFailedException;
 
45
import org.jboss.remoting.InvocationRequest;
 
46
import org.jboss.remoting.InvokerLocator;
 
47
import org.jboss.remoting.invocation.InternalInvocation;
 
48
import org.jboss.remoting.marshal.Marshaller;
 
49
import org.jboss.remoting.marshal.UnMarshaller;
 
50
import org.jboss.remoting.transport.BidirectionalClientInvoker;
 
51
import org.jboss.remoting.transport.socket.SocketClientInvoker;
 
52
import org.jboss.remoting.transport.socket.SocketWrapper;
 
53
import org.jboss.remoting.util.SecurityUtility;
 
54
 
 
55
import EDU.oswego.cs.dl.util.concurrent.Semaphore;
 
56
 
 
57
/**
 
58
 * The bisocket transport, an extension of the socket transport, is designed to allow
 
59
 * a callback server to function behind a firewall.  All connections are created by
 
60
 * a Socket constructor or factory on the client side connecting to a ServerSocket on
 
61
 * the server side.  When a callback client invoker on the server side needs to
 
62
 * open a connection to the callback server, it requests a connection by sending a
 
63
 * request message over a control connection to the client side.
 
64
 *
 
65
 * Because all connections are created in one direction, the bisocket transport is
 
66
 * asymmetric, in the sense that client invokers and server invokers behave differently
 
67
 * on the client side and on the server side.
 
68
 *
 
69
 *
 
70
 *
 
71
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
 
72
 */
 
73
public class BisocketClientInvoker
 
74
extends SocketClientInvoker
 
75
implements BidirectionalClientInvoker
 
76
{
 
77
   private static final Logger log = Logger.getLogger(BisocketClientInvoker.class);
 
78
   private static Map listenerIdToClientInvokerMap = Collections.synchronizedMap(new HashMap());
 
79
   private static Map listenerIdToCallbackClientInvokerMap = Collections.synchronizedMap(new HashMap());
 
80
   private static Map listenerIdToSocketsMap = new HashMap();
 
81
   private static Map listenerIdToControlSocketsMap = new HashMap();
 
82
   private static Timer timer;
 
83
   private static Object timerLock = new Object();
 
84
 
 
85
   protected String listenerId;
 
86
 
 
87
   private int pingFrequency = Bisocket.PING_FREQUENCY_DEFAULT;
 
88
   private int pingWindowFactor = Bisocket.PING_WINDOW_FACTOR_DEFAULT;
 
89
   private int pingWindow = pingWindowFactor * pingFrequency;
 
90
   private int maxRetries = Bisocket.MAX_RETRIES_DEFAULT;
 
91
   private Socket controlSocket;
 
92
   private OutputStream controlOutputStream;
 
93
   private Object controlLock = new Object();
 
94
   private PingTimerTask pingTimerTask;
 
95
   protected boolean isCallbackInvoker;
 
96
   protected BooleanHolder pingFailed = new BooleanHolder(false);
 
97
 
 
98
 
 
99
   /**
 
100
    * @param listenerId
 
101
    * @return
 
102
    */
 
103
   static BisocketClientInvoker getBisocketClientInvoker(String listenerId)
 
104
   {
 
105
      return (BisocketClientInvoker) listenerIdToClientInvokerMap.get(listenerId);
 
106
   }
 
107
 
 
108
 
 
109
   static BisocketClientInvoker getBisocketCallbackClientInvoker(String listenerId)
 
110
   {
 
111
      return (BisocketClientInvoker) listenerIdToCallbackClientInvokerMap.get(listenerId);
 
112
   }
 
113
   
 
114
   
 
115
   static void removeBisocketClientInvoker(String listenerId)
 
116
   {
 
117
      listenerIdToClientInvokerMap.remove(listenerId);
 
118
   }
 
119
 
 
120
 
 
121
   static void transferSocket(String listenerId, Socket socket, boolean isControlSocket)
 
122
   {
 
123
      Set sockets = null;
 
124
      
 
125
      if (isControlSocket)
 
126
      {
 
127
         synchronized (listenerIdToControlSocketsMap)
 
128
         {
 
129
            sockets = (Set) listenerIdToControlSocketsMap.get(listenerId);
 
130
            if (sockets == null)
 
131
            {
 
132
               sockets = new HashSet();
 
133
               listenerIdToControlSocketsMap.put(listenerId, sockets);
 
134
            }
 
135
         }
 
136
      }
 
137
      else
 
138
      {
 
139
         synchronized (listenerIdToSocketsMap)
 
140
         {
 
141
            sockets = (Set) listenerIdToSocketsMap.get(listenerId);
 
142
            if (sockets == null)
 
143
            {
 
144
               sockets = new HashSet();
 
145
               listenerIdToSocketsMap.put(listenerId, sockets);
 
146
            }
 
147
         }
 
148
      }
 
149
 
 
150
      synchronized (sockets)
 
151
      {
 
152
         sockets.add(socket);
 
153
         sockets.notify();
 
154
      }
 
155
   }
 
156
 
 
157
 
 
158
   public BisocketClientInvoker(InvokerLocator locator) throws IOException
 
159
   {
 
160
      this(locator, null);
 
161
   }
 
162
 
 
163
 
 
164
   public BisocketClientInvoker(InvokerLocator locator, Map config) throws IOException
 
165
   {
 
166
      super(locator, config);
 
167
 
 
168
      if (configuration != null)
 
169
      {
 
170
         listenerId = (String) configuration.get(Client.LISTENER_ID_KEY);
 
171
         if (listenerId != null)
 
172
         {
 
173
            isCallbackInvoker = true;
 
174
            listenerIdToCallbackClientInvokerMap.put(listenerId, this);
 
175
            log.debug(this + " :registered " + listenerId + " -> " + this);
 
176
         }
 
177
 
 
178
         // look for pingFrequency param
 
179
         Object val = configuration.get(Bisocket.PING_FREQUENCY);
 
180
         if (val != null)
 
181
         {
 
182
            try
 
183
            {
 
184
               int nVal = Integer.valueOf((String) val).intValue();
 
185
               pingFrequency = nVal;
 
186
               log.debug("Setting ping frequency to: " + pingFrequency);
 
187
            }
 
188
            catch (Exception e)
 
189
            {
 
190
               log.warn("Could not convert " + Bisocket.PING_FREQUENCY +
 
191
                     " value of " + val + " to an int value.");
 
192
            }
 
193
         }
 
194
         
 
195
         val = configuration.get(Bisocket.PING_WINDOW_FACTOR);
 
196
         if (val != null && val instanceof String && ((String) val).length() > 0)
 
197
         {
 
198
            try
 
199
            {
 
200
               pingWindowFactor = Integer.valueOf(((String) val)).intValue();
 
201
               log.debug(this + " setting pingWindowFactor to " + pingWindowFactor);
 
202
            }
 
203
            catch (NumberFormatException e)
 
204
            {
 
205
               log.warn("Invalid format for " + "\"" + Bisocket.PING_WINDOW_FACTOR + "\": " + val);
 
206
            }
 
207
         }
 
208
         else if (val != null)
 
209
         {
 
210
            log.warn("\"" + Bisocket.PING_WINDOW_FACTOR + "\" must be specified as a String");
 
211
         }
 
212
         
 
213
         pingWindow = pingWindowFactor * pingFrequency;
 
214
         
 
215
         val = configuration.get(Bisocket.MAX_RETRIES);
 
216
         if (val != null)
 
217
         {
 
218
            try
 
219
            {
 
220
               int nVal = Integer.valueOf((String) val).intValue();
 
221
               maxRetries = nVal;
 
222
               log.debug("Setting retry limit: " + maxRetries);
 
223
            }
 
224
            catch (Exception e)
 
225
            {
 
226
               log.warn("Could not convert " + Bisocket.MAX_RETRIES +
 
227
                     " value of " + val + " to an int value.");
 
228
            }
 
229
         }
 
230
      }
 
231
   }
 
232
 
 
233
   public int getMaxRetries()
 
234
   {
 
235
      return maxRetries;
 
236
   }
 
237
 
 
238
 
 
239
   public void setMaxRetries(int maxRetries)
 
240
   {
 
241
      this.maxRetries = maxRetries;
 
242
   }
 
243
   
 
244
 
 
245
   public int getPingFrequency()
 
246
   {
 
247
      return pingFrequency;
 
248
   }
 
249
 
 
250
 
 
251
   public void setPingFrequency(int pingFrequency)
 
252
   {
 
253
      this.pingFrequency = pingFrequency;
 
254
   }
 
255
 
 
256
   
 
257
   public int getPingWindowFactor()
 
258
   {
 
259
      return pingWindowFactor;
 
260
   }
 
261
   
 
262
   
 
263
   public void setPingWindowFactor(int pingWindowFactor)
 
264
   {
 
265
      this.pingWindowFactor = pingWindowFactor;
 
266
      pingWindow = pingWindowFactor * pingFrequency;
 
267
   }
 
268
   
 
269
   
 
270
   protected void handleConnect() throws ConnectionFailedException
 
271
   {
 
272
      // Callback client on server side.
 
273
      if (isCallbackInvoker)
 
274
      {
 
275
         Set sockets = null;
 
276
 
 
277
         synchronized (listenerIdToControlSocketsMap)
 
278
         {
 
279
            sockets = (Set) listenerIdToControlSocketsMap.get(listenerId);
 
280
            if (sockets == null)
 
281
            {
 
282
               sockets = new HashSet();
 
283
               listenerIdToControlSocketsMap.put(listenerId, sockets);
 
284
            }
 
285
         }
 
286
 
 
287
         synchronized (sockets)
 
288
         {
 
289
            if (sockets.isEmpty())
 
290
            {
 
291
               long wait = timeout; 
 
292
               long start = System.currentTimeMillis(); 
 
293
               
 
294
               while (timeout == 0 || wait > 0)
 
295
               {
 
296
                  try
 
297
                  {
 
298
                     sockets.wait(wait);
 
299
                     break;
 
300
                  }
 
301
                  catch (InterruptedException e)
 
302
                  {
 
303
                     log.debug("unexpected interrupt");
 
304
                     if (timeout > 0)
 
305
                        wait = timeout - (System.currentTimeMillis() - start);
 
306
                  }
 
307
               }
 
308
            }
 
309
            
 
310
            if (sockets.isEmpty())
 
311
               throw new ConnectionFailedException("Timed out trying to create control socket");
 
312
 
 
313
            Iterator it = sockets.iterator();
 
314
            controlSocket = (Socket) it.next();
 
315
            it.remove();
 
316
            try
 
317
            {
 
318
               controlOutputStream = controlSocket.getOutputStream();
 
319
            }
 
320
            catch (IOException e1)
 
321
            {
 
322
               throw new ConnectionFailedException("Unable to get control socket output stream");
 
323
            }
 
324
            log.debug("got control socket( " + listenerId + "): " + controlSocket);
 
325
 
 
326
            if (pingFrequency > 0)
 
327
            {
 
328
               pingTimerTask = new PingTimerTask(this);
 
329
 
 
330
               synchronized (timerLock)
 
331
               {
 
332
                  if (timer == null)
 
333
                  {
 
334
                     timer = new Timer(true);
 
335
                  }
 
336
                  try
 
337
                  {
 
338
                     timer.schedule(pingTimerTask, pingFrequency, pingFrequency);
 
339
                  }
 
340
                  catch (IllegalStateException e)
 
341
                  {
 
342
                     log.debug("Unable to schedule TimerTask on existing Timer", e);
 
343
                     timer = new Timer(true);
 
344
                     timer.schedule(pingTimerTask, pingFrequency, pingFrequency);
 
345
                  }
 
346
               }
 
347
            }
 
348
         }
 
349
 
 
350
         // Bisocket callback client invoker doesn't share socket pools because of the danger
 
351
         // that two distinct callback servers could have the same "artifical" port.
 
352
         pool = new LinkedList();
 
353
         log.debug("Creating semaphore with size " + maxPoolSize);
 
354
         semaphore = new Semaphore(maxPoolSize);
 
355
         return;
 
356
      }
 
357
 
 
358
      // Client on client side.
 
359
      super.handleConnect();
 
360
   }
 
361
   
 
362
   
 
363
   protected void handleDisconnect()
 
364
   {
 
365
      if (listenerId != null)
 
366
      {
 
367
         if (isCallbackInvoker)
 
368
         {
 
369
            if (controlSocket != null)
 
370
            {
 
371
               try
 
372
               {
 
373
                  controlSocket.close();
 
374
               }
 
375
               catch (IOException e)
 
376
               {
 
377
                  log.debug("unable to close control socket: " + controlSocket);
 
378
               }
 
379
            }
 
380
 
 
381
            listenerIdToCallbackClientInvokerMap.remove(listenerId);
 
382
            for (Iterator it = pool.iterator(); it.hasNext();)
 
383
            {
 
384
               SocketWrapper socketWrapper = (SocketWrapper) it.next();
 
385
               try
 
386
               {
 
387
                  socketWrapper.close();
 
388
               }
 
389
               catch (Exception ignored)
 
390
               {
 
391
               }
 
392
            }
 
393
         }
 
394
         else
 
395
         {
 
396
            listenerIdToClientInvokerMap.remove(listenerId);
 
397
            super.handleDisconnect();
 
398
         }
 
399
 
 
400
         synchronized (listenerIdToControlSocketsMap)
 
401
         {
 
402
            listenerIdToControlSocketsMap.remove(listenerId);
 
403
         }
 
404
         
 
405
         Set sockets = null;
 
406
         synchronized (listenerIdToSocketsMap)
 
407
         {
 
408
            sockets = (Set) listenerIdToSocketsMap.remove(listenerId);
 
409
         }
 
410
         
 
411
         // Wake up any threads blocked in createSocket().
 
412
         if (sockets != null)
 
413
         {
 
414
            synchronized (sockets)
 
415
            {
 
416
               sockets.notifyAll();
 
417
            }
 
418
         }
 
419
         
 
420
         if (pingTimerTask != null)
 
421
            pingTimerTask.shutDown();
 
422
      }
 
423
      else
 
424
      {
 
425
         super.handleDisconnect();
 
426
      }
 
427
   }
 
428
 
 
429
 
 
430
   protected Object transport(String sessionId, Object invocation, Map metadata,
 
431
                              Marshaller marshaller, UnMarshaller unmarshaller)
 
432
   throws IOException, ConnectionFailedException, ClassNotFoundException
 
433
   {
 
434
      String listenerId = null;
 
435
      if (invocation instanceof InvocationRequest)
 
436
      {
 
437
         InvocationRequest ir = (InvocationRequest) invocation;
 
438
         Object o = ir.getParameter();
 
439
         if (o instanceof InternalInvocation)
 
440
         {
 
441
            InternalInvocation ii = (InternalInvocation) o;
 
442
            if (InternalInvocation.ADDLISTENER.equals(ii.getMethodName())
 
443
                && ir.getLocator() != null) // getLocator() == null for pull callbacks
 
444
            {
 
445
               Map requestPayload = ir.getRequestPayload();
 
446
               listenerId = (String) requestPayload.get(Client.LISTENER_ID_KEY);
 
447
               listenerIdToClientInvokerMap.put(listenerId, this);
 
448
               BisocketServerInvoker callbackServerInvoker;
 
449
               callbackServerInvoker = BisocketServerInvoker.getBisocketServerInvoker(listenerId);
 
450
               callbackServerInvoker.createControlConnection(listenerId, true);
 
451
            }
 
452
            
 
453
            // Rather than handle the REMOVELISTENER case symmetrically, it is
 
454
            // handled when a REMOVECLIENTLISTENER message is received by
 
455
            // BisocketServerInvoker.handleInternalInvocation().  The reason is that
 
456
            // if the Client executes removeListener() with disconnectTimeout == 0, 
 
457
            // no REMOVELISTENER message will be sent.
 
458
         }
 
459
      }
 
460
 
 
461
      return super.transport(sessionId, invocation, metadata, marshaller, unmarshaller);
 
462
   }
 
463
 
 
464
 
 
465
   protected Socket createSocket(String address, int port, int timeout) throws IOException
 
466
   {
 
467
      if (!isCallbackInvoker)
 
468
         return super.createSocket(address, port, timeout);
 
469
 
 
470
      if (timeout < 0)
 
471
      {
 
472
         timeout = getTimeout();
 
473
         if (timeout < 0)
 
474
            timeout = 0;
 
475
      }
 
476
      
 
477
      Set sockets = null;
 
478
 
 
479
      synchronized (listenerIdToSocketsMap)
 
480
      {
 
481
         sockets = (Set) listenerIdToSocketsMap.get(listenerId);
 
482
 
 
483
         if (sockets == null)
 
484
         {
 
485
            sockets = new HashSet();
 
486
            listenerIdToSocketsMap.put(listenerId, sockets);
 
487
         }
 
488
      }
 
489
 
 
490
      synchronized (controlLock)
 
491
      {
 
492
         if (log.isTraceEnabled()) log.trace(this + " writing Bisocket.CREATE_ORDINARY_SOCKET on " + controlOutputStream);
 
493
         try
 
494
         {
 
495
            controlOutputStream.write(Bisocket.CREATE_ORDINARY_SOCKET);
 
496
            if (log.isTraceEnabled()) log.trace(this + " wrote Bisocket.CREATE_ORDINARY_SOCKET");
 
497
            
 
498
            synchronized (sockets)
 
499
            {
 
500
               if (!sockets.isEmpty())
 
501
               {
 
502
                  Iterator it = sockets.iterator();
 
503
                  Socket socket = (Socket) it.next();
 
504
                  it.remove();
 
505
                  log.debug(this + " found socket (" + listenerId + "): " + socket);
 
506
                  return socket;
 
507
               }
 
508
            }
 
509
         }
 
510
         catch (IOException e)
 
511
         {
 
512
            log.debug(this + " unable to write Bisocket.CREATE_ORDINARY_SOCKET", e);
 
513
         }
 
514
      }
 
515
      
 
516
      long timeRemaining = timeout; 
 
517
      long pingFailedWindow = 2 * pingWindow;
 
518
      long pingFailedTimeRemaining = pingFailedWindow;
 
519
      long start = System.currentTimeMillis();
 
520
      OutputStream savedControlOutputStream = controlOutputStream;
 
521
 
 
522
      while (isConnected() && (!pingFailed.flag || pingFailedTimeRemaining > 0) && (timeout == 0 || timeRemaining > 0))
 
523
      {
 
524
         synchronized (sockets)
 
525
         {  
 
526
            try
 
527
            {
 
528
               sockets.wait(1000);
 
529
            }
 
530
            catch (InterruptedException e)
 
531
            {
 
532
               log.debug(this + " unexpected interrupt");
 
533
            }
 
534
            
 
535
            if (!sockets.isEmpty())
 
536
            {
 
537
               Iterator it = sockets.iterator();
 
538
               Socket socket = (Socket) it.next();
 
539
               it.remove();
 
540
               log.debug(this + " found socket (" + listenerId + "): " + socket);
 
541
               return socket;
 
542
            }
 
543
         }
 
544
         
 
545
         if (savedControlOutputStream != controlOutputStream)
 
546
         {
 
547
            savedControlOutputStream = controlOutputStream;
 
548
            log.debug(this + " rewriting Bisocket.CREATE_ORDINARY_SOCKET on " + controlOutputStream);
 
549
            try
 
550
            {
 
551
               controlOutputStream.write(Bisocket.CREATE_ORDINARY_SOCKET);
 
552
               log.debug(this + " rewrote Bisocket.CREATE_ORDINARY_SOCKET");
 
553
            }
 
554
            catch (IOException e)
 
555
            {
 
556
               log.debug(this + " unable to rewrite Bisocket.CREATE_ORDINARY_SOCKET" + e.getMessage());
 
557
            }
 
558
         }
 
559
         
 
560
         long elapsed = System.currentTimeMillis() - start;
 
561
         if (timeout > 0)
 
562
            timeRemaining = timeout - elapsed;
 
563
         pingFailedTimeRemaining = pingFailedWindow - elapsed; 
 
564
      }
 
565
 
 
566
      if (!isConnected())
 
567
      {
 
568
         throw new IOException("Connection is closed");
 
569
      }
 
570
      
 
571
      if (pingFailed.flag)
 
572
      {
 
573
         throw new IOException("Unable to create socket");
 
574
      }
 
575
 
 
576
      throw new IOException("Timed out trying to create socket");
 
577
   }
 
578
 
 
579
 
 
580
   void replaceControlSocket(Socket socket) throws IOException
 
581
   {
 
582
      synchronized (controlLock)
 
583
      {
 
584
         if (controlSocket != null)
 
585
         {
 
586
            controlSocket.close();
 
587
         }
 
588
         
 
589
         log.debug(this + " replacing control socket: " + controlSocket);
 
590
         controlSocket = socket;
 
591
         log.debug(this + " control socket replaced by: " + socket);
 
592
         controlOutputStream = controlSocket.getOutputStream();
 
593
         log.debug("controlOutputStream replaced by: " + controlOutputStream);
 
594
      }
 
595
 
 
596
      if (pingTimerTask != null)
 
597
         pingTimerTask.cancel();
 
598
 
 
599
      if (pingFrequency > 0)
 
600
      {
 
601
         pingTimerTask = new PingTimerTask(this);
 
602
 
 
603
         synchronized (timerLock)
 
604
         {
 
605
            if (timer == null)
 
606
            {
 
607
               timer = new Timer(true);
 
608
            }
 
609
            try
 
610
            {
 
611
               timer.schedule(pingTimerTask, pingFrequency, pingFrequency);
 
612
            }
 
613
            catch (IllegalStateException e)
 
614
            {
 
615
               log.debug("Unable to schedule TimerTask on existing Timer", e);
 
616
               timer = new Timer(true);
 
617
               timer.schedule(pingTimerTask, pingFrequency, pingFrequency);
 
618
            }
 
619
         }
 
620
      }
 
621
   }
 
622
 
 
623
 
 
624
   InvokerLocator getSecondaryLocator() throws Throwable
 
625
   {
 
626
      InternalInvocation ii = new InternalInvocation(Bisocket.GET_SECONDARY_INVOKER_LOCATOR, null);
 
627
      InvocationRequest r = new InvocationRequest(null, null, ii, null, null, null);
 
628
      log.debug("getting secondary locator");
 
629
      Exception savedException = null;
 
630
      
 
631
      for (int i = 0; i < maxRetries; i++)
 
632
      {
 
633
         try
 
634
         {
 
635
            Object o = invoke(r);
 
636
            log.debug("secondary locator: " + o);
 
637
            return (InvokerLocator) o;
 
638
         }
 
639
         catch (Exception e)
 
640
         {
 
641
            savedException = e;
 
642
            log.debug("unable to get secondary locator: trying again");
 
643
         }
 
644
      }
 
645
      
 
646
      throw savedException;
 
647
   }
 
648
 
 
649
 
 
650
   public InvokerLocator getCallbackLocator(Map metadata)
 
651
   {
 
652
      String transport = (String) metadata.get(Client.CALLBACK_SERVER_PROTOCOL);
 
653
      String host = (String) metadata.get(Client.CALLBACK_SERVER_HOST);
 
654
      String sPort = (String) metadata.get(Client.CALLBACK_SERVER_PORT);
 
655
      int port = -1;
 
656
      if (sPort != null)
 
657
      {
 
658
         try
 
659
         {
 
660
            port = Integer.parseInt(sPort);
 
661
         }
 
662
         catch (NumberFormatException e)
 
663
         {
 
664
            throw new RuntimeException("Can not set internal callback server port as configuration value (" + sPort + " is not a number.");
 
665
         }
 
666
      }
 
667
 
 
668
      return new InvokerLocator(transport, host, port, "callback", metadata);
 
669
   }
 
670
 
 
671
 
 
672
   static class PingTimerTask extends TimerTask
 
673
   {
 
674
      private Object controlLock;
 
675
      private OutputStream controlOutputStream;
 
676
      private int maxRetries;
 
677
      private Exception savedException;
 
678
      private boolean running = true;
 
679
      private boolean pingSent;
 
680
      private BooleanHolder pingFailed;
 
681
      
 
682
      PingTimerTask(BisocketClientInvoker invoker)
 
683
      {
 
684
         controlLock = invoker.controlLock;
 
685
         controlOutputStream = invoker.controlOutputStream;
 
686
         maxRetries = invoker.getMaxRetries();
 
687
         pingFailed = invoker.pingFailed;
 
688
         pingFailed.flag = false;
 
689
      }
 
690
      
 
691
      public void shutDown()
 
692
      {
 
693
         synchronized (controlLock)
 
694
         {
 
695
            controlOutputStream = null;
 
696
         }
 
697
         cancel();
 
698
         
 
699
         try
 
700
         {
 
701
            Method purge = getDeclaredMethod(Timer.class, "purge", new Class[]{});
 
702
            purge.invoke(timer, new Object[]{});
 
703
         }
 
704
         catch (Exception e)
 
705
         {
 
706
            log.debug("running with jdk 1.4: unable to purge Timer");
 
707
         }
 
708
      }
 
709
 
 
710
      public void run()
 
711
      {     
 
712
         pingSent = false;
 
713
         
 
714
         for (int i = 0; i < maxRetries; i++)
 
715
         {
 
716
            try
 
717
            {
 
718
               synchronized (controlLock)
 
719
               {
 
720
                  if (!running)
 
721
                     return;
 
722
                     
 
723
                  controlOutputStream.write(Bisocket.PING);
 
724
               }
 
725
               pingSent = true;
 
726
               break;
 
727
            }
 
728
            catch (Exception e)
 
729
            {
 
730
               savedException = e;
 
731
               log.debug("Unable to send ping: trying again");
 
732
            }
 
733
         }
 
734
         
 
735
         if (!running)
 
736
            return;
 
737
         
 
738
         if (!pingSent)
 
739
         {
 
740
            log.warn("Unable to send ping: shutting down PingTimerTask", savedException);
 
741
            pingFailed.flag = true;
 
742
            shutDown();  
 
743
         }
 
744
      }
 
745
   }
 
746
   
 
747
   static class BooleanHolder
 
748
   {
 
749
      public boolean flag;
 
750
      
 
751
      public BooleanHolder(boolean flag)
 
752
      {
 
753
         this.flag = flag;
 
754
      }
 
755
   }
 
756
   
 
757
   static private Method getDeclaredMethod(final Class c, final String name, final Class[] parameterTypes)
 
758
   throws NoSuchMethodException
 
759
   {
 
760
      if (SecurityUtility.skipAccessControl())
 
761
      {
 
762
         Method m = c.getDeclaredMethod(name, parameterTypes);
 
763
         m.setAccessible(true);
 
764
         return m;
 
765
      }
 
766
 
 
767
      try
 
768
      {
 
769
         return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
 
770
         {
 
771
            public Object run() throws NoSuchMethodException
 
772
            {
 
773
               Method m = c.getDeclaredMethod(name, parameterTypes);
 
774
               m.setAccessible(true);
 
775
               return m;
 
776
            }
 
777
         });
 
778
      }
 
779
      catch (PrivilegedActionException e)
 
780
      {
 
781
         throw (NoSuchMethodException) e.getCause();
 
782
      }
 
783
   }
 
784
}
 
 
b'\\ No newline at end of file'