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.
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.
Download and install J-Integra®.
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.
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.
This project contains one form: HelloWorldForm.
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 |
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.
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®.
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)); } } } |
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
Click New JVM and enter oraclejvm as the name of the new JVM. Click OK.
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.
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.
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.
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.
Set the CLASSPATH to include the jintegra.jar, common.jar, and Oracle's library files.
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