IDL Constructed Types   «Prev  Next»
Lesson 5 Java code: Constructed types
Objective Write Java code based on IDL using the constructed types covered in this module.

Java Code and Constructed Types

In this module so far, you have learned about the IDL-to-Java mappings for structs, sequences, and enums. As a CORBA developer, you do not actually apply these mappings yourself, of course--they are affected by the IDL compiler that produces the corresponding Java code. Nonetheless, it is important to remember that understanding these mappings makes it far easier to use the resulting Java code in your own CORBA development. We will now focus on a more extended example demonstrating how to write such Java code.

Example IDL

Once again, we will look at the IDL for a Weather Service. This IDL includes an enum, a struct, and a sequence. Based on this IDL, we will write some Java code, implicitly making use of the mappings we have learned thus far. The IDL for our example is:

// recap.idl
// Recap example - Weather Service
module Module4
{
 enum ForecastType {sunny, cloudy, rainy, snowy, 
  stayhome};
 struct WeatherReport
 {
  short high;
  short low;
  ForecastType forecast;
 };
 
 const short DaysInAWeek = 7;
 typedef sequence<WeatherReport, daysInAWeek>
  ReportsForWeek;
 interface WeatherService
     {
  WeatherReport getReport(in string city);
  ReportsForWeek getReportsForWeek(in string city);
     };
};

Note that we use an enum, a struct, and a sequence. We also define a const to be used for the bound on our bounded sequence. It is good practice to define consts for bounded sequences, rather than simply hard-coding the bound. It is easier to keep track of and change the constants than it is to keep track of and change all the places in your code where the bound is used.

Example Java implementation

We will implement a simple sample server-side object for this IDL. We can use the delegation-based approach and implement the WeatherServiceOperations interface.
View Code the code below.
package Module4Java;
import Module4.*;
//Implementation to be used with TIE class
public class WeatherServiceOperationsImpl implements WeatherServiceOperations{
 public Module4.WeatherReport getReport(java.lang.String city){
  WeatherReport report = null;
  //Would really do some lookup and meteorological calculations but ...
  if(city.equals("New York")){
   report = new WeatherReport((short)90,(short)78,ForecastType.sunny);
  }
  else{
   report = new WeatherReport((short)97, (short)82, ForecastType.stayhome);
  }
  return report;
 }

 
 public Module4.WeatherReport[] getReportsForWeek(java.lang.String city) {
  WeatherReport[] reports = new WeatherReport[DaysInAWeek.value];
  //We'll simply return a week's worth of the same report
  for(int i=0; i<reports.length;i++) {
   reports[i]  = getReport(city);
  }
  return reports;
 }
}

You can see from this code how straightforward the actual use of struct, sequence, and enum-based Java code is. The sequence is simply handled as an array of objects of the struct type. The struct has become a simple Java class, which can be instantiated with all values fully loaded. Referencing the enum types can be done just by accessing the static object members of the enum class.
In the next two lessons, you will learn how IDL exceptions are mapped to and used in Java.

Using Structs Sequences Enums - Exercise

Click the Exercise link below to write your own Java code by working on the course project.
Using Structs Sequences Enums - Exercise