IorDelegate.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* gnuDelegate.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;
  32. import gnu.CORBA.CDR.BufferredCdrInput;
  33. import gnu.CORBA.GIOP.ReplyHeader;
  34. import org.omg.CORBA.CompletionStatus;
  35. import org.omg.CORBA.Context;
  36. import org.omg.CORBA.ContextList;
  37. import org.omg.CORBA.ExceptionList;
  38. import org.omg.CORBA.MARSHAL;
  39. import org.omg.CORBA.NVList;
  40. import org.omg.CORBA.NamedValue;
  41. import org.omg.CORBA.ORB;
  42. import org.omg.CORBA.Request;
  43. import org.omg.CORBA.portable.ApplicationException;
  44. import org.omg.CORBA.portable.InputStream;
  45. import org.omg.CORBA.portable.OutputStream;
  46. import org.omg.CORBA.portable.RemarshalException;
  47. import org.omg.PortableInterceptor.ForwardRequest;
  48. import java.io.IOException;
  49. /**
  50. * The Classpath implementation of the {@link Delegate} functionality in the
  51. * case, when the object was constructed from an IOR object. The IOR can be
  52. * constructed from the stringified object reference.
  53. *
  54. * There is an different instance of this delegate for each CORBA object.
  55. *
  56. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  57. */
  58. public class IorDelegate extends SimpleDelegate
  59. {
  60. /**
  61. * Contructs an instance of object using the given IOR.
  62. */
  63. public IorDelegate(ORB an_orb, IOR an_ior)
  64. {
  65. super(an_orb, an_ior);
  66. }
  67. /**
  68. * Creates the request to invoke the method on this object.
  69. *
  70. * @param target the object, for that the operation must be invoked.
  71. * @param context context (null allowed)
  72. * @param operation the method name
  73. * @param parameters the method parameters
  74. * @param returns the return value holder
  75. *
  76. * @return the created request.
  77. */
  78. public Request create_request(org.omg.CORBA.Object target, Context context,
  79. String operation, NVList parameters, NamedValue returns
  80. )
  81. {
  82. gnuRequest request = getRequestInstance(target);
  83. request.setIor(getIor());
  84. request.set_target(target);
  85. request.setOperation(operation);
  86. request.set_args(parameters);
  87. request.m_context = context;
  88. request.set_result(returns);
  89. request.setORB(orb);
  90. return request;
  91. }
  92. /**
  93. * Creates the request to invoke the method on this object.
  94. *
  95. * @param target the object, for that the operation must be invoked.
  96. * @param context context (null allowed)
  97. * @param operation the method name
  98. * @param parameters the method parameters
  99. * @param returns the return value holder
  100. *
  101. * @return the created request.
  102. */
  103. public Request create_request(org.omg.CORBA.Object target, Context context,
  104. String operation, NVList parameters, NamedValue returns,
  105. ExceptionList exceptions, ContextList ctx_list
  106. )
  107. {
  108. gnuRequest request = getRequestInstance(target);
  109. request.setIor(ior);
  110. request.set_target(target);
  111. request.setOperation(operation);
  112. request.set_args(parameters);
  113. request.m_context = context;
  114. request.set_result(returns);
  115. request.set_exceptions(exceptions);
  116. request.set_context_list(ctx_list);
  117. request.setORB(orb);
  118. return request;
  119. }
  120. /**
  121. * Get the instance of request.
  122. */
  123. protected gnuRequest getRequestInstance(org.omg.CORBA.Object target)
  124. {
  125. return new gnuRequest();
  126. }
  127. /**
  128. * Invoke operation on the given object, als handling temproray and permanent
  129. * redirections. The ReplyHeader.LOCATION_FORWARD will cause to resend the
  130. * request to the new direction. The ReplyHeader.LOCATION_FORWARD_PERM will
  131. * cause additionally to remember the new location by this delegate, so
  132. * subsequent calls will be immediately delivered to the new target.
  133. *
  134. * @param target the target object.
  135. * @param output the output stream, previously returned by
  136. * {@link #request(org.omg.CORBA.Object, String, boolean)}.
  137. *
  138. * @return the input stream, to read the response from or null for a one-way
  139. * request.
  140. *
  141. * @throws SystemException if the SystemException has been thrown on the
  142. * remote side (the exact type and the minor code matches the data of the
  143. * remote exception that has been thrown).
  144. *
  145. * @throws org.omg.CORBA.portable.ApplicationException as specified.
  146. * @throws org.omg.CORBA.portable.RemarshalException as specified.
  147. */
  148. public InputStream invoke(org.omg.CORBA.Object target, OutputStream output)
  149. throws ApplicationException, RemarshalException
  150. {
  151. StreamBasedRequest request = (StreamBasedRequest) output;
  152. while (true)
  153. {
  154. try
  155. {
  156. if (request.response_expected)
  157. {
  158. RawReply response = request.request.submit();
  159. // Read reply header.
  160. ReplyHeader rh = response.header.create_reply_header();
  161. BufferredCdrInput input = response.getStream();
  162. input.setOrb(orb);
  163. rh.read(input);
  164. request.request.m_rph = rh;
  165. boolean moved_permanently = false;
  166. switch (rh.reply_status)
  167. {
  168. case ReplyHeader.NO_EXCEPTION:
  169. if (request.request.m_interceptor != null)
  170. request.request.m_interceptor.receive_reply(request.request.m_info);
  171. if (response.header.version.since_inclusive(1, 2))
  172. input.align(8);
  173. return input;
  174. case ReplyHeader.SYSTEM_EXCEPTION:
  175. if (response.header.version.since_inclusive(1, 2))
  176. input.align(8);
  177. showException(request, input);
  178. throw ObjectCreator.readSystemException(input,
  179. rh.service_context);
  180. case ReplyHeader.USER_EXCEPTION:
  181. if (response.header.version.since_inclusive(1, 2))
  182. input.align(8);
  183. showException(request, input);
  184. throw new ApplicationException(
  185. request.request.m_exception_id, input);
  186. case ReplyHeader.LOCATION_FORWARD_PERM:
  187. moved_permanently = true;
  188. case ReplyHeader.LOCATION_FORWARD:
  189. if (response.header.version.since_inclusive(1, 2))
  190. input.align(8);
  191. IOR forwarded = new IOR();
  192. try
  193. {
  194. forwarded._read_no_endian(input);
  195. }
  196. catch (IOException ex)
  197. {
  198. MARSHAL t = new MARSHAL("Cant read forwarding info",
  199. 5102, CompletionStatus.COMPLETED_NO);
  200. t.initCause(ex);
  201. throw t;
  202. }
  203. gnuRequest prev = request.request;
  204. gnuRequest r = getRequestInstance(target);
  205. r.m_interceptor = prev.m_interceptor;
  206. r.m_slots = prev.m_slots;
  207. r.m_args = prev.m_args;
  208. r.m_context = prev.m_context;
  209. r.m_context_list = prev.m_context_list;
  210. r.m_environment = prev.m_environment;
  211. r.m_exceptions = prev.m_exceptions;
  212. r.m_operation = prev.m_operation;
  213. r.m_parameter_buffer = prev.m_parameter_buffer;
  214. r.m_parameter_buffer.request = r;
  215. r.m_result = prev.m_result;
  216. r.m_target = prev.m_target;
  217. r.oneWay = prev.oneWay;
  218. r.m_forward_ior = forwarded;
  219. if (r.m_interceptor != null)
  220. r.m_interceptor.receive_other(r.m_info);
  221. r.setIor(forwarded);
  222. IorObject it = new IorObject(orb,
  223. forwarded);
  224. r.m_target = it;
  225. request.request = r;
  226. IOR prev_ior = getIor();
  227. setIor(forwarded);
  228. try
  229. {
  230. return invoke(it, request);
  231. }
  232. finally
  233. {
  234. if (!moved_permanently)
  235. setIor(prev_ior);
  236. }
  237. default:
  238. throw new MARSHAL("Unknow reply status: "
  239. + rh.reply_status, 8000 + rh.reply_status,
  240. CompletionStatus.COMPLETED_NO);
  241. }
  242. }
  243. else
  244. {
  245. request.request.send_oneway();
  246. return null;
  247. }
  248. }
  249. catch (ForwardRequest forwarded)
  250. {
  251. ForwardRequest fw = forwarded;
  252. Forwarding2: while (true)
  253. {
  254. try
  255. {
  256. gnuRequest prev = request.request;
  257. gnuRequest r = getRequestInstance(target);
  258. r.m_interceptor = prev.m_interceptor;
  259. r.m_args = prev.m_args;
  260. r.m_context = prev.m_context;
  261. r.m_context_list = prev.m_context_list;
  262. r.m_environment = prev.m_environment;
  263. r.m_exceptions = prev.m_exceptions;
  264. r.m_operation = prev.m_operation;
  265. r.m_parameter_buffer = prev.m_parameter_buffer;
  266. r.m_parameter_buffer.request = r;
  267. r.m_result = prev.m_result;
  268. r.m_target = prev.m_target;
  269. r.oneWay = prev.oneWay;
  270. r.m_forwarding_target = fw.forward;
  271. if (r.m_interceptor != null)
  272. r.m_interceptor.receive_other(r.m_info);
  273. r.m_target = fw.forward;
  274. request.request = r;
  275. break Forwarding2;
  276. }
  277. catch (ForwardRequest e)
  278. {
  279. forwarded = e;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Show exception to interceptor.
  287. */
  288. void showException(StreamBasedRequest request, BufferredCdrInput input)
  289. throws ForwardRequest
  290. {
  291. input.mark(2048);
  292. request.request.m_exception_id = input.read_string();
  293. input.reset();
  294. if (request.request.m_interceptor != null)
  295. request.request.m_interceptor.receive_exception(request.request.m_info);
  296. }
  297. /**
  298. * Create a request to invoke the method of this CORBA object.
  299. *
  300. * @param target the CORBA object, to that this operation must be applied.
  301. * @param operation the name of the method to invoke.
  302. *
  303. * @return the request.
  304. */
  305. public Request request(org.omg.CORBA.Object target, String operation)
  306. {
  307. gnuRequest request = getRequestInstance(target);
  308. request.setIor(ior);
  309. request.set_target(target);
  310. request.setOperation(operation);
  311. request.setORB(orb);
  312. return request;
  313. }
  314. /**
  315. * Create a request to invoke the method of this CORBA object.
  316. *
  317. * @param target the CORBA object, to that this operation must be applied.
  318. * @param operation the name of the method to invoke.
  319. * @param response_expected specifies if this is one way message or the
  320. * response to the message is expected.
  321. *
  322. * @return the stream where the method arguments should be written.
  323. */
  324. public OutputStream request(org.omg.CORBA.Object target, String operation,
  325. boolean response_expected
  326. )
  327. {
  328. gnuRequest request = getRequestInstance(target);
  329. request.setIor(ior);
  330. request.set_target(target);
  331. request.setOperation(operation);
  332. StreamBasedRequest out = request.getParameterStream();
  333. out.response_expected = response_expected;
  334. request.setORB(orb);
  335. out.setOrb(orb);
  336. return out;
  337. }
  338. /**
  339. * If there is an opened cache socket to access this object, close that
  340. * socket.
  341. *
  342. * @param target The target is not used, this delegate requires a single
  343. * instance per object.
  344. */
  345. public void release(org.omg.CORBA.Object target)
  346. {
  347. // Do nothing here.
  348. }
  349. /**
  350. * Reset the remote_ior flag, forcing to check if the object is local on the
  351. * next getRequestInstance call.
  352. */
  353. public void setIor(IOR an_ior)
  354. {
  355. super.setIor(an_ior);
  356. }
  357. /**
  358. * Checks if the ior is local so far it is easy.
  359. */
  360. public boolean is_local(org.omg.CORBA.Object self)
  361. {
  362. return false;
  363. }
  364. }