Chapter 16 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:
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.
Calling EJB Components from JavaScript
A Sample Script
Handling Exceptions
Data Conversion Issues
Figure 16.1    Programming logic for calling an EJB component
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
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 ) == false) { 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 ) == false) { 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 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.
... 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 16.2:
Figure 16.2    Setting an exception manager in Process Builder
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 + "";