OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / CORBA / ServiceRequestAdapter.java
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.cdrBufOutput;
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 exists to handle obsolete invocation style using
57  * ServerRequest.
58  *
59  * @deprecated The method {@link ObjectImpl#_invoke} is much faster.
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   cdrBufOutput reply = new cdrBufOutput();
70
71   /**
72    * If set to true, an exception has been thrown during the invocation.
73    */
74   boolean isException;
75
76   public OutputStream createExceptionReply()
77   {
78     isException = true;
79     return reply;
80   }
81
82   public OutputStream createReply()
83   {
84     isException = false;
85     return reply;
86   }
87
88   /**
89    * The old style invocation using the currently deprecated server
90    * request class.
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 to read
95    * the result or null for void.
96    */
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         cdrBufOutput buffer = new cdrBufOutput();
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             universalHolder uku = new universalHolder(h.reply);
130             exc.insert_Streamable(uku);
131             request.set_exception(exc);
132           }
133         else
134           {
135             if (result != null)
136             {
137               result._read(in);
138               gnuAny r = new gnuAny();
139               r.insert_Streamable(result);
140               request.set_result(r);
141             };
142
143             // Unpack the arguments
144             for (int i = 0; i < args.count(); i++)
145               {
146                 if ((args.item(i).flags() & OUT) != 0)
147                   {
148                     Any a = args.item(i).value();
149                     a.read_value(in, a.type());
150                   }
151               }
152           }
153       }
154     catch (Bounds ex)
155       {
156         throw new InternalError();
157       }
158   }
159 }