HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Interfacing with Legacy Libraries
 Using Remote Method Invocation"
Volume: 4 Issue: 7, p. 54

	

Listing 1: LEGACY.h 

/*********************************************** 
 * LEGACY.h defines the legacy structures and the 
 * associated function prototypes 
 */ 
typedef enum { 
   LEGACY_P__A, 
   LEGACY_P__B, 
   LEGACY_P__C, 
   LEGACY_P__D 
} Legacy_P_Type 
  

typedef enum { 
   LEGACY_M__A, 
   LEGACY_M__B, 
   LEGACY_M__C, 
   LEGACY_M__D, 
   LEGACY_M__E, 
   LEGACY_M__F, 
   LEGACY_M__G, 
   LEGACY_M__H 
} Legacy_M_Type; 
  

typedef struct { 
   time_t                   Timestamp; 
   Legacy_P_Type    P_Type; 
   unsigned char       Id; 
   Legacy_M_Type   M_Type; 
   char                      String_A[5]; 
   char                      String_B[5]; 
   char                      String_C[5]; 
   char                      String_D[5]; 
   char                      String_E[5]; 
   char                      String_F[5]; 
   char                      String_G[5]; 
   char                      String_H[5]; 
} Legacy_Type; 
  

int Get_Legacy_Data ( Legacy_Type *legacy ); 
  

Listing 2: JLegacy.java 

/*********************************************** 
 * JLegacy.java provides a class for the legacy data. A 
 * populated instance of a JLegacy object is returned by 
 * JLegacyC.get(). 
 */ 
package my.jlegacy.classes; 
  

public class JLegacy implements java.io.Serializable 
{ 
   public long     timestamp; 
   public int        pType; 
   public byte      id; 
   public int        mType; 
   public String   stringA; 
   public String   stringB; 
   public String   stringC; 
   public String   stringD; 
   public String   stringE; 
   public String   stringF; 
   public String   stringG; 
   public String   stringH; 
  

    public JLegacy() {} 
} 
  

Listing 3: JLegacyIF.java 

/*********************************************** 
 * JLegacyIF.java defines the method used to return a 
 * populated instance of a JLegacy object from a remote 
 * object. 
 */ 
package my.jlegacy.classes; 
  

public interface JLegacyIF extends java.rmi.Remote 
{ 
   public JLegacy getJLegacy() 
             throws java.rmi.RemoteException; 
} 
  

Listing 4: JLegacyRO.java 

/*********************************************** 
 * JLegacyRO.java provides a remote object which returns a 
 * populated instance of a JLegacy object. 
 */ 
package my.jlegacy.classes; 
  

import java.rmi.*; 
import java.rmi.registry.*; 
import java.rmi.server.*; 
import java.net.*; 
import java.io.*; 
  

public class JLegacyRO extends UnicastRemoteObject 
                                      implements JLegacyIF 
{ 
   // JLegacyRO listens on this port in remote object registry 
   public static final int RO_REGISTRY_PORT = 1099; 
  

   // The host address of JLegacyRO 
   private String host; 
  

   // Native method declaration 
   public static native JLegacy getN(); 
  

   // Static initializer 
   static 
   { 
      // Load the native library which includes getN 
      try { 
         System.loadLibrary("JLEG"); 
      } 
      catch (SecurityException    e) { e.printStackTrace(); } 
      catch (UnsatisfiedLinkError e) { e.printStackTrace(); } 
   } 
  

   public JLegacyRO() throws RemoteException { super();} 
  

   public JLegacy getJLegacy()throws RemoteException 
   { 
      JLegacy jleg = null; 
  

      jleg = JLegacyRO.getN(); 
  

      return jleg; 
   } 
  

   // Application 
   public static void main (String args[]) 
   { 
      JLegacyRO remote = null; 
  

      System.setSecurityManager( 
         new RMISecurityManager()); 
  

      try 
      { 
         remote = new JLegacyRO(); 
      } 
      catch (RemoteException e) { e.printStackTrace(); } 
  

      if (remote != null) 
      { 
         if (args.length == 1) 
         { 
            // Get host address of remote object 
            remote.host = args[0]; 
  

            // Start registry and register remote object 
            try 
            { 
               System.out.println( 
                  "JLegacyRO: creating registry"); 
  

               /* Create registry listening on 
                * RO_REGISTRY_PORT. We can do this since 
                * this application is the only one that's going to 
                * use this registry. 
                */ 
               LocateRegistry.createRegistry( 
                  RO_REGISTRY_PORT); 
  

               Naming.bind(  "rmi://" + remote.host + ":" 
                  + RO_REGISTRY_PORT 
                  + "/JLegacyRO", 
                  remote); 
  

               System.out.println( 
                  "JLegacyRO: bound in registry"); 
            } 
            catch (Exception e) { e.printStackTrace(); } 
         } 
         else 
         { 
            System.out.println( 
               "usage: JLegacyRO host_address"); 
         }  // if (args.length == 1) 
      }  // if ( remote != null ) 
   }  // main 
} 
  

