Interaction with the API

The TwineBall adapter makes all its calls to the application server through the TwineBall interface. In Java RMI, calls to a remote interface are automatically marshaled by the JVM. Figure 2 shows the original interaction over Java RMI.



Figure 2. Interaction over RMI

To demonstrate JNI, the server side of the picture has been replaced with a C++ shared library. Now the picture looks like Figure 3.



Figure 3. Modified TwineBall architecture

Because a well-defined Java interface was already calling application, very few code changes were required on the adapter side to change the implementation of that interface. This allows us to demonstrate an adapter using JNI and an adapter using Java RMI with the same adapter code. Only the implementation behind the interface changes.

The TwineBall interface makes heavy use of Java vectors. Rather than try to bring vectors into the C++ world, we decided to convert the vectors into arrays of strings. This is what the VectorToArrayBridge does. If it is more convenient for the adapter to use Java utility classes, you may wish to use a similar approach.

The JNI Stub Library declares the native methods. This is what connects the methods implemented in C++ with the Java world.

The C++ shared library in this case is implemented with a single C++ module. However, this shared library includes the JNI.H header file, and is therefore aware of Java types. Conversion between basic Java types and C++ types still largely occurs in the C++ space. Interfacing with a pre-existing C++ application/API will require new C++ code to be written as well as Java.

Please note that a database is no longer involved. In this example, the interface is implemented with pre-determined return values.

In the sample JNI source code, you will find the following pieces.


Table 3. Components of JNI source code

Filename Function
Makefile.PLATFORM Builds the C++ shared library on the given platform.
com_ibm_wbia_TwineBall_jnibridge_JNIStubLibrary.cc Source code for C++ shared library. Contains the
C++ "glue code".
com_ibm_wbia_TwineBall_jnibridge_JNIStubLibrary.h Header file for C++ shared library.
com.ibm.wbia.TwineBall.jnibridge.JNIStubLibrary The JNI Stub Library
com.ibm.wbia.TwineBall.jnibridge.VectorToArrayBridge The Vector to Array Bridge
com.ibm.wbia.TwineBall.jnibridge.test.TestDriver Test driver for JNIStubLibrary

Copyright IBM Corp. 1997, 2004