Lesson 5: Using Stringified Object References


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:

  1. Making a Stringified Object Reference
  2. Getting a Stringified Object Reference
  3. Destringifying the Object Reference
  4. Compiling and Running a Stringified Hello World

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.
  1. Copy your existing file HelloServer.java (or take a copy from [Path_to_JDK]/docs/guide/idl/tutorial/server).

  2. Because the new server will write a file to disk, you need to add an import statement. Add the following:
    import java.io.*; // needed for output to the file system.
    
  3. The new server won't use the naming service, so you don't need the CosNaming packages. Delete these lines from the code:
    import org.omg.CosNaming.*;                       // not needed for stringified version
    import org.omg.CosNaming.NamingContextPackage.*;  // remove from code
    
  4. 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);
    
  5. 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);
    
  6. 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";
    
  7. 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();
    
When HelloServer runs, instead of calling the ORB and registering the servant object with naming, it creates the text file HelloIOR containing a stringified reference to the servant. The file is stored in your home directory.

Getting a Stringified Object Reference

Note to Windows users: You should substitute backslashes (\) for the slashes (/) in all paths in this document.

  1. Copy your existing file HelloClient.java (or take a copy from [Path_to_JDK]/docs/guide/idl/tutorial/app).

  2. Because the new client will read a file from the disk, you need to change the import statements. Add the following:
    import java.io.*; // needed for input from the file system.
    
  3. The new client won't use the naming service, so you don't need the CosNaming package. Delete this line from the code:
    import org.omg.CosNaming;*  // not needed for stringified version
    
  4. 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));
    
  5. 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 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 a Stringified Hello World

Compiling and running the new version of Hello World requires most of the same steps as for the naming service version.

Hello World Setup

  1. Create a new project directory, called String.

  2. Copy your stringified versions of HelloServer.java and HelloClient.java into the String directory.

  3. Copy [Path_to_JDK]/docs/guide/idl/tutorial/string/helloApp and its entire contents to the String directory.

Your project directory should look like this:

string
 |-HelloServer.java
 |-HelloClient.java
 |-HelloApp
    |-_HelloImplBase.class
    |-_HelloStub.class
    |-Hello.class
    |-HelloHelper.class
    |-HelloHolder.class

Compiling Hello World

  1. Change directory to the String directory you created.

  2. Run the Java compiler on your source code:
    javac *.java
    
  3. Correct any errors in your files and recompile if necessary. (You can copy the files from the [Path_to_JDK]/docs/guide/idl/tutorial/string directory if you have trouble finding your typographical errors.)

  4. You should see HelloServer.class, HelloServant.class, and HelloClient.class in the String directory.

Running Hello World

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.

  1. Start the Hello server:
    java HelloServer -ORBInitialPort 1050 &
  2. Run the Hello application client from another window:

    java HelloClient -ORBInitialPort 1050

    The string prints to the command line:

    Hello world!!

For More Information

Initialization: Obtaining Initial Object References
Explains the various ways of getting an initial object reference


Previous lesson | Tutorial home | HelloClient.java | HelloServer.java
Home

Copyright © 1996, 1997 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA. 94043-1100 USA., All rights reserved.