Start of Tutorial > Start of Trail > Start of Lesson | Search |
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 callingORB.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. You can find the completed versions of the source code:
HelloStringifiedServer.java
andHelloStringifiedClient.java
.
For a stringified object reference to be available to the client, the server must create the reference and store it somewhere that the client can access. Your reference will be written to disk in the form of a text file.When
- Copy your existing file
HelloServer.java
.- Because the new server will write a file to disk, you need to add an import statement. Add the following:
// needed for output to the file system. import java.io.*;- The new server won't use the naming service, so you don't need the
CosNaming
packages. Delete these lines from the code:// not needed for stringified version // remove from code import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*;- Delete the code that gets the initial naming context and resolves the reference to a
Hello
object:// 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);- Call the ORB's
object_to_string
method and pass it the reference to the servant object. This returns the object reference in a string form that can be saved in a file on disk.String ior = orb.object_to_string(helloRef);- Build the path to the file that will be stored, using system properties to determine the path structure and syntax.
String filename = System.getProperty("user.home") + System.getProperty("file.separator")+"HelloIOR";- Use standard Java operations to write the stringified
ior
to disk:FileOutputStream fos = new FileOutputStream(filename); PrintStream ps = new PrintStream(fos); ps.print(ior); ps.close();HelloStringifiedServer
runs, instead of calling the ORB and registering the servant object with naming, it creates the text fileHelloIOR
containing a stringified reference to the servant. The file is stored in your home directory.
- Copy your existing file
HelloStringifiedClient.java
.- Because the new client will read a file from the disk, you need to change the import statements. Add the following:
// needed for input from the file system. import java.io.*;- The new client won't use the naming service, so you don't need the
CosNaming
package. Delete this line from the code:// not needed for stringified version import org.omg.CosNaming.*;- Delete the code that gets the initial naming context and registers the servant with the naming service:
// 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));- Use standard Java operations to read the file that has the object reference. Note that client and server programs must know the name of the file and where it is stored.
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
HelloStringifiedClient
application now has aString
object containing the stringified object reference.
To destringify the object reference inior
, 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.
To compile Hello World:
- Compile your source code:
javac *.java- Correct any errors in your files and recompile if necessary.
- You should see
HelloStringifiedServer.class
,HelloServant.class
, andHelloStringifiedClient.class
in your directory.
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.
- Start the Hello server:
java HelloStringifiedServer -ORBInitialPort 1050 &- Run the Hello application client from another window:
java HelloStringifiedClient -ORBInitialPort 1050- The client prints the string from the server to the command line:
Hello world!!Remember to stop the
HelloStringifiedServer
process after the client application returns successfully.
Start of Tutorial > Start of Trail > Start of Lesson | Search |