Powered by Blogger.

An application that access CORBA service using JIDL




Aim: Write an CORBA application using JAVA IDL
Required programs:
  • IDL file(interface) for simple HelloWorld
  • A Server class
  • A Client class

Hello.idl
module HelloApp
{
  interface Hello
  {
  string sayHello();
  oneway void shutdown();
  };
};
Implementing the Server (HelloServer.java)
HelloServer.java

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

import java.util.Properties;

class HelloImpl extends HelloPOA{
  private ORB orb;

  public void setORB(ORB orb_val){
    orb = orb_val;
  }
 
  public String sayHello(){
    return "\nHello world !!\n";
  }
 
  public void shutdown(){
    orb.shutdown(false);
  }
}

public class HelloServer{

  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 and register it with the ORB
      HelloImpl helloImpl = new HelloImpl();
      helloImpl.setORB(orb);

      // create a tie, with servant being the delegate.
      HelloPOATie tie = new HelloPOATie(helloImpl, rootpoa);

      // obtain the objectRef for the tie
      // this step also implicitly activates the
      // the object
      Hello href = tie._this(orb);
           
      // get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
     
      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = "Hello";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println("HelloServer 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("HelloServer Exiting ...");
       
  }
}

HelloClient.java


import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class HelloClient{

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

      // get the root naming context
      org.omg.CORBA.Object objRef =
          orb.resolve_initial_references("NameService");
         
      // Use NamingContextExt instead of NamingContext. This is
      // part of the Interoperable naming Service. 
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

      // resolve the Object Reference in Naming
      String name = "Hello";
      Hello helloImpl = HelloHelper.narrow(ncRef.resolve_str(name));

      System.out.println("Obtained a handle on server object: " + helloImpl);
      System.out.println(helloImpl.sayHello());
      helloImpl.shutdown();
      }
     
    catch (Exception e) {
      System.out.println("ERROR : " + e) ;
      e.printStackTrace(System.out);
    }
  }
}






To run this client-server application on your development machine:
  1. Change to the directory that contains the file Hello.idl.
  2. Run the IDL-to-Java compiler, idlj, twice on the IDL file to create stubs and skeletons. This step assumes that you have included the path to the java/bin directory in your path.
3.    idlj -fall  Hello.idl
4.    idlj -fallTie Hello.idl
    • It generates

    • HelloPOA.java
    • _HelloStub.java
    • Hello.java
    • HelloHelper.java
    • HelloHolder.java
    • HelloOperations.java

  1. Compile the .java files, including the stubs and skeletons (which are in the directory HelloApp). This step assumes the java/bin directory is included in your path.

6.     javac *.java HelloApp/*.java

  1. Start orbd.
  start orbd -ORBInitialPort 1050 -ORBInitialHost localhost
  1. Start the Hello server.
start java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost
  1. Run the client application:
 java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost

output:

Obtained a handle on server object: IOR:000000000000001749444c3a48656c6c6f417070
2f48656c6c6f3a312e300000000000010000000000000082000102000000000a3132372e302e302e
3100055a00000031afabcb00000000203a12163f00000001000000000000000100000008526f6f74
504f4100000000080000000100000000140000000000000200000001000000200000000000010001
00000002050100010001002000010109000000010001010000000026000000020002

Hello world !!
Share on Google Plus

About rajasekhar reddy

Author is a Tech savvy and Web Enthusiast by nature and really love to help users by providing how-to posts and tech tutorials. He is a founder and author of technolamp.co.in and programming9.com. YouTube Channel to SUBSCRIBE:
    https://www.youtube.com/user/nvrajasekhar
Available: info@programming9.com
Post a Comment