PortableRemoteObjectDelegateImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* PortableRemoteObjectDelegateImpl.java --
  2. Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.rmi.CORBA;
  32. import gnu.CORBA.SimpleDelegate;
  33. import gnu.CORBA.Unexpected;
  34. import gnu.CORBA.Poa.LocalDelegate;
  35. import gnu.CORBA.Poa.ORB_1_4;
  36. import gnu.CORBA.Poa.AOM;
  37. import java.rmi.NoSuchObjectException;
  38. import java.rmi.Remote;
  39. import java.rmi.RemoteException;
  40. import java.rmi.server.RMIClassLoader;
  41. import javax.rmi.CORBA.PortableRemoteObjectDelegate;
  42. import javax.rmi.CORBA.Stub;
  43. import javax.rmi.CORBA.Tie;
  44. import javax.rmi.CORBA.Util;
  45. import org.omg.CORBA.BAD_PARAM;
  46. import org.omg.CORBA.ORB;
  47. import org.omg.CORBA.portable.Delegate;
  48. import org.omg.CORBA.portable.ObjectImpl;
  49. import org.omg.PortableServer.POA;
  50. import org.omg.PortableServer.POAHelper;
  51. import org.omg.PortableServer.Servant;
  52. import org.omg.PortableServer.POAManagerPackage.State;
  53. /**
  54. * Implements PortableRemoteObjectDelegate.
  55. *
  56. * @author Wu Gansha (gansha.wu@intel.com) (stub)
  57. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) (implementation)
  58. */
  59. public class PortableRemoteObjectDelegateImpl
  60. implements PortableRemoteObjectDelegate
  61. {
  62. /**
  63. * <p>
  64. * Makes the remote object <code>a_target</code> ready for remote
  65. * communication using the same communications runtime as for the passed
  66. * <code>a_source</code> parameter. The a_target is connected to the same
  67. * ORB (and, if applicable, to the same POA) as the a_source.
  68. *
  69. * @param a_target the target to connect to ORB, must be an instance of either
  70. * {@link ObjectImpl} (Stubs and old-style ties) or {@link Servant} (POA-bases
  71. * ties).
  72. *
  73. * @param a_source the object, providing the connection information, must be
  74. * an instance of either {@link ObjectImpl} (Stubs and old-style ties) or
  75. * {@link Servant} (POA-bases ties).
  76. *
  77. * @throws RemoteException if the target is already connected to another ORB.
  78. */
  79. public void connect(Remote a_target, Remote a_source)
  80. throws RemoteException
  81. {
  82. ORB orb = null;
  83. POA poa = null;
  84. boolean ok = false;
  85. try
  86. {
  87. if (a_source instanceof Servant)
  88. {
  89. Servant s = (Servant) a_source;
  90. orb = s._orb();
  91. poa = s._poa();
  92. ok = true;
  93. }
  94. if (!ok && a_source instanceof ObjectImpl)
  95. {
  96. ObjectImpl o = (ObjectImpl) a_source;
  97. orb = o._orb();
  98. ok = true;
  99. try
  100. {
  101. if (orb instanceof ORB_1_4)
  102. {
  103. // POA information available.
  104. ORB_1_4 xorb = (ORB_1_4) orb;
  105. Delegate d = o._get_delegate();
  106. if (d instanceof LocalDelegate)
  107. {
  108. LocalDelegate l = (LocalDelegate) d;
  109. poa = l.poa;
  110. }
  111. else if (d instanceof SimpleDelegate)
  112. {
  113. byte[] ior_key = ((SimpleDelegate) d).getIor().key;
  114. AOM.Obj ref = xorb.rootPOA.findIorKey(ior_key);
  115. if (ref != null)
  116. poa = ref.poa;
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. // OK, POA info is not available, but as ORB is available, we
  123. // will connect in a default way.
  124. }
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. RuntimeException rex = new RuntimeException("Unable to get info from "
  130. + a_source);
  131. rex.initCause(ex);
  132. throw rex;
  133. }
  134. if (!ok && a_source instanceof Tie)
  135. {
  136. Tie t = (Tie) a_source;
  137. orb = t.orb();
  138. poa = null;
  139. ok = true;
  140. }
  141. if (orb == null)
  142. throw new RemoteException("Unable to determine ORB from " + a_source);
  143. if (a_target instanceof Stub)
  144. {
  145. StubDelegateImpl.connect((Stub) a_target, orb, poa);
  146. }
  147. else if (a_target 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. poa.servant_to_reference((Servant) a_target);
  159. }
  160. catch (Exception ex)
  161. {
  162. throw new Unexpected(ex);
  163. }
  164. }
  165. else if (a_target instanceof org.omg.CORBA.Object)
  166. {
  167. // Connect as object.
  168. orb.connect((org.omg.CORBA.Object) a_target);
  169. }
  170. else if (a_target instanceof Tie)
  171. {
  172. // We avoid calling this because it will aways connect to the root poa.
  173. ((Tie) a_target).orb(orb);
  174. }
  175. }
  176. /**
  177. * Narrow the given object to the instance of the given class. The currently
  178. * supported narrowing types are:
  179. *
  180. * 1. Simple widening conversion.<br>
  181. * 2. ObjectImpl -> RMI interface.<br>
  182. * 3. ObjectImpl -> ObjectImpl.<br>
  183. * 4. Tie -> Remote (implementation)<br>
  184. * 5. Remote (implementation) -> Tie.<br>
  185. *
  186. * The narrowing has sense only for derived classes.
  187. */
  188. public Object narrow(Object narrowFrom, Class narrowTo)
  189. throws ClassCastException
  190. {
  191. if (narrowTo == null)
  192. throw new ClassCastException("Can't narrow to null class");
  193. else if (narrowFrom == null)
  194. return null;
  195. else
  196. // Simple narrowing case.
  197. if (narrowTo.isAssignableFrom(narrowFrom.getClass()))
  198. return narrowFrom;
  199. else if (narrowTo.isInterface() || narrowFrom instanceof ObjectImpl)
  200. {
  201. // Narrow CORBA object to passed interface.
  202. String interf = narrowTo.getName();
  203. String stubClassName;
  204. stubClassName = getStubClassName(interf);
  205. try
  206. {
  207. // Replace the interface class by the stub class.
  208. narrowTo = Util.loadClass(stubClassName, null,
  209. narrowTo.getClassLoader());
  210. }
  211. catch (ClassNotFoundException e)
  212. {
  213. ClassCastException cex = new ClassCastException("Class not found: "
  214. + stubClassName);
  215. cex.initCause(e);
  216. throw cex;
  217. }
  218. }
  219. else if (narrowFrom instanceof Tie)
  220. {
  221. // Try to substitute the return tie target as a return value.
  222. Remote target = ((Tie) narrowFrom).getTarget();
  223. if (target != null && narrowTo.isAssignableFrom(target.getClass()))
  224. return target;
  225. }
  226. Object narrowed;
  227. try
  228. {
  229. narrowed = narrowTo.newInstance();
  230. }
  231. catch (Exception e)
  232. {
  233. ClassCastException cex = new ClassCastException("Cannot instantiate "
  234. + narrowTo.getName());
  235. cex.initCause(e);
  236. throw cex;
  237. }
  238. if (narrowed instanceof ObjectImpl)
  239. {
  240. // This also works for the instances of the Stub.
  241. ObjectImpl target = (ObjectImpl) narrowed;
  242. // Set the delegate, as is done in *Helper.narrow(..).
  243. target._set_delegate(((ObjectImpl) narrowFrom)._get_delegate());
  244. }
  245. else if (narrowed instanceof Tie && narrowFrom instanceof Remote)
  246. {
  247. // Try to set the narrowing object as a target for the Tie.
  248. ((Tie) narrowed).setTarget((Remote) narrowFrom);
  249. }
  250. else
  251. throw new ClassCastException("Narrowing of " + narrowFrom.getClass()
  252. + " to " + narrowTo + " is either not possible or not implemented.");
  253. return narrowed;
  254. }
  255. /**
  256. * Get the Stub class name for the name, representing the given interface.
  257. */
  258. static String getStubClassName(String interf)
  259. {
  260. String stubClassName;
  261. int p = interf.lastIndexOf('.');
  262. if (p < 0)
  263. // The interface is defined in the default package.
  264. stubClassName = "_" + interf + "_Stub";
  265. else
  266. stubClassName = interf.substring(0, p + 1) + "_"
  267. + interf.substring(p + 1) + "_Stub";
  268. return stubClassName;
  269. }
  270. /**
  271. * Get stub for the given implementation, searching by class name pattern. The
  272. * found stub must implement Remote for this method to succeed.
  273. */
  274. public Remote toStub(Remote ObjImpl)
  275. throws NoSuchObjectException
  276. {
  277. String icn = ObjImpl.getClass().getName();
  278. if (!icn.endsWith("Impl"))
  279. throw new BAD_PARAM("Invalid class name '" + icn
  280. + "', must end with 'Impl'");
  281. String sn = "_" + icn.substring(0, icn.length() - "Impl".length())
  282. + "_Stub";
  283. Class stubClass;
  284. Object o_stub;
  285. try
  286. {
  287. stubClass = RMIClassLoader.loadClass(sn);
  288. o_stub = stubClass.newInstance();
  289. }
  290. catch (Exception e)
  291. {
  292. NoSuchObjectException n = new NoSuchObjectException(sn);
  293. n.initCause(e);
  294. throw n;
  295. }
  296. if (!Remote.class.isAssignableFrom(stubClass))
  297. throw new ClassCastException(stubClass.getName()
  298. + " exists but cannot be returned as it does not inherit from "
  299. + Remote.class.getName());
  300. return (Remote) o_stub;
  301. }
  302. /**
  303. * If the object tie is no longer in use, disconnet it from the orb.
  304. */
  305. public void unexportObject(Remote obj)
  306. throws NoSuchObjectException
  307. {
  308. Util.unexportObject(obj);
  309. }
  310. /**
  311. * Find or create a tie for this target and mark it as being used by the given
  312. * object.
  313. */
  314. public void exportObject(Remote obj)
  315. throws RemoteException
  316. {
  317. if (obj instanceof Stub)
  318. Util.registerTarget(StubDelegateImpl.getTieFromStub((Stub) obj), obj);
  319. else if (obj instanceof Tie)
  320. {
  321. Tie t = (Tie) obj;
  322. Util.registerTarget(t, null);
  323. }
  324. }
  325. }