Using Dynamic Invocation Interface (DII)

In our previous examples, for method invocation, client makes use of stub, helper classes, etc., which are generated from the given IDL interface and idij compiler. What happens if the IDL interface is not available? Note that a reference to the remote object may be obtained by querying the naming service. However, how do you invoke a method using this reference if interfaces to the object is not known at compile time? Fortunately, CORBA specification defines an API, called Dynamic Invocation Interface (DII) for this purpose.

To make use of DII, the following steps are required:

  • Obtain an IOR to the object
  • Create a Request object from this IOR passing method name, parameters and result
  • Invoke the request
  • Retrieve the result (if any)

The primary classes to work with DII are as follows:

Request

This class is the heart of DII, which allows creation and invocation of requests dynamically. A Request object basically contains method name (a String) together with list of arguments (a NVList object) and an optional result from the method (a NamedVaiue object).

Any

An Any object may contain the Java equivalent of any IDL type and is used to specify an argument.

NamedVaiue

It, as its name implies, is an Any value tied with a name and is used to specify a named argument.

NVList

A list of NamedVaiue arguments to be passed to a remote method request.

We have already seen how to get an IOR using different approaches. We assume that the following statement gets an IOR of our remote calculator object:

org.omg.CORBA.Object ref = …

Once we get this reference (IOR), we can create and fire a method request to the object by building a NVList object (holding parameter list), a NamedValue object (to hold the result) and then putting all of these items to a Request object.

We first create and populate two Any objects for two integer arguments for our add() method:

Any argl = orb.create_any(), arg2 = orb.create_any();

int x =2, y = 3;

arg1.insert_long(x);

arg2.insert_long(y);

We then create a NVList to hold all arguments:

NVList argList = orb.create_list(2);

This list is populated by two named Anys as follows:

argList.add_value(”a”, argl, org.omg.CORBA.ARG_IN.value);

argList.add_value(”b”, arg2, org.omg.CORBA.ARG_IN.value);

The add_value() method essentially creates a new NamedValue object initialized with the given name, value, and flag, and adds it to the end of this NVList object. So, argList now contains NamedValue objects for two arguments. We also create a NamedValue object to hold the return value:

Any ret = orb.create_any();

ret.type(orb.get_primitive_tc(TCKind.tk_long));

NamedValue resultVal = orb.create_named_value(”result”, ret, org.omg.CORBA.ARG_OUT.value);

We then create a Request object using _create_request() method of org.omg.CORBA.Object class: Request req = ref._create_request(null, “add”, argList, resultVal);

The request is fired using invoke() method: req.invoke();

Finally the result is extracted as follows:

int result = req.return_value().extract_long();

The entire piece of code (calculatorClientDii.java) is given below:

//CalculatorClientDII.java import org.omg.CosNaming.*;

import org.omg.CosNaming.NamingContextPackage.*;

import org.omg.CORBA.*;

public class CalculatorClientDII {

public static void main(String args[]) {

try{

ORB orb = ORB.init(args, null);

org.omg.CORBA.Object objRef = orb.resolve_initial_references(”NameService”);

NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

org.omg.CORBA.Object ref = ncRef.resolve_str(”Calculator”);

Any arg1 = orb.create_any(), arg2 = orb.create_any();

int x =2, y = 3;

arg1.insert_long(x);

arg2.insert_long(y);

NVList argList = orb.create_list(2);

argList.add_value(”a”, arg1, org.omg.CORBA.ARG_IN.value);

argList.add_value(”b”, arg2, org.omg.CORBA.ARG_IN.value);

Any ret = orb.create_any();

ret.type(orb.get_primitive_tc(TCKind.tk_long));

NamedValue resultVal = orb.create_named_value(”result”, ret, org.omg.CORBA.ARG_OUT.value);

Request req = ref._create_request(null, “add”, argList, resultVal); req.invoke();

int result = req.return_value().extract_long();

System.out.println(”Sent :” + x + ” and ” + y);

System.out.println(”Received : ” + result);

} catch (Exception e) {e.printStackTrace();}

}

}

http://docstore.mik.ua/oreUy/java-ent/jenut/ch04_05.htm

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *