Invoking EJB components from CORBA Java clients

CORBA Java clients can instantiate an EJB component using a proxy for the EJB component’s home interface, then call business methods using a proxy for the EJB component’s remote interface.

Chapter 12, “Creating CORBA Java Clients,” describes how to use EAServer’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 EAServer client runtime, the EJB and CORBA interfaces offer identical functionality unless EJB 2.0 intervendor interoperability features are required (see “Intervendor EJB interoperability”). In this case, you must use the EJB client interace. If intervendor EJB interoperability is not required, choose the interfaces that you are most comfortable with.

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:

StepsGenerating stubs in EAServer Manager

  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 EJB component is installed.

  3. Choose File | Generate Stub/Skeleton.

  4. In the Generate Stubs and Skeletons wizard, 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 EAServer’s html/classes subdirectory.

Using the home interface

The Java representation of the home interface follows the standard IDL-to-Java language mappings. In EAServer’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 EJB component’s home interface name. Call the narrow method in the helper class for the EJB component’s home interface to narrow the returned object to the home interface.

In this example, the IDL home interface is bookStore::custMaintenanceHome and the EJB component’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 EJB component. 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 EJB component proxies as apply to any other remote object. See Chapter 12, “Creating CORBA Java Clients” for details.