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:
- 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);
}
}
}
- 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
- IDL sequences map to Java arrays, but they come with helper and holder classes to facilitate CORBA stream operations.
- The
Holder
class is useful when passing sequences as inout
or out
parameters.
- CORBA Helper classes (e.g.,
LongSeqHelper
) are used for handling serialization (read
/write
methods) and inserting into Any
.
- 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.
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: