# Howto: Interoperability between JOnAS and CORBA

This guide describes the basic interoperability between JOnAS and CORBA using RMI-IIOP (the examples in this document assume that the Sun rmi/iiop of the JDK 1.4 is used on the remote side, JOnAS using JacORB).

The content of this guide is the following:

1. Accessing an EJB deployed on JOnAS server by a CORBA client
2. Accessing a CORBA service by an EJB deployed on JOnAS server

## Accessing an EJB deployed a on JOnAS server by a CORBA client

### JOnAS Configuration

No modification to the EJB code is necessary. However, the EJB should be deployed for the iiop protocol (e.g. when the build.xml is created, add the tag "protocols" and specify "iiop").
For example:

       <jonas destdir="${dist.ejbjars.dir}" classpath="${classpath}" jonasroot="${jonas.root}"
                                            protocols="iiop"/>

If GenIC is used for deployment, the `-protocols` option can be used. Note also that an EJB can be deployed for several protocols. Refer to the JOnAS Configuration Guide for more details about configuring the communication protocol.

The JOnAS configuration must be modified for the JOnAS server to use RMI-IIOP.
Choose the iiop protocol in the file carol.properties. Refer also to the JOnAS Configuration Guide for details about configuring the communication protocol.
These modifications will make it possible to create an EJB using the RMI-IIOP protocol.

### RMIC to create IDL files used by the Corba Client

To call an EJB deployed on JOnAS that is accessible through RMI-IIOP, use the rmic tool on the EJB Remote interface and EJB Home interface to create the idl files. Example: `rmic -classpath $JONAS_ROOT/lib/common/j2ee/ejb.jar -idl package1.Hello`

This action generates several idl files:

    package1/Hello.idl
    package1/HelloHome.idl

    java/io/FilterOutputStream.idl
    java/io/IOException.idl
    java/io/IOEx.idl
    java/io/OutputStream.idl
    java/io/PrintStream.idl
    java/io/Writer.idl
    java/io/PrintWriter.idl

    java/lang/Exception.idl
    java/lang/Ex.idl
    java/lang/Object.idl
    java/lang/StackTraceElement.idl
    java/lang/ThrowableEx.idl
    java/lang/Throwable.idl

    javax/ejb/EJBHome.idl
    javax/ejb/EJBMetaData.idl
    javax/ejb/EJBObject.idl
    javax/ejb/Handle.idl
    javax/ejb/HomeHandle.idl
    javax/ejb/RemoveException.idl
    javax/ejb/RemoveEx.idl

    org/omg/boxedRMI/seq1_octet.idl
    org/omg/boxedRMI/seq1_wchar.idl

    org/javax/rmi/CORBA/ClassDesc.idl
    org/omg/boxedRMI/java/lang/seq1_StackTraceElement.idl

Copy these files to the directory in which CORBA client development is being done.

### CORBA Client Development

1. idlj

Once idl files are generated, apply the idlj tool to build java files corresponding to the idl files (idlj = idl to java). To do this, apply the idlj tool to the Remote interface idl file and the Home interface idl file. Example: `idlj -fclient -emitAll package1/Hello.idl`

The idlj tool also generates bugged classes. Be sure to put the _read and _write method in comment in the class _Exception.java, CreateException.java, RemoveException.java.

Additionally, the class OutputStream.java, PrintStream.java, PrintWriter.java, Writer.java, FilterOuputStream.java must extend Serializable and then replace

     ((org.omg.CORBA_2_3.portable.OutputStream) ostream).write_value(value,id());

with

     ((org.omg.CORBA_2_3.portable.OutputStream) ostream).write_value((Serializable) value,id());

in the write method.

2. Client

Create the Corba client.

    import org.omg.CosNaming.*;
    import org.omg.CORBA.*;

    public class Client {
        public static void main(String args[]) {
            try {
                //Create and initialize the ORB
                ORB orb=ORB.init(args,null);
                //Get the root naming context
                org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService");
                NamingContext ncRef= NamingContextHelper.narrow(objRef);

                //Resolve the object reference in naming
                //make sure there are no spaces between ""
                NameComponent nc= new NameComponent("HelloHome","");
                NameComponent path[] = {nc};
                HelloHome tradeRef=HelloHomeHelper.narrow(ncRef.resolve(path));

                //Call the Trader EJB and print results
                Hello hello=tradeRef.create();
                String tr=hello.say();
                System.out.println("Result = "+tr);
            }
            catch (Exception e) {
                System.out.println("ERROR / "+e);
                e.printStackTrace(System.out);
            }
        }
    }

3. Compilation

Compile the generated files.

WARNING: Compile the file corresponding to the client parts, the files Hello.java, HelloHome.java, _Exception.java, ..., and _*Stub.java, *Helper.java, *ValueFactory.java, *Operation.java (* represents the name of the interface).

## Accessing a CORBA service by an EJB deployed on JOnAS server

### CORBA Service

Create the CORBA service.

Create the idl file corresponding to this service (e.g. the interface name, which is "Hello").

Generate the java file corresponding to the idl service with the `idlj -fall Hello.idl` tool.

Files used on this example can be found on the Sun tutorial web site available at http://java.sun.com/j2se/1.4.2/docs/guide/idl/tutorial/GSIDL.html.

Implement the java interface (in this example, the service will be bound with the name "Hello" in the server implementation).

Start the orb.

Start the CORBA service.

### EJB on JOnAS

To call the CORBA service, generate the java file corresponding to the idl file.

For this, apply the idlj tool on the idl file corresponding to the CORBA service description:.

    idlj -fclient Hello.idl

Then, create an EJB.

For calling the CORBA service, use the InitialContext object (by running JOnAS in iiop mode).

Get the java object corresponding to the CORBA service with the environment.

Call the method on this object.

Example code:

    try {

              // Get the reference on the object
              // Here the remote ORBd is running on localhost with the port 1050
              // and the remote object is bound with the name "Hello".
              org.omg.CORBA.Object o = (org.omg.CORBA.Object) initialContext.lookup("corbaname:iiop:1.2@localhost:1050#Hello");

              // Do the narrow
              Hello hello = (Hello) HelloHelper.narrow(o);

              // Call the method
              System.out.println(hello.sayHello());
              return hello.sayHello();
              }
              catch (Exception e) {
                  ...
              }