Listing 5: JLegacyC.java 

/*********************************************** 
 * JLegacyC.java provides a static method which returns a 
 * populated instance of a JLegacy object. 
 */ 
package my.jlegacy.classes; 
  

import java.applet.*; 
import java.rmi.*; 
  

public class JLegacyC implements java.io.Serializable 
{ 
   public JLegacyC() {} 
  

   public static JLegacy get(Applet parent) 
   { 
      JLegacy   jleg  = null; 
      JLegacyIF ifc = null; 
  

      try 
      { 
         ifc = (JLegacyIF) 
                 Naming.lookup( 
                    "rmi://"+parent.getCodeBase().getHost() 
                    +"/JLegacyRO"); 
         if (ifc != null) jleg  = ifc.getJLegacy(); 
      } 
      catch(Exception e) { e.printStackTrace(); } 
  

      return jleg; 
   } 
} 
  

Listing 6: my_jlegacy_classes_JLegacyRO.h 

/* DO NOT EDIT THIS FILE - it is machine generated */ 
#include <jni.h> 
/* Header for class my_jlegacy_classes_JLegacyRO */ 
  

#ifndef _Included_my_jlegacy_classes_JLegacyRO 
#define _Included_my_jlegacy_classes_JLegacyRO 
#ifdef __cplusplus 
extern "C" { 
#endif 
/* 
 * Class:     my_jlegacy_classes_JLegacyRO 
 * Method:    getN 
 * Signature: ()Lmy/jlegacy/classes/JLegacy; 
 */ 
JNIEXPORT jobject JNICALL Java_my_jlegacy_classes_JLegacyRO_getN 
  (JNIEnv *, jclass); 
  

#ifdef __cplusplus 
} 
#endif 
#endif 
  

Listing 7: Java_my_jlegacy_classes_JLegacyRO_getN.c 

/*********************************************** 
 * Java_my_jlegacy_classes_JLegacyRO_getN.c contains the 
 * native function which retrieves the legacy data using the 
 * legacy library routine, instantiates a JLegacy object, and 
 * populates the object with the legacy data. 
 */ 
  

#include <unistd.h> 
#include <jni.h> 
  

#include "my_jlegacy_classes_JLegacyRO.h" 
#include <LEGACY.h> 
  

/* The Java Native Interface functions used by this native 
 * method were wrapped to promote greater readability and 
 * ease of maintainability 
 */ 
#define JNI_ALLOCOBJECT(class) 
   (*env)->AllocObject(env, (class)) 
#define JNI_DELETELOCALREF(ref) 
   (*env)->DeleteLocalRef(env, (ref)) 
#define JNI_FINDCLASS(name) 
   (*env)->FindClass(env, (name)) 
#define JNI_GETFIELDID(name, sig) 
   (*env)->GetFieldID(env,jlClass, (name),(sig)) 
#define JNI_NEWSTRINGUTF(bytes) 
   (*env)->NewStringUTF(env, (bytes)) 
#define JNI_SETBYTEFIELD(id, val) 
   (*env)->SetByteField(env,jObject, (id),(val)) 
#define JNI_SETINTFIELD(id, val) 
   (*env)->SetIntField(env,jObject, (id), (val)) 
#define JNI_SETLONGFIELD(id, val) 
   (*env)->SetLongField(env,jObject, (id),(val)) 
#define JNI_SETOBJFIELD(id, val) 
   (*env)->SetObjectField(env,jObject,(id),(val)) 
  

/* Prototypes of functions found only in this file */ 
int JL_SetStringField(JNIEnv   *env, 
   jobject   jObject, 
   jfieldID   jFieldID, 
   const char *bytes); 
  

/* 
 * Class:     my_jlegacy_classes_JLegacyRO 
 * Method:    getN 
 * Signature: ()Lmy/jlegacy/classes/JLegacy; 
 */ 
  

/*********************************************** 
 * Java_my_jlegacy_classes_JLegacyRO_getN retrieves the 
 * legacy data using the legacy library routine, instantiates a 
 * JLegacy object, and populates the object with the legacy 
 * data.  Failure is indicated by returning a null object. 
 */ 
