To invoke an operation on a CORBA object, a client application needs a reference to the object. You can get such references in a number of ways, such as calling ORB.resolve_initial_references() or using another CORBA object (like the name service). In previous lessons, you used both of these methods to get an initial object reference.
Often, however, there is no naming service available in the distributed environment. In that situation, CORBA clients use a stringified object reference to find their first object.
In this lesson, you will learn how to create a stringified object reference as a part of the server startup, and how the client gets that reference and destringifies it for use as a real object reference.
The steps in this lesson are:
To see completed versions of the source code, follow the links to
HelloServer.java and
HelloClient.java.
Making a Stringified Object Reference
For a stringified object reference to be available to the client, the server
must create it and store it somewhere that the client can access. Your reference
will be written to disk in the form of a text file.
import java.io.*; // needed for output to the file system.
import org.omg.CosNaming.*; // not needed for stringified version import org.omg.CosNaming.NamingContextPackage.*; // remove from code
// Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Bind the object reference in naming NameComponent nc = new NameComponent("Hello", " "); NameComponent path[] = {nc}; ncRef.rebind(path, helloRef);
String ior = orb.object_to_string(helloRef);
String filename = System.getProperty("user.home")+ System.getProperty("file.separator")+"HelloIOR";
FileOutputStream fos = new FileOutputStream(filename); PrintStream ps = new PrintStream(fos); ps.print(ior); ps.close();
Note to Windows users: You should substitute backslashes (\) for the slashes (/) in all paths in this document.
import java.io.*; // needed for input from the file system.
import org.omg.CosNaming;* // not needed for stringified version
// Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Resolve the object reference in naming NameComponent nc = new NameComponent("Hello", " "); NameComponent path[] = {nc}; Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
String filename = System.getProperty("user.home")+ System.getProperty("file.separator")+"HelloIOR"; FileInputStream fis = new FileInputStream(filename); DataInputStream dis = new DataInputStream(fis); String ior = dis.readLine();
The HelloClient application now has a String object
containing the stringified object reference.
Destringifying the Object Reference
To destringify the object reference in ior, call the standard ORB method:
org.omg.CORBA.Object obj = orb.string_to_object(ior);
Finally, narrow the CORBA object to its proper type, so that the client can invoke on it:
Hello helloRef = HelloHelper.narrow(obj);The rest of the client code stays the same.
Compiling and running the new version of Hello World requires most of the
same steps as for the naming service version.
Hello World Setup
Your project directory should look like this:
string |-HelloServer.java |-HelloClient.java |-HelloApp |-_HelloImplBase.class |-_HelloStub.class |-Hello.class |-HelloHelper.class |-HelloHolder.class
javac *.java
To be certain that you are running your own server, check that all Hello server and name server processes have been stopped. Stop them if they are running.
java HelloServer -ORBInitialPort 1050 &
java HelloClient -ORBInitialPort 1050
The string prints to the command line:
Hello world!!
Previous lesson | Tutorial home | HelloClient.java | HelloServer.java
Home |