Chapter 10 Jaguar EJB Interoperability


Invoking EJB components from CORBA Java clients

CORBA Java clients can instantiate an Enterprise JavaBean using a proxy for the Bean's home interface, then call business methods using a proxy for the Bean's remote interface.

Chapter 11, "Creating CORBA Java Clients" in the Jaguar CTS Programmer's Guide describes how to use Jaguar's Java client ORB. This chapter provides new information on implementing clients that call EJB component methods.

Deciding whether to use EJB or CORBA interfaces

When using the Jaguar client runtime, the EJB and CORBA interfaces offer identical functionality. You may wish to use CORBA interfaces rather than the EJB interfaces for the following reasons:

Generating CORBA stubs

You cannot generate CORBA stubs to the same Java package as EJB stubs. Therefore, you must generate CORBA stubs using either a different code base or a different Java package than used by the existing EJB stubs. For example:

Steps In Jaguar Manager, generate stubs as follows:

  1. Make sure the CORBA stubs will be generated to a different package or code base than existing EJB stubs. To change the Java package for CORBA stubs, follow the steps under "Specifying Java package mappings for IDL modules".

  2. Highlight the component icon, or to generate stubs for all components in a Package, highlight the package where the Bean is installed.

  3. Choose File | Generate Stub/Skeleton.

  4. In the Generate Stubs and Skeletons dialog, select both Generate Stubs and Generate Java Stubs, and choose CORBA from the drop-down list of stub types. Uncheck Generate C++ Stubs and Generate Skeletons.

  5. Specify a code base for the generated files. The default is Jaguar's html/classes subdirectory.


Using the home interface

The Java representation of the home interface follows the standard IDL-to-Java language mappings. In Jaguar's interface repository, the EJB FinderException and CreateException exceptions are represented by the IDL exceptions CtsComponents::FinderException and CtsComponents::CreateException, respectively.

Instantiating the home interface To instantiate a home interface, use a SessionManager::Manager instance to create a SessionManager::Session instance, then call the SessionManager::Session::lookup method, passing the Bean's home interface name. Call the narrow method in the helper class for the Bean's home interface to narrow the returned object to the home interface.

In this example, the IDL home interface is bookStore::custMaintenanceHome and the Bean's home interface name is bookStore/custMaintenance:

org.omg.CORBA.Orb orb;

... deleted standard Orb initialization ...

org.omg.CORBA.Object obj = orb.string_to_object(_url);
Manager manager = ManagerHelper.narrow(obj);

Session session = manager.createSession("Guest", "GuestPassword");

// Create an instance of the home interface.

custMaintenanceHome custHome = custMaintenanceHomeHelper.narrow (
session.lookup("bookStore/custMaintenance") );

Instantiating a session Bean The example below instantiates a home interface named bookStore/inventory, then calls the create method that takes no parameters. The IDL home interface type is bookStore::inventoryHome and the remote interface is bookStore::inventory.

SessionManager.Session session;

... deleted code that instantiated session ...

inventoryHome home = inventoryHomeHelper.narrow (
session.lookup(_compName) );

if (home == null)
{
System.out.println("Error: home interface is null.");
return;
}

inventory inv = home.create();

Instantiating an entity Bean This example instantiates an entity Bean that represents a customer credit account. The primary key structure has two members: custName is a string and creditType is also a string. The example looks for a customer named Morry using the findByPrimaryKey method. CtsComponents::FinderException can be thrown to indicate that the requested entity does not exist. In this case, the example calls a create method to create a new entity.

// Obtain an instance of the remote interface. First check 
// to see if the requested customer exists. If not, create
// a new entity.
String _custName = "Morry";
custCreditKey custKey = new custCreditKey();
custKey.custName = _custName;
custKey.creditType = _creditType;
custMaintenance cust;

try
{
System.out.println("Looking for customer " + _custName + " ...");
cust = custHome.findByPrimaryKey(custKey);
}
catch (CtsComponents.FinderException fe)
{
System.out.println("Not found. Creating customer " + _custName + ".");
try
{
cust = custHome.create(_custName, 2000);
}
catch (FinderException fe)
{
System.out.println("Error: could not create customer " + _custName);
}
}

Serializing and deserializing instance references

An EJB 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.

In a CORBA client, you can obtain the same functionality using the Orb.object_to_string and Orb.string_to_object methods. The same restrictions apply when deserializing Bean proxies as apply to any other remote object. See Chapter 13, "Creating CORBA-Compatible Java Clients" for details.

 


Copyright © 2000 Sybase, Inc. All rights reserved.