COM integration with the Oracle9i EJBs: HelloWorld

This example shows you how you can allow COM clients, such as Visual Basic™ clients, to access Enterprise JavaBeans™ (EJBs) hosted in Oracle9i™, using J-Integra®.

This example is based on Oracle's HelloWorld EJB. In this example we will create a Visual Basic client that uses the J-Integra®® bridge to access Oracle's EJB. The following diagram shows the overal concept of accessing Oracle's HelloWorld EJB from a VB client using J-Integra®.

This makes for a compelling solution from the deployment perspective, since nothing needs to be installed or configured on the Windows client machines .

In order to run the example, you will need the J-Integra® kit, as well as Oracle9i, and Visual Basic. You can run Oracle9i on any platform it supports, and you will not need to install a JVM on the Windows client machine, although you can run everything on the same machine if you wish.

Prerequisites

  1. You should be familiar with Oracle9i™ Application Server and be able to install and configure your application server to access and run the examples provided by Oracle.

  2. Download and install J-Integra®.

  3. Install the J-Integra® license.

The steps involved

  1. VB client

  2. Compile the J-Integra® bridging server

  3. Register a listening JVM

  4. Start J-Integra® bridging server

  5. Run the VB client

VB client

The example talks to one of Oracle's standard examples: HelloWorld. You can find this example under ORACLE_HOME\javavm\demo\examples\ejb\basic\helloWorld. This is a simple Session Bean EJB.

  1. Go to JINTEGRA_HOME\examples\vb-ejb\oracle\HelloWorld\Client\vb. Open HelloWorld.vbp in Visual Basic.

    Note This is a Visual Basic 6.0 project.

  2. This project contains one form: HelloWorldForm.

  3. Here is the source code for this form.

    Option Explicit
    Dim helloWorld As Object ' HelloWorldHome
    
    Private Sub btnHelloWorld_Click()
    On Error GoTo errh
    Dim hw As Object
    Rem creating a new HelloWorld object
    Set hw = helloWorld.Create()
    Rem Calling the helloWorld() method of the new HelloWorld object
    MsgBox "Oracle EJB says: " & hw.helloWorld()
    Exit Sub
    errh:
    MsgBox "Error Calling Oracle EJB:" + Err.Description
    
    End Sub
    
    Private Sub Form_Load()
    Rem creating the home object of the HelloWorld EJB
    Set helloWorld = GetObject("oraclejvm:sess_iiop://localhost:2481:ORCL/test/HelloBean")
    End Sub
            

    Note You must change the highlighted sections of the above code (in red color) appropriately. The first section is the name of the computer in which your Oracle Application Server resides, and the other section is the SID of the database containing the EJBs. If you do not have Visual Basic, you can still run this example as long as the Oracle Application Server is on localhostand the SID is ORCL. If that's the case you can use the HelloWorld.exe, in jintegra\examples\vb-ejb\oracle\HelloWorld\Client\vb. You will find the Visual Basic project and the class files for this example in jintegra\examples\vb-ejb\oracle\HelloWorld\Client\vb.

Compile the J-Integra® Bridging Server

