IDL Data Types


IDL Data Types

IDL has roughly the same set of primitive data types as Java except for a few exceptions:

Summary

The following table lists the main constructs of IDL and the corresponding constructs in Java.

IDL Construct  Java Construct 
module  package 
interface (non-abstract)  signature interface and an operations interface, helper class, holder class 
interface (abstract)  signature interface, helper class, holder class 
constant (not within an interface)  public interface 
boolean  boolean 
char, wchar  char 
octet  byte 
string, wstring  java.lang.String 
short, unsigned short  short 
long, unsigned long  int 
long long, unsigned long long  long 
float  float 
double  double 
fixed  java.math.BigDecimal 
enum, struct, union  class 
sequence, array  array 
exception  class 
readonly attribute  accessor method 
readwrite attribute  accessor and modifer methods 
operation  method 

 

Enumerated Types

Unlike Java, IDL allows you to create enumerated types that represent integer values. The Java IDL system turns the enumerated type into a class with public static final values.

Here is an example IDL enumerated type:

module EnumModule{
  interface Winner {
   enum Medals { gold, silver, bronze };
   Medals winner(in short i);
  };
};
This definition would produce a directory EnumModule which contains the following Java class (an interface does not create a new directory !):
Winner.java        WinnerHolder.java             _WinnerStub.java
WinnerHelper.java  WinnerOperations.java  _WinnerImplBase.java
and the Directory : WinnerPackage  which contains :
Medals.java  MedalsHelper.java  MedalsHolder.java

The class Medals.java contains information about the mapping of the enum type :

package EnumModule.WinnerPackage;
 

/**
* EnumModule/WinnerPackage/Medals.java
* Generated by the IDL-to-Java compiler (portable), version "3.0"
* from example1.idl
* Montag, 21. Mai 2001 9.48 Uhr GMT-01:00
*/

public class Medals implements org.omg.CORBA.portable.IDLEntity
{
  private        int __value;
  private static int __size = 3;
  private static EnumModule.WinnerPackage.Medals[] __array = new EnumModule.WinnerPackage.Medals [__size];

  public static final int _gold = 0;
  public static final EnumModule.WinnerPackage.Medals gold = new EnumModule.WinnerPackage.Medals(_gold);
  public static final int _silver = 1;
  public static final EnumModule.WinnerPackage.Medals silver = new EnumModule.WinnerPackage.Medals(_silver);
  public static final int _bronze = 2;
  public static final EnumModule.WinnerPackage.Medals bronze = new EnumModule.WinnerPackage.Medals(_bronze);

  public int value ()
  {
    return __value;
  }

  public static EnumModule.WinnerPackage.Medals from_int (int value)
  {
    if (value >= 0 && value < __size)
      return __array[value];
    else
      throw new org.omg.CORBA.BAD_PARAM ();
  }

  protected Medals (int value)
  {
    __value = value;
    __array[__value] = this;
  }
} // class Medals

 
You can create a Medal by using its static method or with the constructor :
Medals medal = new Medals.from_int(2);
Medals medal = new Medales(_bronze);
Medals medal = new Medales(2);
This method performs range checking on values and throws an exception if the argument is outside the bounds of the enumeration. The next method returns the value passed to it, so you can use it to perform passive bounds checking. For example,
int x = medal.value(medal);
will assign medal to x only if medal is in the range of enumerated values for Medals; otherwise, it will throw an exception org.omg.CORBA.BAD_PARAM ().

Structures

An IDL struct is like a Java class without methods. In fact, JavaIDL converts an IDL struct into a Java class whose only methods is a null constructor and a constructor that takes all the structure's attributes.

Here is an example IDL struct definition:

module StructModule {
  interface DB {
    struct Person {
       string name;
       long age;
     };
    Person get(in short i);
  };
};
This definition would produce with idlj -fall StructModule.idl the following simple Java class Person.java declaration which is in the subdirectory DBPackage of StructModule :
package StructModule.DBPackage;


