Accessing naming contexts

Why and when to perform this task

Use this task to add code to the source file for a CORBA server to access naming contexts. This code is used to create a new naming context within which the CORBA server can bind servant objects. The code performs the following actions after the server environment has been initialized:

  1. Gets a pointer to the root naming context
  2. Creates a ::CosNaming::Name for the domain and getting a pointer to the domain naming context
  3. Gets a new servant naming context for servant objects

To add the get_naming_context() function to the source file for a CORBA server, edit the server source file, servantServer.cpp, and add the following code:

//
// This function accesses the Name Service and then gets or creates
// the desired naming contexts. It returns the naming context for
// the servant context.
//
::CosNaming::NamingContext_ptr get_naming_context ( ::CORBA::ORB_ptr orbPtr ) 
{
   ::CosNaming::NamingContext_var rootNameContext = NULL;
   ::CosNaming::NamingContext_var domainNameContext = NULL;
   ::CosNaming::NamingContext_ptr servantNameContext = NULL;
   ::CORBA::Object_var objPtr;
   
   // Get access to the Naming Service.
   try
   {
     objPtr = orbPtr->resolve_initial_references ( "NameService" );
   }
   // catch exceptions
   
   // Narrow the returned object to a NamingContext object.
   rootNameContext = ::CosNaming::NamingContext::_narrow ( objPtr );    
   if ( ::CORBA::is_nil(rootNameContext) )
   {
      cerr << "Error: could not narrow the root naming context." << endl;
      return NULL;
   }

   // Create the "domain" Name.
   ::CosNaming::Name name1;
   name1.length(1);
   name1[0].kind = ::CORBA::string_dup("");
   name1[0].id   = ::CORBA::string_dup("domain");

   // Find the "domain" naming context.
   try
   {
      objPtr = rootNameContext->resolve ( name1 );
   }
   // catch exceptions

   // Next, narrow the domain naming context object.
   domainNameContext = ::CosNaming::NamingContext::_narrow(objPtr);
   if ( ::CORBA::is_nil( domainNameContext ) )
   {
      cerr << "Error: could not narrow the domain naming context." << endl;
      return NULL;
   }

   // Create the "servantContext" Name.   
   ::CosNaming::Name name2;
   name2.length(1);
   name2[0].kind = ::CORBA::string_dup("");
   name2[0].id   = ::CORBA::string_dup("servantContext");
   
   // Create the "servantContext" naming context.
   try
   {
      objPtr = domainNameContext->bind_new_context ( name2 );
   }

   // If the servant naming context already exists,
   // then just resolve it...
   catch ( ::CosNaming::NamingContext::AlreadyBound &e )
   {
      cout << "Warning: servant's naming context already exists." << endl;
      cout << "Trying to resolve the context." << endl;
      try
      {
  	 objPtr = domainNameContext->resolve( name2 );
      }
      // catch exceptions ...
   }

   // Next, narrow the new naming context object.
   servantNameContext = ::CosNaming::NamingContext::_narrow(objPtr);
   if ( ::CORBA::is_nil( servantNameContext ) )
   {
      cerr << "Error: could not narrow the servant's naming context." << endl;
      return NULL;
   }

   return servantNameContext;
}

Next, modify the server's main() function so that it calls the get_naming_context() function:

int main( int argc, char *argv[] )
{
   .
   .  initialization code
   .
   
   // Get the naming contexts to which the servant object will be bound.
   ::CosNaming::NamingContext_var servantNameContext = 
      get_naming_context ( orbPtr );

   if ( servantNameContext == NULL )
   {
      cerr << "Error: failed to obtain the name context." << endl;
      return 4;
   }

   .
   .
   .

} 

Results

This task creates and returns a pointer object, servantNameContext, to the naming context for servant objects.

What to do next

Add code to the server source file to name, create, and bind servant objects as described in Adding code to create and bind servant objects.

Related concepts
CORBA server naming contexts
Related tasks
Creating the CORBA server main code (server.cpp)



Searchable topic ID:   tcor_pgms5d
Last updated: Jun 21, 2007 8:07:48 PM CDT    WebSphere Business Integration Server Foundation, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.wasee.doc/info/ee/corba/tasks/tcor_pgms5d.html

Library | Support | Terms of Use | Feedback