The Visual Basic example uses the GetObject function to create a home object of the HelloWorld EJB. When the Visual Basic client invokes GetObject, a J-Integra® bridging server picks up the call, creates the appropriate object, and returns it to the Visual Basic client. The J-Integra® bridging server is a simple Java application that listens to all incoming requests on a specific TCP port. When it receives a request, it creates a Java object and returns the object to the requester.

  1. You will find the source code for the J-Integra® bridging server in the jintegra\examples\vb-ejb\oracle\HelloWorld\Client\java directory when you download J-Integra®.

  2. Here is the source code:

    import java.util.*;
    import javax.naming.*;
    import com.linar.jintegra.Jvm;
    import com.linar.jintegra.AutomationException;
    import com.linar.jintegra.Instanciator;
    import oracle.aurora.jndi.sess_iiop.ServiceCtx;
    
    public class COMtoOracle {
      public static void main(String[] args) throws Exception {
        Jvm.register("oraclejvm", new OracleInstanciator(args[0], args[1]));
        while (true) { // endless loop to keep the bridge alive
          Thread.sleep(10000000);
        }
      }
    }
    
    class OracleInstanciator implements Instanciator {
      Context ctx;
    
      OracleInstanciator(String user, String password) throws NamingException {
        Hashtable env = new Hashtable();
        env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");
        env.put(Context.SECURITY_PRINCIPAL, user);
        env.put(Context.SECURITY_CREDENTIALS, password);
        env.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN);
        ctx = new InitialContext (env);
      }
    
      // This method is called by the J-Integra® runtime when a COM client tries
      // to instantiate an object. First we try to instantiate it as a Java class
      // and if that fails, we do a JNDI lookup
      public Object instanciate(String javaClass) throws AutomationException {
        try {
          try {
            return Class.forName(javaClass).newInstance();
          } catch(Exception e) {}
          return ctx.lookup(javaClass);
        } catch (Throwable t) {
          t.printStackTrace();
          throw new AutomationException(new Exception("Unexpected: " + t));
        }
      } 
    }
        

  3. Open a DOS session and go to the java sub-folder. Setup your CLASSPATH environment variable. You must include jintegra.jar to your CLASSPATH. Also you must include the path to Oracle jar files in your CLASSPATH. After setting up the CLASSPATH, run the following command to compile J-Integra® bridging server:

    javac COMtoOracle.java

Register a listening JVM

The Visual Basic client invokes GetObject to get the Home object of the HelloWorld EJB. The first part of the parameter of this method identifies the server that can process the request. If you look at the Visual Basic client code, you will see that the name of this server is oraclejvm. In this step we register this server so when Visual Basic client invokes GetObject, the Operating System will find the server which is capable of responding to the request.
  1. To register a J-Integra® bridging server, use the regjvm.exe tool, located in jintegra\bin folder. Go to that folder and run regjvm.exe.

  2. Click New JVM and enter oraclejvm as the name of the new JVM. Click OK.

  3. You can specify the host computer in which the J-Integra® bridging server will reside. Also you can specify the TCP/IP port which this server will listen to. The default host name is localhost and the default port is 1350.

Start the J-Integra® Bridging Server

Now we are ready to launch the J-Integra® bridging server. Because this server is going to create HelloWorld EJB objects, it must have access to the classes related to this EJB. You must make sure that the HelloWorld EJB example runs on your system which means you have to deploy it into your Application Server. Oracle provides a batch file called makeit.bat which you can use to deploy this example into Oracle Application server.

  1. Go to ORACLE_HOME\javavm\demo\examples\ejb\basic\helloWorld and run makeit.bat. You may need to modify common.bat before launching makeit.bat. common.bat is in ORACLE_HOME\javavm\demo and configures some of the environment variables that are used in makeit.bat.

  2. After running makeit.bat, run runit.bat to make sure that the HelloWorld example has been deployed correctly into your Oracle Application Server. After running makeit.bat, this batch file creates a few jar files. One of them is called common.jar. This jar file contains the home and remote interface and their corresponding helper classes. You must include the path to this jar file in your CLASSPATH environment variable before starting J-Integra® bridging server. If your J-Integra® server is running on a different machine than the Application Server, you should copy common.jar from that machine onto the computer in which J-Integra® bridging server is launched.

  3. Set the CLASSPATH to include the jintegra.jar, common.jar, and Oracle's library files.

  4. Start the J-Integra® bridging server by entering the following command (Start the J-Integra® bridging server by entering the following command (replace the username and password with appropriate values. The username and password correspond to a valid username/password on the database in which the HelloWorld EJB has been deployed):

    java -DJINTEGRA_DCOM_PORT=1350 COMtoOracle username password

Run the VB client

Before running th VB client make sure the standard HelloWorld example runs successfully, as explained in Start the J-Integra® Bridging Server.
  1. Go to the HelloWorld\Client\vb sub-folder and double-click HelloWorld.exe. Upon loading the form, VB client creates the home interface of the HelloWorld EJB.

  2. Click Call Oracle Hello World EJB. The VB client creates the remote interface of HelloWorld EJB, invokes its helloWorld method, and shows the result in a message box.