Invoking an EJB component method

To invoke an EJB component method, you need to execute the PowerScript statements required to perform these operations:

  1. Use the Lookup function to access the component’s home interface.

  2. Invoke the method on the interface to create or find an instance of the component and get a reference to the component’s remote interface.

  3. Invoke the business methods on the remote interface.

NoteDoes not apply to EJBConnection methods This section applies to client applications that use an EAServer proxy object and PowerScript functions. For information about invoking EJB methods in client applications that use an EJB client proxy and EJBConnection methods, see Chapter 27, “Building an EJB client.” However, the EJBConnection object is deprecated for use with EAServer and the PowerBuilder Application Server Plug-in.

Specifying the home interface name

PowerBuilder provides an optional third argument to the Lookup function to specify the name of the home interface. EJB components have a property in EAServer called com.sybase.jaguar.component.home.ids. You do not need to specify the third argument to the Lookup function if the home.ids property looks like this:

IDL:PackageName/ComponentNameHome:1.0

For example:

IDL:vacation/TripFinderHome:1.0

In most cases, however, the home.ids property uses the java package naming scheme and you should use the third argument to make sure that the EJB home interface can be located. The string that you specify for this argument must match the component’s com.sybase.jaguar.component.home.ids property without the leading IDL: and trailing :1.0.

For example, suppose the home.ids property is this:

IDL:com/myproj/myejbs/TripFindHome:1.0

Your Lookup function call should look like this::

myconn.lookup(myTripFindHome,"MyEJBs/TripFindHome", &
   "com/myproj/myejbs/TripFinderHome")

Alternatively, you can use the fully-qualified Java class name of the home interface specified in dot notation. For example:

ts.lookup(MyCartHome, "MyEJBs/TripFindHome", &    "com.myproj.myejbs.TripFinderHome")

NoteLookup is case sensitive Lookup in EAServer is case sensitive. Make sure that the case in the string you specify for the arguments to the Lookup function matches the case in the home.ids property.

Creating or finding an instance of an EJB

EAServer supports three types of EJBs—session beans, entity beans, and message-driven beans.

A session bean is created in response to a client request. A client usually has exclusive use of the session bean for the duration of that client session.

An entity bean represents persistent information stored in a database. A client uses an entity bean concurrently with other clients. Since an entity bean persists beyond the lifetime of the client, you must use a primary key class name to identify or find a preexisting component, if the bean has already been created.

A message-driven bean is similar to a stateless session bean, but it responds only to JMS messages and has no direct client interface.

The following examples assume that an EJB component that provides e-commerce shopping cart functionality is running on EAServer. This component is called Cart and is included in a package called Shopping.

Example 1 This script instantiates the Cart component and invokes several component methods. In this example, the second argument to the Lookup method specifies the component name as well as the EAServer package name:

//Instance variable:
//Connection myconnect

CartHome MyCartHome // EJB's home interface
Cart MyShoppingCart // EJB's remote interface
long ll_result

//Get the home interface
ll_result = &
myconnect.Lookup(MyCartHome, "Shopping/Cart", &
   "com.sybase.shopping.Cart")

//Get a reference to Cart component's business logic
TRY
   MyShoppingCart = MyCartHome.Create()
CATCH (ctscomponents_createexception ce)
   MessageBox("Create exception", ce.getmessage())
   // handle exception
END TRY

//Use the shopping cart
MyShoppingCart.AddItem(66)
MyShoppingCart.Purchase()

Example 2 If the Cart EJB component is defined as an entity bean, then the script must use the findByPrimaryKey method to find and reference a preexisting or persistent component if one exists:

//Instance variable:
//Connection myconnect

CartHome MyCartHome // EJB's home interface
Cart MyCart // EJB's remote interface
long ll_result

//Get the home interface
ll_result = &
myconnect.Lookup(MyCartHome, "Shopping/Cart", &
   "com.sybase.shopping.Cart")

//Get a reference to Cart from a previous session
TRY
   MyCart = MyCartHome.findByPrimaryKey("MYkey")
CATCH ( ctscomponents_finderexception fe )
   MessageBox("Finder exception", &
      fe.getmessage())
   // handle exception
END TRY
//Use the shopping cart
MyCart.AddItem(66)
MyCart.Purchase()

Restrictions

PowerBuilder clients to EJB objects act as CORBA clients, which means that they do not have the full capabilities of Java clients. Java clients can use methods inherited from the javax.ejb.EJBObject interface.

For example, a Java client can obtain a handle for a remote interface instance. The handle is a binary encoding of the session state between the client and the bean. The client can obtain a handle, save it to disk or mail it to another location, then reestablish the session at a later time. PowerBuilder clients can obtain similar functionality using the Object_To_String and String_To_Object functions of the JaguarORB object.

Handling exceptions

The remote interface of an EJB component can indicate errors or warnings. Standard exceptions thrown by the EJB component are mapped to CORBA system exceptions. The EJB component can also throw user exceptions. For information about handling exceptions thrown by EAServer components, see “Handling errors”.

For information about calling an EJB component from a PowerBuilder component in EAServer, see “Accessing an EJB component”.