Using Out and Inout Parameter

So far we have used in CORBA parameters, which is passed into the operation from the caller. A in CORBA parameter is translated into normal Java parameter. However, a true local copy of the actual argument passed by the caller is created at the receiver’s end. So, any modification on the local copy has no effect on the actual argument.

CORBA supports two more types of parameter passing out and inout. An out parameter is one which is passed out of the method to the caller, but no value is passed in. An inout parameter, as its name implies, is a combination of in and out, where the parameter is passed into the operation and returned to the caller.

However, there is no standard mechanism available in Java for these two parameters. So, these concepts are simulated with the help of some classes called holder classes. These classes are available for all of the basic IDL datatypes in the org.omg.coRBA package and are generated for all named user-defined types, except those defined by typedefs. The holder classes provide a level of indirection and are passed instead of the actual type.

A client creates an instance of the respective holder class for each out and inout parameter. This instance holds some content in its public member value and is passed to the method. The server may then set or modify the content of the holder object. The client gets the modified content and processes further. Since, the encapsulated actual object is modified without affecting the holder object itself, the semantics of out and inout parameters are supported.

Holder classes for basic IDL types are available in the package org.omg.coRBA. The holder class name is of the form [type]Hoider, where [type] is the name of the mapped Java type, with the first letter capitalized. For example, for IDL type long (corresponding Java type is int), the name of the Java holder class is IntHolder.

Each holder class has a default constructor, a one argument constructor and has a public instance member value, which holds the content. The default constructor sets the value field to the default value for the type as defined by the Java language: false for boolean, 0 for numeric and char types, null for strings, null for object references. The source code of IntHolder looks like this:

package org.omg.CORBA;

/*..*/

public final class IntHolder implements Streamable {

public int value;

public IntHolder() {}

public IntHolder(int initial) { value = initial; }

/*…*/

}

1. Invoking Operation Using Holder Classes

The following are the basic steps to follow to pass inout and out parameters using holder classes:

  • For each IDL out or inout parameter, the client creates an instance of the appropriate holder class
  • The contents of the holder instance are set or modified by the receiver, and the client then uses the modified contents after the call returns.
  • For the inout parameter, the client must provide a valid content. The method typically uses this content and changes the value if it wishes. The final content is returned to the client at the end of the operation.
  • For the out parameter, the client does not initialize the holder with a content. The operation should not use the initial value in the holder and must supply a valid value to be returned to the client.

2. An Example

Consider the following IDL definition:

interface Service {

void replace(inout string s, in char c, out long times);

};

The replace() operation replaces all space characters in the string s by the specified character c and returns the number of times the replacement occurred. The first parameter comes in from the caller and the modified string goes out to the caller. Hence it is declared as inout. The second one comes from the caller but is not returned back. Accordingly, it is declared as in. The last one is only used to send a value back to the caller, hence is declared as out. The idlj compiler maps this operation to a Java method as follows:

void replace (org.omg.CORBA.StringHolder s, char c, org.omg.CORBA.IntHolder times);

A client application can be coded as follows:

//Get a reference to the remote CORBA object Service s = …

StringHolder str = new StringHolder(”CORBA parameter passing example”);

char c = ‘_’;

IntHolder times = new IntHolder();

System.out.println(”Before call: ”+str.value+” ”+c+” ”+times.value);

s.replace(str, c, times);

System.out.println(”After call: ”+str.value+” ”+c+” ”+times.value);

The implementation of replace() method looks like this:

public void replace (org.omg.CORBA.StringHolder s, char c, org.omg.CORBA.IntHolder times)

{

String temp = s.value;

times.value = temp.length() – temp.replace(” ”, ””).length();

s.value = s.value.replace(‘ ‘, c);

}

A sample output of the client program is shown below:

Before call: CORBA parameter passing example _ 0

After call: CORBA_parameter_passing_example _ 3

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 *