Accessing EJBs Hosted in WebLogic from ASP

Java/J2EE COM Interoperability Products Page

Summary

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

Introduction

WebLogic is a J2EE application server. To find more about it go here. This example is based on the Enterprise JavaBean container-managed persistence example, which is included in the standard WebLogic kit.

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 WebLogic, including the samples that are distributed with it. Follow the instructions in the WebLogic documentation to run the Container Managed example and make sure it works.

  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. Create the file COMtoWebLogic.java with the following content. If you are not running WebLogic using the default configuration then you will need to modify the initial context.

    // This is the bridge.  The ASP client talks to this
    // process via DCOM, and this process talks to WebLogic via RMI.  
    // It all happens behind the scenes in the J-Integra® runtime. See // href="/"
    
    import javax.naming.*;
    import java.util.Hashtable;
    
    import com.linar.jintegra.*;
    
    public class COMtoWebLogic {
      public static void main(String[] args) throws Exception {
        Jvm.register("ejb", new EjbInstanciator()); // For COM access to objects loaded via JNDI lookup
        Thread.sleep(10000000);
      }
    }
    
    class EjbInstanciator implements Instanciator {
      Context ctx;
    
      EjbInstanciator() throws NamingException {
        Hashtable env = new Hashtable(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, "t3://localhost:7001");
        ctx = new InitialContext(env);
      }
    
    
      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));
        }
      }
    }

  2. Make sure your CLASSPATH includes the jintegra.jar runtime and the necessary WebLogic classes:

    CLASSPATH=d:\jintegra\lib\jintegra.jar;D:\bea\wlserver6.1\lib\weblogic.jar
  3.   Compile COMtoWebLogic.java, and then run it, setting the JINTEGRA_DCOM_PORT property to 7050

    > javac COMtoWebLogic.java
    > java -DJINTEGRA_DCOM_PORT=7050 COMtoWebLogic

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.exe ejb localhost[7050]
  2. Create an ASP with the following contents:

  3. Using VBScript:

    <%@ LANGUAGE = VBSCRIPT %>
    <html><head><title>Accessing an EJB in WebLogic</title></head><body>
    <%
    Set home = GetObject("ejb:containerManaged.AccountHome")
    On Error Resume Next
    Set account = home.findByPrimaryKey("MyAccount")
    If Err.Number <> 0 Then
        Response.write("Could not find Account (" & Err.Description & ")<br>")
        Response.write("Account being created; opening balance is $1500<br>")
        Set account = home.Create("MyAccount", 1500, "MyAccount")
    Else
        Response.write("Account found<br>Balance is $" & account.balance & "<br>")
    End If
    account.Deposit(10)
    Response.write("Deposited $10<br>Account balance is now $" & account.balance & "<br><br>")
    account.Withdraw(account.balance + 100)
    If Err.Number <> 0 Then
            Response.write(Err.Description)
    End If
    
    %>
    </body></html>
     
  4. 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.

  5. If you are running ASP under Windows 2000 and the ASP virtual directory is set to be Pooled (the default), then configure the virtual directory to be Low(IIS Process).

Run the ASP application

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

  2.  Refresh the page a few times to see how the numbers are updated.