ServiceRequestAdapter.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* ServiceRequestConverter.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 org.omg.CORBA.ARG_IN;
  34. import org.omg.CORBA.ARG_OUT;
  35. import org.omg.CORBA.Any;
  36. import org.omg.CORBA.Bounds;
  37. import org.omg.CORBA.ServerRequest;
  38. import org.omg.CORBA.portable.InputStream;
  39. import org.omg.CORBA.portable.InvokeHandler;
  40. import org.omg.CORBA.portable.OutputStream;
  41. import org.omg.CORBA.portable.ResponseHandler;
  42. import org.omg.CORBA.portable.Streamable;
  43. /**
  44. * This class supports invocation using ServerRequest. When possible,
  45. * it is better to use the {@link ObjectImpl#_invoke} rather than
  46. * working via ServerRequest. However since 1.4 the ServerRequest is
  47. * involved into POA machinery making this type of call is sometimes
  48. * inavoidable.
  49. *
  50. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  51. */
  52. public class ServiceRequestAdapter
  53. implements ResponseHandler
  54. {
  55. /**
  56. * A buffer for writing the response.
  57. */
  58. BufferedCdrOutput reply = new BufferedCdrOutput();
  59. /**
  60. * If set to true, an exception has been thrown during the invocation.
  61. */
  62. boolean isException;
  63. public OutputStream createExceptionReply()
  64. {
  65. isException = true;
  66. return reply;
  67. }
  68. public OutputStream createReply()
  69. {
  70. isException = false;
  71. return reply;
  72. }
  73. /**
  74. * Make an invocation.
  75. *
  76. * @param request a server request, containg the invocation information.
  77. * @param target the invocation target
  78. * @param result the result holder with the set suitable streamable.
  79. * Using this parameter only increase the performance. It can be
  80. * null if the return type is void or unknown.
  81. */
  82. public static void invoke(ServerRequest request, InvokeHandler target,
  83. Streamable result
  84. )
  85. {
  86. try
  87. {
  88. int IN = ARG_IN.value;
  89. int OUT = ARG_OUT.value;
  90. // Write all arguments to the buffer output stream.
  91. BufferedCdrOutput buffer = new BufferedCdrOutput();
  92. gnuNVList args = new gnuNVList();
  93. request.arguments(args);
  94. for (int i = 0; i < args.count(); i++)
  95. {
  96. if ((args.item(i).flags() & IN) != 0)
  97. {
  98. args.item(i).value().write_value(buffer);
  99. }
  100. }
  101. ServiceRequestAdapter h = new ServiceRequestAdapter();
  102. target._invoke(request.operation(), buffer.create_input_stream(), h);
  103. InputStream in = h.reply.create_input_stream();
  104. if (h.isException)
  105. {
  106. // Write the exception information
  107. gnuAny exc = new gnuAny();
  108. GeneralHolder uku = new GeneralHolder(h.reply);
  109. exc.insert_Streamable(uku);
  110. request.set_exception(exc);
  111. }
  112. else
  113. {
  114. if (result != null)
  115. {
  116. // Use the holder for the return value, if provided.
  117. result._read(in);
  118. gnuAny r = new gnuAny();
  119. r.insert_Streamable(result);
  120. request.set_result(r);
  121. }
  122. else
  123. {
  124. // Use the universal holder otherwise.
  125. gnuAny r = new gnuAny();
  126. r.insert_Streamable(new StreamHolder(in));
  127. }
  128. // Unpack the arguments
  129. for (int i = 0; i < args.count(); i++)
  130. {
  131. if ((args.item(i).flags() & OUT) != 0)
  132. {
  133. Any a = args.item(i).value();
  134. a.read_value(in, a.type());
  135. }
  136. }
  137. }
  138. }
  139. catch (Bounds ex)
  140. {
  141. throw new InternalError();
  142. }
  143. }
  144. }