/**
* StructModule/DBPackage/Person.java
* Generated by the IDL-to-Java compiler (portable), version "3.0"
* from example2.idl
* Montag, 21. Mai 2001 10.08 Uhr GMT-01:00
*/

public final class Person implements org.omg.CORBA.portable.IDLEntity
{
  public String name = null;
  public int age = (int)0;

  public Person ()
  {
  } // ctor

  public Person (String _name, int _age)
  {
    name = _name;
    age = _age;
  } // ctor

} // class Person

    
How can we create a Person now, and can access to its elements ????

    

Unions

The union is another construct which does not survive the transition to Java. The IDL union actually works more like the variant record in Pascal, since it requires a "discriminator" value. An IDL union is essentially a group of attributes, only one of which can be active at a time. The discriminator indicates which attribute is in use at the current time. A short example should make this a little clearer. Here is an IDL union declaration:
module UnionModule {
     union MyUnion switch (char) {
          case 'a':      string aValue;
          case 'b':      long bValue;
          case 'c':      boolean cValue;
          default:       string defValue;
     };

};
Remark that we have no interface and no operations, thus only one directory UnionModule will be generated with the three files : MyUnion.java  MyUnionHelper.java  MyUnionHolder.java. Once again, for the mapping and the implementation of the remote methods (if there are any :-)), the file MyUnion.java is interesting.

The character value in the switch, known as the discriminator, indicates which of the three variables in the union is active. If the discriminator is ''a", the aValue variable is active. Since Java doesn't have unions, a union is turned into a class with accessor methods for the different variables and a variable for the discriminator. The class is fairly complex. Here is a subset of the definition for the MyUnion union:

package UnionModule;


/**
* UnionModule/MyUnion.java
* Generated by the IDL-to-Java compiler (portable), version "3.0"
* from example3.idl
* Montag, 21. Mai 2001 10.24 Uhr GMT-01:00
*/

public final class MyUnion implements org.omg.CORBA.portable.IDLEntity
{
  private String ___aValue;
  private int ___bValue;
  private boolean ___cValue;
  private String ___defValue;
  private char __discriminator;
  private boolean __uninitialized = true;

  public MyUnion ()
  {
  }

  public char discriminator ()
  {
    if (__uninitialized)
      throw new org.omg.CORBA.BAD_OPERATION ();
    return __discriminator;
  }

  public String aValue ()
  {
    if (__uninitialized)
      throw new org.omg.CORBA.BAD_OPERATION ();
    verifyaValue (__discriminator);
    return ___aValue;
  }

  public void aValue (String value)
  {
    __discriminator = 'a';
    ___aValue = value;
    __uninitialized = false;
  }

  private void verifyaValue (char discriminator)
  {
    if (discriminator != 'a')
      throw new org.omg.CORBA.BAD_OPERATION ();
  }
...
}
The holder structure should be a familiar theme to you by now. JavaIDL generates a holder structure for a union. The holder structure for MyUnion would be called MyUnionHolder and would contain a single instance variable called value.

How can we create a MyUnion now ?

 

Sequences and Arrays

IDL sequences and arrays both map very neatly to Java arrays. Sequences in IDL may be either unbounded (no maximum size) or bounded (a specific maximum size). IDL arrays are always of a fixed size. Since Java arrays have a fixed size but the size isn't known at compile-time, the JavaIDL system performs runtime checks on arrays to make sure they fit within the restrictions defined in the IDL module.

Here is a sample IDL definition containing an array, a bounded sequence, and an unbounded sequence:

module ArrayModule {
     struct SomeStructure {
          long longArray[15];
          sequence <boolean> unboundedBools;
          sequence <char, 15> boundedChars;
     };

};
The arrays would be defined in Java as
public int[] longArray;
public boolean[] unboundedBools;
public char[] boundedChars;

Exceptions

CORBA has the notion of exceptions. Unlike Java, however, exceptions are not just a type of object, they are separate entities. IDL exceptions cannot inherit from other exceptions. Other than that, they work like Java exceptions and may contain instance variables.

