KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > replication > SendHelper


1 package org.sapia.ubik.rmi.replication;
2
3 import org.sapia.ubik.net.Connection;
4 import org.sapia.ubik.net.ServerAddress;
5 import org.sapia.ubik.rmi.server.transport.Connections;
6 import org.sapia.ubik.rmi.server.transport.TransportManager;
7
8 import java.io.IOException JavaDoc;
9
10 import java.rmi.RemoteException JavaDoc;
11
12
13 /**
14  * @author Yanick Duchesne
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 class SendHelper implements Runnable JavaDoc {
22   private Object JavaDoc _toSend;
23   private ServerAddress _addr;
24   private boolean _sync;
25
26   SendHelper(Object JavaDoc toSend, ServerAddress addr, boolean sync) {
27     _toSend = toSend;
28     _addr = addr;
29     _sync = sync;
30   }
31
32   /**
33    * @see java.lang.Runnable#run()
34    */

35   public void run() {
36     try {
37       doSend();
38     } catch (Throwable JavaDoc t) {
39       t.printStackTrace();
40     }
41   }
42
43   Object JavaDoc send() throws Throwable JavaDoc {
44     if (_sync) {
45       return doSend();
46     }
47
48     Thread JavaDoc t = new Thread JavaDoc(this);
49     t.setName("ubik.rmi.SendHelper");
50     t.setDaemon(true);
51     t.start();
52
53     return null;
54   }
55
56   private Object JavaDoc doSend() throws Throwable JavaDoc {
57     Connections conns = TransportManager.getConnectionsFor(_addr);
58     Connection conn = conns.acquire();
59     Object JavaDoc toReturn;
60
61     try {
62       conn.send(_toSend);
63       toReturn = conn.receive();
64     } catch (RemoteException JavaDoc e) {
65       conn.close();
66       throw e;
67     } catch (IOException JavaDoc e) {
68       RemoteException JavaDoc re = new RemoteException JavaDoc("Could not send replicated command",
69           e);
70       conn.close();
71       throw re;
72     } catch (ClassNotFoundException JavaDoc e) {
73       RemoteException JavaDoc re = new RemoteException JavaDoc("Could not receive response for replicated command",
74           e);
75       conn.close();
76       throw re;
77     }
78
79     conns.release(conn);
80
81     return toReturn;
82   }
83 }
84
Popular Tags