HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

Introduction to CORBA
CORBA, which stands for Common Object Request Broker Architecture, is an industry-standard developed by the Object Management Group (OMG), a consortium of more than 500 companies. CORBA is actually a specification for creating and using distributed objects. CORBA objects are different from typical programming language objects in three ways: CORBA objects can run on any platform; they can be located anywhere on the network; and they can be written in any language that has IDL mappings.

Figure 1 shows a request being sent by a client to an object implementation. The client is the entity that wishes to perform an operation on the object, and the object implementation is the actual code and data that implements the object. The Object Request Broker (ORB), which is the heart of CORBA, is responsible for all the mechanisms required to: (1) find the object implementation for the request; (2) prepare the object implementation to receive the request; and (3) communicate the data making up the request.

Figure 1
Figure 1:

The CORBA specification is nothing if there is no software that implements it. The software that implements the CORBA specification is called an Object Request Broker (ORB). There are a number of CORBA implementations that exist in the market today. The most popular ones are: ORBIX from IONA Technologies, VisiBroker from Visigenic Software and JavaIDL from JavaSoft. Note that the latest version of Netscape Communicator comes with the VisiBroker ORB embedded into it.

As with some other distributed programming environments (e.g., RMI), CORBA objects are specified with interfaces - the contract between the client and the server. In the case of CORBA, however, the interface is specified in a special language known as the Interface Definition Language or IDL.

What is IDL?
IDL defines the types of objects by defining their interfaces. An interface consists of a set of named operations and the parameters to those operations. It is important to note that IDL is used to describe interfaces only, and not implementations. That is to say, IDL is not a programming language; however, its syntax is similar to C++ and Java.

Through IDL, a particular object implementation tells its potential clients what operations are available and how they should be invoked.

From the IDL definitions, the CORBA objects are mapped into different languages. Some of the languages that have IDL mappings include: Java, C, C++, Smalltalk, Lisp and Python. In this article we are concerned only with the Java mapping. The mapping includes definitions of Java-specific types and method interfaces to access objects through the ORB. It also defines the interaction between object invocations and the threads of control in the client or implementation.

The scope of this article doesn't allow me to discuss IDL and its mappings to Java; that subject deserves a book on its own. However, since most readers have some knowledge of Java, I think the best way to teach you a little IDL is through analogy. Table 1 shows IDL constructs and the equivalent Java constructs.

In Table 1, note that the IDL module construct is mapped to a Java package. All IDL types within the module are mapped to Java classes or interfaces. So, for example, the following IDL definition:

Table 1 // Sample.idl
module Sample {
const long dist = 23456;
}

is mapped to:

// Sample/dist.java
package Sample;
public final class dist {
public final static int value =
(int) 23456;
}

The application developed here will give you a better understanding of the IDL mapping to Java. This application was developed using VisiBroker 3.1 for Java, which is an ORB with a complete implementation of the CORBA specification.

Anatomy of a CORBA-based Application

    We will use the following steps to develop our application:
  1. Describe interface(s) using IDL
  2. Implement the CORBA classes
  3. Develop the Server
  4. Develop the Client
  5. Start the smart agent, the server and the client(s)
We will talk about each one separately by walking you through the development of an Arithmetic Server.

Arithmetic Server
The Arithmetic Server we will develop in this article will be capable of performing arithmetic operations (addition, subtraction, multiplication, etc.) on arrays of integers. However, for demonstration purposes, we will show only the addition operation. As an exercise, modify the code by adding more operations.

The first step in developing the Arithmetic Server is to define one or more interfaces in IDL.

1. Defining an Interface
When defining an interface, think of what kind of operations are needed and what parameters the operations should have. Listing 1 shows an IDL interface for the Add object. Note that the sum_arrays operation here has three parameters. The first two are the input arrays; the third will hold the result of the sum. Also, note that the first two parameters are declared as "in" and the third is declared as "out". IDL defines three parameter passing modes: in, out and inout. As the names imply, the in parameters are used for input, the out parameter for output and the inout parameter for both input and output.

Once we finish defining the IDL interface, we are ready to compile it. VisiBroker for Java comes with the IDL compiler idl2java which is used to map IDL definitions into Java. We run idl2java from the command line and we feed it the IDL interface or module we want to compile, as shown in Listing 2.

As you can see, it has generated a number of files. The important ones to use in this article are:

  • Add.java: The Arith interface declaration
  • AddOperations.java: Declares the sum_arrays method
  • _st_Add.java: Stub code for the Arith object on the client side
  • _sk_Add.java: Stub code for the Arith object implementation on the server side
  • _example_Add.java: Code you can fill in to implement the Arith object on the server side
The Add.java file contains the Java code generated from the Arith.idl definition. We show the generated code from the IDL definition, stripped of its comments, in Listing 3. This code will help us implement the sum_arrays operation.

2. Implementing the CORBA classes
Implementing the sum_arrays operation is really easy. As shown in Listing 2, the idl2java compiler has generated a file named: _exmple_Add.java. This file actually contains some constructors as well as the definition of the sum_arrays method. I copied the file into a new file: AddImpl.java and implemented the sum_arrays method as shown in Listing 4. Now we can compile it:

% javac AddImpl.java

We are ready now to develop our server program.

3. Develop the Server Program
Listing 5 shows the implementation of the Server class for the server-side of the Arithmetic Server application. The Server class does the following:

  • Initializes the Object Request Broker
  • Initializes the Basic Object Adapter (BOA)
  • Creates an AddImpl object
  • Activates the newly created object
  • Prints out a status message
  • Waits for incoming client requests
As you can see, the Server class is really small and fairly easy to follow. Once we complete the implementation of our Server program, we can compile it:

% javac Server.java

