Visual Basic Accessing EJBs Hosted in Sun iPlanet Application Server

Java/J2EE COM Interoperability Products Page

Accessing EJBs within the iPlanet Application Server

Summary

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

Introduction

We will first show you how you can access an EJB from a VB client using a technique which will require some that some J-Integra® software be installed on the COM client machine (although no JVM will need to be installed). We will then show how the example can be modified to allow the VB client to run on any Windows machine without installing anything on the Windows machine other than the VB client -- this is the zero client deployment overhead option:

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

Finally we will show how an EJB hosted in IAS can instantiate and access COM components, where again IAS can be running on any Operating System that iPlanet support:

Prerequisites

In order to run the example, you will need the J-Integra® kit, as well as the iPlanet Application Server, and Visual BASIC. You can run the Application Server 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.

The Steps Involved

  1. VB Client to the Currency Converter EJB

  2. The VB Client (Zero Client Installation)

VB Client to the Currency Converter EJB

This example shows a VB client accessing an EJB hosted in the iPlanet Application Server.

The example talks to one of the standard examples: Currency Converter EJB.

The standard Java client

Before trying to get the VB client working you MUST get the standard example working first. When it runs properly, this is the result:

D:\rmiclient>java j2eeguide.converter.ConverterClient localhost 9010
12160.0
0.77

Please get this working, prior to attempting to follow the rest of this example. Examine the code associated with the client. This is an extract:

env.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
env.put("java.naming.provider.url", "iiop://" + host + ":" + port);
Context initial = new InitialContext(env);
Object objref = initial.lookup("ejb/MyConverter");

ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objref, ConverterHome.class);

Converter currencyConverter = home.create();
double amount = currencyConverter.dollarToYen(100.00);
System.out.println(String.valueOf(amount));
amount = currencyConverter.yenToEuro(100.00);
System.out.println(String.valueOf(amount));

As you can see the example looks up an ejb, narrows it to the home interface, and then invokes a couple of methods on the bean.

Running the bridging JVM

In order for the VB client to talk to the EJB, a "bridging JVM" will run, which the VB client talks to. It in turn talks to the EJB. This bridging JVM can be run anywhere, although we would suggest running it on the same machine as the iPlanet Application Server.

This is the code associated with the bridging JVM:


import javax.naming.*;

import com.linar.jintegra.Jvm;
import com.linar.jintegra.AutomationException;
import com.linar.jintegra.Instanciator;

public class COMtoCORBA {
  public static void main(String[] args) throws Exception {
    Jvm.register("InstructorJvm", new CorbaInstanciator());
    while (true) {
      // endless loop to keep the bridge alive 
      Thread.sleep(10000000);
    }
  }

  public Object narrow(Object narrowFrom, String narrowTo) throws AutomationException {
    try {
      return javax.rmi.PortableRemoteObject.narrow(narrowFrom, Class.forName(narrowTo));
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      throw new AutomationException(e);
    }
  }
}

class CorbaInstanciator implements Instanciator {
  Context ctx;

  CorbaInstanciator() throws NamingException {
    ctx = new InitialContext();
  }

  // This method is called by the J-Integra® runtime when a COM client tries 
  // to instantiate an object. We have special handling if the object is a CORBA object 
  public Object instantiate(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));
    }
  }
}

Compile and run this program with the same environment set up as when you ran the Java client above, except add the J-Integra® runtime (jintegra.jar) from the J-Integra® kit, to your CLASSPATH:

D:\rmiclient>set CLASSPATH=%CLASSPATH%;d:\jintegra\lib\jintegra.jar
D:\rmiclient>javac COMtoCORBA.java

When running the bridge, set properties defining the TCP/IP port that the J-Integra® runtime should run on (in this case 1333), and specifying the properties used to look up EJBs (the same values are set in the Java client):

D:\rmiclient>java -DJINTEGRA_DCOM_PORT=1333
-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:9010 COMtoCORBA

Registering the JVM on the Windows client machine

On the Windows client machine, you will register a JVM name which, which can be used to instantiate Java objects in the JVM from COM clients. Run the \jintegra\bin\regjvm.exe command, which is installed as part of the J-Integra® kit. Click the New JVM... button, and enter iasjvm as the JVM id. Enter the TCP/IP name of the machine running the bridging jvm as the Hostname, and 1333 as the Port:

You can now use this JVM name to instantiate Java objects as COM objects, and access them as COM objects.

Running the VB client

Start up Visual BASIC, and create a new Standard EXE project. Create a form that looks like this. Name the textbox Amount:

Double-click on the form, and enter the following code:

Option Explicit
Dim currencyConverter As Object

Private Sub Form_Load()
Dim Bridge As Object
Set Bridge = GetObject("iasjvm:COMtoCORBA")

Dim home As Object
Set home = Bridge.narrow(GetObject("iasjvm:ejb/MyConverter"), _
                         "j2eeguide.converter.ConverterHome")
Set currencyConverter = home.Create
End Sub

Private Sub Command1_Click()
amount.Text = currencyConverter.dollarToYen(amount.Text)
End Sub

Private Sub Command2_Click()
amount.Text = currencyConverter.yenToEuro(amount.Text)
End Sub

When you run this client, you can enter a value in the amount textbox, and click on one of the buttons. The VB client will then talk to the EJB and calculate the resulting value:

The VB Client (Zero Client Installation)

A step further is to modify the VB client so that it is not necessary to register the JVM on the VB client machine. In fact it is not necessary to install anything at all on the VB client machine (neither iPlanet, J-Integra®, or a JVM). This makes for a compelling solution from the deployment perspective, since nothing needs to be installed or configured on the Windows client machines.

You won't need to modify anything as regards the bridging JVM. You will need to modify the VB client code, so that instead of using the GetObject("iasjvm:COMtoCORBA"), you'll use a different mechanism to gain access to Java objects in the JVM.

On the machine running the JVM, run this command:

java com.linar.jintegra.GetJvmMoniker mymachine.mycompany.com 1333

Replace "mymachine.mycompany.com" with the IP address or full DNS name of the machine running the JVM.

Note: If you run the COM client and it crashes with Automation error: The object exporter specified was not found, then you must run the GetJvmMoniker command with the IP address of the machine hosting the JVM (and not the full DNS name).

When you run this command a message is displayed, and the same message is copied to your clipboard. It will explain that you can access the JVM from a VB client, using a specific VB command:

Set jvm = GetObject("objref:...:")

The value after "objref" is an encoding of the host and port for the JVM, just like that specified when running regjvm. In your VB code you can simply do:

Option Explicit
Dim currencyConverter As Object

Private Sub Form_Load()

Dim jvm As Object
Set jvm = GetObject("objref:...:")

Dim Bridge As Object
Set Bridge = jvm.get("iasjvm:COMtoCORBA")

Dim home As Object
Set home = Bridge.narrow(jvm.get("iasjvm:ejb/MyConverter"), _
                         "j2eeguide.converter.ConverterHome")
Set currencyConverter = home.Create
End Sub

Private Sub Command1_Click()
amount.Text = currencyConverter.dollarToYen(amount.Text)
End Sub

Private Sub Command2_Click()
amount.Text = currencyConverter.yenToEuro(amount.Text)
End Sub

That is it! You can run the above VB client on any Windows machine that has DCOM installed on it. This includes all the MS Windows operating systems (on some environments, such as Windows 95, you may need to install DCOM, which is available from the MS Web Site).

You will not need to install a JVM or JDK, or J-Integra®, or IAS on the Windows machines.