Source for gnu.CORBA.ServiceRequestAdapter

   1: /* ServiceRequestConverter.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA;
  40: 
  41: import gnu.CORBA.CDR.BufferedCdrOutput;
  42: 
  43: import org.omg.CORBA.ARG_IN;
  44: import org.omg.CORBA.ARG_INOUT;
  45: import org.omg.CORBA.ARG_OUT;
  46: import org.omg.CORBA.Any;
  47: import org.omg.CORBA.Bounds;
  48: import org.omg.CORBA.ServerRequest;
  49: import org.omg.CORBA.portable.InputStream;
  50: import org.omg.CORBA.portable.InvokeHandler;
  51: import org.omg.CORBA.portable.OutputStream;
  52: import org.omg.CORBA.portable.ResponseHandler;
  53: import org.omg.CORBA.portable.Streamable;
  54: 
  55: /**
  56:  * This class supports invocation using ServerRequest. When possible,
  57:  * it is better to use  the {@link ObjectImpl#_invoke} rather than
  58:  * working via ServerRequest. However since 1.4 the ServerRequest is
  59:  * involved into POA machinery making this type of call is sometimes
  60:  * inavoidable.
  61:  *
  62:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  63:  */
  64: public class ServiceRequestAdapter
  65:   implements ResponseHandler
  66: {
  67:   /**
  68:    * A buffer for writing the response.
  69:    */
  70:   BufferedCdrOutput reply = new BufferedCdrOutput();
  71: 
  72:   /**
  73:    * If set to true, an exception has been thrown during the invocation.
  74:    */
  75:   boolean isException;
  76: 
  77:   public OutputStream createExceptionReply()
  78:   {
  79:     isException = true;
  80:     return reply;
  81:   }
  82: 
  83:   public OutputStream createReply()
  84:   {
  85:     isException = false;
  86:     return reply;
  87:   }
  88: 
  89:   /**
  90:    * Make an invocation.
  91:    *
  92:    * @param request a server request, containg the invocation information.
  93:    * @param target the invocation target
  94:    * @param result the result holder with the set suitable streamable.
  95:    * Using this parameter only increase the performance. It can be
  96:    * null if the return type is void or unknown.
  97:    */
  98:   public static void invoke(ServerRequest request, InvokeHandler target,
  99:                             Streamable result
 100:                            )
 101:   {
 102:     try
 103:       {
 104:         int IN = ARG_IN.value;
 105:         int OUT = ARG_OUT.value;
 106: 
 107:         // Write all arguments to the buffer output stream.
 108:         BufferedCdrOutput buffer = new BufferedCdrOutput();
 109:         gnuNVList args = new gnuNVList();
 110:         request.arguments(args);
 111: 
 112:         for (int i = 0; i < args.count(); i++)
 113:           {
 114:             if ((args.item(i).flags() & IN) != 0)
 115:               {
 116:                 args.item(i).value().write_value(buffer);
 117:               }
 118:           }
 119: 
 120:         ServiceRequestAdapter h = new ServiceRequestAdapter();
 121: 
 122:         target._invoke(request.operation(), buffer.create_input_stream(), h);
 123: 
 124:         InputStream in = h.reply.create_input_stream();
 125: 
 126:         if (h.isException)
 127:           {
 128:             // Write the exception information
 129:             gnuAny exc = new gnuAny();
 130:             GeneralHolder uku = new GeneralHolder(h.reply);
 131:             exc.insert_Streamable(uku);
 132:             request.set_exception(exc);
 133:           }
 134:         else
 135:           {
 136:             if (result != null)
 137:               {
 138:                 // Use the holder for the return value, if provided.
 139:                 result._read(in);
 140: 
 141:                 gnuAny r = new gnuAny();
 142:                 r.insert_Streamable(result);
 143:                 request.set_result(r);
 144:               }
 145:             else
 146:               {
 147:                 // Use the universal holder otherwise.
 148:                 gnuAny r = new gnuAny();
 149:                 r.insert_Streamable(new StreamHolder(in));
 150:               }
 151: 
 152:             // Unpack the arguments
 153:             for (int i = 0; i < args.count(); i++)
 154:               {
 155:                 if ((args.item(i).flags() & OUT) != 0)
 156:                   {
 157:                     Any a = args.item(i).value();
 158:                     a.read_value(in, a.type());
 159:                   }
 160:               }
 161:           }
 162:       }
 163:     catch (Bounds ex)
 164:       {
 165:         throw new InternalError();
 166:       }
 167:   }