In CORBA (Common Object Request Broker Architecture), Interface Definition Language (IDL) is used to define interfaces and data types that can be used across various platforms and languages. When using CORBA with Java, IDL constructs are mapped to equivalent Java constructs through a process known as IDL-to-Java mapping.
Here’s an explanation of how IDL types are mapped to Java:
- Basic Data Type Mappings: IDL basic types have direct mappings to Java primitive types or classes:
- `short` → `short`
- `unsigned short` → `short` (with additional utility classes for handling unsigned values)
- `long` → `int`
- `unsigned long` → `int` (with helper classes for unsigned values)
- `long long` → `long`
- `unsigned long long` → `long` (again, with helper classes)
- `float` → `float`
- `double` → `double`
- `char` → `char`
- `boolean` → `boolean`
- `octet` → `byte`
- `string` → `java.lang.String`
- `wstring` → `java.lang.String` (for wide characters)
- Constructed Data Types
Constructed types in IDL, such as structures, unions, arrays, and sequences, are more complex and map to specific Java constructs as follows:
- IDL Structs: IDL structs map to Java classes. Each field in the struct corresponds to a public member of the Java class.
Example:
struct Person {
string name;
long age;
};
Java Mapping:
public final class Person implements org.omg.CORBA.portable.IDLEntity {
public String name;
public int age;
// Constructor, equals, hashCode, etc. can be generated}
- IDL Unions:
IDL unions are mapped to Java classes that contain one field for each possible member of the union and an additional discriminator (a `public` variable) that indicates which field is active.
Example:
union Data switch (long) {
case 0: string text;
case 1: long number;
};
public final class Data implements org.omg.CORBA.portable.IDLEntity {
public int _discriminator;
public String text;
public int number;
// Getters, setters, and logic for managing the discriminator
}
- IDL Arrays: IDL arrays are mapped to Java arrays of the corresponding type.
Example:s
typedef long MyArray[10];
Java Mapping:
public int[] MyArray = new int[10];
- IDL Sequences: IDL sequences are variable-length arrays and map to Java arrays as well. The CORBA runtime handles the dynamic sizing of the sequence.
Example:
sequence<string> NameList;
public String[] NameList;
- IDL Enumerations: IDL enumerations are mapped to Java classes with a constant for each enumerator.
Example:
enum Color { RED, GREEN, BLUE };
public final class Color {
public static final int _RED = 0;
public static final int _GREEN = 1;
public static final int _BLUE = 2;
public static final Color RED = new Color(_RED);
public static final Color GREEN = new Color(_GREEN);
public static final Color BLUE = new Color(_BLUE);
public int value() {
return _value;
}
protected Color(int value) {
_value = value;
}
private int _value;
}
- Interfaces: IDL interfaces are mapped to Java interfaces. Each method in the IDL interface becomes a corresponding method in the Java interface. The Java interface also extends `org.omg.CORBA.Object` and `org.omg.CORBA.portable.IDLEntity`.
Example:
interface Calculator {
long add(in long a, in long b);
};
Java Mapping:
public interface Calculator extends org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity {
int add(int a, int b);
}
- Exception Handling: IDL exceptions are mapped to Java exceptions, extending `java.lang.Exception`.
Example:
exception Overflow {
string reason;
};
Java Mapping:
public final class Overflow extends Exception {
public String reason;
public Overflow(String reason) {
this.reason = reason;
}
}
Summary
- IDL-to-Java mappings transform IDL constructs into equivalent Java types
- Basic data types are mapped to primitive Java types.
- Complex types like structs and unions map to Java classes with public fields or discriminators.
- Sequences and arrays map to Java arrays, while enums become classes with constants.
- Exceptions and interfaces are directly translated into Java classes and interfaces.
These mappings allow seamless integration between CORBA's platform-independent IDL and the Java language.
The IDL compiler translates IDL into stub and skeleton code for a given language, in this case, Java.
As long as the client and server programs comply with the definitions in the generated stub and skeleton code, the runtime ORB enables type-safe interaction between the client and the server.
Example 2: Java Stub Code for the Building Interface
// File: Building.java
package BuildingExample;
In the next lesson, you will learn the Java mapping for IDL structs.