Previous     Contents     Index     DocHome     Next     
Process Builder's Guide 6.0 (SP1)



Chapter 12   Scripting with EJB Components


This chapter describes how to call an Enterprise JavaBean (EJB) from JavaScript. You should be familiar with EJBs to understand the information in this chapter.

This chapter describes the following topics:



Calling EJB Components from JavaScript

Your JavaScript process automation script can create an instance of an EJB and call the EJB's methods to perform business logic. For example, an order-entry application might create an instance of an EJB that represents a credit card authorization gateway. The application would call methods to check the credit card number, authorize the transaction, and create a charge on the account, as shown in Figure 12-1:

Figure 12-1    Programming logic for calling an EJB component




A Sample Script



To use an EJB, the calling JavaScript must perform the following actions:

  • Look up the bean using the home interface's JNDI name

  • Create an instance of the bean

  • Call the bean's methods to perform tasks supported by the bean

The following JavaScript function shows an example of these actions:

function billCC ()
{
   var pi = getProcessInstance ();
   var wi = getWorkItem();
   pi.setData("ccError",null);
   var ccNumber = pi.getData("ccNumber");

   // Get bean home, create it, and call the check method on it.
   var home = ejbLookup("Netscape/CreditCardServer");
   var ccs = home.create ( pi.getData("ccType") );
   
   var result = ccs.check ( ccNumber );
   if (! result.equals
         ( Packages.com.netscape.pm.sample.ICreditCardServer.CARD_OK ))
   {
          pi.setData("ccError", result);
          return true;
   }

   result = ccs.authorize ( ccNumber, 100, 25 );
   if (result.startsWith (
         Packages.com.netscape.pm.sample.ICreditCardServer.CREDIT_LIMIT_EXCEEDED)
         || result.startsWith (
         Packages.com.netscape.pm.sample.ICreditCardServer.CREDIT_DENIED) )
   {
          pi.setData("ccError", result);
          return true;
   }

   // Charge the card the amount shown.
   result = ccs.charge ( ccNumber, result );
   if (! result.startsWith (
         Packages.com.netscape.pm.sample.ICreditCardServer.OPERATION_OK ))
   {
          pi.setData("ccError", result);
          return true;
   }
   
   pi.setData("ccAuthcode", result);
   return true;
}

You call the ejbLookup() function to create a reference to the EJB. This method uses the bean's JNDI name to determine the EJB to use. In this example, the name is Netscape/CreditCardServer. You can examine the bean's BeanHomeName property to determine the JNDI name.

You call the bean's factory method, which is defined in the bean's home interface class, to create the instance of the bean. In this example, the method is create(). You can examine the bean's HomeInterfaceClassName property to determine the home interface class.

You can then call the bean's methods, which are defined in the bean's remote interface class, to perform specific tasks. In this example, the JavaScript calls the bean's check(), authorize(), and charge() methods. You can examine the bean's RemoteInterfaceClassName property to determine the remote interface class.



Handling Exceptions



Java exceptions cannot be handled in JavaScript. You can implement your bean so that the script receives error information without needing to deal with an exception, as in the following example:


...
catch (...Exception e) {
   e.PrintStackTrace();
       return true;
   }
}

If an exception is thrown, the process instance moves to the exception manager that you specify in the Inspector window, as shown in Figure 12-2:

Figure 12-2    Setting an exception manager in Process Builder




Data Conversion Issues



Process Manager uses LiveConnect's rules to convert between Java and JavaScript data types. When you call a Java method from JavaScript, JavaScript does not convert the data type of the data until it is manipulated by the script. For example, the result variable in the script contains a Java string data type after executing the Java EJB check() method in the following statement:

var result = ccs.check ( ccNumber );

You must compare the variable's contents using Java methods; comparing with JavaScript functions will not work as expected. The following statement shows the comparison using Java:


if (result.equals
( Packages.com.netscape.pm.sample.ICreditCardServer.CARD_OK )
   == false) { ... }

To access Java data, you must qualify the package name with the Packages keyword unless the Java data is from the java package. JavaScript recognizes the java package without qualification. You can also create an instance of an object by specifying the new operator, as in the following examples:


var aResult = new java.lang.String;
var CARD_OK = new
   Packages.com.netscape.pm.sample.ICreditCardServer.CARD_OK

Note. Conversion between Java and JavaScript is implicit and occurs when the value is manipulated as a JavaScript data type. For example, if result contains a Java string data type, the following statement causes it to be converted to a JavaScript string data type:

result = result + "";


Previous     Contents     Index     DocHome     Next     
Copyright © 2000 Sun Microsystems, Inc. Some preexisting portions Copyright © 2000 Netscape Communications Corp. All rights reserved.

Last Updated October 12, 2000