KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > RemoteRef


1 package org.sapia.ubik.rmi.server;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInput JavaDoc;
6 import java.io.ObjectOutput JavaDoc;
7 import java.lang.reflect.Method JavaDoc;
8 import java.rmi.RemoteException JavaDoc;
9 import java.util.HashSet JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import org.sapia.ubik.net.Connection;
14 import org.sapia.ubik.net.ServerAddress;
15 import org.sapia.ubik.rmi.server.transport.Connections;
16 import org.sapia.ubik.rmi.server.transport.TransportManager;
17
18
19 /**
20  * This class implements the basic behavior of dynamic proxies that implement the
21  * logic for performing RPC on remote objects.
22  *
23  * @author Yanick Duchesne
24  * <dl>
25  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
26  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
27  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
28  * </dl>
29  */

30 public abstract class RemoteRef implements StubInvocationHandler,
31   Externalizable JavaDoc, HealthCheck {
32   static final long serialVersionUID = 1L;
33   protected boolean _callBack;
34   protected OID _oid;
35   protected VmId _vmId = VmId.getInstance();
36   protected ServerAddress _serverAddress;
37   protected boolean _isFirstVoyage = true;
38   protected transient Connections _pool;
39   protected transient Object JavaDoc _lock = new Object JavaDoc();
40
41   public RemoteRef() {
42   }
43
44   /**
45    * Creates an instance of this class, with the given object and host
46    * identifiers.
47    *
48    * @param oid an <code>OID</code>
49    * @param serverAddress a <code>ServerAddress</code>
50    *
51    */

52   public RemoteRef(OID oid, ServerAddress serverAddress) {
53     this();
54     _oid = oid;
55     _serverAddress = serverAddress;
56   }
57
58   /**
59    * Returns <code>true</code> if this stub is in call-back mode.
60    *
61    * @return <code>true</code> if this stub is in call-back mode.
62    */

63   public boolean isCallBack() {
64     return _callBack;
65   }
66   
67   /**
68    * Returns the identifier of the remote object to which this instance
69    * corresponds.
70    *
71    * @return an <code>OID</code>.
72    */

73   public OID getOID(){
74     return _oid;
75   }
76   
77   /**
78    * Sets this stub's call-back mode.
79    *
80    * @param callBack must be <code>true</code> if this stub should
81    * be in call-back mode.
82    */

83   protected void setCallBack(boolean callBack) {
84     _callBack = callBack;
85   }
86
87   /**
88    * @see java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
89    */

90   public abstract Object JavaDoc invoke(Object JavaDoc obj, Method JavaDoc toCall, Object JavaDoc[] params)
91     throws Throwable JavaDoc;
92
93   /**
94    * Returns the address of the server to which this stub "belongs".
95    *
96    * @return a <code>ServerAddress</code>
97    */

98   public ServerAddress getServerAddress() {
99     return _serverAddress;
100   }
101
102   /**
103    * Returns this instance's object identifier.
104    *
105    * @return a <code>OID</code>
106    */

107   public OID getOid() {
108     return _oid;
109   }
110
111   /**
112    * Tests the connection between this handler and its server; returns false
113    * if connection is invalid.
114    *
115    * @return <code>false</code> if connection is invalid.
116    */

117   public boolean isValid() {
118     try {
119       return ((Boolean JavaDoc) sendCommand(new CommandPing())).booleanValue();
120     } catch (Throwable JavaDoc t) {
121       return false;
122     }
123   }
124
125   /**
126    * @see StubInvocationHandler#toStubContainer(Object)
127    */

128   public StubContainer toStubContainer(Object JavaDoc proxy) {
129     Set JavaDoc interfaces = new HashSet JavaDoc();
130     ServerTable.appendInterfaces(proxy.getClass(), interfaces);
131
132     String JavaDoc[] names = new String JavaDoc[interfaces.size()];
133     int count = 0;
134
135     for (Iterator JavaDoc iter = interfaces.iterator(); iter.hasNext();) {
136       names[count++] = ((Class JavaDoc) iter.next()).getName();
137     }
138
139     return new StubContainerBase(names, this);
140   }
141
142   /**
143    * @see java.io.Externalizable#readExternal(ObjectInput)
144    */

145   public void readExternal(ObjectInput JavaDoc in)
146     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
147     _callBack = in.readBoolean();
148     _oid = (OID) in.readObject();
149     _vmId = (VmId) in.readObject();
150     _serverAddress = (ServerAddress) in.readObject();
151     _isFirstVoyage = in.readBoolean();
152     _lock = new Object JavaDoc();
153     Hub.clientRuntime.gc.register(_serverAddress, _oid, this);
154
155     if (_isFirstVoyage) {
156       _isFirstVoyage = false;
157     } else {
158       Hub.createReference(_serverAddress, _oid);
159     }
160   }
161
162   /**
163    * @see java.io.Externalizable#writeExternal(ObjectOutput)
164    */

165   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
166     out.writeBoolean(_callBack);
167     out.writeObject(_oid);
168     out.writeObject(_vmId);
169     out.writeObject(_serverAddress);
170     out.writeBoolean(_isFirstVoyage);
171   }
172
173   protected Object JavaDoc sendCommand(RMICommand cmd) throws Throwable JavaDoc {
174     synchronized (_lock) {
175       if (_pool == null) {
176         initPool(false);
177       }
178     }
179
180     Connection conn = _pool.acquire();
181
182     try {
183       try {
184         conn.send(cmd);
185       } catch (RemoteException JavaDoc e) {
186         synchronized (_lock) {
187           _pool.clear();
188         }
189
190         conn = _pool.acquire();
191         conn.send(cmd);
192       }
193
194       Object JavaDoc toReturn = conn.receive();
195
196       if (toReturn == null) {
197         return toReturn;
198       } else if (toReturn instanceof Throwable JavaDoc) {
199         Throwable JavaDoc err = (Throwable JavaDoc) toReturn;
200         err.fillInStackTrace();
201         throw err;
202       }
203
204       return toReturn;
205     } finally {
206       _pool.release(conn);
207     }
208   }
209
210   protected synchronized void initPool(boolean force)
211     throws java.rmi.RemoteException JavaDoc {
212     if (_pool == null) {
213       _pool = TransportManager.getConnectionsFor(_serverAddress);
214     } else if (force) {
215       _pool.clear();
216       _pool = TransportManager.getConnectionsFor(_serverAddress);
217     }
218   }
219 }
220
Popular Tags