Accessing EJBs Hosted in JRun from ASP

Java/J2EE COM Interoperability Products Page

Summary

In this example you will see how to access Enterprise Java Beans hosted in the JRun application server.

Introduction

JRun is a J2EE application server and integrated development environment for building and deploying server-side Java applications. To find out more about JRun go to http://www.macromedia.com/software/jrun/.

This example is based on the BalanceBean EJB sample that is included in the JRun server installation.

The standard example has a Java client that accesses a bank account, and adds/removes money, etc. Below we will show an ASP program that can access the same objects and execute the same operations.

Prerequisites

  1. You will need an installation of JRun 3.0, including the samples that are distributed with it. Follow the instructions in the JRun documentation to run the samples and make sure sample2a works properly.

  2. Also ensure that you have installed the J-Integra® license.

The Steps Involved

  1. Create and run the bridge program

  2. Create and install the ASP application

  3. Run the ASP application

Create and Run the Bridge Program

  1. Open a command prompt window and enter the following commands:

    >cd %JRUN_HOME%\samples\sample2a
    >makew standalone
  2.  Create a new directory to hold the files we will create for this example.

  3. Create the file setup.bat with the following contents. Make sure to fill in the path to the J-Integra® and JRun installations.

    @rem +------------------------------------------------------------------+
    @rem | YOU MUST CHANGE THE FOLLOWING VALUES TO MATCH YOUR SYSTEM SETUP  |
    @rem +------------------------------------------------------------------+
    set JINTEGRA_HOME=D:\jintegra
    set JRUN_HOME=D:\Program Files\Allaire\JRun
    @rem +------------------------------------------------------------+
    @rem | You should not have to change anything else in this file.  |
    @rem +------------------------------------------------------------+
    set A=.
    set B=%JINTEGRA_HOME%\lib\jintegra.jar
    set D=%JINTEGRA_HOME%\lib
    set E=%JRUN_HOME%\servers\default\deploy\ejipt_exports.jar
    set F=%JRUN_HOME%\servers\default\deploy\sample2a_ejb.jar
    set G=%JRUN_HOME%\servers\default\deploy\ejipt_objects.jar
    set H=%JRUN_HOME%\lib\ejipt_client.jar
    set I=%JRUN_HOME%\lib\ext\ejb.jar
    set J=%JRUN_HOME%\lib\ext\jms.jar
    set K=%JRUN_HOME%\lib\ext\jndi.jar
    set L=%JRUN_HOME%\lib\ext\jta.jar
    set M=%JRUN_HOME%\lib\ext\jdbc.jar
    set N=%JRUN_HOME%\lib\ext\iioprt.jar
    @rem +-------------------------------------------+
    @rem | HERE'S THE CLASSPATH... DO NOT EDIT THIS! |
    @rem +-------------------------------------------+
    set CLASSPATH=%A%;%B%;%C%;%D%;%E%;%F%;%G%;%H%;%I%;%J%;%K%;%L%;%M%;%N%;
    

  4.  Create the java bridge Com2Jrun.java with the following content:

    // This is the bridge. The ASP client talks to this process via DCOM,
    // and this process talks to the EJB. It all happens behind the scenes
    // in the J-Integra® runtime.   
    
    import com.linar.jintegra.*;
    
    import java.util.*;
    import javax.ejb.*;
    import javax.naming.*;
    
    import ejbeans.*;
    
    public class Com2Jrun {
      private static EjbInstanciator ejbIns = new EjbInstanciator();
    
      // main: This method registers the JVM and keeps the Com2Java
      // bridge running continuously so that COM clients can access it.
      public static void main(final String[] args) throws Exception {
        //com.linar.jintegra.Log.logImmediately(3, System.err);
        Jvm.register("jrunejb", ejbIns);
        while (true) {
          Thread.sleep(100000);
        }
      }
    
      // login: This is JRun-specific and provides the login context to access 
      // the Balance EJB. Please see EjbClient.java in the JRun sample2a directory 
      // to see where this code comes from.
      public void login() {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                               "allaire.ejipt.ContextFactory");
        properties.setProperty(Context.PROVIDER_URL, "ejipt://localhost:2323");
        properties.setProperty(Context.SECURITY_PRINCIPAL, "chief");
        properties.setProperty(Context.SECURITY_CREDENTIALS, "pass");
    
        try {
          ejbIns.context = new InitialContext(properties);
        } catch (NamingException e) {
          e.printStackTrace();
        }
      }
    
      // getHome: This is JRun-specific and returns the Balance EJB Home object. 
      // Please see EjbClient.java in the JRun sample2a directory to see where this
      // code comes from.
      public BalanceHome getHome() {
        BalanceHome home = null;
    
        try {
          home = (BalanceHome) javax.rmi.PortableRemoteObject.narrow(
            ejbIns.context.lookup("java:comp/env/ejb/sample2a.BalanceHome"),
            BalanceHome.class);
        } catch (NamingException e) {
          e.printStackTrace();
        }
        return home;
      }
    
      // getBalance: This is JRun-specific and returns (or creates) the balance 
      // associated with the Balance EJB. Please see EjbClient.java in the JRun 
      // sample2a directory to see where this code comes from.
      public Balance getBalance(BalanceHome home) throws Exception {
        Balance b = null;
        int key = 123; // default key for accessing the balance in the JRun DB
    
        try {
          // return existing Balance if already exists
          b = home.findByPrimaryKey(new Integer(key));
        } catch (FinderException e) {
          // create new Balance if not there
          b = home.create(key);
        }
        return b;
      }
    }
    
    class EjbInstanciator implements Instanciator {
    
      public Context context;
    
      // instanciate: This method is called by the J-Integra® runtime when a COM 
      // client tries to instantiate an object.
      public Object instanciate(String javaClass) throws AutomationException {
        try {
          try {
            return Class.forName(javaClass).newInstance();
          } catch (Exception e) {
            e.printStackTrace();
          }
          return context.lookup(javaClass);
        } catch (Throwable t) {
    
          t.printStackTrace();
          throw new AutomationException(new Exception("Unexpected: " + t));
        }
      }
    }

  5.  Compile Com2Jrun.java, and then run it:

    >javac Com2Jrun.java
    >java -DJINTEGRA_MATCH_THREADS -DJINTEGRA_DCOM_PORT=1351 Com2Jrun
    

