OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / CORBA / SimpleCommunication / communication / RequestTest.java
1 /* RequestTest.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.classpath.examples.CORBA.SimpleCommunication.communication;
40
41 import java.io.File;
42 import java.io.FileReader;
43 import java.io.IOException;
44
45 import org.omg.CORBA.BAD_OPERATION;
46 import org.omg.CORBA.ExceptionList;
47 import org.omg.CORBA.NVList;
48 import org.omg.CORBA.ORB;
49 import org.omg.CORBA.Request;
50 import org.omg.CORBA.TCKind;
51 import org.omg.CORBA.UnknownUserException;
52
53 /**
54  * This code uses CORBA to call various methods of the remote object,
55  * passing data structures in both directions. It finds the server by
56  * reading the IOR.txt file that must be present in the folder,
57  * where the program has been started.
58  *
59  * The IOR.txt file is written by the server
60  * {@link gnu.classpath.examples.CORBA.SimpleCommunication.DemoServer}.
61  * The server should be reachable over Internet, unless blocked by
62  * security tools.
63  *
64  * This code is tested for interoperability with Sun Microsystems
65  * java implementation 1.4.2 (08.b03). Server, client of both can
66  * be started either on Sun's or on Classpath CORBA implementation,
67  * in any combinations.
68  *
69  * BE SURE TO START THE SERVER BEFORE STARTING THE CLIENT.
70  *
71  * Test invocations using org.omg.CORBA.Request. The methods are
72  * called by "name", like in java.lang.reflect.
73  * No need to have the local pre-compiled stub classes.
74  *
75  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
76  */
77 public class RequestTest
78 {
79   /*
80   * The IOR.txt file, used to find the server and the object on the server. is written when starting the accompanying
81   */
82   public static final String IOR_FILE = "IOR.txt";
83
84   /**
85    * The Object Request Brocker, used for various CORBA operations.
86    */
87   ORB orb;
88
89   /**
90    * Our remote object - the invocation target.
91    */
92   org.omg.CORBA.Object object;
93
94   /**
95    * Prepare for work. Read the file IOR.txt in the current folder
96    * and find the server using its information.
97    */
98   public static void main(String[] args)
99   {
100     RequestTest we = new RequestTest();
101
102     we.orb = org.omg.CORBA.ORB.init(new String[ 0 ], null);
103
104     char[] c = null;
105     try
106       {
107         File f = new File(IOR_FILE);
108         c = new char[ (int) f.length() ];
109
110         FileReader fr = new FileReader(f);
111         fr.read(c);
112         fr.close();
113       }
114     catch (IOException ex)
115       {
116         System.out.println("Unable to write the IOR.txt into the current folder");
117         ex.printStackTrace();
118       }
119
120     String ior = new String(c);
121
122     we.object = we.orb.string_to_object(ior);
123     we.Demo();
124     we.orb.shutdown(false);
125   }
126
127   /** Run all demos. */
128   public void Demo()
129   {
130     testHello();
131     try
132       {
133         testParameters();
134       }
135     catch (Exception ex)
136       {
137         // Not expected.
138         throw new InternalError();
139       }
140     testSystemException();
141     testWideNarrowStrings();
142   }
143
144   /**
145    * Send the hello message, one way.
146    */
147   public void testHello()
148   {
149     System.out.println("***** Test 'HELLO WORLD' (see the server console).");
150
151     Request hello =
152       object._create_request(null, "sayHello", orb.create_list(0), null);
153
154     // No response expected.
155     hello.send_oneway();
156   }
157
158   /**
159    * Test passing various parameters in both directions.
160    */
161   public void testParameters()
162                       throws Exception
163   {
164     System.out.println("***** Test passing multiple parameters:");
165
166     Request r =
167       object._create_request(null, "passSimple", orb.create_list(0), null);
168
169     r.add_inout_arg().insert_octet((byte) 0);
170     r.add_in_arg().insert_long(2);
171     r.add_inout_arg().insert_short((short) 3);
172     r.add_inout_arg().insert_string("[string 4]");
173     r.add_out_arg().type(orb.get_primitive_tc(TCKind.tk_double));
174
175     NVList para = r.arguments();
176
177     System.out.println(" --- Parameters before invocation: ");
178
179     System.out.println("  octet " + para.item(0).value().extract_octet());
180     System.out.println("  long (in parameter) " +
181                        para.item(1).value().extract_long()
182                       );
183     System.out.println("  short " + para.item(2).value().extract_short());
184     System.out.println("  string " + para.item(3).value().extract_string());
185
186     // For the last parameter, the value is not set.
187     r.set_return_type(orb.get_primitive_tc(TCKind.tk_long));
188
189     r.invoke();
190
191     para = r.arguments();
192
193     System.out.println(" --- Parameters after invocation:");
194
195     System.out.println("  octet " + para.item(0).value().extract_octet());
196     System.out.println("  long (in parameter, must not be changed) " +
197                        para.item(1).value().extract_long()
198                       );
199     System.out.println("  short " + para.item(2).value().extract_short());
200     System.out.println("  string " + para.item(3).value().extract_string());
201     System.out.println("  double " + para.item(4).value().extract_double());
202
203     System.out.println("  Returned value " + r.result().value().extract_long());
204   }
205
206   /**
207    * Test catching the system exception, thrown on the remote side.
208    */
209   public void testSystemException()
210   {
211     System.out.println("**** Test system exception:");
212     try
213       {
214         ExceptionList exList = orb.create_exception_list();
215         exList.add(WeThrowThisExceptionHelper.type());
216
217         Request rq =
218           object._create_request(null, "throwException", orb.create_list(1),
219                                  null, exList, null
220                                 );
221
222         rq.add_in_arg().insert_long(-55);
223
224         rq.invoke();
225
226         throw new InternalError();
227       }
228     catch (BAD_OPERATION ex)
229       {
230         System.out.println("  The expected BAD_OPERATION, minor code " +
231                            ex.minor + ", has been thrown on remote side."
232                           );
233       }
234   }
235
236   /**
237    * Test catching the user exception, thrown on the remote side.
238    */
239   public void testUserException()
240   {
241     System.out.println("**** Test user exception:");
242
243     ExceptionList exList = orb.create_exception_list();
244     exList.add(WeThrowThisExceptionHelper.type());
245
246     Request rq =
247       object._create_request(null, "throwException", orb.create_list(1), null,
248                              exList, null
249                             );
250
251     rq.add_in_arg().insert_long(123);
252     rq.invoke();
253
254     UnknownUserException uku = (UnknownUserException) rq.env().exception();
255     WeThrowThisException our_exception = WeThrowThisExceptionHelper.extract(uku.except);
256
257     System.out.println("  Our user exception, field " + our_exception.ourField +
258                        ", has been thrown on remote side."
259                       );
260   }
261
262   /**
263    * Passes wide (UTF-16) string and narrow (ISO8859_1) string.
264    * @see gnu.CORBA.GIOP.CharSets_OSF for supported and default
265    * encodings.
266    */
267   public void testWideNarrowStrings()
268                              throws BAD_OPERATION
269   {
270     System.out.println("**** Test 8 bit and 16 bit char strings");
271
272     Request rq =
273       object._create_request(null, "passCharacters", orb.create_list(0), null);
274
275     rq.add_in_arg().insert_wstring("wide string");
276     rq.add_in_arg().insert_string("narrow string");
277
278     rq.set_return_type(orb.get_primitive_tc(TCKind.tk_wstring));
279
280     rq.invoke();
281
282     System.out.println("  Returned ' " + rq.result().value().extract_wstring());
283   }
284 }