Earlier, when we started nameserv (JavaIDL's Naming Service program), we saw that it output a line containing the characters IOR: followed by a very long string of hexadecimal characters. (In the output given earlier, the single line containing this string wraps round on to three lines.) This is an example of an Interoperable Object Reference (IOR). In this case, it is the IOR of the object that is the Naming Service.
Each object that is known to an ORB has an IOR that is unique to the object. In fact, all you need to communicate with a remote object is to know its IOR. So, if you have got the IORs of all the remote objects you want to access, then there is no need for you to run a Naming Service.
The API has two methods that help us work with IORs: they are called object_to_string and string_to_object. In the CountServer program, we currently have statements that register realCount with the Naming Service:
0104: CountServant realCount = new CountServant();
0105: orb.connect(realCount);
0106: // get the root naming context
0107: org.omg.CORBA.Object objRef =
0108: orb.resolve_initial_references("NameService");
0109: NamingContext ncRef = NamingContextHelper.narrow(objRef);
0110: // bind the Object Reference in Naming
0111: NameComponent nc = new NameComponent("second Count", "");
0112: NameComponent path[] = {nc};
0113: ncRef.rebind(path, realCount);
These can be replaced by statements that write the IOR of the
realCount
variable to a file called
Count.ior:
0176: CountServant realCount = new CountServant();
0177: orb.connect(realCount);
0178: String realCountString = orb.object_to_string(realCount);
0179: PrintStream tPrintStream =
0180: new PrintStream(new FileOutputStream("Count.ior"));
0181: tPrintStream.println(realCountString);
0182: tPrintStream.close();
And, in the
CountClient
program,
the statements that look up the remote object in
the Naming Service:
0139: // get the root naming context
0140: org.omg.CORBA.Object objRef =
0141: orb.resolve_initial_references("NameService");
0142: NamingContext ncRef = NamingContextHelper.narrow(objRef);
0143: // resolve the Object Reference in Naming
0144: NameComponent nc = new NameComponent("second Count", "");
0145: NameComponent path[] = {nc};
0146: Count stubCount = CountHelper.narrow(ncRef.resolve(path));
can be replaced by statements that read the IOR of the
realCount
variable from the file
Count.ior:
0206: DataInputStream tDataInputStream =
0207: new DataInputStream(new FileInputStream("Count.ior"));
0208: String realCountString = tDataInputStream.readLine();
0209: Count stubCount =
0210: CountHelper.narrow(orb.string_to_object(realCountString));
The complete texts of the programs that do not use a Naming Service are at http://www.dur.ac.uk/barry.cornelius/papers/IDL/CountNoNS/.
Using the IOR of a remote object may be a useful technique if you are only communicating with one object. However, it becomes more cumbersome if several objects are involved because you will have to look after several IORs (one for each object).