1 |
781 |
jeremybenn |
/* DirectTest.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 org.omg.CORBA.BAD_OPERATION;
|
42 |
|
|
import org.omg.CORBA.ByteHolder;
|
43 |
|
|
import org.omg.CORBA.DoubleHolder;
|
44 |
|
|
import org.omg.CORBA.ORB;
|
45 |
|
|
import org.omg.CORBA.ShortHolder;
|
46 |
|
|
import org.omg.CORBA.StringHolder;
|
47 |
|
|
import org.omg.CORBA.UserException;
|
48 |
|
|
|
49 |
|
|
import java.io.File;
|
50 |
|
|
import java.io.FileReader;
|
51 |
|
|
import java.io.IOException;
|
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 |
|
|
* This version uses direct casting. This is the most convenient
|
72 |
|
|
* method, but it is normally used together with the IDL compiler.
|
73 |
|
|
*
|
74 |
|
|
* @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
|
75 |
|
|
*/
|
76 |
|
|
public class DirectTest
|
77 |
|
|
{
|
78 |
|
|
/*
|
79 |
|
|
* The IOR.txt file, used to find the server and the object on the server. is written when starting the accompanying
|
80 |
|
|
*/
|
81 |
|
|
public static final String IOR_FILE = "IOR.txt";
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* The invocation target.
|
85 |
|
|
*/
|
86 |
|
|
DemoTester object;
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
|
|
* Get the object reference.
|
90 |
|
|
*/
|
91 |
|
|
public static void main(String[] args)
|
92 |
|
|
{
|
93 |
|
|
try
|
94 |
|
|
{
|
95 |
|
|
ORB orb = org.omg.CORBA.ORB.init(args, null);
|
96 |
|
|
|
97 |
|
|
File f = new File(IOR_FILE);
|
98 |
|
|
char[] c = new char[ (int) f.length() ];
|
99 |
|
|
FileReader fr = new FileReader(f);
|
100 |
|
|
fr.read(c);
|
101 |
|
|
fr.close();
|
102 |
|
|
|
103 |
|
|
String ior = new String(c);
|
104 |
|
|
DirectTest we = new DirectTest();
|
105 |
|
|
we.object = (DemoTester) orb.string_to_object(ior);
|
106 |
|
|
we.Demo();
|
107 |
|
|
orb.shutdown(false);
|
108 |
|
|
}
|
109 |
|
|
catch (IOException ex)
|
110 |
|
|
{
|
111 |
|
|
System.out.println("Cannot find or read the IOR file " +
|
112 |
|
|
"in the current folder"
|
113 |
|
|
);
|
114 |
|
|
ex.printStackTrace();
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/** Run all demos. */
|
119 |
|
|
public void Demo()
|
120 |
|
|
{
|
121 |
|
|
testHello();
|
122 |
|
|
testField();
|
123 |
|
|
testParameters();
|
124 |
|
|
testStringArray();
|
125 |
|
|
testStructure();
|
126 |
|
|
testWideNarrowStrings();
|
127 |
|
|
testTree();
|
128 |
|
|
testSystemException();
|
129 |
|
|
testUserException();
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/**
|
133 |
|
|
* Test the field getter/setter.
|
134 |
|
|
*/
|
135 |
|
|
public void testField()
|
136 |
|
|
{
|
137 |
|
|
System.out.println("***** Test the remote field getter/setter.");
|
138 |
|
|
System.out.println("The field value is now " + object.theField());
|
139 |
|
|
System.out.println("Setting it to 555");
|
140 |
|
|
object.theField(555);
|
141 |
|
|
System.out.println("The field value is now " + object.theField());
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
/** The simple invocation of the parameterless remote method. */
|
145 |
|
|
public void testHello()
|
146 |
|
|
{
|
147 |
|
|
System.out.println("***** Say hello (see the server console).");
|
148 |
|
|
object.sayHello();
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
/**
|
152 |
|
|
* Test passing multiple parameters in both directions.
|
153 |
|
|
*/
|
154 |
|
|
public void testParameters()
|
155 |
|
|
{
|
156 |
|
|
System.out.println("***** Pass multiple parameters.");
|
157 |
|
|
|
158 |
|
|
// Holder classes are required to simulate passing
|
159 |
|
|
// "by reference" (modification is returned back to the server).
|
160 |
|
|
ByteHolder a_byte = new ByteHolder((byte) 0);
|
161 |
|
|
ShortHolder a_short = new ShortHolder((short) 3);
|
162 |
|
|
StringHolder a_string = new StringHolder("[string 4]");
|
163 |
|
|
|
164 |
|
|
// This is an 'out' parameter; the value must not be passed to servant.
|
165 |
|
|
DoubleHolder a_double = new DoubleHolder(56.789);
|
166 |
|
|
|
167 |
|
|
int returned = object.passSimple(a_byte, 2, a_short, a_string, a_double);
|
168 |
|
|
|
169 |
|
|
System.out.println(" Returned value " + returned);
|
170 |
|
|
System.out.println(" Returned parameters: ");
|
171 |
|
|
System.out.println(" octet " + a_byte.value);
|
172 |
|
|
System.out.println(" short " + a_short.value);
|
173 |
|
|
System.out.println(" string '" + a_string.value+"'");
|
174 |
|
|
System.out.println(" double " + a_double.value);
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/**
|
178 |
|
|
* Test passing the string array, flexible size.
|
179 |
|
|
*/
|
180 |
|
|
public void testStringArray()
|
181 |
|
|
{
|
182 |
|
|
System.out.println("***** Pass string array.");
|
183 |
|
|
|
184 |
|
|
String[] x = new String[] { "one", "two" };
|
185 |
|
|
|
186 |
|
|
// The array is passed as CORBA sequence, variable size is supported.
|
187 |
|
|
String[] y = object.passStrings(x);
|
188 |
|
|
|
189 |
|
|
for (int i = 0; i < y.length; i++)
|
190 |
|
|
{
|
191 |
|
|
System.out.println(" Passed " + x [ i ] + ", returned: " + y [ i ]);
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/**
|
196 |
|
|
* Test passing the structures.
|
197 |
|
|
*/
|
198 |
|
|
public void testStructure()
|
199 |
|
|
{
|
200 |
|
|
System.out.println("***** Pass structure");
|
201 |
|
|
|
202 |
|
|
StructureToPass arg = new StructureToPass();
|
203 |
|
|
arg.a = "A";
|
204 |
|
|
arg.b = "B";
|
205 |
|
|
|
206 |
|
|
StructureToReturn r = object.passStructure(arg);
|
207 |
|
|
|
208 |
|
|
System.out.println(" Fields of the returned structure:");
|
209 |
|
|
|
210 |
|
|
System.out.println(" c: " + r.c);
|
211 |
|
|
System.out.println(" n: " + r.n);
|
212 |
|
|
|
213 |
|
|
// The field r.arra is declared as the fixed size CORBA array.
|
214 |
|
|
System.out.println(" r[0]: " + r.arra [ 0 ]);
|
215 |
|
|
System.out.println(" r[1]: " + r.arra [ 1 ]);
|
216 |
|
|
System.out.println(" r[3]: " + r.arra [ 2 ]);
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* Test catching the system exception, thrown on the remote side.
|
221 |
|
|
*/
|
222 |
|
|
public void testSystemException()
|
223 |
|
|
{
|
224 |
|
|
System.out.println("**** Test system exception:");
|
225 |
|
|
try
|
226 |
|
|
{
|
227 |
|
|
// Negative parameter = system exception.
|
228 |
|
|
object.throwException(-55);
|
229 |
|
|
}
|
230 |
|
|
catch (BAD_OPERATION ex)
|
231 |
|
|
{
|
232 |
|
|
System.out.println(" The expected BAD_OPERATION, minor code " +
|
233 |
|
|
ex.minor + ", has been thrown on remote side."
|
234 |
|
|
);
|
235 |
|
|
}
|
236 |
|
|
catch (UserException uex)
|
237 |
|
|
{
|
238 |
|
|
throw new InternalError();
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
/**
|
243 |
|
|
* Test passing the tree structure. Any shape of the tree is
|
244 |
|
|
* supported without rewriting the code.
|
245 |
|
|
*/
|
246 |
|
|
public void testTree()
|
247 |
|
|
{
|
248 |
|
|
// Manually create the tree of nodes:
|
249 |
|
|
// Root
|
250 |
|
|
// +-- a
|
251 |
|
|
// |
|
252 |
|
|
// +-- b
|
253 |
|
|
// +-- ba
|
254 |
|
|
// | |
|
255 |
|
|
// | +-- bac
|
256 |
|
|
// |
|
257 |
|
|
// +-- bb
|
258 |
|
|
System.out.println("***** Pass and return the tree.");
|
259 |
|
|
|
260 |
|
|
TreeNode n = nod("Root");
|
261 |
|
|
|
262 |
|
|
n.children = new TreeNode[] { nod("a"), nod("b") };
|
263 |
|
|
n.children [ 1 ].children = new TreeNode[] { nod("ba"), nod("bb") };
|
264 |
|
|
n.children [ 1 ].children [ 0 ].children = new TreeNode[] { nod("bac") };
|
265 |
|
|
|
266 |
|
|
TreeNodeHolder nh = new TreeNodeHolder(n);
|
267 |
|
|
|
268 |
|
|
// The server should add '++' to each TreeNode name.
|
269 |
|
|
object.passTree(nh);
|
270 |
|
|
|
271 |
|
|
// Convert the returned tree to some strig representation.
|
272 |
|
|
StringBuilder img = new StringBuilder();
|
273 |
|
|
getImage(img, nh.value);
|
274 |
|
|
|
275 |
|
|
System.out.println("Returned tree: " + img.toString());
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
/**
|
279 |
|
|
* Test catching the user exception, thrown on the remote side.
|
280 |
|
|
*/
|
281 |
|
|
public void testUserException()
|
282 |
|
|
{
|
283 |
|
|
System.out.println("**** Test user exception:");
|
284 |
|
|
try
|
285 |
|
|
{
|
286 |
|
|
// The user exception contains one user-defined field that will
|
287 |
|
|
// be initialised to the passed parameter.
|
288 |
|
|
object.throwException(123);
|
289 |
|
|
throw new InternalError();
|
290 |
|
|
}
|
291 |
|
|
catch (WeThrowThisException uex)
|
292 |
|
|
{
|
293 |
|
|
System.out.println(" The user exception with field " + uex.ourField +
|
294 |
|
|
", has been thrown on remote side."
|
295 |
|
|
);
|
296 |
|
|
}
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
/**
|
300 |
|
|
* Passes wide (UTF-16) string and narrow (ISO8859_1) string.
|
301 |
|
|
* @see gnu.CORBA.GIOP.CharSets_OSF for supported and default
|
302 |
|
|
* encodings.
|
303 |
|
|
*/
|
304 |
|
|
public void testWideNarrowStrings()
|
305 |
|
|
{
|
306 |
|
|
System.out.println("**** Test 8 bit and 16 bit char strings");
|
307 |
|
|
|
308 |
|
|
String r = object.passCharacters("wide string", "narrow string");
|
309 |
|
|
System.out.println(" returned: '" + r + "'");
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
/**
|
313 |
|
|
* Get the string representation of the passed tree.
|
314 |
|
|
* @param b the string buffer to accumulate the representation.
|
315 |
|
|
* @param n the tree (root TreeNode).
|
316 |
|
|
*/
|
317 |
|
|
private void getImage(StringBuilder b, TreeNode n)
|
318 |
|
|
{
|
319 |
|
|
b.append(n.name);
|
320 |
|
|
b.append(": (");
|
321 |
|
|
|
322 |
|
|
for (int i = 0; i < n.children.length; i++)
|
323 |
|
|
{
|
324 |
|
|
getImage(b, n.children [ i ]);
|
325 |
|
|
b.append(' ');
|
326 |
|
|
}
|
327 |
|
|
b.append(") ");
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
/**
|
331 |
|
|
* Create a TreeNode with the given header.
|
332 |
|
|
*
|
333 |
|
|
* @param hdr the TreeNode header.
|
334 |
|
|
* @return the created TreeNode.
|
335 |
|
|
*/
|
336 |
|
|
private TreeNode nod(String hdr)
|
337 |
|
|
{
|
338 |
|
|
TreeNode n = new TreeNode();
|
339 |
|
|
n.children = new TreeNode[ 0 ];
|
340 |
|
|
n.name = hdr;
|
341 |
|
|
|
342 |
|
|
return n;
|
343 |
|
|
}
|
344 |
|
|
}
|