Here is an example IDL exception definition:

module ExceptionModule {
     exception YikesError {
          string info;
     };};
This definition would create the following Java file YikesError.java :

package ExceptionModule;
 

/**
* ExceptionModule/YikesError.java
* Generated by the IDL-to-Java compiler (portable), version "3.0"
* from example5.idl
* Montag, 21. Mai 2001 10.35 Uhr GMT-01:00
*/

public final class YikesError extends org.omg.CORBA.UserException implements ....
{
  public String info = null;

  public YikesError ()
  {
  } // ctor

  public YikesError (String _info)
  {
    info = _info;
  } // ctor

} // class YikesError

How can we throw an Exception YikesError now ? 

Example 

Writing the IDL
Let's take a look at a sample IDL schema. The following schema shows a  simple CORBA service, which allows clients to get and store the name associated with an email address. For the purposes of this example, we won't worry about modifying or deleting an existing user.

// Address book system module
module address_book_system
{
// Specify interface to our address book interface address_book
{
// Unknown user exception exception unknown_user {};

// User already exists exception exception user_exists {};

// Lookup name from email address string name_from_email(in string email)
raises (unknown_user);

// Lookup email from full name string email_from_name(in string name)
raises (unknown_user);

// Record a new name and email void record_user(in string name, in string email)
raises user_exists;
};
};


Writing the Servant Class
The servant is responsible for handling service requests. When we ran the idltojava tool, classes were created that handles most of the hard work for us. All we need to do is create an AddressBookServant class, that implements the _address_bookImplBase. It will contain the code for our three methods described in the IDL schema.

package address_book_system;
import address_book_system.address_bookPackage.*;

import java.util.Hashtable;
import java.util.Enumeration;
//
//
// AddressBookServant
//
// This servant class is responsible for implementing
// three methods
//
// * String name_from_email ( String email );
// * String email_from_name ( String name );
// * void record_user ( String name, String email );
//
//
class AddressBookServant extends _address_bookImplBase
{
private Hashtable name2email;

public AddressBookServant()
{
// Create a new hashtable to store name & email name2email = new Hashtable(); } // Get the name of this email user public String name_from_email ( String email )
throws unknown_user
{
if (name2email.contains(email))
{
// Begin search for that name for (Enumeration e = name2email.keys();
e.hasMoreElements();)
{
String name = (String) e.nextElement();
String e_mail = (String) name2email.get(name);

// Match on email ? if (email.compareTo(e_mail) == 0)
{
return name;
}
}
}

// User not found - throw unknown user exception throw new unknown_user();
}

// Get the email of this person public String email_from_name ( String name )
throws unknown_user
{
// If user exists if (name2email.containsKey(name))
{
// Return email address return (String) name2email.get(name);
}

// User doesn't exist throw new unknown_user();
}

// Add a new user to the system public void record_user ( String name, String email )
throws user_exists
{
// Is the user already listed if (name2email.containsKey( name ) ||
name2email.contains( email ) )
{
// If so, throw exception throw new user_exists();
}

// Add to our hash table name2email.put (name, email); } }

The servant class maintains a list of people's email addresses, and stores this information inside a Hashtable. When the CORBA server receives a request, it will call the name_from_email, email_from_name, and record_user methods of the servant to fulfill the request.

Outlook

One of the ongoing criticisms of CORBA 2.0 is the lack of support for passing objects by value. This has been addressed by adding support for passing objects by value (see the document Objects By Value, which can be found at http://www.omg.org). This has led to the addition of a new construct to the OMG IDL called the valuetype.

A valuetype supports both data members and operations, much the same as a Java class definition. When a valuetype is passed as an argument to a remote operation, it will be created as a copy in the receiving address space. The identity of the valuetype copy is separate from the original, so operations on one have no effect on the other.

Valuetype is not supported by Java IDL.


harald.kosch@itec.uni-klu.ac.at - Institute HomePage
Last updated 21/05/2003.