2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package gnu.classpath.examples.CORBA.SimpleCommunication.communication;
42 import java.io.FileReader;
43 import java.io.IOException;
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;
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.
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
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.
69 * BE SURE TO START THE SERVER BEFORE STARTING THE CLIENT.
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.
75 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
77 public class RequestTest
80 * The IOR.txt file, used to find the server and the object on the server. is written when starting the accompanying
82 public static final String IOR_FILE = "IOR.txt";
85 * The Object Request Brocker, used for various CORBA operations.
90 * Our remote object - the invocation target.
92 org.omg.CORBA.Object object;
95 * Prepare for work. Read the file IOR.txt in the current folder
96 * and find the server using its information.
98 public static void main(String[] args)
100 RequestTest we = new RequestTest();
102 we.orb = org.omg.CORBA.ORB.init(new String[ 0 ], null);
107 File f = new File(IOR_FILE);
108 c = new char[ (int) f.length() ];
110 FileReader fr = new FileReader(f);
114 catch (IOException ex)
116 System.out.println("Unable to write the IOR.txt into the current folder");
117 ex.printStackTrace();
120 String ior = new String(c);
122 we.object = we.orb.string_to_object(ior);
124 we.orb.shutdown(false);
127 /** Run all demos. */
138 throw new InternalError();
140 testSystemException();
141 testWideNarrowStrings();
145 * Send the hello message, one way.
147 public void testHello()
149 System.out.println("***** Test 'HELLO WORLD' (see the server console).");
152 object._create_request(null, "sayHello", orb.create_list(0), null);
154 // No response expected.
159 * Test passing various parameters in both directions.
161 public void testParameters()
164 System.out.println("***** Test passing multiple parameters:");
167 object._create_request(null, "passSimple", orb.create_list(0), null);
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));
175 NVList para = r.arguments();
177 System.out.println(" --- Parameters before invocation: ");
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()
183 System.out.println(" short " + para.item(2).value().extract_short());
184 System.out.println(" string " + para.item(3).value().extract_string());
186 // For the last parameter, the value is not set.
187 r.set_return_type(orb.get_primitive_tc(TCKind.tk_long));
191 para = r.arguments();
193 System.out.println(" --- Parameters after invocation:");
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()
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());
203 System.out.println(" Returned value " + r.result().value().extract_long());
207 * Test catching the system exception, thrown on the remote side.
209 public void testSystemException()
211 System.out.println("**** Test system exception:");
214 ExceptionList exList = orb.create_exception_list();
215 exList.add(WeThrowThisExceptionHelper.type());
218 object._create_request(null, "throwException", orb.create_list(1),
222 rq.add_in_arg().insert_long(-55);
226 throw new InternalError();
228 catch (BAD_OPERATION ex)
230 System.out.println(" The expected BAD_OPERATION, minor code " +
231 ex.minor + ", has been thrown on remote side."
237 * Test catching the user exception, thrown on the remote side.
239 public void testUserException()
241 System.out.println("**** Test user exception:");
243 ExceptionList exList = orb.create_exception_list();
244 exList.add(WeThrowThisExceptionHelper.type());
247 object._create_request(null, "throwException", orb.create_list(1), null,
251 rq.add_in_arg().insert_long(123);
254 UnknownUserException uku = (UnknownUserException) rq.env().exception();
255 WeThrowThisException our_exception = WeThrowThisExceptionHelper.extract(uku.except);
257 System.out.println(" Our user exception, field " + our_exception.ourField +
258 ", has been thrown on remote side."
263 * Passes wide (UTF-16) string and narrow (ISO8859_1) string.
264 * @see gnu.CORBA.GIOP.CharSets_OSF for supported and default
267 public void testWideNarrowStrings()
270 System.out.println("**** Test 8 bit and 16 bit char strings");
273 object._create_request(null, "passCharacters", orb.create_list(0), null);
275 rq.add_in_arg().insert_wstring("wide string");
276 rq.add_in_arg().insert_string("narrow string");
278 rq.set_return_type(orb.get_primitive_tc(TCKind.tk_wstring));
282 System.out.println(" Returned ' " + rq.result().value().extract_wstring());