<

# Sample Code for Example.idl

The JavaTM 2 SDK, Standard Edition, v1.3 includes a new version of the IDL-to-Java compiler, `idlj`. This compiler supports new CORBA-standard features required for RMI-IIOP. This document shows the code generated by the IDL-to-Java compiler in accordance with the CORBA 2.3 specification.

The followng file, `Example.idl`, is written in the OMG Interface Definition Language. OMG IDL is a purely declarative language designed for specifying programming-language-independent operational interfaces for distributed applications. OMG specifies a mapping from IDL to several different programming languages, including C, C++, Smalltalk, COBOL, Ada, and Java. When mapped, each statement in OMG IDL is translated to a corresponding statement in the programming language of choice.

Example.idl

       module Example {
         interface Counter {
           boolean increment (in long arg);
         };
       };

## Generated Files

The `idlj` compiler uses the IDL-to-Java mapping to convert IDL interface definitions to corresponding Java interfaces, classes, and methods, which you can then use to implement your client and server code. The following files are generated when `Example.idl` is compiled with the IDL-to-Java compiler, using the following command:

       idlj -fall Example.idl

### Counter.java, the signature interface

The signature interface file, `Counter.java` extends `org.omg.portable.IDLEntity`, `org.omg.CORBA.Object`, and the operations interface, `CounterOperations`. The signature interface is used as the signature type in method declarations when interfaces of the specified type are used in other interfaces. From the client's point of view, an object reference for a CORBA `Counter` object implements this interface.

Note: The Stub implements the `Counter` interface, where it generates code for each method to marshall the arguments, invoke the method, and then unmarshall the arguments.

Example/Counter.java

       package Example;

       /**
       * Example/Counter.java
      * Generated by the IDL-to-Java compiler (portable), version "3.0"
       * from Example.idl
       * Monday, November 6, 2000 6:01:53 PM PST
       */

       public interface Counter extends CounterOperations,
         org.omg.CORBA.Object,
           org.omg.CORBA.portable.IDLEntity
       {
       } // interface Counter

### CounterOperations.java, the operations interface

The Java operations interface, `CounterOperations.java`, is used in the server-side mapping and as a mechanism for providing optimized calls for co-located clients and server. The server developer provides implementation for the methods indicated by the operations interface.

Example/CounterOperations.java

       package Example;

       /**
       * Example/CounterOperations.java
       * Generated by the IDL-to-Java compiler (portable), version "3.0"
       * from Example.idl
       * Thursday, November 9, 2000 7:04:12 PM PST
       */

       public interface CounterOperations
       {
         boolean increment (int arg);
       } // interface CounterOperations

### CounterHelper.java, the Helper class

The Java class `CounterHelper` defines auxiliary methods, notably `narrow()`, which is the CORBA counterpart of Java casting.

