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