1 |
/* StubDelegateImpl.java -- |
/* StubDelegateImpl.java -- |
2 |
Copyright (C) 2002, 2004 Free Software Foundation, Inc. |
Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
38 |
|
|
39 |
package gnu.javax.rmi.CORBA; |
package gnu.javax.rmi.CORBA; |
40 |
|
|
41 |
|
import gnu.CORBA.ObjectCreator; |
42 |
|
import gnu.CORBA.Unexpected; |
43 |
|
import gnu.CORBA.CDR.BufferredCdrInput; |
44 |
|
import gnu.CORBA.CDR.BufferedCdrOutput; |
45 |
|
|
46 |
import java.io.IOException; |
import java.io.IOException; |
47 |
import java.io.ObjectInputStream; |
import java.io.ObjectInputStream; |
48 |
import java.io.ObjectOutputStream; |
import java.io.ObjectOutputStream; |
49 |
|
import java.rmi.Remote; |
50 |
import java.rmi.RemoteException; |
import java.rmi.RemoteException; |
51 |
|
|
52 |
|
import javax.rmi.PortableRemoteObject; |
53 |
import javax.rmi.CORBA.Stub; |
import javax.rmi.CORBA.Stub; |
54 |
import javax.rmi.CORBA.StubDelegate; |
import javax.rmi.CORBA.StubDelegate; |
55 |
|
import javax.rmi.CORBA.Tie; |
56 |
|
import javax.rmi.CORBA.Util; |
57 |
|
|
58 |
|
import org.omg.CORBA.BAD_PARAM; |
59 |
|
import org.omg.CORBA.ORB; |
60 |
|
import org.omg.CORBA.portable.Delegate; |
61 |
|
import org.omg.CORBA.portable.ObjectImpl; |
62 |
|
import org.omg.PortableServer.POA; |
63 |
|
import org.omg.PortableServer.POAHelper; |
64 |
|
import org.omg.PortableServer.Servant; |
65 |
|
import org.omg.PortableServer.POAManagerPackage.State; |
66 |
|
|
67 |
|
/** |
68 |
|
* The default stub delegate. |
69 |
|
* |
70 |
|
* @author Wu Gansha (gansha.wu@intel.com) (stub) |
71 |
|
* @author Audrius Meskauskas (AudriusA@Bioinformatics.org) (implementation) |
72 |
|
*/ |
73 |
public class StubDelegateImpl |
public class StubDelegateImpl |
74 |
implements StubDelegate |
implements StubDelegate |
75 |
{ |
{ |
76 |
|
/** |
77 |
private int hashCode; |
* <p> |
78 |
|
* Finds the suitable {@link Tie} for this Stub and connects it to the given |
79 |
public StubDelegateImpl(){ |
* ORB. The tie is found by the name pattern. If the found tie is derived from |
80 |
hashCode = 0; |
* {@link org.omg.CORBA.PortableServer.Servant}, it is connected to the root |
81 |
|
* POA, also activating it (if not already active). |
82 |
|
* </p> |
83 |
|
* <p> |
84 |
|
* This method does not allow to specify, to which POA the found Tie must be |
85 |
|
* connected and requires to use the deprecated method {@link ORB#connect}. |
86 |
|
* Many useful POA features remain unaccessible. A better alternative it might |
87 |
|
* be to generate a {@link org.omg.CORBA.PortableServer.Servant} - derived Tie |
88 |
|
* (-poa key in rmic) and connect it to POA in one of the many ways, listed in |
89 |
|
* the description of the {@link orb.omg.PortableServer} package). The |
90 |
|
* obtained CORBA object can be narrowed into stub using |
91 |
|
* {@link PortableRemoteObject#narrow}. |
92 |
|
* </p> |
93 |
|
* |
94 |
|
* @param orb the ORB where the Stub must be connected. |
95 |
|
* |
96 |
|
* @throws RemoteException if the stub is already connected to some other ORB. |
97 |
|
* If the stub is already connected to the ORB that was passed as parameter, |
98 |
|
* the method returns without action. |
99 |
|
* |
100 |
|
* @throws BAD_PARAM if the name of this stub does not match the stub name |
101 |
|
* pattern, "_*_Stub" or if the Tie class, "_*Impl_Tie", does not exists or an |
102 |
|
* instance of this class cannot be instantiated. |
103 |
|
*/ |
104 |
|
public void connect(Stub self, ORB orb) |
105 |
|
throws RemoteException |
106 |
|
{ |
107 |
|
connect(self, orb, null); |
108 |
} |
} |
109 |
// XXX javax.rmi.ORB -> org.omg.CORBA.ORB |
|
110 |
public void connect(Stub self, javax.rmi.ORB orb) |
/** |
111 |
|
* Connect when the POA is specified. |
112 |
|
*/ |
113 |
|
public static void connect(Stub self, ORB orb, POA poa) |
114 |
throws RemoteException |
throws RemoteException |
115 |
{ |
{ |
116 |
throw new Error("Not implemented for StubDelegate"); |
ORB oorb = null; |
117 |
|
try |
118 |
|
{ |
119 |
|
Delegate d = self._get_delegate(); |
120 |
|
if (d != null) |
121 |
|
oorb = d.orb(self); |
122 |
|
} |
123 |
|
catch (Exception e) |
124 |
|
{ |
125 |
|
// Failed to get Delegate or ORB. |
126 |
|
// (possible ony for user-written Stubs). |
127 |
|
} |
128 |
|
|
129 |
|
if (oorb != null) |
130 |
|
{ |
131 |
|
if (!oorb.equals(orb)) |
132 |
|
throw new RemoteException("Stub " + self |
133 |
|
+ " is connected to another ORB, " + orb); |
134 |
|
else |
135 |
|
return; |
136 |
|
} |
137 |
|
|
138 |
|
Tie t = null; |
139 |
|
if (self instanceof Remote) |
140 |
|
t = Util.getTie((Remote) self); |
141 |
|
|
142 |
|
// Find by name pattern. |
143 |
|
if (t == null) |
144 |
|
t = getTieFromStub(self); |
145 |
|
|
146 |
|
Delegate delegate; |
147 |
|
|
148 |
|
if (t instanceof Servant) |
149 |
|
{ |
150 |
|
try |
151 |
|
{ |
152 |
|
if (poa == null) |
153 |
|
{ |
154 |
|
poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); |
155 |
|
// Activate if not active. |
156 |
|
if (poa.the_POAManager().get_state().value() == State._HOLDING) |
157 |
|
poa.the_POAManager().activate(); |
158 |
|
} |
159 |
|
|
160 |
|
ObjectImpl obj = (ObjectImpl) poa.servant_to_reference((Servant) t); |
161 |
|
delegate = obj._get_delegate(); |
162 |
|
} |
163 |
|
catch (Exception ex) |
164 |
|
{ |
165 |
|
throw new Unexpected(ex); |
166 |
|
} |
167 |
|
} |
168 |
|
else if (t instanceof ObjectImpl) |
169 |
|
{ |
170 |
|
ObjectImpl o = (ObjectImpl) t; |
171 |
|
orb.connect(o); |
172 |
|
delegate = o._get_delegate(); |
173 |
|
} |
174 |
|
else |
175 |
|
throw new BAD_PARAM("The Tie must be either Servant or ObjectImpl"); |
176 |
|
|
177 |
|
self._set_delegate(delegate); |
178 |
} |
} |
179 |
|
|
180 |
public boolean equals(Stub self, Object obj) |
/** |
181 |
|
* Locate a tie class, appropriate to the given stub class, by the name |
182 |
|
* pattern. |
183 |
|
*/ |
184 |
|
public static Tie getTieFromStub(java.lang.Object self) |
185 |
{ |
{ |
186 |
if(self == null || obj == null) |
Tie t; |
187 |
return self == obj; |
String sn = self.getClass().getName(); |
188 |
if(!(obj instanceof Stub)) |
if (!sn.endsWith("_Stub")) |
189 |
return false; |
throw new BAD_PARAM("The stub name, " + sn |
190 |
return self.hashCode() == ((Stub)obj).hashCode(); |
+ ", does not match _*_Stub pattern"); |
191 |
|
|
192 |
|
String tn = sn.substring(0, sn.length() - "_Stub".length()) + "Impl_Tie"; |
193 |
|
Class tieClass = null; |
194 |
|
|
195 |
|
try |
196 |
|
{ |
197 |
|
tieClass = ObjectCreator.forName(tn); |
198 |
|
t = (Tie) tieClass.newInstance(); |
199 |
|
if (self instanceof Remote) |
200 |
|
Util.registerTarget(t, (Remote) self); |
201 |
|
} |
202 |
|
catch (Exception e) |
203 |
|
{ |
204 |
|
BAD_PARAM bad = new BAD_PARAM("Unable to instantiate '" + tn + "'"); |
205 |
|
bad.initCause(e); |
206 |
|
throw bad; |
207 |
|
} |
208 |
|
return t; |
209 |
} |
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Compare two stubs for equality. |
213 |
|
*/ |
214 |
|
public boolean equals(Stub self, java.lang.Object obj) |
215 |
|
{ |
216 |
|
if (obj instanceof ObjectImpl) |
217 |
|
{ |
218 |
|
ObjectImpl other = (ObjectImpl) obj; |
219 |
|
Delegate d1 = other._get_delegate(); |
220 |
|
Delegate d2 = self._get_delegate(); |
221 |
|
if (d1 == null || d2 == null) |
222 |
|
return d1 == d2; |
223 |
|
else |
224 |
|
return d1.equals(d2); |
225 |
|
} |
226 |
|
else return false; |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
|
* Get the hash code (from IOR reference). |
231 |
|
*/ |
232 |
public int hashCode(Stub self) |
public int hashCode(Stub self) |
233 |
{ |
{ |
234 |
//FIX ME |
Delegate d = self._get_delegate(); |
235 |
return hashCode; |
return d==null?0:d.hashCode(); |
236 |
} |
} |
237 |
|
|
238 |
|
/** |
239 |
|
* Returns the IOR reference of the connected ORB. |
240 |
|
* |
241 |
|
* @see ORB#object_to_string(org.omg.CORBA.Object); |
242 |
|
*/ |
243 |
public String toString(Stub self) |
public String toString(Stub self) |
244 |
{ |
{ |
245 |
try |
try |
246 |
{ |
{ |
247 |
return self._orb().object_to_string(self); |
return self._orb().object_to_string(self); |
248 |
} |
} |
249 |
// XXX javax.rmi.BAD_OPERATION -> org.omg.CORBA.BAD_OPERATION |
catch (Exception ex) |
|
catch(javax.rmi.BAD_OPERATION bad_operation) |
|
250 |
{ |
{ |
251 |
return null; |
return null; |
252 |
} |
} |
253 |
} |
} |
254 |
|
|
255 |
public void readObject(Stub self, ObjectInputStream s) |
/** |
256 |
|
* This should never be called. The ORB must be supplied. |
257 |
|
* |
258 |
|
* @see #connect |
259 |
|
*/ |
260 |
|
public void readObject(Stub self, ObjectInputStream input) |
261 |
throws IOException, ClassNotFoundException |
throws IOException, ClassNotFoundException |
262 |
{ |
{ |
263 |
throw new Error("Not implemented for StubDelegate"); |
readObject(self, input, null); |
264 |
} |
} |
265 |
|
|
266 |
public void writeObject(Stub self, ObjectOutputStream s) |
/** |
267 |
|
* Read as CORBA object when the ORB is known. The ORB must be set under the |
268 |
|
* previous call of Stub.connect. The Stub is automatically registered with |
269 |
|
* this ORB. |
270 |
|
*/ |
271 |
|
public void readObject(Stub self, ObjectInputStream input, ORB orb) |
272 |
|
throws IOException, ClassNotFoundException |
273 |
|
{ |
274 |
|
byte[] b = (byte[]) input.readObject(); |
275 |
|
BufferredCdrInput in = new BufferredCdrInput(b); |
276 |
|
|
277 |
|
if (orb != null) |
278 |
|
in.setOrb(orb); |
279 |
|
|
280 |
|
ObjectImpl r = (ObjectImpl) in.read_Object(); |
281 |
|
|
282 |
|
self._set_delegate(r._get_delegate()); |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
|
* Write as CORBA object. The ORB is taken from the |
287 |
|
* org.omg.CORBA.portable.Delegate. The Stub is automatically registered with |
288 |
|
* this ORB (if not already done). |
289 |
|
*/ |
290 |
|
public void writeObject(Stub self, ObjectOutputStream output) |
291 |
throws IOException |
throws IOException |
292 |
{ |
{ |
293 |
throw new Error("Not implemented for StubDelegate"); |
writeObject(self, output, null); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
|
* Write as CORBA object. The ORB must be either set under the previous call |
298 |
|
* of Stub.connect or it is taken from the org.omg.CORBA.portable.Delegate. |
299 |
|
* The Stub is automatically registered with this ORB (if not already done). |
300 |
|
*/ |
301 |
|
public void writeObject(Stub self, ObjectOutputStream output, ORB orb) |
302 |
|
throws IOException |
303 |
|
{ |
304 |
|
BufferedCdrOutput out = new BufferedCdrOutput(); |
305 |
|
out.setOrb(orb == null ? self._orb() : orb); |
306 |
|
out.write_Object(self); |
307 |
|
|
308 |
|
output.writeObject(out.buffer.toByteArray()); |
309 |
} |
} |
310 |
|
} |
|
} |
|