Naming Service

In the near future I will put up general code illustrating how one obtains a reference to a remote object using the naming service. For the moment, here is an example taken from the JavaDatabase implementation.

This describes how the server associates itself with a given name. The orb variable is assumed to be a CORBA::ORB (or org.omg.CORBA.ORB in Java) and to have been initialized using the static method CORBA::ORB_init(command line arguments). (and in the OmniBroker case, the command line arguments must include the IOR from the naming service generated by the command, e.g. nsserv --ior > /tmp/Naming.ref )


        /* Connect to the NameSerice and obtain a reference to it as
           an object. This will allow us to invoke remote methods on it.
         */

       org.omg.CORBA.Object obj = orb.resolve_initial_references("NameService");
    	nameserver = NamingContextHelper.narrow(obj);


       /* Create the Name object to be passed to the Naming Service.
          Since we will place this object in the toplevel naming
          context, the depth/length of the NameComponent object is 1.
          We simply specify the kind field as "".
        */

        NameComponent[] name = new NameComponent[1];
         name[0] = new NameComponent();

          /* Fill in the fields of the name. */

         name[0].id = db.name();
         name[0].kind = "";

        /* Now perform the actual binding or unbinding in the name server. */

       if(advertize)
         nameserver.bind(name, db);
       else
         nameserver.unbind(name);

Now, a client uses the following code to get a reference to a remote object with a given name. Then it narrows the object to the approriate class using the static method compiled from the IDL description available in the client stubs. In the code below, we connect to the WaferGraphicsServer. Things proceed as for the client except we use the resolve method on the naming server/Naming Context rather than the bind method. Finally, we narrow the object to the appropriate class using the Helper. This is Java code, but almost identical to the C++ code modulo the way of initializing the NameComponent array (which can be done with aName.length(1);)

       org.omg.CORBA.Object nameserver = orb.resolve_initial_references("NameService");
       NamingContext nc =  NamingContextHelper.narrow(nameserver);
       NameComponent aName[] = new NameComponent[1];
          aName[0].id = server_name;
          aName[0].kind = "";
          org.omg.CORBA.Object obj = nc.resolve(aName);
          return(WaferGraphicsServerHelper.narrow(obj));

Duncan Temple Lang <duncan@research.bell-labs.com>
Last modified: Thu May 21 17:52:27 EDT 1998