IDL Constructed Types   «Prev  Next»
Lesson 1

Java Mapping of Constructed types using Corba

This module discusses IDL-to-Java mapping by examining how IDL constructed types are mapped to Java and how the resulting generated Java code is used.
  • Module Objectives
    By the end of this module, you will be able to
    1. Describe how IDL structs, sequences, and enums map to Java
    2. Use the Java generated for structs, sequences, and enums
    3. Describe how IDL exceptions map to Java
    4. Use the Java generated for exceptions
    5. Describe how IDL unions and arrays map to Java
    6. Use the Java generated for unions and arrays

How IDL-to-Java mappings and IDL constructed types are mapped to Java in Corba

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:
  1. 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)
  2. 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:
    1. 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}
      
    2. 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
      }
      
    3. 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];
      
    4. 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;
      
    5. 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;
      }
      
  3. 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);
    }
    
  4. 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.

Corba Programming C++
  1. Write an Interface Definition Language (IDL) file that describes the remote object's interface
  2. Choose the programming language that you will use to implement the object
  3. Run the IDL compiler to generate language-specific implementation files, stubs and skeletons
  4. Code the implementation in chosen language
  5. Write a server main program that creates an instance of the implementation object and assigns the object a name

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.

SEMrush Software