ResponseHandlerImpl.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* ResponseHandlerImpl.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.BufferedCdrOutput;
  33. import gnu.CORBA.GIOP.MessageHeader;
  34. import gnu.CORBA.GIOP.ReplyHeader;
  35. import gnu.CORBA.GIOP.RequestHeader;
  36. import gnu.CORBA.GIOP.CodeSetServiceContext;
  37. import org.omg.CORBA.ORB;
  38. import org.omg.CORBA.portable.OutputStream;
  39. import org.omg.CORBA.portable.ResponseHandler;
  40. /**
  41. * Provides the CDR output streams for writing the response to the given buffer.
  42. *
  43. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  44. */
  45. public class ResponseHandlerImpl
  46. implements ResponseHandler
  47. {
  48. /**
  49. * The message header. This field is used to compute the size and alignments.
  50. * It is, however, never directly written to the buffer stream.
  51. */
  52. public final MessageHeader message_header;
  53. /**
  54. * The associated orb.
  55. */
  56. public final ORB orb;
  57. /**
  58. * The reply header.
  59. */
  60. public final ReplyHeader reply_header;
  61. /**
  62. * The request header.
  63. */
  64. public final RequestHeader request_header;
  65. /**
  66. * True if the stream was obtained by invoking {@link #createExceptionReply()},
  67. * false otherwise.
  68. */
  69. private boolean exceptionReply;
  70. /**
  71. * The buffer to write into.
  72. */
  73. private BufferedCdrOutput buffer;
  74. /**
  75. * Create a new buffered response handler that uses the given message headers.
  76. * The headers are used to compute sizes and check the versions. They are not
  77. * written into a stream inside this class.
  78. *
  79. * @param m_header a message header.
  80. * @param r_header a reply header.
  81. */
  82. ResponseHandlerImpl(ORB an_orb, MessageHeader m_header,
  83. ReplyHeader r_header, RequestHeader rq_header)
  84. {
  85. message_header = m_header;
  86. reply_header = r_header;
  87. request_header = rq_header;
  88. orb = an_orb;
  89. prepareStream();
  90. }
  91. /**
  92. * Get an output stream for providing details about the exception. Before
  93. * returning the stream, the handler automatically writes the message header
  94. * and the reply about exception header, but not the message header.
  95. *
  96. * @return the stream to write exception details into.
  97. */
  98. public OutputStream createExceptionReply()
  99. {
  100. exceptionReply = true;
  101. prepareStream();
  102. return buffer;
  103. }
  104. /**
  105. * Get an output stream for writing a regular reply (not an exception).
  106. *
  107. * Before returning the stream, the handler automatically writes the regular
  108. * reply header, but not the message header.
  109. *
  110. * @return the output stream for writing a regular reply.
  111. */
  112. public OutputStream createReply()
  113. {
  114. exceptionReply = false;
  115. prepareStream();
  116. reply_header.reply_status = ReplyHeader.NO_EXCEPTION;
  117. return buffer;
  118. }
  119. /**
  120. * Get the buffer, normally containing the written reply. The reply includes
  121. * the reply header (or the exception header) but does not include the message
  122. * header.
  123. *
  124. * The stream buffer can also be empty if no data have been written into
  125. * streams, returned by {@link #createReply()} or
  126. * {@link #createExceptionReply()}.
  127. *
  128. * @return the CDR output stream, containing the written output.
  129. */
  130. public BufferedCdrOutput getBuffer()
  131. {
  132. return buffer;
  133. }
  134. /**
  135. * True if the stream was obtained by invoking {@link #createExceptionReply()},
  136. * false otherwise (usually no-exception reply).
  137. */
  138. public boolean isExceptionReply()
  139. {
  140. return exceptionReply;
  141. }
  142. /**
  143. * Compute the header offset, set the correct version number and codeset.
  144. */
  145. private void prepareStream()
  146. {
  147. buffer = new BufferedCdrOutput();
  148. buffer.setOrb(orb);
  149. buffer.setVersion(message_header.version);
  150. buffer.setCodeSet(CodeSetServiceContext.find(reply_header.service_context));
  151. // Since 1.2, the data section is always aligned on the 8 byte boundary.
  152. // In older versions, it is necessary to set the offset correctly.
  153. if (message_header.version.until_inclusive(1, 1))
  154. {
  155. buffer.setOffset(message_header.getHeaderSize());
  156. // Get the position after the reply header would be written.
  157. reply_header.write(buffer);
  158. int new_offset = message_header.getHeaderSize() + buffer.buffer.size();
  159. buffer.buffer.reset();
  160. buffer.setOffset(new_offset);
  161. }
  162. }
  163. }