Visual Basic Accessing Oracle (Oracle9i) EJBs |
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 Customer 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 Customer 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: Customer. You can find this example under ORACLE_HOME\javavm\demo\examples\ejb\entities\customer. This is a simple Entity Bean EJB.
Go to JINTEGRA_HOME\examples\vb-ejb\oracle\Customer\Client\vb. Click Customer.vbp
to open the project in Visual Basic.
Note This is a Visual Basic 6.0 project.
This project contains one form: CustomerForm.
Here is the source code for this form.
Option Explicit Dim ch As Object ' CustomerHome Private Sub Form_Load() Rem creating the home object of the Customer EJB Set ch = GetObject("oraclejvm:sess_iiop://localhost:2481:ORCL/test/CustomerBean") End Sub Private Sub btnCreate_Click() On Error GoTo errh Dim cust As Object Rem creating a new Customer object Set cust = ch.Create(CustomerForm.edtName.Text, CustomerForm.edtAddress.Text) Rem Calling the getPrimaryKey() method of the new Customer object MsgBox "Created customer. PK = " & cust.getPrimaryKey() Exit Sub errh: MsgBox "Error Creating:" + Err.Description End Sub Private Sub btnDelete_Click() On Error GoTo errh Dim cust As Object Rem Find an existing Customer object based on customer name Set cust = ch.findByPrimaryKey(CustomerForm.edtName.Text) Rem Call Remove on the found object to remove it from database cust.Remove CustomerForm.edtName = "" CustomerForm.edtAddress = "" MsgBox "Deleted customer" Exit Sub errh: MsgBox "Error Deleting:" & Err.Description & Err.Source End Sub Private Sub btnFind_Click() On Error GoTo errh Dim cust As Object Rem Find an existing Customer object based on customer name Set cust = ch.findByPrimaryKey(CustomerForm.edtName.Text) Rem Calling the getAddress() method of the found Customer object CustomerForm.edtAddress = cust.getAddress() MsgBox "Found customer" Exit Sub errh: MsgBox "Error Finding:" + Err.Description End Sub Private Sub btnShowAll_Click() On Error GoTo errh Dim e As Object Rem Find all Customer objects Set e = ch.findAllCustomers("") Rem Display customer information one by one While e.hasMoreElements Dim cust As Object Set cust = e.nextElement() Rem Calling the getName() method of the found Customer object CustomerForm.edtName = cust.getName() Rem Calling the getAddress() method of the found Customer object CustomerForm.edtAddress = cust.getAddress() MsgBox "Found customer" Wend Exit Sub errh: MsgBox "Error Finding All:" + Err.Description 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 in localhost and the SID is ORCL. If that's the case you can use the Customer.exe in jintegra\examples\vb-ejb\oracle\Customer\Client\vb. You will find the Visual Basic project and the class files for this example in jintegra\examples\vb-ejb\oracle\Customer\Client\vb.
The Visual Basic example uses the GetObject function to create a home object of the Customer EJB. When the Visual Basic client invokes GetObject, a J-Integra® bridging server picks up the call and 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 jintegra\examples\vb-ejb\oracle\Customer\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
The Visual Basic client invokes GetObject to get the Home object of the Customer 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.
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.
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 Customer EJB objects, it must have access to the classes related to this EJB. You must make sure that the Customer 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\entities\customer
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 Customer 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 (replace the username and password with appropriate values. The username and password correspond to a valid username/password on the database in which the Customer EJB has been deployed):
java -DJINTEGRA_DCOM_PORT=1350 COMtoOracle username password
Before running the VB client, make sure the standard Customer example runs successfully, as explained in Start the J-Integra® Bridging Server.
Go to the Customer\Client\vb sub-folder and double-click Customer.exe. Upon loading the form, the VB client creates the home interface of the Customer EJB.
Enter a Name and Address and click Create. The VB Client creates a new Customer object by invoking the Create method on the home interface of the Customer EJB, and providing the name and address.
After the new customer has been successfully created, the VB client shows a message to confirm the success, otherwise it informs you of a problem in creating the new customer.
Enter a Name and click Delete. The VB client finds the customer by invoking findByPrimaryKey on the home interface, and if the customer exists it invokes Remove on this object to delete the customer from the database.
After the found customer has been successfully removed, the VB client shows a message to confirm the success, otherwise it informs you of a problem in removing the customer.
Enter a name and click Find. The VB client finds the customer by invoking findByPrimaryKey on the home interface, and if the customer exists it invokes getAddress on this object to get the customer's address and displays it on the form.
After finding a customer, the VB client shows a message to confirm the success and displays the address of the found customer on the form, otherwise it informs you of a problem in finding the customer.
Click Show All. The VB client finds all customers by invoking findAllCustomers on the home interface, and after finding all customers it invokes getAddress on each one of them to get the customer's address and displays it on the form, one by one.
After finding a customer, the VB client shows a message to confirm the success and displays the address of the found customer on the form, otherwise it informs you of a problem in finding the customer.