Arithmetic Client
Now we are ready to implement a client program that uses the services offered by the server.

Develop the Client Program
Most of the files we use in implementing the client program are actually contained in the Arith package generated by the idl2java compiler. The Client class, shown in Listing 6, implements the client application that obtains the sum of two arrays. The Client class performs the following:

  • Initializes the ORB
  • Binds to an Add
  • Requests the Add to sum two arrays with the specified values
  • Gets the sum of the two arrays using the object reference returned by the sum_arrays method
  • Prints out the sum of the two arrays

This brings us to the final step in developing a CORBA application using VisiBroker for Java.

Starting the Smart Agent, Server and Client
The VisiBroker Smart Agent, osagent, provides a fault-tolerant object location service and provides runtime licensing of VisiBroker applications. We start the osagent on a UNIX system, as follows:

% osagent &

Once the osagent is running, we can start the server program, which can be started as a normal standalone Java application:

% vbj Server & AddImpl[Server,oid=PersistentId[repId=IDL:Ar
ith/Add:1.0,objectName=Arithmetic Server]] is ready.

Finally, we can start the client program:
% vbj Client
The sum is: 6 6 6 6 6 6 6 6
6 6

Note: vbj is a korn shell script that sets up the paths, does some initialization and invokes the Java interpreter.

Running Applications on Different Hosts
In the final step above, we assumed that both the client and server were running on the same host. It is more common, however, to have the client and server run on different hosts. One way to do this is by having an osagent running on each host. However, VisiBroker makes it unnecessary to do so by providing the OSAGENT_ADDR property. Thus, to start a client or a server on a host that is not running the osagent, we can use the OSAGENT_ADDR property to specify the host that is running an osagent. As an example, if there is an osagent running on the host "java100", the following command can be used to start a client on a different host:

% java -DOSAGENT_ADRR=java100 Client

Conclusion
In this article, we introduced CORBA and presented the development of a small application using VisiBroker ORB for Java. In future articles I will introduce more advanced tutorials on CORBA.

Resources

  1. http:www.omg.org
  2. http://www.visigenic.com
  3. http://www.iona.com
  4. http://www.javasoft.com
About the Author
Qusay H. Mahmoud is a Technical Instructor at the Etisalat College of Engineering, United Arab Emirates. Previously he worked as a Senior Software Engineer in the School of Computer Science at Carleton University, Canada and a Software Designer at Newbridge Networks, Canada. He is the author of an upcoming book on distributed programming using Java. Qusay can be contacted at [email protected]

	

Listing 1: Arith.idl
  
 module Arith {  
     interface Add {  
         const unsigned short SIZE = 10;  
         typedef long array[SIZE];  
         void sum_arrays(in array a, in array b, out array c);  
     }  
 }  

Listing 2: % idl2java Arith.idl
  
 Creating: Arith  
 Creating: Arith/AddPackage  
 Creating: Arith/AddPackage/SIZE.java  
 Creating: Arith/AddPackage/arrayHolder.java  
 Creating: Arith/AddPackage/arrayHelper.java  
 Creating: Arith/Add.java  
 Creating: Arith/AddHolder.java  
 Creating: Arith/AddHelper.java  
 Creating: Arith/_st_Add.java  
 Creating: Arith/_sk_Add.java  
 Creating: Arith/_AddImplBase.java  
 Creating: Arith/AddOperations.java  
 Creating: Arith/_tie_Add.java  
 Creating: Arith/_example_Add.java  
 %  

Listing 3: Add.java
  
 public interface Add extends org.omg.CORBA.Object {  
    public void sum_arrays(  
       int[] a;  
       int[] b;  
       Arith.AddPackage.arrayHolder c  
    );  
 }  

Listing 4: AddImpl.java
  
 public class AddImpl extends Arith._AddImplBase {  
     /** Construct a persistently named object. */  
     public AddImpl(java.lang.String name) {  
           super(name);  
     }  
     /** Construct a transient object. */  
     public void sum_arrays() {  
         super();  
     }  
      public void sum_arrays(  
           int[] a,  
           int[] b,  
           Arith.AddPackage.arrayHolder c  
      ) {  
            c.value = new int[10];  
          for (int i = 0; i < Arith.AddPackage.SIZE.value; i++) {  
              c.value[i] = a[i] + b[i];  
          }  
      }  
 }  
   
Listing 5: Server.java
  
import org.omg.CORBA.*;  

public class Server {  
   public static void main(String argv[]) {  
 try {  
    // initialize the ORB  
    ORB orb = ORB.init();  
    // initialize the BOA  
    BOA boa = orb.BOA_init();  
    // create the AddImpl object  
    AddImpl arr = new AddImpl(“Arithmetic Server”);  
    // export the newly created object  
    boa.obj_is_ready(arr);  
    System.out.println(arr + “ is ready.”);  
    // wait for incoming requests  
    boa.impl_is_ready();  
 } catch(SystemException se) {  
    se.printStackTrace();  
 }  
   }  
}  

Listing 6: Client.java
  
import org.omg.CORBA.*;  

public class Client {  
   public static void main(String argv[]) {  
 int a[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2};  
 int b[] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4};  
 Arith.AddPackage.arrayHolder result = new  
 Arith.AddPackage.arrayHolder();  
 try {  
    // initialize the ORB  
    ORB orb = ORB.init();  
    // Locate an Add object  
    Arith.Add add = Arith.AddHelper.bind(orb, “Arithmetic Server”);  
    add.sum_arrays(a, b, result);  
    System.out.print(“The sum is: “);  
    for (int i = 0; i < Arith.AddPackage.SIZE.value; i++) {  
  System.out.println(result.value[i];  
    }  
 } catch(SystemException se) {  
    se.printStackTrace();  
 }  
   }  
}
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.