The Initialization Service
When a CORBA application starts up, it must get references to a basic set of initial objectsfor example, the ORB, the object adapter, and the naming service. The CORBA standard defines the CORBA initialization service to take care of this task.
At the beginning of every CORBA application, you will see the initialization service being used to initialize the ORB. It is a simple service that declares just a few associated operations, given in Table 3.
Table 3 Initialization Service Operations
Operation Name |
Description |
CORBA::ORB_init() |
Returns an object reference to the CORBA::ORB object |
CORBA::ORB::resolve_initial_references() |
Returns an object reference to the named service |
CORBA::ORB::list_initial_services() |
Returns a list of services available from this ORB instance |
The signatures of these operations depend on the details of the language mapping. The pseudo-IDL for these operations is given at the end of this chapter.
The operations are illustrated by example in the rest of this chapter.
A common use of the initialization service is to get hold of a reference to an initial naming context. Once an application has a reference to the initial naming context, it can resolve references to numerous other CORBA and application-specific services.
Typical steps followed by a CORBA application during its initialization phase are:
Get a reference to an ORB object using CORBA::ORB_init().
Get a reference to a POA object using CORBA::ORB::resolve_initial_references() (server only).
Get a reference to a NamingContext object (or a NamingContextExt object) using the operation CORBA::ORB::resolve_initial_references().
These initialization steps are demonstrated in Listing 1 and Listing 2 for C++ and Java.
Listing 1: C++ Obtaining Initial Reference to the Naming Service
//C++ int main (int argc, char *argv[]) { CORBA::ORB_var orbV; try { cout << "Initializing the ORB" << endl; //-------------------------------------------------------- // Step 1. Get reference to ORB //-------------------------------------------------------- orbV = CORBA::ORB_init(argc, argv); if (CORBA::is_nil(orbV.in() )) { cerr << "Nil ORB object reference" << endl; return 1; } //-------------------------------------------------------- // Step 2. (Server only) Get reference to POA //-------------------------------------------------------- CORBA::Object_var objV; PortableServer::POA_var poaV; objV = orbV->resolve_initial_references("RootPOA"); poaV = PortableServer::POA::_narrow(objV.in() ); if (CORBA::is_nil(poaV.in() )) { cerr << "Nil POA object reference" << endl; return 1; } //-------------------------------------------------------- // Step 3. Get reference to CosNaming::NamingContextExt //-------------------------------------------------------- CosNaming::NamingContextExt_var rootContextExtV; // INS Root ContextExt try { objV = orbV->resolve_initial_references("NameService"); rootContextExtV = CosNaming::NamingContextExt:: _narrow(objV.in() ); } catch (CORBA::SystemException &sysEx) { cerr << sysEx << endl; return 1; } if (CORBA::is_nil(rootContextExtV.in() )) { cerr << "Nil root naming context" << endl; return 1; } // Now make use of 'rootContextExtV'... ... }
Listing 2: Java Obtaining Initial Reference to the Naming Service
//Java import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.PortableServer.POAManagerPackage.*; public class Server { private static ORB m_orb; public static void main(String args[]) { //-------------------------------------------------------- // Step 1. Get reference to ORB //-------------------------------------------------------- m_orb = ORB.init(args,null); //-------------------------------------------------------- // Step 2. (Server only) Get reference to POA //-------------------------------------------------------- org.omg.CORBA.Object obj = null; org.omg.PortableServer.POA poa = null; try { obj = m_orb.resolve_initial_references("RootPOA"); poa = org.omg.PortableServer.POAHelper.narrow(obj); } catch (org.omg.CORBA.SystemException ex) { System.err.println("error: failed to get reference to POA" + ex); System.exit(1); } catch (org.omg.CORBA.UserException ux) { System.err.println("error: failed to get reference to POA" + ux); System.exit(1); } //-------------------------------------------------------- // Step 3. Get reference to CosNaming::NamingContextExt //-------------------------------------------------------- NamingContextExt rootContextExt=null; try { obj = m_orb.resolve_initial_references("NameService"); rootContextExt = NamingContextExtHelper.narrow(obj); } catch (org.omg.CORBA.SystemException ex) { System.err.println(ex); System.exit(1); } catch (org.omg.CORBA.UserException ux) { System.err.println(ux); System. exit(1); } } }
The function resolve_initial_references() is used twice. The first time it is invoked with the argument RootPOA. The second time it is invoked with the argument NameService. In each case, the returned object reference has to be cast to the correct type using a narrow function (_narrow() in C++ and narrow() in Java).
TIP
In C++, always test an object reference for nilness with the CORBA::is_nil() function after it has been returned from a _narrow() method.
The argument passed to resolve_initial_references() is known as an ObjectId (this choice of name is unfortunateit has nothing to do with object IDs in the context of the POA). The ObjectId string is used to select a particular service type and determines the type of object reference returned by resolve_initial_references(). Allowable values for ObjectId are specified by the OMG. Some of these values are given in Table 4.
Table 4 Some OMG-Defined Values for ObjectId
ObjectId String |
Type of Reference Returned |
RootPOA |
PortableServer::POA |
POACurrent |
PortableServer::Current |
NameService |
CosNaming::NamingContextExt or CosNaming::NamingContext |
TradingService |
CosTrading::Lookup |
InterfaceRepository |
CORBA::Repository |
SecurityCurrent |
SecurityLevel1::Current or SecurityLevel2::Current |
TransactionCurrent |
CosTransactions::Current |
DynAnyFactory |
DynamicAny::DynAnyFactory |
The list in Table 4 may be extended from time to time as the OMG adds new services to the CORBA specification.
The naming service is a special case, since the type of object returned depends on the naming service version. If it conforms to the CORBA Interoperable Naming Service specification, you will obtain a reference to a CosNaming::NamingContextExt object. Alternatively, if it conforms to the older naming service specification, you will obtain a reference to a CosNaming::NamingContext object instead.