Writing the Interface Definition Language (IDL) fileNote: All command and troubleshooting instructions apply to Java 2 SDK v1.3.0 and its version of idlj only. Before you start working with Java IDL, you need to install version 1.3 of the Java 2 SDK. J2SDK v1.3.0 provides the Application Programming Interface (API) and Object Request Broker (ORB) needed to enable CORBA-based distributed object interaction, as well as the idlj compiler. The idlj compiler uses the IDL-to-Java mapping to convert IDL interface definitions to corresponding Java interfaces, classes, and methods, which you can then use to implement your client and server code. This section teaches you how to write a simple IDL interface definition and how to translate the IDL interface to Java. It also describes the purpose of each file generated by the idlj compiler. These topics are included in this section: 1. Writing Hello.idl 2. Understanding the IDL file 3. Mapping Hello.idl to Java 4. Understanding the idlj Compiler Output Writing Hello.idl To create the Hello.idl file, 1. Create a new directory, named Hello, for this application. 2. Start your favorite text editor and create a file named Hello.idl in this directory. 3. In your file, enter the code for the interface definition:
module HelloApp
{ interface Hello { string sayHello(); }; }; 4. Save the file. Understanding the IDL file OMG IDL is a purely declarative language designed for specifying programming-languageindependent operational interfaces for distributed applications. OMG specifies a mapping from IDL to several different programming languages, including C, C++, Smalltalk, COBOL, Ada, and Java. When mapped, each statement in OMG IDL is translated to a corresponding statement in the programming language of choice. You can use the tool idlj to map an IDL interface to Java and implement the client class. When you map the same IDL to C++ and implement the server in that language, the Java client and C++ server interoperate through the ORB as though they were written in the same language. The IDL for Hello World is extremely simple; its single interface has a single operation. You need perform only three steps: 1. Declare the CORBA IDL module 2. Declare the interface 3. Declare the operations Declaring the CORBA IDL Module A CORBA module is a namespace that acts as a container for related interfaces and declarations. It corresponds closely to a Java package. Each module statement in an IDL file is mapped to a Java package statement. The module statement looks like this:
module HelloApp
{ // Subsequent lines of code here. }; When you compile the IDL, the module statement will generate a package statement in the Java code. Declaring the Interface Like Java interfaces, CORBA interfaces declare the API contract an object has with other objects. Each interface statement in the IDL maps to a Java interface statement when mapped. In your Hello.idl file, the interface statement looks like this:
module HelloApp
{ interface Hello // These { // are the // interface }; // statement. }; When you compile the IDL, this statement will generate an interface statement in the Java code. Your client and server classes will implement the Hello interface in different ways. Declaring the Operations CORBA operations are the behavior that servers promise to perform on behalf of clients that invoke them. Each operation statement in the IDL generates a corresponding method statement in the generated Java interface. In your Hello.idl file, the operation statement looks like this:
module HelloApp
{ interface Hello { string sayHello(); // This line is the operation statement. }; }; Our little Hello World application has only a single operation, so Hello.idl is now complete. |