Access Java Corba NameService

4.8k Views Asked by At

Is there any way to access the corba nameservice from a running Java (1.5) program. I would like to see which other processes are registered to a given one.

I do know, that there are three tools within the JRE. servertool, orbd and tnameserv but either I didn't use them properly or they are not the right tools.

Additional Information: The program is started with -ORBInitialPort 1234

Execute orbd -ORBInitialPort 1234 -> Returnes an error due to already in use ( yes fine, because the application is running ) same with tnameserv. But if I use servertool -ORBInitialPort 1234, no errors occur. But if I type "list" on the command prompt or another command, it will always return an empty list.

Sincerely Christian

2

There are 2 best solutions below

7
On BEST ANSWER

The existing, running CORBA naming service can be accessed through the classes in the org.omg.CosNaming package. You need to obtain the NamingContextExt. It has methods to iterate through all existing bindings as well as to resolve objects by name.

When you start a tnameserv tool, it prints for you IOR - this is CORBA URL. You can get the CORBA object (name service including) from any ORB in the world if it is accessible through network and you supply the IOR:

public static void main(String args[]) throws Exception {
   ORB orb = ORB.init(args, null);
   // pass the IOR as command line parameter for this program
   String ior = args[0];
   org.omg.CORBA.Object objRef = orb.string_to_object(ior);
   NamingContextExt nameService = NamingContextExtHelper.narrow(objRef);

   // Now you can work with your naming service. 
}

See here for tutorial on how to access CORBA objects and here on how to work with naming service.

The orb.resolve_initial_references("NameService") by default (if not configured) returns the local service on the running virtual machine and you need to query the external one. To use this, you need to pass the correct configuration properties (second parameter that is null in my example) with the ORBInitRef.NameService property set to the address of your name service, as described here. Many (or most of) production environments have this property set so that this method returns the correct remote name service.

servertool is a command line tool that allows to list the registered CORBA objects without writing Java code. You need to specify on which host the name service of interest is running. servertool will not complain if the service is running at the given host and port. It should complain if it does not find one!

Most important, your CORBA object must register with the obtained name service by calling

nameService.bind(yourName, yourCORBAObject);

In case the name service is remote, this will send the network message containing the URL of your orb and reference to your object. If your do not register your object, of course the reference will not be available there and the servertool will show you an empty list, just as you complain.

0
On

You need to call rebind method. Here is the example

First to run orbd to start naming service

orbd -port 8888
/** Run with prgoram param:
java Server -ORBInitialPort 8888 -ORBInitialHost localhost
 */
public class Server {

    public static void main(String args[]) {
        try{
            // create and initialize the ORB
            ORB orb = ORB.init(args, null);

            // get reference to rootpoa & activate the POAManager
            POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
            rootpoa.the_POAManager().activate();

            // create servant
            EchoServer server = new EchoServer();

            // get object reference from the servant
            org.omg.CORBA.Object ref = rootpoa.servant_to_reference(server);
            Echo href = EchoHelper.narrow(ref);

            org.omg.CORBA.Object objRef =  orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            NameComponent path[] = ncRef.to_name( "ECHO-SERVER" );
            ncRef.rebind(path, href);

            System.out.println("Server ready and waiting ...");

            // wait for invocations from clients
            orb.run();
        }

        catch (Exception e) {
            System.err.println("ERROR: " + e);
            e.printStackTrace(System.out);
        }

        System.out.println("Exiting ...");

    }
}

Client side

/** Run with prgoram param:
java Client -ORBInitialPort 8888 -ORBInitialHost localhost
 */
public class Client {

    public static void main(String args[]) {
        try {
            // create and initialize the ORB
            ORB orb = ORB.init(args, null);
            org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
            Echo href = EchoHelper.narrow(ncRef.resolve_str("ECHO-SERVER"));

            String hello = href.echoString();
            System.out.println(hello);
        } catch (InvalidName invalidName) {
            invalidName.printStackTrace();
        } catch (CannotProceed cannotProceed) {
            cannotProceed.printStackTrace();
        } catch (org.omg.CosNaming.NamingContextPackage.InvalidName invalidName) {
            invalidName.printStackTrace();
        } catch (NotFound notFound) {
            notFound.printStackTrace();
        }

    }

}