CORBA, Java IDL and RMI-IIOP

·  CORBA

Common Object Broker Architecture

Is a standard architecture for a distributed object system to

allows distributed objects to interoperate in a heterogeneous environment where

object can be implemented in different programming language and deployed on different platform.

Cross-language CORBA application

object client in Java object implementation in C++

stub in Java generated by skeleton in C++ generated by

compiling the CORBA object compiling the CORBA object

interface interface

ORB written in Java ORB written in C++

…… ….

·  A distributed object is defined using a interface file in IDL

CORBA Interface Definition Language (IDL) similar to Java and C++ syntax

Utilities to map CORBA IDL to other languages such as C, C++, Java, Ada, Python…

·  Inter-ORB Protocols

The Object Management Group defined protocols to allow ORBs to be interoperable.

A special case is the Internet Inter-ORB Protocol (IIOP) for TCP/IP transport layer.

Application Objects

------

Stub/Skeleton

------

ORB

------

IIOP

------

TCP/IP

------

……….

·  Java IDL

Adds CORBA capability to Java and provides interoperability with other CORBA objects implemented in various languages and platform. It includes:

CORBA Object Request Broker (ORB) (in Java)

IDL-to-Java compiler (to generate stubs and skeleton)

Naming and other services

·  Java IDL Tools

idlj IDL-to-Java compiler

orbd a server process for naming service

·  Java IDL Program Compilation and Run

% idlj -fall Hello.idl

(Will generate stub, skeleton/POA, help,

Utility files under the created HelloApp

subdirectory)

% javac HelloClient.java

HelloServer.java HelloApp/*.java

% orbd –ORBInitialPort 1080

-ORBInitialHost localhost&

% java HelloServer -ORBInitialPort 1080

-ORBInitialHost localhost&

% java HelloClient -ORBInitialPort 1080

-ORBInitialHost localhost

·  Java RMI-IIOP

RMI over IIOP, which allows a CORBA application to be written using the RMI syntax and semantics

It enables Java applications to transparently invoke operations on remote services using IIOP defined by Object Management Group

Allow interfaces to be defined in RMI.

IDL can be generated from RMI interfaces.

(for other languages)

–  Generate IIOP stubs/skeletons rmic –iiop …

–  Generate IDL from Java Interfaces (RMI) rmic –idl ...

Write Java RMI interfaces

Write Java implementation code

Use tools to generate IDL if desired (C++)

Write other language implementation

·  RMI-IIOP Programming (Java-Java)

//Publish the object reference in the Naming

//Service using JNDI

HelloImpl helloreference = new HelloImpl();

Context initialNamingContext = new

InitialContext(System.getProperties());

initialNamingContext.rebind(“Helloservice”,

helloreference);

//Client gets object reference from Naming Service //using JNDI

Context ic = new

InitialContext(System.getProperties());

Object objreference=ic.lookup(“HelloService”);

hi = (HelloInterface)

javax.rmi.PortableRemoteObject.narrow

(objreference, HelloInterface.class);

hi.sayHello( " MARS " );

·  RMI-IIOP Compile and Run

// generate skeletons and stubs

rmic –iiop HelloImpl

// Start the Naming Service

orbd –ORBInitialPort 1085&

// Server:

java –classpath .

-Djava.naming.factory.initial =

com.sun.jndi.cosnaming.CNCtxFactory

-Djava.naming.provider.url =

iiop://localhost:1085

HelloServer

// Client:

java –classpath .

–Djava.naming.factory.initial=

com.sun.jndi.cosnaming.CNCtxFactory

-Djava.naming.provider.url =

iiop://localhost:1085

HelloClient

·  RMI-IIOP Example: Code, Compile, Run

//HelloInterface.java

import java.rmi.Remote;

public interface HelloInterface extends java.rmi.Remote {

public void sayHello( String from ) throws java.rmi.RemoteException;

}

//HelloImpl.java

import javax.rmi.PortableRemoteObject;

public class HelloImpl extends PortableRemoteObject implements HelloInterface {

public HelloImpl() throws java.rmi.RemoteException {

super(); // invoke rmi linking and remote object initialization

}

public void sayHello( String from ) throws java.rmi.RemoteException {

System.out.println( "Hello from " + from + "!!" );

System.out.flush();

}

}

//HelloServer.java

import javax.naming.InitialContext;

import javax.naming.Context;

public class HelloServer {

public static void main(String[] args) {

try {

// Step 1: Instantiate the Hello servant

HelloImpl helloRef = new HelloImpl();

// Step 2: Publish the reference in the Naming Service

// using JNDI API

Context initialNamingContext = new InitialContext();

initialNamingContext.rebind("HelloService", helloRef);

System.out.println("Hello Server: Ready...");

} catch (Exception e) {

System.out.println("Trouble: " + e);

e.printStackTrace();

}

}

}

//HelloClient.java

import java.rmi.RemoteException;

import java.net.MalformedURLException;

import java.rmi.NotBoundException;

import javax.rmi.*;

import java.util.Vector;

import javax.naming.NamingException;

import javax.naming.InitialContext;

import javax.naming.Context;

public class HelloClient {

public static void main( String args[] ) {

Context ic;

Object objref;

HelloInterface hi;

try {

ic = new InitialContext();

// STEP 1: Get the Object reference from the Name Service

// using JNDI call.

objref = ic.lookup("HelloService");

System.out.println("Client: Obtained a ref. to Hello

server.");

// STEP 2: Narrow the object reference to the concrete

// type and

// invoke the method.

hi = (HelloInterface) PortableRemoteObject.narrow(

objref, HelloInterface.class);

hi.sayHello( " MARS " );

} catch( Exception e ) {

System.err.println( "Exception " + e + "Caught" );

e.printStackTrace( );

return;

}

}

}

Compile and Run

javac HelloImpl.java

rmic -iiop HelloImpl

javac HelloInterface.java HelloServer.java HelloClient.java

orbd -ORBInitialPort 1050 -ORBInitialHost localhost&

runserver

runclient

runserver:

java -classpath . -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:1050 HelloServer &

runclient:

java -classpath . -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:1050 HelloClient