Visual Basic Accessing Oracle (Oracle8i) EJBs

Java/J2EE COM Interoperability Products Page

Summary

This example shows you how you can allow COM clients, such as Visual Basic clients, to access Enterprise JavaBeans (EJBs) hosted in Oracle8i using J-Integra®. It also shows how the EJBs can in turn access COM objects.

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 Oracle8i can instantiate and access COM components, where again Oracle8i can be running on any Operating System that Oracle8i supports:


About J-Integra®

J-Integra® is a bi-directional pure Java-COM bridge. It incorporates a pure Java implementation of a DCOM engine (the wire protocol defined by Microsoft to allow remote access to COM components). This means that it can allow COM clients running on MS Windows to access Java objects as though they were COM objects, where the Java object is running on any Operating System running on any platform, and it can allow Java clients running anywhere to access Windows based COM components.


VB client to the Customers EJB

This example shows a VB client accessing an EJB hosted in Oracle8i.

In order to run the example, you will need the J-Integra® kit, as well as Oracle8i, and Visual BASIC. You can run Oracle8i 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 example talks to one of the standard examples: http://otn.oracle.com/docs/products/oracle8i/doc_library/817_doc/java.817/a83725/appejbe6.htm#634682 Customer Container-Managed Entity Bean Example.

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:

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

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);
Context ic = new InitialContext (env);
CustomerHome ch = (CustomerHome)ic.lookup (serviceURL + objectName);

Customer cust = ch.create("Jake Terwilliger", "Pine Drive");
System.out.println("created " + cust.getName());
System.out.println (" address = " + cust.getAddress());
String pk = (String) cust.getPrimaryKey();
System.out.println("Primarykey = " + pk);
The example is installed in \oracle\ora81\javavm\demo\examples\ejb\entities\customer under MS Windows.

As you can see the example establishes an initial JNDI context, gets a home interface, then creates an EJB and displays a couple of its properties. The example goes on to delete EJBs, search for them, etc.

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 Oracle8i.

This is the code associated with the bridging JVM:

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 COMtoCORBA {
	public static void main(String[] args) throws Exception {
		Jvm.register("oraclejvm", new CorbaInstanciator(args[0], args[1]));
		while (true) {
			// endless loop to keep the bridge alive
			Thread.sleep(10000000);
		}
	}
}

class CorbaInstanciator implements Instanciator {
	Context ctx;

	CorbaInstanciator(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));
		}
	}
}

Open the make file you used to build the normal Java client. In it you will see the CLASSPATH being initialised -- under MS Windows, the make file is makeit.bat, and the line begins set CLASSPATH= Append the full path the J-Integra® runtime (jintegra.jar) to the set CLASSPATH, separated by a semicolon.

For example, if you installed J-Integra® under your C: drive, the line would end ...%ORACLE_HOME%\lib\vbjapp.jar;%JDK_CLASSPATH%;c:\jintegra\lib\jintegra.jar

Further down the make file, several Java programs are compiled using the javac command, including CustomerClient.java. Add a new line after that line:

javac -g COMtoCORBA.java

Run the make file in order to compile the bridge class.

Copy the file used to run the normal Java client, calling it runbridge.bat instead of runit.bat (if running under MS Windows). Edit the file, and again append the full path the the J-Integra® runtime to the line that sets the CLASSPATH.

Replace the line that runs the CustomerClient, with this line:

java -DJINTEGRA_DCOM_PORT=1350 COMtoCORBA scott tiger

The JINTEGRA_DCOM_PORT property tells the J-Integra® runtime what TCP/IP port to listen for DCOM requests on. It is repeated below when registering the JVM.

Run the bridge:

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 on the New JVM... button, and enter "oraclejvm" as the name. Enter the TCP/IP name of the machine running the bridging jvm under "Hostname", and "1350" 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 first textbox "NameField", and the second one "AddressField". Name the buttons "Create", "Delete", "Find" and "ShowAll":

Double-click on the form, and enter the following code. Replace the bold text with whatever value you are using for the ORACLE_SERVICE environment variable when running the standard Java client (sess_iiop://...):

Option Explicit
Dim ch As Object ' CustomerHome

Private Sub Form_Load()
	Set ch = GetObject("oraclejvm:TO DO: REPLACE WITH ORACLE_SERVICE/test/customer2")
End Sub

Private Sub Create_Click()
	On Error GoTo errh
	Dim cust As Object
	Set cust = ch.Create(Form1.NameField.Text, Form1.AddressField.Text)
	MsgBox "Created customer. PK = " &
	cust.getPrimaryKey()
	Exit Sub
errh:
		MsgBox "Error Creating:" + Err.Description
End Sub

Private Sub Delete_Click()
	On Error GoTo errh
	Dim cust As Object
	Set cust = ch.findByPrimaryKey(Form1.NameField.Text)
	cust.Remove Form1.NameField = ""
	Form1.AddressField = ""
	MsgBox "Deleted customer"
	Exit Sub
errh:
		MsgBox "Error Deleting:" & Err.Description & Err.Source
End Sub

Private Sub Find_Click
	On Error GoTo errh
	Dim cust As Object
	Set cust = ch.findByPrimaryKey(Form1.NameField.Text)
	Form1.AddressField = cust.getAddress() MsgBox "Found customer"
	Exit Sub
errh:
		MsgBox "Error Finding:" + Err.Description
End Sub


Private Sub ShowAll_Click()
	On Error GoTo errh
	Dim e As Object
	Set e = ch.findAllCustomers("")
	While e.hasMoreElements
		Dim cust As Object
		Set cust = e.nextElement()
		Form1.NameField = cust.getName()
		Form1.AddressField = cust.getAddress()
		MsgBox "Found customer"
	Wend
	Exit Sub
errh:
		MsgBox "Error Finding All:" + Err.Description
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 Oracle8i on the Windows machines.

Try it out:

กก

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 Oracle8i, 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("oraclejvm:..."), 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 1350

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 jvm As Object
Dim ch As Object ' CustomerHome

Private Sub Form_Load()
	Set jvm = GetObject("objref:...:")
	Set ch = jvm.get("oraclejvm:TO DO: REPLACE WITH ORACLE_SERVICE/test/customer2")
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 Oracle8i on the Windows machines.