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