OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libjava / classpath / examples / gnu / classpath / examples / CORBA / SimpleCommunication / communication / RequestTest.java
1
2
3 package gnu.classpath.examples.CORBA.SimpleCommunication.communication;
4
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.IOException;
8
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;
20
21 /**
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.
26  *
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
30  * security tools.
31  *
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.
36  *
37  * BE SURE TO START THE SERVER BEFORE STARTING THE CLIENT.
38  *
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.
42  *
43  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
44  */
45 public class RequestTest
46 {
47   /*
48   * The IOR.txt file, used to find the server and the object on the server. is written when starting the accompanying
49   */
50   public static final String IOR_FILE = "IOR.txt";
51
52   /**
53    * The Object Request Brocker, used for various CORBA operations.
54    */
55   ORB orb;
56
57   /**
58    * Our remote object - the invocation target.
59    */
60   org.omg.CORBA.Object object;
61
62   /**
63    * Prepare for work. Read the file IOR.txt in the current folder
64    * and find the server using its information.
65    */
66   public static void main(String[] args)
67   {
68     RequestTest we = new RequestTest();
69
70     we.orb = org.omg.CORBA.ORB.init(new String[ 0 ], null);
71
72     char[] c = null;
73     try
74       {
75         File f = new File(IOR_FILE);
76         c = new char[ (int) f.length() ];
77
78         FileReader fr = new FileReader(f);
79         fr.read(c);
80         fr.close();
81       }
82     catch (IOException ex)
83       {
84         System.out.println("Unable to write the IOR.txt into the current folder");
85         ex.printStackTrace();
86       }
87
88     String ior = new String(c);
89
90     we.object = we.orb.string_to_object(ior);
91     we.Demo();
92     we.orb.shutdown(false);
93   }
94
95   /** Run all demos. */
96   public void Demo()
97   {
98     testHello();
99     try
100       {
101         testParameters();
102       }
103     catch (Exception ex)
104       {
105         // Not expected.
106         throw new InternalError();
107       }
108     testSystemException();
109     testUserException();
110     testWideNarrowStrings();
111   }
112
113   /**
114    * Send the hello message, one way.
115    */
116   public void testHello()
117   {
118     System.out.println("***** Test 'HELLO WORLD' (see the server console).");
119
120     Request hello =
121       object._create_request(null, "sayHello", orb.create_list(0), null);
122
123     // No response expected.
124     hello.send_oneway();
125   }
126
127   /**
128    * Test passing various parameters in both directions.
129    */
130   public void testParameters()
131                       throws Exception
132   {
133     System.out.println("***** Test passing multiple parameters:");
134
135     Request r =
136       object._create_request(null, "passSimple", orb.create_list(0), null);
137
138     ByteHolder a_byte = new ByteHolder((byte) 0);
139     ShortHolder a_short = new ShortHolder((short) 3);
140     StringHolder a_string = new StringHolder("[string 4]");
141
142     // This is an 'out' parameter; the value must not be passed to servant.
143     DoubleHolder a_double = new DoubleHolder(56.789);
144
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));
150
151     NVList para = r.arguments();
152
153     System.out.println(" --- Parameters before invocation: ");
154
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()
158                       );
159     System.out.println("  short " + para.item(2).value().extract_short());
160     System.out.println("  string " + para.item(3).value().extract_string());
161
162     // For the last parameter, the value is not set.
163     r.set_return_type(orb.get_primitive_tc(TCKind.tk_long));
164
165     r.invoke();
166
167     para = r.arguments();
168
169     System.out.println(" --- Parameters after invocation:");
170
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()
174                       );
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());
178
179     System.out.println("  Returned value " + r.result().value().extract_long());
180   }
181
182   /**
183    * Test catching the system exception, thrown on the remote side.
184    */
185   public void testSystemException()
186   {
187     System.out.println("**** Test system exception:");
188     try
189       {
190         ExceptionList exList = orb.create_exception_list();
191         exList.add(ourUserExceptionHelper.type());
192
193         Request rq =
194           object._create_request(null, "throwException", orb.create_list(1),
195                                  null, exList, null
196                                 );
197
198         rq.add_in_arg().insert_long(-55);
199
200         rq.invoke();
201
202         throw new InternalError();
203       }
204     catch (BAD_OPERATION ex)
205       {
206         System.out.println("  The expected BAD_OPERATION, minor code " +
207                            ex.minor + ", has been thrown on remote side."
208                           );
209       }
210   }
211
212   /**
213    * Test catching the user exception, thrown on the remote side.
214    */
215   public void testUserException()
216   {
217     System.out.println("**** Test user exception:");
218
219     ExceptionList exList = orb.create_exception_list();
220     exList.add(ourUserExceptionHelper.type());
221
222     Request rq =
223       object._create_request(null, "throwException", orb.create_list(1), null,
224                              exList, null
225                             );
226
227     rq.add_in_arg().insert_long(123);
228     rq.invoke();
229
230     UnknownUserException uku = (UnknownUserException) rq.env().exception();
231     ourUserException our_exception = ourUserExceptionHelper.extract(uku.except);
232
233     System.out.println("  Our user exception, field " + our_exception.ourField +
234                        ", has been thrown on remote side."
235                       );
236   }
237
238   /**
239    * Passes wide (UTF-16) string and narrow (ISO8859_1) string.
240    * @see gnu.CORBA.GIOP.CharSets_OSF for supported and default
241    * encodings.
242    */
243   public void testWideNarrowStrings()
244                              throws BAD_OPERATION
245   {
246     System.out.println("**** Test 8 bit and 16 bit char strings");
247
248     Request rq =
249       object._create_request(null, "passCharacters", orb.create_list(0), null);
250
251     rq.add_in_arg().insert_wstring("wide string");
252     rq.add_in_arg().insert_string("narrow string");
253
254     rq.set_return_type(orb.get_primitive_tc(TCKind.tk_wstring));
255
256     rq.invoke();
257
258     System.out.println("  Returned ' " + rq.result().value().extract_wstring());
259   }
260 }