Example/CounterHelper.java

       package Example;

       /**
       * Example/CounterHelper.java
       * Generated by the IDL-to-Java compiler (portable), version "3.0"
       * from Example.idl
       * Thursday, November 9, 2000 7:04:12 PM PST
       */

       abstract public class CounterHelper
       {
         private static String  _id = "IDL:Example/Counter:1.0";

         public static void insert (org.omg.CORBA.Any a,
           Example.Counter that)
         {
           org.omg.CORBA.portable.OutputStream out =
             a.create_output_stream ();
           a.type (type ());
           write (out, that);
           a.read_value (out.create_input_stream (), type ());
         }

         public static Example.Counter extract (org.omg.CORBA.Any a)
         {
           return read (a.create_input_stream ());
         }

         private static org.omg.CORBA.TypeCode __typeCode = null;
         synchronized public static org.omg.CORBA.TypeCode type ()
         {
           if (__typeCode == null)
           {
             __typeCode = org.omg.CORBA.ORB.init
                ().create_interface_tc (Example.CounterHelper.id (),
                "Counter");
           }
           return __typeCode;
         }

         public static String id ()
         {
           return _id;
         }

         public static Example.Counter read
           (org.omg.CORBA.portable.InputStream istream)
         {
           return narrow (istream.read_Object (_CounterStub.class));
         }

         public static void write (org.omg.CORBA.portable.OutputStream
           ostream, Example.Counter value)
         {
           ostream.write_Object ((org.omg.CORBA.Object) value);
         }

         public static Example.Counter narrow (org.omg.CORBA.Object
           obj)
         {
           if (obj == null)
             return null;
           else if (obj instanceof Example.Counter)
             return (Example.Counter)obj;
           else if (!obj._is_a (id ()))
             throw new org.omg.CORBA.BAD_PARAM ();
           else
           {
             org.omg.CORBA.portable.Delegate delegate =
               ((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate
                 ();
             return new Example._CounterStub (delegate);
           }
         }

       }

### CounterHolder.java, the Holder class

The Java class called `CounterHolder` holds a public `Counter` member. Holders are required for CORBA operations that take `out` or `inout` arguments, which, unlike CORBA `in` arguments, don't map directly to Java's pass-by-value semantics.

Example/CounterHolder.java

       package Example;

       /**
       * Example/CounterHolder.java
       * Generated by the IDL-to-Java compiler (portable), version "3.0"
       * from Example.idl
       * Thursday, November 9, 2000 7:04:11 PM PST
       */

       public final class CounterHolder implements
         org.omg.CORBA.portable.Streamable
       {
         public Example.Counter value = null;

         public CounterHolder ()
         {
         }

         public CounterHolder (Example.Counter initialValue)
         {
           value = initialValue;
         }

         public void _read (org.omg.CORBA.portable.InputStream i)
         {
           value = Example.CounterHelper.read (i);
         }

         public void _write (org.omg.CORBA.portable.OutputStream o)
         {
           Example.CounterHelper.write (o, value);
         }

         public org.omg.CORBA.TypeCode _type ()
         {
           return Example.CounterHelper.type ();
         }

       }

### _CounterStub.java, the client stub

The Java class `_CounterStub` is the stub file for the client-side mapping.

Example/_CounterStub.java

       package Example;

       /**
       * Example/_CounterStub.java
       * Generated by the IDL-to-Java compiler (portable), version "3.0"
       * from Example.idl
       * Thursday, November 9, 2000 7:04:12 PM PST
       */

       public class _CounterStub extends
         org.omg.CORBA.portable.ObjectImpl implements Example.Counter
       {
         // Constructors
         // NOTE:  If the default constructor is used, the
         //        object is useless until _set_delegate (...)
         //        is called.
         public _CounterStub ()
         {
           super ();
         }

         public _CounterStub (org.omg.CORBA.portable.Delegate
           delegate)
         {
           super ();
           _set_delegate (delegate);
         }

         public boolean increment (int arg)
         {
           org.omg.CORBA.portable.InputStream _in = null;
           try {
              org.omg.CORBA.portable.OutputStream _out = _request
                ("increment", true);
              _out.write_long (arg);
              _in = _invoke (_out);
              boolean __result = _in.read_boolean ();
              return __result;
           } catch (org.omg.CORBA.portable.ApplicationException _ex)
              {
              _in = _ex.getInputStream ();
              String _id = _ex.getId ();
              throw new org.omg.CORBA.MARSHAL (_id);
              } catch (org.omg.CORBA.portable.RemarshalException _rm)
              {
              return increment (arg);
           } finally {
               _releaseReply (_in);
           }
         } // increment

         // Type-specific CORBA::Object operations
         private static String[] __ids = {
           "IDL:Example/Counter:1.0"};

         public String[] _ids ()
         {
           return (String[])__ids.clone ();
         }

         private void readObject (java.io.ObjectInputStream s)
         {
            try
            {
              String str = s.readUTF ();
              org.omg.CORBA.Object obj = org.omg.CORBA.ORB.init
                ().string_to_object (str);
              org.omg.CORBA.portable.Delegate delegate =
                ((org.omg.CORBA.portable.ObjectImpl)
                  obj)._get_delegate ();
              _set_delegate (delegate);
            } catch (java.io.IOException e) {}
         }

         private void writeObject (java.io.ObjectOutputStream s)
         {
            try
            {
              String str = org.omg.CORBA.ORB.init ().object_to_string
                (this);
              s.writeUTF (str);
            } catch (java.io.IOException e) {}
         }
       } // class _CounterStub

### _CounterImplBase.java, the server skeleton

The Java class `_CounterImplBase` is the skeleton file for the server-side mapping.

Example/_CounterImplBase.java

       package Example;

       /**
       * Example/_CounterImplBase.java
       * Generated by the IDL-to-Java compiler (portable), version "3.0"
       * from Example.idl
       * Thursday, November 9, 2000 7:04:11 PM PST
       */

       public abstract class _CounterImplBase extends
         org.omg.CORBA.portable.ObjectImpl
           implements Example.Counter,
             org.omg.CORBA.portable.InvokeHandler
       {

         // Constructors
         public _CounterImplBase ()
         {
         }

         private static java.util.Hashtable _methods = new
           java.util.Hashtable ();
         static
         {
           _methods.put ("increment", new java.lang.Integer (0));
         }

         public org.omg.CORBA.portable.OutputStream _invoke (String
           method, org.omg.CORBA.portable.InputStream in,
             org.omg.CORBA.portable.ResponseHandler rh)
         {
           org.omg.CORBA.portable.OutputStream out = null;
           java.lang.Integer __method =
             (java.lang.Integer)_methods.get (method);
           if (__method == null)
             throw new org.omg.CORBA.BAD_OPERATION (0,
               org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);

           switch (__method.intValue ())
           {
              case 0:  // Example/Counter/increment
              {
                int arg = in.read_long ();
                boolean __result = false;
                __result = this.increment (arg);
                out = rh.createReply();
                out.write_boolean (__result);
                break;
              }

           default:
                throw new org.omg.CORBA.BAD_OPERATION (0,
                  org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
           }

           return out;
         } // _invoke

         // Type-specific CORBA::Object operations
         private static String[] __ids = {
           "IDL:Example/Counter:1.0"};

         public String[] _ids ()
         {
           return __ids;
         }

       } // class _CounterImplBase

## Completing the application

To complete the application, the developer must write the client and server code.

Copyright © 2000 Sun Microsystems, Inc. All Rights Reserved. Send questions or comments to: JavaIDL@eng.sun.com. | Java Software