Tutorial for CORBA Hello World using Java

The Common Object Request Broker Architecture (CORBA) is a standard developed by the Object Management Group (OMG) to provide interoperability among distributed objects. CORBA is the world’s leading middleware solution enabling the exchange of information, independent of hardware platforms, programming languages, and operating systems. CORBA is essentially a design specification for an Object Request Broker (ORB), where an ORB provides the mechanism required for distributed objects to communicate with one another, whether locally or on remote devices, written in different languages, or at different locations on a network.

In this CORBA Programming tutorial, we will learn how to create :

  • Simple Remote Object.
  • Server to instantiate (create ) and bind a remote object.
  • Client to invoke remotely an object

You need to install the Java Development Kit ( JDK ) as well as a programming editor ( IDE ) such as Eclipse. For more details of how to install JDK and Eclipse, Use the following tutorial:

The structure of the files for the projects created using Eclipse throughout this tutorials is shown below:

corba16

1. Server Side

1 Let’s create a new Java Project using Eclipse ( or NetBeans or other editor you prefer), and call it : CorbaAdditionServer-> Click Finish once done

2 Under the project CorbaAdditionServer, Create a new file called Addition.idl. Copy the code below into the new file.

/**
 *
 * @author imed
 */
module AdditionApp
{
  interface Addition
  {
    long add(in long a,in long b);
    oneway void shutdown();
    };
};

3 Open your CMD console, Change directory ( cd ) to src folder within the project location. The location of your project can be known through clicking: Select the project CorbaAdditionServer, click : File -> Properties

cd C:\Users\imed\workspace\CorbaAdditionServer\src

corba2
4 Compile the idl file using the idlj command as below:

idlj  -fall  Addition.idl

You will see that inside the src folder, new directory called AdditionApp is created with a number of Java files inside.

corba3

5 Right click on the src folder and select Refresh

corba4

6 Under the project CorbaAdditionServer, Create a new Class called AdditionObj. Copy the code below into the new class.

/**
 *
 * @author imed
 */
import AdditionApp.*;
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 AdditionObj extends AdditionPOA {
  private ORB orb;

  public void setORB(ORB orb_val) {
    orb = orb_val; 
  }

  // implement add() method
  public int add(int a, int b) {
    int r=a+b;
    return r;
  }

  // implement shutdown() method
  public void shutdown() {
    orb.shutdown(false);
  }
}

7 Under the project CorbaAdditionServer, Create a new Class called StartServer. Copy the code below into the new class.

/**
 *
 * @author imed
 */
import AdditionApp.*;

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;

public class StartServer {

  public static void main(String args[]) {
    try{
      // create and initialize the ORB //// get reference to rootpoa & activate the POAManager
      ORB orb = ORB.init(args, null);      
      POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
      rootpoa.the_POAManager().activate();

      // create servant and register it with the ORB
      AdditionObj addobj = new AdditionObj();
      addobj.setORB(orb); 

      // get object reference from the servant
      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(addobj);
      Addition href = AdditionHelper.narrow(ref);

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

      NameComponent path[] = ncRef.to_name( "ABC" );
      ncRef.rebind(path, href);

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

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

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

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

  }
}

8 Open a new windows of CMD console, type in the following command to start the ORB

start orbd -ORBInitialPort 1050

9 Under Eclipse now, click on the project  CorbaAdditionServer, then Click Run -> Run Configuration as shown below:

corba5

10 Make sure you select the StartServer for the CorbaAdditionServer.

If it does NOT show up, Double click on Java Application

corba6

11 Click on Arguments and type in the following arguments inside the Program arguments. Click Apply once done:

-ORBInitialPort 1050 -ORBInitialHost localhost

corba7

12 That’s all for the server, it should be running.

corba8

Client Side.

1 Let’s create a new Java Project using Eclipse and call it : CorbaAdditionClient-> Click Finish once done

2 Right click on AdditionApp Package under CorbaAdditionServer and click Copy

corba11

3 Right click on src Package under CorbaAdditionClient and click Paste

corba12

4 Under the project CorbaAdditionClient,  Create a new Class called StartClient. Copy the code below into the new class.

/**
 *
 * @author imed
 */
import AdditionApp.*;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
import java.util.*;

public class StartClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try {
	    ORB orb = ORB.init(args, null);
	    org.omg.CORBA.Object objRef =   orb.resolve_initial_references("NameService");
	    NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
	    Addition addobj = (Addition) AdditionHelper.narrow(ncRef.resolve_str("ABC"));

            Scanner c=new Scanner(System.in);
            System.out.println("Welcome to the addition system:");          		    
		    for(;;){
		      System.out.println("Enter a:");
		      String aa = c.nextLine();
		      System.out.println("Enter b:");
		      String bb = c.nextLine();
		      int a=Integer.parseInt(aa);
		      int b=Integer.parseInt(bb);
		      int r=addobj.add(a,b);
		      System.out.println("The result for addition is : "+r);
		      System.out.println("-----------------------------------");
            }
       }
       catch (Exception e) {
          System.out.println("Hello Client exception: " + e);
	  e.printStackTrace();
       }

    }

}

5 Click on Run -> Run Configuration, Make sure you select the StartClient for the CorbaAdditionClient.

corba14

 

6 Click on Arguments, then under the Program arguments, type in the following :

-ORBInitialPort 1050 -ORBInitialHost localhost

corba13

7 Click Apply, then Run. That’s all for the client, it should be running.

corba15

Latest Comments

Leave a Reply

Your email address will not be published. Required fields are marked *