What is the VBJ 3.x equivalent of the VBC++ VISUtil::to ior() method?

From Support
Jump to: navigation, search

Question:

In VBC++ there are VISUtil class methods  to_ior() and to_string()  to convert between stringified IORs and IOR structures.  These are defined in the vutil.h header file.

Are there any equivalent methods in VBJ 3.x?

Answer:

The methods needed to convert between java.lang.String
and com.visigenic.vbroker.IOP.IOR are available in VisiBroker
ORB classes and are shown below.

These methods are not formally exposed as an ORB API, so
they should be used with the caution that it may be changed
without notice and are not guaranteed to exist in future releases
of the product.

// Class: com.visigenic.vbroker.orb.ORB
//
// Method: public IOR string_to_ior(String iorString)
//
// If using this method, the application should be prepared to catch
// org.omg.CORBA.INV_OBJREF, and org.omg.CORBA.MARSHAL exceptions.
//
// Example:

java.lang.String iorString =  orb.object_to_string(manager);
com.visigenic.vbroker.IOP.IOR ior =
        ((com.visigenic.vbroker.orb.ORB)orb).string_to_ior(iorString);
 

//
// Class: com.visigenic.vbroker.orb.ORB
//
// Method: public org.omg.CORBA.Object iorToObject(IOR ior)
//
// Example:

org.omg.CORBA.Object obj =
        ((com.visigenic.vbroker.orb.ORB)orb).iorToObject(ior);
java.lang.String refString = orb.object_to_string(obj);

How does the application  get to the profile body
structure?  One approach is to build an input stream on the
profile data (octet form) and then use a ProfileBodyHelper to read the
stream and create the ProfileBody structure.  Also, since java doesn't
really have unsigned integers there is a conversion on the port to make
it readable.  Again, this is using several undocumented methods (such as
newGiopInputStream), but these methods will probably be stable for the
duration of VBJ 3.x releases.  Just be aware that this kind of code may
be impacted if Inprise does need to make a change in these classes.

This particular example builds the IOR structure directly from a
stringified IOR read from a file.

import com.visigenic.vbroker.IOP.*;
import com.visigenic.vbroker.GIOP.*;
import com.visigenic.vbroker.IIOP_1_1.*;

...

    org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

    java.lang.String iorString = null;

    try {
       DataInputStream input =
          new DataInputStream(new FileInputStream("test.ior"));
       iorString = input.readLine();
    } catch (java.io.IOException e) {
       e.printStackTrace();
    }

    com.visigenic.vbroker.orb.GiopInputStream input;
    com.visigenic.vbroker.orb.ORB _orb =
       (com.visigenic.vbroker.orb.ORB)orb;

    com.visigenic.vbroker.IOP.IOR ior =
        _orb.string_to_ior(iorString);

    System.out.println("IOR has " + ior.profiles.length + " profiles");

    for (int k = 0; k < ior.profiles.length; k++) {

        if ( ior.profiles[k].tag == TAG_INTERNET_IOP.value) {
 
          input = _orb.newGiopInputStream(ior.profiles[k].profile_data);
          input.byteOrder(input.read_boolean());
          ProfileBody profileBody = ProfileBodyHelper.read(input);
          int port = (int) profileBody.port;
          if (profileBody.port < 0) port += 65536;

          System.out.println("Profile # " + k
                + " for Host: " + profileBody.host
                + " and Port: " + port);
        }
        else {
          System.out.println("Profile # " + k + ": Unknown");
        }

    }
 

Note: The conversion for the port number ( if (profileBody.port < 0) port += 65536) is to allow for large port numbers assigned by some hosts (such as Solaris). The OMG IDL definition for the ProfileBody defines the port as an unsigned short. Because there is no support in the Java language for unsigned types, theapplication is responsible for ensuring that large unsigned IDL type values are handled correctly as negative integers in Java. The port number is large enough that java thinks it is a negative short. The conversion changes the negative short into an equivalent positive integer using integer arithmetic.

[This information was verified against VBJ 3.3 and VBJ 3.4]



Article originally contributed by