StubDelegateImpl.java 9.2 KB

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