3 package gnu.classpath.examples.CORBA.SimpleCommunication.communication;
6 import java.io.FileReader;
7 import java.io.IOException;
9 import org.omg.CORBA.BAD_OPERATION;
10 import org.omg.CORBA.ByteHolder;
11 import org.omg.CORBA.DoubleHolder;
12 import org.omg.CORBA.ExceptionList;
13 import org.omg.CORBA.NVList;
14 import org.omg.CORBA.ORB;
15 import org.omg.CORBA.Request;
16 import org.omg.CORBA.ShortHolder;
17 import org.omg.CORBA.StringHolder;
18 import org.omg.CORBA.TCKind;
19 import org.omg.CORBA.UnknownUserException;
22 * This code uses CORBA to call various methods of the remote object,
23 * passing data structures in both directions. It finds the server by
24 * reading the IOR.txt file that must be present in the folder,
25 * where the program has been started.
27 * The IOR.txt file is written by the server
28 * {@link gnu.classpath.examples.CORBA.SimpleCommunication.comServer}.
29 * The server should be reachable over Internet, unless blocked by
32 * This code is tested for interoperability with Sun Microsystems
33 * java implementation 1.4.2 (08.b03). Server, client of both can
34 * be started either on Sun's or on Classpath CORBA implementation,
35 * in any combinations.
37 * BE SURE TO START THE SERVER BEFORE STARTING THE CLIENT.
39 * Test invocations using org.omg.CORBA.Request. The methods are
40 * called by "name", like in java.lang.reflect.
41 * No need to have the local pre-compiled stub classes.
43 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
45 public class RequestTest
48 * The IOR.txt file, used to find the server and the object on the server. is written when starting the accompanying
50 public static final String IOR_FILE = "IOR.txt";
53 * The Object Request Brocker, used for various CORBA operations.
58 * Our remote object - the invocation target.
60 org.omg.CORBA.Object object;
63 * Prepare for work. Read the file IOR.txt in the current folder
64 * and find the server using its information.
66 public static void main(String[] args)
68 RequestTest we = new RequestTest();
70 we.orb = org.omg.CORBA.ORB.init(new String[ 0 ], null);
75 File f = new File(IOR_FILE);
76 c = new char[ (int) f.length() ];
78 FileReader fr = new FileReader(f);
82 catch (IOException ex)
84 System.out.println("Unable to write the IOR.txt into the current folder");
88 String ior = new String(c);
90 we.object = we.orb.string_to_object(ior);
92 we.orb.shutdown(false);
106 throw new InternalError();
108 testSystemException();
110 testWideNarrowStrings();
114 * Send the hello message, one way.
116 public void testHello()
118 System.out.println("***** Test 'HELLO WORLD' (see the server console).");
121 object._create_request(null, "sayHello", orb.create_list(0), null);
123 // No response expected.
128 * Test passing various parameters in both directions.
130 public void testParameters()
133 System.out.println("***** Test passing multiple parameters:");
136 object._create_request(null, "passSimple", orb.create_list(0), null);
138 ByteHolder a_byte = new ByteHolder((byte) 0);
139 ShortHolder a_short = new ShortHolder((short) 3);
140 StringHolder a_string = new StringHolder("[string 4]");
142 // This is an 'out' parameter; the value must not be passed to servant.
143 DoubleHolder a_double = new DoubleHolder(56.789);
145 r.add_inout_arg().insert_octet((byte) 0);
146 r.add_in_arg().insert_long(2);
147 r.add_inout_arg().insert_short((short) 3);
148 r.add_inout_arg().insert_string("[string 4]");
149 r.add_out_arg().type(orb.get_primitive_tc(TCKind.tk_double));
151 NVList para = r.arguments();
153 System.out.println(" --- Parameters before invocation: ");
155 System.out.println(" octet " + para.item(0).value().extract_octet());
156 System.out.println(" long (in parameter) " +
157 para.item(1).value().extract_long()
159 System.out.println(" short " + para.item(2).value().extract_short());
160 System.out.println(" string " + para.item(3).value().extract_string());
162 // For the last parameter, the value is not set.
163 r.set_return_type(orb.get_primitive_tc(TCKind.tk_long));
167 para = r.arguments();
169 System.out.println(" --- Parameters after invocation:");
171 System.out.println(" octet " + para.item(0).value().extract_octet());
172 System.out.println(" long (in parameter, must not be changed) " +
173 para.item(1).value().extract_long()
175 System.out.println(" short " + para.item(2).value().extract_short());
176 System.out.println(" string " + para.item(3).value().extract_string());
177 System.out.println(" double " + para.item(4).value().extract_double());
179 System.out.println(" Returned value " + r.result().value().extract_long());
183 * Test catching the system exception, thrown on the remote side.
185 public void testSystemException()
187 System.out.println("**** Test system exception:");
190 ExceptionList exList = orb.create_exception_list();
191 exList.add(ourUserExceptionHelper.type());
194 object._create_request(null, "throwException", orb.create_list(1),
198 rq.add_in_arg().insert_long(-55);
202 throw new InternalError();
204 catch (BAD_OPERATION ex)
206 System.out.println(" The expected BAD_OPERATION, minor code " +
207 ex.minor + ", has been thrown on remote side."
213 * Test catching the user exception, thrown on the remote side.
215 public void testUserException()
217 System.out.println("**** Test user exception:");
219 ExceptionList exList = orb.create_exception_list();
220 exList.add(ourUserExceptionHelper.type());
223 object._create_request(null, "throwException", orb.create_list(1), null,
227 rq.add_in_arg().insert_long(123);
230 UnknownUserException uku = (UnknownUserException) rq.env().exception();
231 ourUserException our_exception = ourUserExceptionHelper.extract(uku.except);
233 System.out.println(" Our user exception, field " + our_exception.ourField +
234 ", has been thrown on remote side."
239 * Passes wide (UTF-16) string and narrow (ISO8859_1) string.
240 * @see gnu.CORBA.GIOP.CharSets_OSF for supported and default
243 public void testWideNarrowStrings()
246 System.out.println("**** Test 8 bit and 16 bit char strings");
249 object._create_request(null, "passCharacters", orb.create_list(0), null);
251 rq.add_in_arg().insert_wstring("wide string");
252 rq.add_in_arg().insert_string("narrow string");
254 rq.set_return_type(orb.get_primitive_tc(TCKind.tk_wstring));
258 System.out.println(" Returned ' " + rq.result().value().extract_wstring());