IDL Constructed Types   «Prev  Next»
Lesson 3 Mapping for sequences
Objective Describe and use the Java produced by the Mapping for IDL sequences.

Java produced by the mapping for IDL Sequences in CORBA

In CORBA (Common Object Request Broker Architecture), the mapping for IDL sequences in Java produces classes that represent one-dimensional arrays with an associated length. These sequences are commonly used for handling dynamically sized lists of elements in CORBA-based distributed applications.
Understanding IDL Sequences in CORBA Java Mapping
When an IDL sequence is defined, the Java mapping generates a corresponding holder class that encapsulates an array and a length field. This helps to manage the dynamic nature of sequences efficiently.
Example IDL Definition idl module ExampleModule { typedef sequence LongSeq; }; The IDL compiler (such as `idlj`) generates the following corresponding Java class:
Generated Java Class for the Sequence
public final class LongSeqHelper {
    public static void insert(org.omg.CORBA.Any any, int[] s) { /* Code to insert into CORBA Any */ }
    public static int[] extract(org.omg.CORBA.Any any) { /* Code to extract from CORBA Any */ }
    public static org.omg.CORBA.TypeCode type() { /* Returns TypeCode of sequence */ }
    public static String id() { return "IDL:ExampleModule/LongSeq:1.0"; }
    public static int[] read(org.omg.CORBA.portable.InputStream istream) { /* Reads sequence from stream */ }
    public static void write(org.omg.CORBA.portable.OutputStream ostream, int[] value) { /* Writes sequence to stream */ }
}

A corresponding holder class is also generated:
public final class LongSeqHolder implements org.omg.CORBA.portable.Streamable {
    public int[] value;  // The actual sequence (array)
    
    public LongSeqHolder() {}
    public LongSeqHolder(int[] initial) {
        value = initial;
    }

    public void _read(org.omg.CORBA.portable.InputStream istream) {
        value = LongSeqHelper.read(istream);
    }
    public void _write(org.omg.CORBA.portable.OutputStream ostream) {
        LongSeqHelper.write(ostream, value);
    }
    public org.omg.CORBA.TypeCode _type() {
        return LongSeqHelper.type();
    }
}

Using the Generated Java Code in a CORBA Client or Server
Here’s how you can use the generated Java sequence class:
  1. Creating and Using a Sequence
    public class CORBASequenceExample {
        public static void main(String[] args) {
            // Create a sequence (array)
            int[] numbers = {10, 20, 30, 40, 50};
    
            // Use the holder class
            LongSeqHolder seqHolder = new LongSeqHolder(numbers);
    
            // Access the stored sequence
            System.out.println("Sequence Length: " + seqHolder.value.length);
            for (int num : seqHolder.value) {
                System.out.println("Value: " + num);
            }
        }
    }
    
  2. Passing a Sequence in a CORBA Remote Method If an IDL method takes or returns a sequence, the corresponding Java method will use the array type directly.
    Example IDL:
    interface ExampleService {
        LongSeq getNumbers();
        void sendNumbers(in LongSeq nums);
    };
    

Generated Java Interface:
public interface ExampleService extends org.omg.CORBA.Object {
    int[] getNumbers();
    void sendNumbers(int[] nums);
}

Java Server Implementation:
public class ExampleServiceImpl extends ExampleServicePOA {
    public int[] getNumbers() {
        return new int[]{100, 200, 300}; // Returning a sequence
    }

    public void sendNumbers(int[] nums) {
        System.out.println("Received sequence of length: " + nums.length);
        for (int num : nums) {
            System.out.println("Number: " + num);
        }
    }
}

Java Client Code:
ExampleService exampleService = ExampleServiceHelper.narrow(namingContext.resolve_str("ExampleService"));

int[] receivedNumbers = exampleService.getNumbers();
System.out.println("Received sequence length: " + receivedNumbers.length);

exampleService.sendNumbers(new int[]{5, 10, 15, 20});

Key Takeaways
  1. IDL sequences map to Java arrays, but they come with helper and holder classes to facilitate CORBA stream operations.
  2. The Holder class is useful when passing sequences as inout or out parameters.
  3. CORBA Helper classes (e.g., LongSeqHelper) are used for handling serialization (read/write methods) and inserting into Any.
  4. When sequences are used in IDL methods, they map directly to Java arrays, making it easy to work with them.
By following this approach, you can seamlessly use IDL sequences in CORBA-based Java applications.


Corba Fundamentals

IDL Sequences are basically one-dimensional Arrays

IDL sequences are basically one-dimensional arrays with an associated length. IDL sequences are mapped to Java arrays wherever they are used. No explicit sequence Java class is generated for an IDL sequence. An IDL sequence X is mapped to a Java array X of a Java element type that corresponds to the Java mapping for the IDL element type. IDL has both bounded and unbounded sequences. The only difference in the way that they are treated is that bounded sequences are length checked during marshaling, when an IDL CORBA::MARSHAL exception can be raised. Helper and holder classes, XHelper and XHolder, are generated for a sequence X.
  • Example of bounded and unbounded IDL Sequences
    Let us take a look at an example with both bounded and unbounded IDL sequences and see how they are mapped to Java. Where the FullWeekForecasts and ForecastList sequences appear in the WeatherService interface, corresponding Java arrays are generated:

Sequences Interface Weather Service
1) The IDL interface using bounded and unbounded sequences is mapped to its Java operations
// sequence.idl
// Sequence example - Weather Service
module Module4{ 
  // Bounded sequence of 7 strings
 

The IDL interface using bounded and unbounded sequences is mapped to its Java operations interface using corresponding Java Arrays.

2) The operation returning the bounded sequence is mapped to a method returning the corresponding array type
The operation returning the bounded sequence is mapped to a method returning the corresponding array type

3) The operation returning the unbounded sequence is mapped to a method returning the corresponding array type
package Module4;
public interface WeatherServiceOperations{
 public java.lang.String[] getForecastsForWeek(java.lang.String city);
 public java.lang.String[] getCurrentForecasts(java.lang.String city);
}
The operation returning the unbounded sequence is mapped to a method returning the corresponding array type

  1. The operation returning the bounded sequence is mapped to a method returning the corresponding array type
  2. The operation returning the unbounded sequence is mapped to a method returning the corresponding array type


No explicit sequence classes are generated for FullWeekForecasts and ForecastList.
There are the expected helpers and holders: ForecastListHelper, ForecastListHolder, FullWeekForecastsHelper, and FullWeekForecastsHolder.
Because no explicit classes are generated for sequences, using them in Java is simply a matter of using the corresponding Java arrays.
In the next lesson, you will learn the Java mapping for IDL enums and how to use the resulting Java.

IDL To Java Mapping For Sequences - Quiz

Click on the Quiz link below to test your knowledge of IDL-to-Java mapping for sequences.
IDL To Java Mapping For Sequences - Quiz

SEMrush Software