LocalRequest.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* LocalRequest.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.BufferedCdrOutput;
  33. import gnu.CORBA.GIOP.MessageHeader;
  34. import gnu.CORBA.GIOP.v1_2.ReplyHeader;
  35. import gnu.CORBA.GIOP.v1_2.RequestHeader;
  36. import gnu.CORBA.Interceptor.gnuClientRequestInfo;
  37. import gnu.CORBA.Interceptor.gnuServerRequestInfo;
  38. import gnu.CORBA.typecodes.RecordTypeCode;
  39. import gnu.CORBA.ObjectCreator;
  40. import gnu.CORBA.Unexpected;
  41. import gnu.CORBA.gnuAny;
  42. import gnu.CORBA.gnuRequest;
  43. import gnu.CORBA.StreamHolder;
  44. import gnu.CORBA.StreamBasedRequest;
  45. import org.omg.CORBA.ARG_OUT;
  46. import org.omg.CORBA.Any;
  47. import org.omg.CORBA.BAD_INV_ORDER;
  48. import org.omg.CORBA.BAD_OPERATION;
  49. import org.omg.CORBA.Bounds;
  50. import org.omg.CORBA.NamedValue;
  51. import org.omg.CORBA.ORB;
  52. import org.omg.CORBA.SystemException;
  53. import org.omg.CORBA.TCKind;
  54. import org.omg.CORBA.UnknownUserException;
  55. import org.omg.CORBA.UserException;
  56. import org.omg.CORBA.portable.ApplicationException;
  57. import org.omg.CORBA.portable.InputStream;
  58. import org.omg.CORBA.portable.InvokeHandler;
  59. import org.omg.CORBA.portable.ObjectImpl;
  60. import org.omg.CORBA.portable.OutputStream;
  61. import org.omg.CORBA.portable.ResponseHandler;
  62. import org.omg.PortableInterceptor.ClientRequestInterceptorOperations;
  63. import org.omg.PortableInterceptor.ForwardRequest;
  64. import org.omg.PortableInterceptor.ServerRequestInterceptorOperations;
  65. import org.omg.PortableServer.CurrentOperations;
  66. import org.omg.PortableServer.CurrentPackage.NoContext;
  67. import org.omg.PortableServer.DynamicImplementation;
  68. import org.omg.PortableServer.POA;
  69. import org.omg.PortableServer.Servant;
  70. import org.omg.PortableServer.ServantLocatorPackage.CookieHolder;
  71. import org.omg.PortableServer.portable.Delegate;
  72. import java.io.IOException;
  73. /**
  74. * Directs the invocation to the locally available servant. The POA servant does
  75. * not longer implement the CORBA object and cannot be substituted directly.
  76. *
  77. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  78. */
  79. public class LocalRequest extends gnuRequest implements ResponseHandler,
  80. CurrentOperations
  81. {
  82. /**
  83. * Used by servant locator, if involved.
  84. */
  85. CookieHolder cookie;
  86. /**
  87. * The object Id.
  88. */
  89. final byte[] Id;
  90. /**
  91. * The message header (singleton is sufficient).
  92. */
  93. private static final MessageHeader header = new MessageHeader();
  94. /**
  95. * True if the stream was obtained by invoking {@link #createExceptionReply()},
  96. * false otherwise.
  97. */
  98. boolean exceptionReply;
  99. /**
  100. * The buffer to write into.
  101. */
  102. BufferedCdrOutput buffer;
  103. /**
  104. * The responsible POA.
  105. */
  106. final gnuPOA poa;
  107. /**
  108. * The servant delegate to obtain the handler.
  109. */
  110. gnuServantObject object;
  111. /**
  112. * Used (reused) with dynamic implementation.
  113. */
  114. LocalServerRequest serverRequest;
  115. /**
  116. * Create an instance of the local request.
  117. */
  118. public LocalRequest(gnuServantObject local_object, gnuPOA a_poa, byte[] an_id)
  119. {
  120. Id = an_id;
  121. poa = a_poa;
  122. // Instantiate the cookie holder only if required.
  123. if (poa.servant_locator != null)
  124. {
  125. cookie = new CookieHolder();
  126. }
  127. object = local_object;
  128. prepareStream();
  129. }
  130. /**
  131. * Make an invocation and return a stream from where the results can be read
  132. * and throw ApplicationException, where applicable.
  133. */
  134. org.omg.CORBA.portable.InputStream s_invoke(InvokeHandler handler)
  135. throws ApplicationException
  136. {
  137. try
  138. {
  139. poa.m_orb.currents.put(Thread.currentThread(), this);
  140. org.omg.CORBA.portable.InputStream input = v_invoke(handler);
  141. if (!exceptionReply)
  142. {
  143. return input;
  144. }
  145. else
  146. {
  147. input.mark(500);
  148. String id = input.read_string();
  149. try
  150. {
  151. input.reset();
  152. }
  153. catch (IOException ex)
  154. {
  155. InternalError ierr = new InternalError();
  156. ierr.initCause(ex);
  157. throw ierr;
  158. }
  159. throw new ApplicationException(id, input);
  160. }
  161. }
  162. finally
  163. {
  164. poa.m_orb.currents.remove(Thread.currentThread());
  165. }
  166. }
  167. /**
  168. * Make an invocation and return a stream from where the results can be read.
  169. *
  170. * @param handler the invoke handler (can be null, then it is obtained self
  171. * dependently).
  172. */
  173. public org.omg.CORBA.portable.InputStream v_invoke(InvokeHandler handler)
  174. {
  175. // Check maybe POA is in the discarding mode (will throw TRANSIENT if it is).
  176. poa.checkDiscarding();
  177. // Local request must be intercepted both by server and request
  178. // interceptors.
  179. boolean s_intercept = false;
  180. ServerRequestInterceptorOperations s_interceptor = null;
  181. gnuServerRequestInfo s_info = null;
  182. boolean c_intercept = false;
  183. ClientRequestInterceptorOperations c_interceptor = null;
  184. gnuClientRequestInfo c_info = null;
  185. try
  186. {
  187. if (poa.m_orb.iServer != null || poa.m_orb.iClient != null)
  188. {
  189. setORB(poa.m_orb);
  190. // These two are only needed with interceptors.
  191. m_rqh = new RequestHeader();
  192. m_rqh.operation = m_operation;
  193. m_rph = new ReplyHeader();
  194. m_rqh.object_key = object.Id;
  195. m_rph.request_id = m_rqh.request_id;
  196. }
  197. if (poa.m_orb.iClient != null)
  198. {
  199. c_interceptor = poa.m_orb.iClient;
  200. c_info = new gnuClientRequestInfo(this);
  201. c_intercept = true;
  202. c_interceptor.send_request(c_info);
  203. m_target = object;
  204. }
  205. if (poa.m_orb.iServer != null)
  206. {
  207. s_interceptor = poa.m_orb.iServer;
  208. s_info = new gnuServerRequestInfo(object, m_rqh, m_rph);
  209. s_info.m_request = this;
  210. s_intercept = true;
  211. s_interceptor.receive_request_service_contexts(s_info);
  212. }
  213. if (handler == null)
  214. {
  215. handler = object.getHandler(operation(), cookie, false);
  216. }
  217. BufferedCdrOutput request_part = new BufferedCdrOutput();
  218. request_part.setOrb(orb());
  219. if (m_args != null && m_args.count() > 0)
  220. {
  221. write_parameters(header, request_part);
  222. if (m_parameter_buffer != null)
  223. {
  224. throw new BAD_INV_ORDER("Please either add parameters or " +
  225. "write them into stream, but not both " + "at once."
  226. );
  227. }
  228. }
  229. if (m_parameter_buffer != null)
  230. {
  231. write_parameter_buffer(header, request_part);
  232. }
  233. Servant servant;
  234. if (handler instanceof Servant)
  235. {
  236. servant = (Servant) handler;
  237. }
  238. else
  239. {
  240. throw new BAD_OPERATION("Unexpected handler type " + handler);
  241. }
  242. org.omg.CORBA.portable.InputStream input =
  243. request_part.create_input_stream();
  244. // Ensure the servant (handler) has a delegate set.
  245. ServantDelegateImpl sd = null;
  246. Delegate d = null;
  247. try
  248. {
  249. d = servant._get_delegate();
  250. }
  251. catch (Exception ex)
  252. {
  253. // In some cases exception is thrown if the delegate is not set.
  254. }
  255. if (d instanceof ServantDelegateImpl)
  256. {
  257. // If the delegate is already set, try to reuse the existing
  258. // instance.
  259. sd = (ServantDelegateImpl) d;
  260. if (sd.object != object)
  261. {
  262. sd = new ServantDelegateImpl(servant, poa, Id);
  263. }
  264. }
  265. else
  266. {
  267. sd = new ServantDelegateImpl(servant, poa, Id);
  268. }
  269. servant._set_delegate(sd);
  270. try
  271. {
  272. ORB o = orb();
  273. if (o instanceof ORB_1_4)
  274. {
  275. ((ORB_1_4) o).currents.put(Thread.currentThread(), this);
  276. }
  277. try
  278. {
  279. if (s_intercept)
  280. {
  281. s_interceptor.receive_request(s_info);
  282. }
  283. handler._invoke(m_operation, input, this);
  284. // Handler is casted into i_handler.
  285. if ((s_intercept || c_intercept) && isExceptionReply())
  286. {
  287. s_info.m_reply_header.reply_status =
  288. ReplyHeader.USER_EXCEPTION;
  289. m_rph.reply_status = ReplyHeader.USER_EXCEPTION;
  290. // Make Any, holding the user exception.
  291. Any a = new gnuAny();
  292. OutputStream buf = getBuffer();
  293. InputStream in = buf.create_input_stream();
  294. String uex_idl = "unknown";
  295. try
  296. {
  297. in.mark(Integer.MAX_VALUE);
  298. uex_idl = in.read_string();
  299. m_exception_id = uex_idl;
  300. in.reset();
  301. }
  302. catch (IOException e)
  303. {
  304. throw new Unexpected(e);
  305. }
  306. try
  307. {
  308. UserException exception =
  309. ObjectCreator.readUserException(uex_idl, in);
  310. m_environment.exception(exception);
  311. ObjectCreator.insertWithHelper(a, exception);
  312. }
  313. catch (Exception e)
  314. {
  315. // Failed due any reason, insert without
  316. // helper.
  317. a.insert_Streamable(new StreamHolder(
  318. buf.create_input_stream()
  319. )
  320. );
  321. RecordTypeCode r =
  322. new RecordTypeCode(TCKind.tk_except);
  323. r.setId(uex_idl);
  324. r.setName(ObjectCreator.getDefaultName(uex_idl));
  325. }
  326. s_info.m_usr_exception = a;
  327. c_info.m_wrapped_exception = a;
  328. s_interceptor.send_exception(s_info);
  329. c_interceptor.receive_exception(c_info);
  330. }
  331. else
  332. {
  333. if (s_intercept)
  334. {
  335. s_info.m_reply_header.reply_status =
  336. ReplyHeader.NO_EXCEPTION;
  337. s_interceptor.send_reply(s_info);
  338. }
  339. if (c_intercept)
  340. {
  341. m_rph.reply_status = ReplyHeader.NO_EXCEPTION;
  342. c_interceptor.receive_reply(c_info);
  343. }
  344. }
  345. }
  346. catch (SystemException sys_ex)
  347. {
  348. if (s_intercept)
  349. {
  350. s_info.m_reply_header.reply_status =
  351. ReplyHeader.SYSTEM_EXCEPTION;
  352. s_info.m_sys_exception = sys_ex;
  353. s_interceptor.send_exception(s_info);
  354. }
  355. if (c_intercept)
  356. {
  357. m_rph.reply_status = ReplyHeader.SYSTEM_EXCEPTION;
  358. Any a = new gnuAny();
  359. if (ObjectCreator.insertSysException(a, sys_ex))
  360. {
  361. c_info.m_wrapped_exception = a;
  362. }
  363. c_interceptor.receive_exception(c_info);
  364. }
  365. throw sys_ex;
  366. }
  367. }
  368. finally
  369. {
  370. ORB o = orb();
  371. if (o instanceof ORB_1_4)
  372. {
  373. ((ORB_1_4) o).currents.remove(Thread.currentThread());
  374. }
  375. }
  376. if (poa.servant_locator != null)
  377. {
  378. poa.servant_locator.postinvoke(object.Id, poa, operation(),
  379. cookie.value, object.getServant()
  380. );
  381. }
  382. return buffer.create_input_stream();
  383. }
  384. catch (ForwardRequest fex)
  385. {
  386. // May be thrown by interceptor.
  387. if (s_intercept)
  388. {
  389. Forwarding:
  390. while (true)
  391. {
  392. s_info.m_reply_header.reply_status =
  393. ReplyHeader.LOCATION_FORWARD;
  394. s_info.m_forward_reference = fex.forward;
  395. try
  396. {
  397. s_interceptor.send_other(s_info);
  398. break Forwarding;
  399. }
  400. catch (ForwardRequest fex2)
  401. {
  402. s_info.m_forward_reference = fex2.forward;
  403. fex.forward = s_info.m_forward_reference;
  404. }
  405. }
  406. }
  407. if (c_intercept)
  408. {
  409. this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
  410. this.m_forwarding_target = fex.forward;
  411. try
  412. {
  413. c_interceptor.receive_other(c_info);
  414. }
  415. catch (ForwardRequest fex2)
  416. {
  417. fex.forward = fex2.forward;
  418. }
  419. }
  420. throw new gnuForwardRequest(fex.forward);
  421. }
  422. catch (gnuForwardRequest fex)
  423. {
  424. // May be thrown during activation.
  425. // May be thrown during activation.
  426. if (s_intercept)
  427. {
  428. Forwarding:
  429. while (true)
  430. {
  431. s_info.m_reply_header.reply_status =
  432. ReplyHeader.LOCATION_FORWARD;
  433. s_info.m_forward_reference = fex.forward_reference;
  434. try
  435. {
  436. s_interceptor.send_other(s_info);
  437. break Forwarding;
  438. }
  439. catch (ForwardRequest fex2)
  440. {
  441. s_info.m_forward_reference = fex2.forward;
  442. fex.forward_reference = (ObjectImpl) fex2.forward;
  443. }
  444. }
  445. }
  446. if (c_intercept)
  447. {
  448. this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
  449. this.m_forwarding_target = fex.forward_reference;
  450. try
  451. {
  452. c_interceptor.receive_other(c_info);
  453. }
  454. catch (ForwardRequest fex2)
  455. {
  456. fex.forward_reference = (ObjectImpl) fex2.forward;
  457. }
  458. }
  459. throw fex;
  460. }
  461. }
  462. /**
  463. * Make an invocation and store the result in the fields of this Request. Used
  464. * with DII only.
  465. */
  466. public void invoke()
  467. {
  468. InvokeHandler handler = object.getHandler(operation(), cookie, false);
  469. if (handler instanceof DynamicImpHandler)
  470. {
  471. DynamicImplementation dyn = ((DynamicImpHandler) handler).servant;
  472. if (serverRequest == null)
  473. {
  474. serverRequest = new LocalServerRequest(this);
  475. }
  476. try
  477. {
  478. poa.m_orb.currents.put(Thread.currentThread(), this);
  479. dyn.invoke(serverRequest);
  480. }
  481. finally
  482. {
  483. poa.m_orb.currents.remove(Thread.currentThread());
  484. }
  485. }
  486. else
  487. {
  488. org.omg.CORBA.portable.InputStream input = v_invoke(handler);
  489. if (!exceptionReply)
  490. {
  491. NamedValue arg;
  492. // Read return value, if set.
  493. if (m_result != null)
  494. {
  495. m_result.value().read_value(input, m_result.value().type());
  496. }
  497. // Read returned parameters, if set.
  498. if (m_args != null)
  499. {
  500. for (int i = 0; i < m_args.count(); i++)
  501. {
  502. try
  503. {
  504. arg = m_args.item(i);
  505. // Both ARG_INOUT and ARG_OUT have this binary flag set.
  506. if ((arg.flags() & ARG_OUT.value) != 0)
  507. {
  508. arg.value().read_value(input, arg.value().type());
  509. }
  510. }
  511. catch (Bounds ex)
  512. {
  513. Unexpected.error(ex);
  514. }
  515. }
  516. }
  517. }
  518. else// User exception reply
  519. {
  520. // Prepare an Any that will hold the exception.
  521. gnuAny exc = new gnuAny();
  522. exc.insert_Streamable(new StreamHolder(input));
  523. UnknownUserException unuex = new UnknownUserException(exc);
  524. m_environment.exception(unuex);
  525. }
  526. }
  527. }
  528. /**
  529. * Get an output stream for providing details about the exception. Before
  530. * returning the stream, the handler automatically writes the message header
  531. * and the reply about exception header, but not the message header.
  532. *
  533. * @return the stream to write exception details into.
  534. */
  535. public OutputStream createExceptionReply()
  536. {
  537. exceptionReply = true;
  538. prepareStream();
  539. return buffer;
  540. }
  541. /**
  542. * Get an output stream for writing a regular reply (not an exception).
  543. *
  544. * Before returning the stream, the handler automatically writes the regular
  545. * reply header, but not the message header.
  546. *
  547. * @return the output stream for writing a regular reply.
  548. */
  549. public OutputStream createReply()
  550. {
  551. exceptionReply = false;
  552. prepareStream();
  553. return buffer;
  554. }
  555. /**
  556. * Get the buffer, normally containing the written reply. The reply includes
  557. * the reply header (or the exception header) but does not include the message
  558. * header.
  559. *
  560. * The stream buffer can also be empty if no data have been written into
  561. * streams, returned by {@link #createReply()} or
  562. * {@link #createExceptionReply()}.
  563. *
  564. * @return the CDR output stream, containing the written output.
  565. */
  566. BufferedCdrOutput getBuffer()
  567. {
  568. return buffer;
  569. }
  570. /**
  571. * True if the stream was obtained by invoking {@link #createExceptionReply()},
  572. * false otherwise (usually no-exception reply).
  573. */
  574. boolean isExceptionReply()
  575. {
  576. return exceptionReply;
  577. }
  578. /**
  579. * Compute the header offset, set the correct version number and codeset.
  580. */
  581. private void prepareStream()
  582. {
  583. buffer = new BufferedCdrOutput();
  584. buffer.setOrb(orb());
  585. }
  586. /**
  587. * Get the parameter stream, where the invocation arguments should be written
  588. * if they are written into the stream directly.
  589. */
  590. public StreamBasedRequest getParameterStream()
  591. {
  592. m_parameter_buffer = new StreamBasedRequest();
  593. m_parameter_buffer.request = this;
  594. m_parameter_buffer.setOrb(poa.orb());
  595. return m_parameter_buffer;
  596. }
  597. public byte[] get_object_id() throws NoContext
  598. {
  599. return Id;
  600. }
  601. public POA get_POA() throws NoContext
  602. {
  603. return poa;
  604. }
  605. }