1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65:
66:
72: public class PortableRemoteObjectDelegateImpl
73: implements PortableRemoteObjectDelegate
74: {
75:
92: public void connect(Remote a_target, Remote a_source)
93: throws RemoteException
94: {
95: ORB orb = null;
96: POA poa = null;
97: boolean ok = false;
98:
99: try
100: {
101: if (a_source instanceof Servant)
102: {
103: Servant s = (Servant) a_source;
104: orb = s._orb();
105: poa = s._poa();
106: ok = true;
107: }
108:
109: if (!ok && a_source instanceof ObjectImpl)
110: {
111: ObjectImpl o = (ObjectImpl) a_source;
112: orb = o._orb();
113: ok = true;
114: try
115: {
116: if (orb instanceof ORB_1_4)
117: {
118:
119: ORB_1_4 xorb = (ORB_1_4) orb;
120: Delegate d = o._get_delegate();
121:
122: if (d instanceof LocalDelegate)
123: {
124: LocalDelegate l = (LocalDelegate) d;
125: poa = l.poa;
126: }
127: else if (d instanceof SimpleDelegate)
128: {
129: byte[] ior_key = ((SimpleDelegate) d).getIor().key;
130: AOM.Obj ref = xorb.rootPOA.findIorKey(ior_key);
131: if (ref != null)
132: poa = ref.poa;
133: }
134: }
135: }
136: catch (Exception ex)
137: {
138:
139:
140: }
141: }
142: }
143: catch (Exception ex)
144: {
145: RuntimeException rex = new RuntimeException("Unable to get info from "
146: + a_source);
147: rex.initCause(ex);
148: throw rex;
149: }
150:
151: if (!ok && a_source instanceof Tie)
152: {
153: Tie t = (Tie) a_source;
154: orb = t.orb();
155: poa = null;
156: ok = true;
157: }
158:
159: if (orb == null)
160: throw new RemoteException("Unable to determine ORB from " + a_source);
161:
162: if (a_target instanceof Stub)
163: {
164: StubDelegateImpl.connect((Stub) a_target, orb, poa);
165: }
166: else if (a_target instanceof Servant)
167: {
168: try
169: {
170: if (poa == null)
171: {
172: poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
173:
174: if (poa.the_POAManager().get_state().value() == State._HOLDING)
175: poa.the_POAManager().activate();
176: }
177: poa.servant_to_reference((Servant) a_target);
178: }
179: catch (Exception ex)
180: {
181: throw new Unexpected(ex);
182: }
183: }
184: else if (a_target instanceof org.omg.CORBA.Object)
185: {
186:
187: orb.connect((org.omg.CORBA.Object) a_target);
188: }
189: else if (a_target instanceof Tie)
190: {
191:
192: ((Tie) a_target).orb(orb);
193: }
194: }
195:
196:
208: public Object narrow(Object narrowFrom, Class narrowTo)
209: throws ClassCastException
210: {
211: if (narrowTo == null)
212: throw new ClassCastException("Can't narrow to null class");
213: else if (narrowFrom == null)
214: return null;
215: else
216:
217: if (narrowTo.isAssignableFrom(narrowFrom.getClass()))
218: return narrowFrom;
219: else if (narrowTo.isInterface() || narrowFrom instanceof ObjectImpl)
220: {
221:
222:
223: String interf = narrowTo.getName();
224: String stubClassName;
225:
226: stubClassName = getStubClassName(interf);
227:
228: try
229: {
230:
231: narrowTo = Util.loadClass(stubClassName, null,
232: narrowTo.getClassLoader());
233: }
234: catch (ClassNotFoundException e)
235: {
236: ClassCastException cex = new ClassCastException("Class not found: "
237: + stubClassName);
238: cex.initCause(e);
239: throw cex;
240: }
241: }
242: else if (narrowFrom instanceof Tie)
243: {
244:
245: Remote target = ((Tie) narrowFrom).getTarget();
246: if (target != null && narrowTo.isAssignableFrom(target.getClass()))
247: return target;
248: }
249:
250: Object narrowed;
251: try
252: {
253: narrowed = narrowTo.newInstance();
254: }
255: catch (Exception e)
256: {
257: ClassCastException cex = new ClassCastException("Cannot instantiate "
258: + narrowTo.getName());
259: cex.initCause(e);
260: throw cex;
261: }
262:
263: if (narrowed instanceof ObjectImpl)
264: {
265:
266: ObjectImpl target = (ObjectImpl) narrowed;
267:
268: target._set_delegate(((ObjectImpl) narrowFrom)._get_delegate());
269: }
270: else if (narrowed instanceof Tie && narrowFrom instanceof Remote)
271: {
272:
273: ((Tie) narrowed).setTarget((Remote) narrowFrom);
274: }
275: else
276: throw new ClassCastException("Narrowing of " + narrowFrom.getClass()
277: + " to " + narrowTo + " is either not possible or not implemented.");
278:
279: return narrowed;
280: }
281:
282:
285: static String getStubClassName(String interf)
286: {
287: String stubClassName;
288: int p = interf.lastIndexOf('.');
289:
290: if (p < 0)
291:
292: stubClassName = "_" + interf + "_Stub";
293: else
294: stubClassName = interf.substring(0, p + 1) + "_"
295: + interf.substring(p + 1) + "_Stub";
296: return stubClassName;
297: }
298:
299:
303: public Remote toStub(Remote ObjImpl)
304: throws NoSuchObjectException
305: {
306: String icn = ObjImpl.getClass().getName();
307: if (!icn.endsWith("Impl"))
308: throw new BAD_PARAM("Invalid class name '" + icn
309: + "', must end with 'Impl'");
310:
311: String sn = "_" + icn.substring(0, icn.length() - "Impl".length())
312: + "_Stub";
313:
314: Class stubClass;
315: Object o_stub;
316:
317: try
318: {
319: stubClass = RMIClassLoader.loadClass(sn);
320: o_stub = stubClass.newInstance();
321: }
322: catch (Exception e)
323: {
324: NoSuchObjectException n = new NoSuchObjectException(sn);
325: n.initCause(e);
326: throw n;
327: }
328:
329: if (!Remote.class.isAssignableFrom(stubClass))
330: throw new ClassCastException(stubClass.getName()
331: + " exists but cannot be returned as it does not inherit from "
332: + Remote.class.getName());
333:
334: return (Remote) o_stub;
335: }
336:
337:
340: public void unexportObject(Remote obj)
341: throws NoSuchObjectException
342: {
343: Util.unexportObject(obj);
344: }
345:
346:
350: public void exportObject(Remote obj)
351: throws RemoteException
352: {
353: if (obj instanceof Stub)
354: Util.registerTarget(StubDelegateImpl.getTieFromStub((Stub) obj), obj);
355: else if (obj instanceof Tie)
356: {
357: Tie t = (Tie) obj;
358: Util.registerTarget(t, null);
359: }
360: }
361:
362: }