Advertisment

Implementing Corba with Java

author-image
PCQ Bureau
New Update

Last month we talked about the specifications of Corba andhow it works. We saw how two applications created in different languages,sitting on different operating systems and even on different platforms can talkto each other using CORBA. We used an example where object A sitting on onemachine asks object B on another machine for information on a project X, andobject B gives it the requested information. In this article we’ll take thisexample and walk you through the coding using Java.

Advertisment

Getting started

Practically, object A (client) and object B (server) resideon different machines. But for simplicity you can run both client and serverobjects on the same machine. You must have TCP/IP installed on both machinesbecause the ORBs communicate using IIOP (Internet Inter-ORB protocol), whichonly understands TCP/IP. You must also have JDK 1.2 or above installed.

The code for running this application is given on this month’sCD in cdrom\ sorc_cod\corba. First you will need to define the interface forobject B using CORBA IDL (Interface Definition Language). It’s through thisinterface that object A comes to know the functionality of object B. In theprogram, object B is called ProjectInfo object.

Advertisment

The file CorbaTest.idl contains this interface definition.

interface ProjectInfo

{

Advertisment

string getInformation(in string projectName);

};

The line between the curly braces { } declares a methodgetInformation( ) which can accept a parameter called projectName. The type ofthis parameter is a CORBA string and the return type of this method is also aCORBA string. The parameter projectName is of type in, which specifies that itis an input parameter to the method getInformation( ).

Advertisment

Now compile Corba Test.idl using an IDL compiler. In thedirectory cdrom/sorc_cod/corba you will find a self-extracting file namedidltojava-ea-win32. Copy this to your hard drive and double clicking on it willcreate a directory named idltojava-win32. This will contain a file calledidltojava.exe. Create a directory pcq and copy this file along withCorbaTest.idl to it. Now compile CorbaTest.idl as:

idltojava -fno-cpp —fclient —fserver CorbaTest.idl

This will create a number of files, including java interfacefor ProjectInfo object, client stub and server skeleton. If you edit the fileProjectInfo.java, you will see that it contains the following:

Advertisment

public interface ProjectInfo

extends org.omg.CORBA.Object,org.omg.CORBA.portable.IDLEntity {

String getInformation(String projectName)

Advertisment

;

}

This is a Java interface called ProjectInfo. You can comparethe signatures of the method getInformation( ) in this file with that in theCorbaTest.idlfile. The CORBA string type has been converted to Java Stringclass. This is called CORBA IDL to Java mapping. In this kind of mapping allstructures or data types of CORBA are converted into those of Java. (See CORBAIDL to Java mapping)

Advertisment

Next you have to provide functionality to the methodgetInformation( ) of the object ProjectInfo. The functionality is to look intoan Access Database for information on the project supplied as the parameterprojectName and then return this information to the client object. Thisfunctionality is provided by the class ProjectInfoImpl coded inProjectInfoImpl.java file.

Copy the file ProjectInfoImpl.java from the CD to the pcqdirectory. Open this file and observe the following line:

public class ProjectInfoImpl extends _ProjectInfoImplBaseimplements

ProjectInfo

The class ProjectInfoImpl inherits the class _ProjectInfoImplBase,which is the server skeleton and implements ProjectInfo interface. This meansthat it is providing functionality to the method declared in this interface.

The main( ) method may look somewhat cryptic, as this is theplace where all the CORBA specific action is taking place. The main( ) methoddoes the following:

1. Initializes the ORB at the server side

ORB orb = ORB.init(args,null);

2. Creates and registers the ProjectInfoImpl object with the ORB

ProjectInfoImpl projectInfo = new ProjectInfoImpl();

orb.connect(projectInfo);

3. Gets a reference to the CORBA naming service

org.omg.CORBA.Object obj =

orb.resolve_initial_references

("Name Service");

NamingContext namingContext =

NamingContextHelper.narrow(obj);

4. Registers the ProjectInfoImpl object with the CORBA naming service with a unique name ProjectInformation Server. This name will be used by the clients (in our case object A) to locate the server Object ProjectInfo

NameComponent nameComponent = new NameComponent("ProjectIn formationServer","");

NameComponent path<> = {name Component};

namingContext.rebind(path, projectInfo);

5. Finally the server waits for the clients in a separate indefinite thread.

The method connectToDatabase( ) connects to an ODBC databasewith data source name as project, and no login name and password (indicated by" " in the last line) as below:

Class.forName ("sun.jdbc.odbc. Jdbc OdbcDriver");

String url = "jdbc:odbc:project";

Connection con = DriverManager.get Connection(url,"","");

We have provided an Access database file named project.mdb inthe CD. This database contains a table named project_info which is filled with asingle row of information on the project named X. Copy this file to the pcqdirectory and then go to the control panel. Using the ODBC Data Sources applet,create a new data source for Access. Name the data source as project and selectthe file project.mdb. If you give a login name and password then modify the lineas:

Connection con = DriverManager. getConnection (url,"username","password");

Substitute username and password with that of yours.

Now the method getInformation( ) in ProjectImpl.java willquery the database for information on the project like: project name, date onwhich the project started, deadline to finish the project, project in charge andnumber of programmers working in the project. This information is returned as asingle string separated by a ? (question mark).



CORBA IDL to Java Mapping



The following table shows some of the data types and data structures in CORBA IDL and their counterparts in Java.

CORBA  Java char char string String object short short long int long long long float float double double interface Java interface array array sequence Vector object struct class

Running the server

You must first start the CORBA naming service as:

start tnameserv

This application comes with JDK. Next compile and start theserver as:

javac ProjectInfoImpl.java

start java ProjectInfoImpl

Client side

This is the object A in our story, called ProjectInfoClient.Copy the file ProjectInfo Client.java to the pcq directory and open it tounderstand what the client is doing.

Again the main( ) method, first initializes the ORB at theclient side and obtains a reference to the naming service. Then looks up thenaming service for the server object in the following lines, using the sameunique name ProjectInformationServer that we gave to it in ProjectInfoImpl.java.

NameComponent nameComponent = new NameComponent("Project InformationServer","");

NameComponent path<> = { name Component };

projectInfoObj = ProjectInfoHelper. narrow (namingContext.resolve(path));

Note that the last line ProjectInfo Helper.narrow (namingContext.resolve (path)) returns a reference which is the Interoperable ObjectReference (IOR) to the server object. This reference is stored in the objectprojectInfoObj which is an instance of the interface ProjectInfo. This is theinterface that the ProjectInfoImpl class implements or inherits. So using theobject projectInfoObj we can now call the method getInformation( ). This is donein the method getProjectInformation( ) of ProjectInfoClient as:

String result = projectInfoObj. getInformation("X");

Thus, the result–which is all the information on theproject with each information separated by a question mark–is stored in theString object result. Next, the StringTokenizer class is used to segregate theinformation and display it in a formatted way. Now compile and run the clientas:

javac ProjectInfoClient.java

java ProjectInfoClient

If you do get an output of the following form it means youwere able to run the CORBA application successfully.

Connected to the server

Project Information

Project Name : X

Project Started On: 2000-10-01

Project Deadline: 2001-02-02

Project Incharge: David

No of programmers working in the project: 10

You can also implement the client of server (or both) in C++. We’ve givethe ORB for C++ in this month’s CD.

Shekhar Govindrajan

Advertisment