Invoking EJB components from CORBA C++ clients

CORBA C++ 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 15, “Creating CORBA C++ Clients,” describes how to use EAServer’s C++ client ORB. This chapter provides new information on implementing clients that call EJB component methods.

Supported datatypes

C ++ clients can call methods that are defined using only IDL datatypes. EAServer allows serializable Java classes to be used as parameters and return values. Methods that use Java classes as a parameter or return value cannot be called from C++ clients.

Generating C++ header files

StepsGenerating C++ stubs for the EJB component’s interfaces

  1. Highlight the component icon, or, to generate stubs for all components in a package, highlight the package in which the EJB component is installed.

  2. Choose File | Generate Stub/Skeleton.

  3. In the Generate Stubs and Skeletons wizard, check Generate Stubs and check Generate C++ stubs. Unselect Generate Java Stubs and Generate Skeletons. Specify a location for the generated files. The default is the EAServer include subdirectory.

EAServer Manager generates a header file for each IDL module that defines an interface or type used by the component. All class and type definitions are generated as inline code, so you need not compile the header files separately.

In the case of nested IDL modules, EAServer Manager generates a separate file for each nested module, following this naming pattern:

OuterModule::Module1::Module2::InnerModule

In this case, OuterModule includes Module1 which includes Module2 which includes InnerModule.

For example, for the IDL interfaces com::foo::interfaces::MyInterface and com::foo::interfaces::MyHomeInterface, these files are generated:

In your client program, you must include only those header file that define types or interfaces used by your program. For example, if you use the com::foo::interfaces::MyInterface and com::foo::interfaces::MyHomeInterface types, you must include com_foo_interfaces.hpp.

Using the home interface

The C++ representation of the home interface follows the standard IDL-to-C++ 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 a proxy for 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. Narrow the returned object to the C++ class for the EJB component’s home interface.

In this example, the IDL home interface is bookStore::custMaintenanceHome and the EJB component’s home interface name is bookStore/custMaintenance:

 // Initialize the ORB
CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, 0);

// Obtain a SessionManager::Manager instance using the URL:
CORBA::Object_var obj = 
    orb->string_to_object(url);
SessionManager::Manager_var manager = 
  SessionManager::Manager::_narrow(obj);

// Create an authenticated session for user Guest
// using password GuestPassword
SessionManager::Session_var session = 
    manager->createSession("Guest", "GuestPassword");
// Look up the EJB component's home interface
obj = session->lookup(component_name);
bookStore::custMaintenanceHome_var home 
    = bookStore::custMaintenanceHome::_narrow(obj);

Instantiating a session bean To instantiate a session bean, call one of the home interface create methods as shown in the example below. All create methods can raise CtsComponents::CreateException. The example below instantiates the home of a bean with home interface name bookStore/inventory. The IDL remote interface is bookStore::inventory:

try { 
    bookStore::inventory_var inventory = home->create();
}
catch (CtsComponents::CreateException &ce) 
{
    cout << "CreateException for component " << component_name << "\n"
         << "Message:" << ce.message << "\n";
}

Instantiating an entity bean An entity bean represents a row in a database relation. In the home interface, create methods create a row in the database, and finder methods return one or more instances that represent existing rows. All create methods can raise CtsComponents::CreateException, and finder methods can raise CtsComponents::FinderException. The example below first tries to find an existing row using findByPrimaryKey, and creates a row if CtsComponents::FinderException is thrown. The entity bean in this example represents customer credit data. The primary key, bookStore::custCreditKey, has two string fields, custName and creditType. The IDL remote interface is bookStore::custMaintenance:

// Initialize a primary key for the bean
bookStore::custCreditKey custPk;
custPk.custName = CORBA::string_dup(customer_name); 
custPk.creditType = CORBA::string_dup(credit_type);

bookStore::custMaintenance_var customer;
long balance = 2000; 

// Look for an existing instance.
try {  
    cout << "Looking for customer named " << customer_name << "\n";
    customer = home->findByPrimaryKey(custPk);
} catch (CtsComponents::FinderException &fe)
{
    // Instance does not exist. Create it.
    cout << "Customer " << customer_name << " does not exist. "
        << "Creating " << customer_name << " with initial balance of "
        << balance << ".\n";
    customer = home->create(customer_name, balance);
}catch (CtsComponents::FinderException &fe)
{
    cout << "Error creating account for customer " << customer_name ;
}

Serializing and deserializing instance references

An EJB client is allowed to 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 that apply to any other remote object. See Chapter 15, “Creating CORBA C++ Clients” for details.