JNIEXPORT jobject JNICALL 
   Java_my_jlegacy_classes_JLegacyRO_getN 
      (JNIEnv *env, jclass jClass) 
{ 
Legacy_Type   legacy; 
   int                   istat                 = 0; 
   jclass               jlClass             = NULL; 
   jfieldID           timestamp_ID = NULL; 
   jfieldID           pType_ID   = NULL; 
   jfieldID           id_ID          = NULL; 
   jfieldID           mType_ID  = NULL; 
   jfieldID           stringA_ID = NULL; 
   jfieldID           stringB_ID = NULL; 
   jfieldID           stringC_ID = NULL; 
   jfieldID           stringD_ID = NULL; 
   jfieldID           stringE_ID = NULL; 
   jfieldID           stringF_ID = NULL; 
   jfieldID           stringG_ID = NULL; 
   jfieldID           stringH_ID = NULL; 
   jobject             jObject         = NULL; 
  

   istat = Get_Legacy_Data(&legacy); 
   if ( istat < 0 ) return NULL; 
  

   jlClass = 
      JNI_FINDCLASS("my/jlegacy/classes/JLegacy"); 
   if ( jlClass ) 
   { 
      jObject = JNI_ALLOCOBJECT(jlClass); 
      if ( jObject ) 
      { 
         /* Establish the field IDs */ 
         timestamp_ID = 
            JNI_GETFIELDID("timestamp", "J"); 
         pType_ID       = JNI_GETFIELDID("pType", "I"); 
         id_ID              = JNI_GETFIELDID("id", "B"); 
         mType_ID      = JNI_GETFIELDID("mType", "I"); 
         stringA_ID     = 
            JNI_GETFIELDID("stringA", 
               "Ljava/lang/String;"); 
         stringB_ID     = 
            JNI_GETFIELDID("stringB", 
               "Ljava/lang/String;"); 
         stringC_ID     = 
            JNI_GETFIELDID("stringC", 
               "Ljava/lang/String;"); 
         stringD_ID     = 
            JNI_GETFIELDID("stringD", 
               "Ljava/lang/String;"); 
         stringE_ID      = 
            JNI_GETFIELDID("stringE", 
               "Ljava/lang/String;"); 
         stringF_ID      = 
            JNI_GETFIELDID("stringF", 
               "Ljava/lang/String;"); 
         stringG_ID     = 
            JNI_GETFIELDID("stringG", 
               "Ljava/lang/String;"); 
         stringH_ID     = 
            JNI_GETFIELDID("stringH", 
               "Ljava/lang/String;"); 
  

         /* Set the instance fields of the object to be returned */ 
         if (       timestamp_ID 
              && pType_ID 
              && id_ID 
              && mType_ID 
              && stringA_ID 
              && stringB_ID 
              && stringC_ID 
              && stringD_ID 
              && stringE_ID 
              && stringF_ID 
              && stringG_ID 
              && stringH_ID ) 
         { 
            JNI_SETLONGFIELD(timestamp_ID, 
               legacy.Timestamp); 
            JNI_SETINTFIELD(pType_ID, legacy.P_Type); 
            JNI_SETBYTEFIELD(id_ID, legacy.Id); 
            JNI_SETINTFIELD(mType_ID, legacy.M_Type); 
  

            if ( 
                  JL_SetStringField(env, jObject, stringA_ID, 
                     legacy.String_A) 
               && JL_SetStringField(env, jObject, stringB_ID, 
                     legacy.String_B) 
               && JL_SetStringField(env, jObject, stringC_ID, 
                     legacy.String_C) 
               && JL_SetStringField(env, jObject, stringD_ID, 
                     legacy.String_D) 
               && JL_SetStringField(env, jObject, stringE_ID, 
                     legacy.String_E) 
               && JL_SetStringField(env, jObject, stringF_ID, 
                     legacy.String_F) 
               && JL_SetStringField(env, jObject, stringG_ID, 
                     legacy.String_G) 
               && JL_SetStringField(env, jObject, stringH_ID, 
                     legacy.String_H)) 
            { 
               /* Instance string fields of object have been set */ 
            } 
            else 
            { 
               JNI_DELETELOCALREF(jObject); 
               return NULL; 
            } 
         } 
         else 
         { 
            JNI_DELETELOCALREF(jObject); 
            return NULL; 
         } 
      } /* if ( jObject ) */ 
   } /* if ( jlClass ) */ 
  

   return jObject; 
} /* End of Java_my_jlegacy_classes_JLegacyRO_getN */ 
  

/*********************************************** 
 * JL_SetStringField sets the indicated field of the object to 
 * a java.lang.String object constructed from the indicated 
 * char array. 
 */ 
int JL_SetStringField(JNIEnv   *env, 
   jobject   jObject, 
   jfieldID   jFieldID, 
   const char *bytes) 
{ 
   int       retval = 1; 
   jstring jString; 
  

   jString = JNI_NEWSTRINGUTF(bytes); 
   if ( jString ) 
   { 
      JNI_SETOBJFIELD(jFieldID, jString); 
      JNI_DELETELOCALREF(jString); 
   } 
   else 
      retval = 0; 
  

   return retval; 
} /* End of JL_SetStringField */ 

  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.