Storing Objects in the Directory

 


CORBA Objects

The CORBA (Common Object Request Broker Architecture) defines a language-independent framework for objects to invoke methods on each other. Before an object can invoke a method on another object, it must first obtain a reference to the object. The target object can use different ways to make its reference known to other objects. The traditional way is to use a naming service, such as the Common Object Services (COS) Name Service. Another way is to publish its object reference in an LDAP server that supports the schema defined RFC 2714. Using the JNDI, the code to use either of these ways are the same. You can select the underlying naming service or directory to use at runtime by selecting the initial context to use. The example shown in this section uses an LDAP directory.

 

 

Binding a CORBA Object Reference


Before you go on: To run this example, you need ldapbp.jar, as stated in the introduction of this lesson. If you are not using the Java 2 SDK, v1.2 or higher release, then you also need to install Java IDL, a version of which comes with the RMI-IIOP Standard Extension.

The following example first defines an interface, HelloApp by using the Interface Description Language (IDL).

module HelloApp {
    interface hello
    {
        string sayHello();
    };
};
It then defines an implementation of this interface, helloServant.
class helloServant extends HelloApp._helloImplBase {
    public String sayHello() {
	return "\nHello world !!\n" + new java.util.Date();
    }
}
Next, it creates an instance of helloServant and binds it to the directory, assigning it the name "cn=CorbaHello".
// Create and initialize the ORB
ORB orb = ORB.init(args, null);

// Create the servant and register it with the ORB
helloServant helloRef = new helloServant();
orb.connect(helloRef);

// Let service provider use the ORB
env.put("java.naming.corba.orb", orb);

// Create the initial context
DirContext ctx = new InitialDirContext(env);

// Bind the object to the directory
ctx.bind("cn=CorbaHello", helloRef);

After the object has been bound in the directory, an application can look it up by using the following code.

// Look up and narrow the object
HelloApp.hello h2 = HelloApp.helloHelper.narrow(
	(org.omg.CORBA.Object)ctx.lookup("cn=CorbaHello"));

// Invoke the method
System.out.println(h2.sayHello());

To run this example, do the following.

  1. Run idltojava with HelloApp.idl as the argument to produce the stubs for the CORBA object:
    # idltojava HelloApp
    
    This generates a directory HelloApp that contains .java and .class files.
  2. Compile this example:
    # javac CorbaObj.java
    
  3. Run the example:
    # java CorbaObj
    
    If you want the helloServant created by the example to hang around for other CORBA clients to access, then run the program with the -wait parameter:
    # java CorbaObj -wait
    
When you later look up this object from the directory, the directory will return the bound helloServant CORBA object. See the Reading Objects from the Directory lesson for an example.

Storing Objects in the Directory: End of Lesson

What's next? Now you can:

Storing Objects in the Directory