2

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

Christian
  • 1,664
  • 1
  • 23
  • 43

2 Answers2

4

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.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • The typical way to get access to the naming service is through resolve_initial_references(). Of course you have to have configured your ORB with the IP address and port of the name service of interest in order for resolve_initial_references() to work. In omniORB for example, you put that information either in a config file, environment variable, or as a command-line argument. I don't use Java or Java ORBs, but I can't imagine why it would be any different there. – Brian Neal Jan 11 '13 at 15:44
  • Yes, you can also set the ORBInitRef.NameService property in the properties of your ORB to the location of your name service and then that service replaces the default local service so you can access it through resolve_initial_references(). This includes more steps but likely should be used in production, my code is along "Hello world" lines. I added now some sentences to dig deeper. – Audrius Meškauskas Jan 11 '13 at 15:52
  • Wonder who voted -1 for this without even explaining that is wrong (about the way to configure with properties is kind of written now). Maybe some SOAP/WDSL fanatic. Ignoring. – Audrius Meškauskas Jan 13 '13 at 12:02
  • You've improved it, but your statement "orb.resolve_initial_references("NameService") returns the local service on the running virtual machine" is not always correct. It is going to return whatever it is configured to return, and is more often going to be a remote naming service. – Brian Neal Jan 13 '13 at 18:55
  • Improved the wording further. Anyway this name service must be configured to work properly, while I agree it is normally configured in production. – Audrius Meškauskas Jan 13 '13 at 19:23
  • Thanks for answer. I will try all this today and mark it as resolved. – Christian Jan 14 '13 at 08:27
  • +1 from me, but I don't know what virtual machines have to do with this. – Brian Neal Jan 14 '13 at 14:07
1

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();
        }

    }

}
sendon1982
  • 9,982
  • 61
  • 44