Create and Install the ASP Application

  1. On the ASP client machine, tell J-Integra® about the location of the JVM, and the name that can be used to access Java objects through it. You will need to know the TCP/IP host name of the machine running the bridge you just started. If it is the same machine as the ASP client, then use localhost, otherwise use the appropriate host name:

    >D:\jintegra\bin\regjvmcmd jrunejb localhost[1351]
    
  2. Create an ASP page with the following contents:

    <%@ LANGUAGE = VBSCRIPT%>
    <html>
    <head>
    <title>ASP to JRun</title>
    </head>
    <body>
    <%
    	Set Bridge = GetObject("jrunejb:Com2Jrun")
    	Bridge.login()
    	Set home = Bridge.getHome
    	Set balance = Bridge.getBalance(home)
    	act = Request.Form("action")
    	amt = Request.Form("amount")
    	If act = "deposit" Then
    		balance.Save(amt)
    	ElseIf act = "withdraw" Then
    		balance.Spend(amt)
    	End If
    	Response.write "Account Balance: " & balance.getValue & "<br>"
    %>
    <FORM METHOD=POST ACTION="jrun.asp">
    Amount: <INPUT TYPE="text" NAME="amount" value="100"><br>
    <INPUT TYPE="radio" NAME="action" value="deposit" checked>Deposit
    <INPUT TYPE="radio" NAME="action" value="withdraw">Withdraw<br>
    <INPUT TYPE="submit" value="Submit">
    </FORM>
    </body>
    </html>
    

  3. To make this file available on a web browser please follow the instructions in the Accessing a Simple Java Class from Active Server Pages example.

  4. If you are running ASP under Windows 2000 configure the virtual directory to have an Application Protection setting of Low (IIS Process).

Run the ASP Application

  1. Open the ASP page in your browser. You should see something similar to the following:

  2. Enter an amount, select Deposit or Withdraw and press submit. The Account Balance should change accordingly.