LocalDelegate.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* LocalDelegate.java --
  2. Copyright (C) 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.CORBA.Poa;
  32. import gnu.CORBA.CDR.AbstractCdrOutput;
  33. import gnu.CORBA.IOR;
  34. import gnu.CORBA.IorProvider;
  35. import gnu.CORBA.StreamBasedRequest;
  36. import org.omg.CORBA.ARG_INOUT;
  37. import org.omg.CORBA.Bounds;
  38. import org.omg.CORBA.Context;
  39. import org.omg.CORBA.ContextList;
  40. import org.omg.CORBA.ExceptionList;
  41. import org.omg.CORBA.NO_IMPLEMENT;
  42. import org.omg.CORBA.NVList;
  43. import org.omg.CORBA.NamedValue;
  44. import org.omg.CORBA.OBJECT_NOT_EXIST;
  45. import org.omg.CORBA.ORB;
  46. import org.omg.CORBA.Request;
  47. import org.omg.CORBA.TypeCodePackage.BadKind;
  48. import org.omg.CORBA.UnknownUserException;
  49. import org.omg.CORBA.portable.ApplicationException;
  50. import org.omg.CORBA.portable.InputStream;
  51. import org.omg.CORBA.portable.InvokeHandler;
  52. import org.omg.CORBA.portable.ObjectImpl;
  53. import org.omg.CORBA.portable.OutputStream;
  54. import org.omg.CORBA.portable.RemarshalException;
  55. import org.omg.PortableServer.ServantLocatorPackage.CookieHolder;
  56. import java.util.Arrays;
  57. /**
  58. * A local delegate, transferring all object requests to the locally available
  59. * servant. This class is involved in handling the method invocations on the
  60. * local object, obtained by POA.create_reference_with_id.
  61. *
  62. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  63. */
  64. public class LocalDelegate
  65. extends org.omg.CORBA_2_3.portable.Delegate
  66. implements IorProvider
  67. {
  68. /**
  69. * The same servant as an invocation handler.
  70. */
  71. gnuServantObject object;
  72. String operation;
  73. public final gnuPOA poa;
  74. final byte[] Id;
  75. /**
  76. * Create a local delegate, forwarding requests to the servant that must also
  77. * be an invocation handler.
  78. */
  79. public LocalDelegate(gnuServantObject an_object, gnuPOA a_poa, byte[] an_id)
  80. {
  81. object = an_object;
  82. poa = a_poa;
  83. Id = an_id;
  84. }
  85. /**
  86. * Get the IOR of the connected object.
  87. */
  88. public IOR getIor()
  89. {
  90. return object.getIor();
  91. }
  92. public Request request(org.omg.CORBA.Object target, String method)
  93. {
  94. operation = method;
  95. LocalRequest rq = new LocalRequest(object, poa, Id);
  96. rq.setOperation(method);
  97. rq.setORB(orb(target));
  98. return rq;
  99. }
  100. public void release(org.omg.CORBA.Object target)
  101. {
  102. }
  103. public boolean is_equivalent(org.omg.CORBA.Object target,
  104. org.omg.CORBA.Object other)
  105. {
  106. if (target == other)
  107. return true;
  108. else if (target instanceof ObjectImpl && other instanceof ObjectImpl)
  109. {
  110. org.omg.CORBA.portable.Delegate a = null;
  111. org.omg.CORBA.portable.Delegate b = null;
  112. try
  113. {
  114. a = ((ObjectImpl) target)._get_delegate();
  115. b = ((ObjectImpl) other)._get_delegate();
  116. }
  117. catch (Exception ex)
  118. {
  119. // Unable to get one of the delegates.
  120. return false;
  121. }
  122. if (a instanceof LocalDelegate && b instanceof LocalDelegate)
  123. {
  124. byte[] k1 = ((LocalDelegate) a).Id;
  125. byte[] k2 = ((LocalDelegate) b).Id;
  126. return Arrays.equals(k1, k2);
  127. }
  128. else
  129. return false;
  130. }
  131. else
  132. return false;
  133. }
  134. /**
  135. * Always return false.
  136. */
  137. public boolean non_existent(org.omg.CORBA.Object target)
  138. {
  139. return false;
  140. }
  141. /**
  142. * Get hash code.
  143. */
  144. public int hash(org.omg.CORBA.Object target, int maximum)
  145. {
  146. return hashCode() % maximum;
  147. }
  148. /**
  149. * Check if this object could be named by the given repository id.
  150. *
  151. * @param idl_id the repository id to check.
  152. *
  153. * @return true if it is one of the possible repository ids of this object.
  154. */
  155. public boolean is_a(org.omg.CORBA.Object a_servant, String idl_id)
  156. {
  157. String[] maybe = object._ids();
  158. for (int i = 0; i < maybe.length; i++)
  159. {
  160. if (maybe[i].equals(idl_id))
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Return <code>this</code>.
  167. */
  168. public org.omg.CORBA.Object duplicate(org.omg.CORBA.Object target)
  169. {
  170. return target;
  171. }
  172. /**
  173. * Create request for using with DII.
  174. */
  175. public Request create_request(org.omg.CORBA.Object target, Context context,
  176. String method, NVList parameters, NamedValue returns,
  177. ExceptionList exceptions, ContextList ctx_list)
  178. {
  179. operation = method;
  180. LocalRequest rq = new LocalRequest(object, poa, Id);
  181. rq.setOperation(method);
  182. rq.set_args(parameters);
  183. rq.set_result(returns);
  184. rq.set_exceptions(exceptions);
  185. rq.set_context_list(ctx_list);
  186. return rq;
  187. }
  188. /**
  189. * Create request for using with DII.
  190. */
  191. public Request create_request(org.omg.CORBA.Object target, Context context,
  192. String method, NVList parameters, NamedValue returns)
  193. {
  194. operation = method;
  195. LocalRequest rq = new LocalRequest(object, poa, Id);
  196. rq.setOperation(method);
  197. rq.set_args(parameters);
  198. rq.set_result(returns);
  199. return rq;
  200. }
  201. /**
  202. * Not in use.
  203. */
  204. public org.omg.CORBA.Object get_interface_def(org.omg.CORBA.Object target)
  205. {
  206. throw new NO_IMPLEMENT();
  207. }
  208. /**
  209. * Create a request to invoke the method of this CORBA object.
  210. *
  211. * @param operation the name of the method to invoke.
  212. * @param response_expected specifies if this is one way message or the
  213. * response to the message is expected.
  214. *
  215. * @return the stream where the method arguments should be written.
  216. */
  217. public org.omg.CORBA.portable.OutputStream request(
  218. org.omg.CORBA.Object target, String method, boolean response_expected)
  219. {
  220. operation = method;
  221. // Check if the object is not explicitly deactivated.
  222. AOM.Obj e = poa.aom.get(Id);
  223. if (e != null && e.isDeactiveted())
  224. {
  225. if (poa.servant_activator != null || poa.servant_locator != null)
  226. {
  227. // This will force the subsequent activation.
  228. object.setServant(null);
  229. e.setServant(null);
  230. e.setDeactivated(false);
  231. }
  232. else
  233. throw new OBJECT_NOT_EXIST("Deactivated");
  234. }
  235. LocalRequest rq = new LocalRequest(object, poa, Id);
  236. rq.setOperation(method);
  237. rq.setORB(orb(target));
  238. return rq.getParameterStream();
  239. }
  240. /**
  241. * Return the associated invocation handler.
  242. */
  243. public InvokeHandler getHandler(String method, CookieHolder cookie)
  244. {
  245. return object.getHandler(method, cookie, false);
  246. }
  247. /**
  248. * Return the ORB of the associated POA. The parameter is not in use.
  249. */
  250. public ORB orb(org.omg.CORBA.Object target)
  251. {
  252. return poa.orb();
  253. }
  254. /**
  255. * Make an invocation.
  256. *
  257. * @param target not in use.
  258. * @param output the stream request that should be returned by
  259. * {@link #m_request} in this method.
  260. * @throws ApplicationException if the use exception is thrown by the servant
  261. * method.
  262. */
  263. public InputStream invoke(org.omg.CORBA.Object target, OutputStream output)
  264. throws ApplicationException
  265. {
  266. try
  267. {
  268. StreamBasedRequest sr = (StreamBasedRequest) output;
  269. LocalRequest lr = (LocalRequest) sr.request;
  270. InvokeHandler handler = lr.object.getHandler(lr.operation(), lr.cookie,
  271. false);
  272. if (handler instanceof DynamicImpHandler)
  273. {
  274. // The local request known how to handle it, but the different
  275. // method must be called.
  276. lr.invoke();
  277. // The encapsulation will inherit orb, endian, charsets, etc.
  278. AbstractCdrOutput buf = sr.createEncapsulation();
  279. // Write all request parameters to the buffer stream.
  280. if (lr.env().exception() != null)
  281. {
  282. try
  283. {
  284. UnknownUserException uex = (UnknownUserException) lr.env().exception();
  285. throw new ApplicationException(uex.except.type().id(),
  286. uex.except.create_input_stream());
  287. }
  288. catch (BadKind ex)
  289. {
  290. InternalError ierr = new InternalError();
  291. ierr.initCause(ex);
  292. throw ierr;
  293. }
  294. }
  295. if (lr.return_value() != null)
  296. lr.return_value().write_value(buf);
  297. NamedValue a;
  298. try
  299. {
  300. for (int i = 0; i < lr.arguments().count(); i++)
  301. {
  302. a = lr.arguments().item(i);
  303. if (a.flags() == ARG_INOUT.value
  304. || a.flags() == ARG_INOUT.value)
  305. {
  306. a.value().write_value(buf);
  307. }
  308. }
  309. }
  310. catch (Bounds ex)
  311. {
  312. InternalError ierr = new InternalError();
  313. ierr.initCause(ex);
  314. throw ierr;
  315. }
  316. return buf.create_input_stream();
  317. }
  318. else
  319. {
  320. LocalRequest lrq = (LocalRequest) sr.request;
  321. return lrq.s_invoke(handler);
  322. }
  323. }
  324. catch (gnuForwardRequest f)
  325. {
  326. try
  327. {
  328. return ((ObjectImpl) f.forward_reference)._invoke(f.forward_reference._request(
  329. operation, true));
  330. }
  331. catch (RemarshalException e)
  332. {
  333. // Never thrown in this place by Classpath implementation.
  334. throw new NO_IMPLEMENT();
  335. }
  336. }
  337. }
  338. public void releaseReply(org.omg.CORBA.Object target, InputStream input)
  339. {
  340. release(target);
  341. }
  342. }