CosNaming::NamingContext_ptr TAO_Hash_Naming_Context::bind_new_context (const CosNaming::Name& n) { // Check to make sure this object didn't have <destroy> method // invoked on it. if (this->destroyed_) throw CORBA::OBJECT_NOT_EXIST (); // Get the length of the name. CORBA::ULong name_len = n.length (); // Check for invalid name. if (name_len == 0) throw CosNaming::NamingContext::InvalidName(); // If we received compound name, resolve it to get the context in // which the binding should take place, then perform the operation on // target context. if (name_len > 1) { CosNaming::NamingContext_var context = this->get_context (n); CosNaming::Name simple_name; simple_name.length (1); simple_name[0] = n[name_len - 1]; return context->bind_new_context (simple_name); } // If we received a simple name, we need to bind it in this context. // Stores our new Naming Context. CosNaming::NamingContext_var result = CosNaming::NamingContext::_nil (); // Create new context. result = this->new_context (); // Bind the new context to the name. try { this->bind_context (n, result.in ()); } catch (const CORBA::Exception&) { // If the bind() operation fails we must destroy the recently // created context, should any exceptions be raised by the // destroy() operation we want to ignore them. { try { result->destroy (); } catch (const CORBA::Exception&) { } } // Re-raise the exception in bind_context() throw; } return result._retn (); }
void VOmniORBHelper::nsRegisterObject(CORBA::Object_ptr obj, const char* program, const char* object, int telescopenumber) throw(CORBA::SystemException, CosNaming::NamingContext::NotFound, CosNaming::NamingContext::CannotProceed, CosNaming::NamingContext::InvalidName, CosNaming::NamingContext::AlreadyBound) { ZThread::Guard<ZThread::RecursiveMutex> guard(m_mutex); CosNaming::NamingContext_var root = nsRootContext(); CosNaming::Name_var name = nsPathToObjectName(program, object, telescopenumber); for(unsigned int n=0; n<name->length()-1; n++) { CosNaming::Name_var child_name = name; child_name->length(n+1); try { CORBA::Object_var object = root->resolve(child_name); } catch(CosNaming::NamingContext::NotFound) { CosNaming::NamingContext_var child = root->bind_new_context(child_name); } } root->rebind(name,obj); }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { // Initialize orb CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); //Get reference to Root POA CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" ); PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() ); // Activate POA Manager PortableServer::POAManager_var mgr = poa->the_POAManager(); mgr->activate(); // Find the Naming Service obj = orb->resolve_initial_references("NameService"); CosNaming::NamingContext_var root = CosNaming::NamingContext::_narrow(obj.in()); if (CORBA::is_nil(root.in())) { std::cerr << "Nil Naming Context reference" << std::endl; return 1; } // Bind the example Naming Context, if necessary CosNaming::Name name; name.length( 1 ); name[0].id = CORBA::string_dup("example"); try { obj = root->resolve(name); } catch(const CosNaming::NamingContext::NotFound&) { CosNaming::NamingContext_var dummy = root->bind_new_context(name); } // Bind the Messenger object name.length(2); name[1].id = CORBA::string_dup("Messenger"); // Create an object PortableServer::Servant_var<Messenger_i> servant = new Messenger_i; PortableServer::ObjectId_var oid = poa->activate_object(servant.in()); obj = poa->id_to_reference(oid.in()); Messenger_var messenger = Messenger::_narrow(obj.in()); root->rebind(name, messenger.in()); std::cout << "Messenger object bound in Naming Service" << std::endl; // Accept requests orb->run(); orb->destroy(); } catch(const CORBA::Exception& ex) { std::cerr << "server: Caught a CORBA::Exception: " << ex << std::endl; return 1; } return 0; }
/** Bind a context to the corba root, with a given name. */ void CorbaServerGeneric:: createContext( const std::string & contextStrName ) { try { /* Obtains a reference to the root context of the Name service * and narrows the reference returned. */ CosNaming::NamingContext_var rootContext = CosNaming::NamingContext:: _narrow(orb->resolve_initial_references("NameService")); if( CORBA::is_nil(rootContext) ) { std::cerr << "Failed to narrow the root naming context." << std::endl; throw "TODO"; } /* Create the name of the context. */ CosNaming::Name contextName; contextName.length(1); contextName[0].id = CORBA::string_dup(contextStrName.c_str()); contextName[0].kind = CORBA::string_dup("context"); /* Note on kind: The kind field is used to indicate the type * of the object. This is to avoid conventions such as that used * by files (name.type -- e.g. hpp.ps = postscript etc.). */ /* Bind a context called <contextName> to the root context. */ try{ hppContext = rootContext->bind_new_context(contextName); } catch(CosNaming::NamingContext::AlreadyBound& ex) { /* If the context already exists, this exception will be raised. * In this case, just resolve the name and assign hppContext * to the object returned. */ hppContext = CosNaming::NamingContext:: _narrow(rootContext->resolve(contextName)); if( CORBA::is_nil(hppContext) ) { std::cerr << "Failed to narrow naming context." << std::endl; throw "TODO"; } } } HPP_RETHROW("Creating context"); return; }
/*! * @if jp * @brief 途中のコンテキストを bind しながら Object を rebind する * @else * @brief Bind intermediate context recursively and rebind object * @endif */ void CorbaNaming::rebindRecursive(CosNaming::NamingContext_ptr context, const CosNaming::Name& name, CORBA::Object_ptr obj) throw (SystemException, CannotProceed, InvalidName) { CORBA::ULong len(name.length()); CosNaming::NamingContext_var cxt; cxt = CosNaming::NamingContext::_duplicate(context); for (CORBA::ULong i = 0; i < len; ++i) { if (i == (len - 1)) { cxt->rebind(subName(name, i, i), obj); return; } else { // If the context is not a NamingContext, CannotProceed is thrown if (isNamingContext(cxt)) { try { cxt = cxt->bind_new_context(subName(name, i, i)); } catch (AlreadyBound& e) { (void)(e); cxt = CosNaming:: NamingContextExt:: _narrow(cxt->resolve(subName(name, i, i))); } } else throw CannotProceed(cxt, subName(name, i)); } } return; }
void ServerActivatorImpl::initialize() { try { CORBA::Object_var root_poa_obj = orb_->resolve_initial_references ("RootPOA"); root_poa_ = PortableServer::POA::_narrow (root_poa_obj); } catch (CORBA::ORB::InvalidName&) { std::cerr << "ServerActivatorImpl: Fatal error - no root POA available." << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: Fatal error - cannot narrow root POA." << std::endl; throw CannotInitialize(); } root_poa_manager_ = root_poa_->the_POAManager(); root_poa_manager_->activate(); // // register in name service // CosNaming::NamingContext_var nameService; // // try to get naming service from config values // CORBA::Object_var obj; std::string ns = Qedo::ConfigurationReader::instance()->lookup_config_value( "/General/NameService" ); if( !ns.empty() ) { try { obj = orb_->string_to_object( ns.c_str() ); } catch(...) { std::cerr << "ServerActivatorImpl: can't resolve NameService " << ns << std::endl; throw CannotInitialize(); } std::cout << "ServerActivatorImpl: NameService is " << ns << std::endl; } // // try to get naming service from orb // else { try { obj = orb_->resolve_initial_references( "NameService" ); } catch (const CORBA::ORB::InvalidName&) { std::cerr << "ServerActivatorImpl: can't resolve NameService" << std::endl; throw CannotInitialize(); } if (CORBA::is_nil(obj.in())) { std::cerr << "ServerActivatorImpl: NameService is a nil object reference" << std::endl; throw CannotInitialize(); } } try { nameService = CosNaming::NamingContext::_narrow( obj.in() ); } catch (const CORBA::Exception&) { std::cerr << "ServerActivatorImpl: NameService is not running" << std::endl; throw CannotInitialize(); } if( CORBA::is_nil(nameService.in()) ) { std::cerr << "NameService is not a NamingContext object reference" << std::endl; throw CannotInitialize(); } CORBA::ULong context_offset; if (global_context_used_) { context_offset = 1; } else { context_offset= 0; }; // Create the Qedo and Activators naming context CosNaming::Name current_name; current_name.length (1); if (global_context_used_) { current_name[0].id = CORBA::string_dup(global_context_.c_str()); current_name[0].kind = CORBA::string_dup(""); try { nameService->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // ignore this exception } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Qedo'" << std::endl; throw CannotInitialize(); } } current_name.length (1 + context_offset); current_name[0 + context_offset].id = CORBA::string_dup ("Qedo"); current_name[0 + context_offset].kind = CORBA::string_dup (""); try { nameService->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // Ignore this exception } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Qedo'" << std::endl; throw CannotInitialize(); } current_name.length(2 + context_offset); current_name[1+context_offset].id = CORBA::string_dup ("Activators"); current_name[1+context_offset].kind = CORBA::string_dup (""); try { nameService->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // Ignore this exception } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Activators'" << std::endl; throw CannotInitialize(); } // Now bind this Component Server Activator with the Name Service, use the name Qedo/Activators/<hostname> char hostname[256]; if (gethostname (hostname, 256)) { std::cerr << "ServerActivatorImpl: Cannot determine my hostname" << std::endl; throw CannotInitialize(); } std::cout << "ServerActivatorImpl: Binding Component Server Activator under Qedo/Activators/" << hostname << std::endl; current_name.length (3 + context_offset); current_name[2+context_offset].id = CORBA::string_dup (hostname); current_name[2+context_offset].kind = CORBA::string_dup (""); CORBA::Object_var my_ref = this->_this(); try { nameService->bind (current_name, my_ref); } catch (CosNaming::NamingContext::AlreadyBound&) { try { nameService->rebind (current_name, my_ref); } catch (CosNaming::NamingContext::InvalidName&) { std::cerr << "ServerActivatorImpl: Name Service complains about an invalid name" << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception in rebind()" << std::endl; throw CannotInitialize(); } } catch (CosNaming::NamingContext::InvalidName&) { std::cerr << "ServerActivatorImpl: Name Service complains about an invalid name" << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during bind()" << std::endl; throw CannotInitialize(); } }
int TestTask::svc() { try { // Start the Naming Service tasks namingServiceA_.activate(); // Wait for the Naming Service initialized. namingServiceA_.waitInit(); namingServiceB_.activate(); // Wait for the Naming Service initialized. namingServiceB_.waitInit(); FILE *output_file= ACE_OS::fopen (ior_output_file, "w"); if (output_file == 0) ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output file for writing IOR: ns.ior\n"), 1); ACE_OS::fprintf (output_file, "%s", namingServiceA_.ior ()); ACE_OS::fclose (output_file); // Get reference to Root POA CORBA::Object_var obj = orb_->resolve_initial_references("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in()); // Activate POA Manager PortableServer::POAManager_var mgr = poa->the_POAManager(); mgr->activate(); // Find the Naming Service obj = orb_->string_to_object (namingServiceB_.ior ()); CosNaming::NamingContext_var root = CosNaming::NamingContext::_narrow(obj.in()); if (CORBA::is_nil(root.in())) { ACE_ERROR ((LM_ERROR, "Error, Nil Naming Context reference\n")); return 1; } // Bind the example Naming Context, if necessary CosNaming::NamingContext_var example_nc; CosNaming::Name name; name.length(1); name[0].id = CORBA::string_dup("example"); try { obj = root->resolve(name); example_nc = CosNaming::NamingContext::_narrow(obj.in()); } catch (const CosNaming::NamingContext::NotFound&) { example_nc = root->bind_new_context(name); } // Bind the Test object name.length(2); name[1].id = CORBA::string_dup("Hello"); // Create an object Hello servant(orb_.in ()); PortableServer::ObjectId_var oid = poa->activate_object(&servant); obj = poa->id_to_reference(oid.in()); root->rebind(name, obj.in()); ACE_DEBUG ((LM_INFO, "Hello object bound in Naming Service B\n")); name.length(1); obj = orb_->string_to_object (namingServiceA_.ior ()); root = CosNaming::NamingContext::_narrow(obj.in()); root->bind_context (name, example_nc.in ()); ACE_DEBUG ((LM_INFO, "'example' context of NS B bound in Naming Service A\n")); if (shutdown_ns_) { namingServiceB_.end(); ACE_DEBUG ((LM_INFO, "Naming Service B shut down\n")); } // Create shutdown server NsShutdown shutdown_servant(orb_.in ()); PortableServer::ObjectId_var shutdown_oid = poa->activate_object(&shutdown_servant); obj = poa->id_to_reference(shutdown_oid.in()); CORBA::String_var ior = orb_->object_to_string (obj.in ()); output_file= ACE_OS::fopen (ior_shutdown_file, "w"); if (output_file == 0) ACE_ERROR_RETURN ((LM_ERROR, "Cannot open output file %s for writing IOR: %C\n", ior_shutdown_file, ior.in ()), 1); ACE_OS::fprintf (output_file, "%s", ior.in ()); ACE_OS::fclose (output_file); // Normally we run the orb and the orb is shutdown by // calling TestTask::end(). // Accept requests orb_->run(); orb_->destroy(); // Shutdown the Naming Services. namingServiceA_.end(); if (!shutdown_ns_) { namingServiceB_.end(); } return 0; } catch (CORBA::Exception& ex) { ex._tao_print_exception ("CORBA exception: "); } return -1; }
int TestTask::svc() { try { // Get reference to Root POA CORBA::Object_var obj = orb_->resolve_initial_references ("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow (obj.in ()); // Activate POA Manager PortableServer::POAManager_var mgr = poa->the_POAManager (); mgr->activate (); // Find the Naming Service obj = orb_->string_to_object ( "corbaloc:iiop:1.2@localhost:9932/NameService"); CosNaming::NamingContext_var rootB = CosNaming::NamingContext::_narrow (obj.in ()); if (CORBA::is_nil (rootB.in ())) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error, Nil Naming Context reference\n"))); return 1; } // Bind the example Naming Context, if necessary CosNaming::NamingContext_var example_nc; CosNaming::Name name; name.length(1); name[0].id = CORBA::string_dup( "example"); try { obj = rootB->resolve (name); example_nc = CosNaming::NamingContext::_narrow (obj.in ()); } catch (const CosNaming::NamingContext::NotFound&) { example_nc = rootB->bind_new_context (name); } // Bind the Test object name.length (2); name[1].id = CORBA::string_dup ("Hello"); // Create an object Hello servant (orb_.in ()); PortableServer::ObjectId_var oid = poa->activate_object (&servant); obj = poa->id_to_reference (oid.in ()); rootB->rebind (name, obj.in ()); ACE_DEBUG ((LM_INFO, ACE_TEXT ("Hello object bound in Naming Service B\n"))); name.length (1); name[0].id = CORBA::string_dup ("nsB"); obj = orb_->string_to_object ( "corbaloc:iiop:1.2@localhost:9931/NameService"); CosNaming::NamingContext_var rootA = CosNaming::NamingContext::_narrow (obj.in ()); rootA->bind_context (name, rootB.in ()); ACE_DEBUG ((LM_INFO, ACE_TEXT ("Root context of NS B bound in Naming Service A ") ACE_TEXT ("under name 'nsB'\n"))); CORBA::String_var ior = orb_->object_to_string (obj.in ()); // Output the IOR to the <ior_output_file> FILE *output_file= ACE_OS::fopen (ior_output_file, "w"); if (output_file == 0) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Cannot open output file %s for writing ") ACE_TEXT ("IOR: %C\n"), ior_output_file, ior.in ()), 1); ACE_OS::fprintf (output_file, ACE_TEXT ("%s"), ior.in ()); ACE_OS::fclose (output_file); ACE_DEBUG ((LM_INFO, ACE_TEXT ("Wrote IOR file\n"))); // Normally we run the orb and the orb is shutdown by // calling TestTask::end(). // Accept requests orb_->run(); orb_->destroy(); return 0; } catch (CORBA::Exception& ex) { ex._tao_print_exception (ACE_TEXT ("CORBA exception: ")); } return -1; }
static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref) { CosNaming::NamingContext_var rootContext; try { // Obtain a reference to the root context of the Name service: CORBA::Object_var obj; obj = orb->resolve_initial_references("NameService"); // Narrow the reference returned. rootContext = CosNaming::NamingContext::_narrow(obj); if( CORBA::is_nil(rootContext) ) { cerr << "Failed to narrow the root naming context." << endl; return 0; } } catch (CORBA::NO_RESOURCES&) { cerr << "Caught NO_RESOURCES exception. You must configure omniORB " << "with the location" << endl << "of the naming service." << endl; return 0; } catch (CORBA::ORB::InvalidName&) { // This should not happen! cerr << "Service required is invalid [does not exist]." << endl; return 0; } try { // Bind a context called "test" to the root context: CosNaming::Name contextName; contextName.length(1); contextName[0].id = (const char*) "test"; // string copied contextName[0].kind = (const char*) "my_context"; // string copied // Note on kind: The kind field is used to indicate the type // of the object. This is to avoid conventions such as that used // by files (name.type -- e.g. test.ps = postscript etc.) CosNaming::NamingContext_var testContext; try { // Bind the context to root. testContext = rootContext->bind_new_context(contextName); } catch(CosNaming::NamingContext::AlreadyBound& ex) { // If the context already exists, this exception will be raised. // In this case, just resolve the name and assign testContext // to the object returned: CORBA::Object_var obj; obj = rootContext->resolve(contextName); testContext = CosNaming::NamingContext::_narrow(obj); if( CORBA::is_nil(testContext) ) { cerr << "Failed to narrow naming context." << endl; return 0; } } // Bind objref with name Echo to the testContext: CosNaming::Name objectName; objectName.length(1); objectName[0].id = (const char*) "Echo"; // string copied objectName[0].kind = (const char*) "Object"; // string copied try { testContext->bind(objectName, objref); } catch(CosNaming::NamingContext::AlreadyBound& ex) { testContext->rebind(objectName, objref); } // Note: Using rebind() will overwrite any Object previously bound // to /test/Echo with obj. // Alternatively, bind() can be used, which will raise a // CosNaming::NamingContext::AlreadyBound exception if the name // supplied is already bound to an object. // Amendment: When using OrbixNames, it is necessary to first try bind // and then rebind, as rebind on it's own will throw a NotFoundexception if // the Name has not already been bound. [This is incorrect behaviour - // it should just bind]. } catch(CORBA::TRANSIENT& ex) { cerr << "Caught system exception TRANSIENT -- unable to contact the " << "naming service." << endl << "Make sure the naming server is running and that omniORB is " << "configured correctly." << endl; return 0; } catch(CORBA::SystemException& ex) { cerr << "Caught a CORBA::" << ex._name() << " while using the naming service." << endl; return 0; } return 1; }
void ServerActivatorImpl::initialize() { try { CORBA::Object_var root_poa_obj = orb_->resolve_initial_references ("RootPOA"); root_poa_ = PortableServer::POA::_narrow (root_poa_obj); } catch (CORBA::ORB::InvalidName&) { cerr << "ServerActivatorImpl: Fatal error - no root POA available." << endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { cerr << "ServerActivatorImpl: Fatal error - cannot narrow root POA." << endl; throw CannotInitialize(); } root_poa_manager_ = root_poa_->the_POAManager(); root_poa_manager_->activate(); CosNaming::NamingContext_var ns; // Now try to bind with the Name Service try { CORBA::Object_var ns_obj = orb_->resolve_initial_references ("NameService"); ns = CosNaming::NamingContext::_narrow (ns_obj); } catch (CORBA::ORB::InvalidName&) { cerr << "ServerActivatorImpl: Name Service not found" << endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { cerr << "ServerActivatorImpl: Cannot narrow object reference of Name Service" << endl; throw CannotInitialize(); } if (CORBA::is_nil (ns)) { cerr << "ServerActivatorImpl: Name Service is nil" << endl; throw CannotInitialize(); } // Create the Qedo and Activators naming context CosNaming::Name current_name; current_name.length (1); current_name[0].id = CORBA::string_dup ("Qedo"); current_name[0].kind = CORBA::string_dup (""); try { ns->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // Ignore this exception } catch (CORBA::SystemException&) { cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Qedo'" << endl; throw CannotInitialize(); } current_name.length(2); current_name[1].id = CORBA::string_dup ("Activators"); current_name[1].kind = CORBA::string_dup (""); try { ns->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // Ignore this exception } catch (CORBA::SystemException&) { cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Activators'" << endl; throw CannotInitialize(); } // Now bind this Component Server Activator with the Name Service, use the name Qedo/Activators/<hostname> char hostname[256]; if (gethostname (hostname, 256)) { cerr << "ServerActivatorImpl: Cannot determine my hostname" << endl; throw CannotInitialize(); } cout << "ServerActivatorImpl: Binding Component Server Activator under Qedo/Activators/" << hostname << endl; current_name.length (3); current_name[2].id = CORBA::string_dup (hostname); current_name[2].kind = CORBA::string_dup (""); CORBA::Object_var my_ref = this->_this(); try { ns->bind (current_name, my_ref); } catch (CosNaming::NamingContext::AlreadyBound&) { try { ns->rebind (current_name, my_ref); } catch (CosNaming::NamingContext::InvalidName&) { cerr << "ServerActivatorImpl: Name Service complains about an invalid name" << endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { cerr << "ServerActivatorImpl: CORBA system exception in rebind()" << endl; throw CannotInitialize(); } } catch (CosNaming::NamingContext::InvalidName&) { cerr << "ServerActivatorImpl: Name Service complains about an invalid name" << endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { cerr << "ServerActivatorImpl: CORBA system exception during bind()" << endl; throw CannotInitialize(); } #ifdef HAVE_JTC #else #if _WIN32 // Create an event handle to be used for the notification from the created component server event_handle_ = CreateEvent (NULL, TRUE /*auto-reset*/, FALSE /*initial: non-signaled*/, "QEDO_NOTIFY_EVENT"); if (event_handle_ == NULL) { cerr << "ServerActivatorImpl: ServerActivatorImpl: Cannot create event object for notify_component_server() operation"<< endl; throw CannotInitialize(); } #endif #endif }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { CORBA::ORB_var orb; try { // Initialize orb orb = CORBA::ORB_init (argc, argv); // Get reference to Root POA. CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow (obj.in ()); // Get POA manager PortableServer::POAManager_var poa_mgr = poa->the_POAManager (); // Create a policy list. We use persistent objects with // user-assigned IDs, and explicit activation. CORBA::PolicyList policy_list; policy_list.length (6); policy_list[0] = poa->create_lifespan_policy ( PortableServer::TRANSIENT // REVISIT ); policy_list[1] = poa->create_id_assignment_policy ( PortableServer::USER_ID ); policy_list[2] = poa->create_implicit_activation_policy ( PortableServer::NO_IMPLICIT_ACTIVATION ); policy_list[3] = poa->create_request_processing_policy ( PortableServer::USE_SERVANT_MANAGER ); policy_list[4] = poa->create_servant_retention_policy ( PortableServer::NON_RETAIN ); policy_list[5] = poa->create_thread_policy ( PortableServer::SINGLE_THREAD_MODEL ); // Create a POA for all CCS elements. PortableServer::POA_var ccs_poa = poa->create_POA ("CCS_POA", poa_mgr.in (), policy_list); // Create a controller and set static m_ctrl member // for thermostats and thermometers. Controller_impl ctrl_servant (ccs_poa.in (), "/tmp/CCS_assets"); Thermometer_impl::m_ctrl = &ctrl_servant; // Create a reference for the controller and // create the corresponding CORBA object. PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId (Controller_oid); CORBA::Object_var ctrl = ccs_poa->create_reference_with_id ( oid.in (), "IDL:acme.com/CCS/Controller:1.0" ); // Get reference to initial naming context. CosNaming::NamingContext_var inc = resolve_init<CosNaming::NamingContext> ( orb.in (), "NameService" ); // Attempt to create CCS context. CosNaming::Name n; n.length (1); n[0].id = CORBA::string_dup ("CCS"); try { CosNaming::NamingContext_var nc = inc->bind_new_context (n); } catch (const CosNaming::NamingContext::AlreadyBound &) { // Fine, CCS context already exists. } // Force binding of controller reference to make // sure it is always up-to-date. n.length (2); n[1].id = CORBA::string_dup ("Controller"); inc->rebind (n, ctrl.in ()); // Instantiate the servant locator for devices. PortableServer::ServantManager_var locator = new DeviceLocator_impl (&ctrl_servant); // Set servant locator. ccs_poa->set_servant_manager (locator.in ()); // Activate the POA manager. poa_mgr->activate (); // Accept requests orb->run (); } catch (const CORBA::Exception & e) { std::cerr << "Uncaught CORBA exception: " << e << std::endl; return 1; } catch (...) { assert (0); // Uncaught exception, dump core } return 0; }
bool NameServiceBase::registerName(std::string name, CORBA::Object_ptr obj, bool rebind) { if (name.empty()) { return false; } // extract name without contexts std::string pure_name = name; std::string contexts = ""; std::string::size_type delimiter_pos = name.find_last_of("/"); if (delimiter_pos != std::string::npos) { pure_name = pure_name.replace(0, delimiter_pos + 1, ""); contexts = name.replace(delimiter_pos, std::string::npos, ""); } CosNaming::Name aName; aName.length(1); aName[0].id = CORBA::string_dup(pure_name.c_str()); aName[0].kind = CORBA::string_dup(""); // make sure each context is bound CosNaming::NamingContext_var context = nameService_; while (contexts.length()) { std::string ctx = contexts; delimiter_pos = contexts.find_first_of("/"); if (delimiter_pos != std::string::npos) { ctx = ctx.replace(delimiter_pos, std::string::npos, ""); contexts = contexts.replace(0, delimiter_pos + 1, ""); } else { contexts = ""; } CosNaming::Name contextName; contextName.length(1); contextName[0].id = CORBA::string_dup(ctx.c_str()); contextName[0].kind = CORBA::string_dup(""); try { context = context->bind_new_context(contextName); } catch (const CosNaming::NamingContext::AlreadyBound&) { try { // already bound -> take it CORBA::Object_var dummy; context = CosNaming::NamingContext::_narrow(dummy = context->resolve(contextName)); } catch (...) { std::cerr << ctx << " is probably no context?" << std::endl; return false; } } catch (const CosNaming::NamingContext::NotFound&) { std::cerr << "Got a `NotFound' exception : " << std::endl; return false; } catch (const CosNaming::NamingContext::CannotProceed&) { std::cerr << "Got a `CannotProceed' exception : " << std::endl; return false; } catch (const CosNaming::NamingContext::InvalidName&) { std::cerr << "Got a `InvalidName' exception : " << std::endl; return false; } } // bind the name try { context->bind(aName, obj); } catch(const CosNaming::NamingContext::AlreadyBound&) { // rebind the name if intended if (rebind) { try { context->rebind(aName, obj); } catch (const CosNaming::NamingContext::NotFound&) { std::cerr << "Got a `NotFound' exception : " << std::endl; return false; } catch (const CosNaming::NamingContext::CannotProceed&) { std::cerr << "Got a `CannotProceed' exception : " << std::endl; return false; } catch (const CosNaming::NamingContext::InvalidName&) { std::cerr << "Got a `InvalidName' exception : " << std::endl; return false; } } else { std::cerr << "Got a `InvalidName' exception" << std::endl; return false; } } catch (const CosNaming::NamingContext::NotFound&) { std::cerr << "Got a `NotFound' exception : " << std::endl; return false; } catch (const CosNaming::NamingContext::CannotProceed&) { std::cerr << "Got a `CannotProceed' exception : " << std::endl; return false; } catch (const CosNaming::NamingContext::InvalidName&) { std::cerr << "Got a `InvalidName' exception : " << std::endl; return false; } return true; }
int MessengerTask::svc() { try { // Get reference to Root POA CORBA::Object_var obj = orb_->resolve_initial_references("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow(obj.in()); // Activate POA Manager PortableServer::POAManager_var mgr = poa->the_POAManager(); mgr->activate(); // Find the Naming Service obj = orb_->resolve_initial_references("NameService"); CosNaming::NamingContext_var root = CosNaming::NamingContext::_narrow(obj.in()); if (CORBA::is_nil(root.in())) { std::cerr << "Nil Naming Context reference" << std::endl; return 1; } // Bind the example Naming Context, if necessary CosNaming::Name name; name.length(1); name[0].id = CORBA::string_dup("example"); try { root->resolve(name); } catch(const CosNaming::NamingContext::NotFound&) { root->bind_new_context(name); } // Bind the Messenger object name.length(2); name[1].id = CORBA::string_dup("Messenger"); // Create an object PortableServer::Servant_var<Messenger_i> servant = new Messenger_i; PortableServer::ObjectId_var oid = poa->activate_object(servant.in()); obj = poa->id_to_reference(oid.in()); root->rebind(name, obj.in()); std::cout << "Messenger object bound in Naming Service" << std::endl; // Normally we run the orb and the orb is shutdown by // calling MessengerTask::end(). To simplify the coordination // between the main thread and this Messenger thread, specify // the time period to let the Messenger thread finish by itself. // Accept requests ACE_Time_Value tv(1); orb_->run(tv); orb_->destroy(); return 0; } catch(const CORBA::Exception& ex) { std::cerr << "CORBA exception: " << ex << std::endl; } return -1; }
void ServerActivatorImpl::initialize() { try { CORBA::Object_var root_poa_obj = orb_->resolve_initial_references ("RootPOA"); root_poa_ = PortableServer::POA::_narrow (root_poa_obj); } catch (CORBA::ORB::InvalidName&) { std::cerr << "ServerActivatorImpl: Fatal error - no root POA available." << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: Fatal error - cannot narrow root POA." << std::endl; throw CannotInitialize(); } root_poa_manager_ = root_poa_->the_POAManager(); root_poa_manager_->activate(); CosNaming::NamingContext_var ns; // Now try to bind with the Name Service try { CORBA::Object_var ns_obj = orb_->resolve_initial_references ("NameService"); ns = CosNaming::NamingContext::_narrow (ns_obj); } catch (CORBA::ORB::InvalidName&) { std::cerr << "ServerActivatorImpl: Name Service not found" << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: Cannot narrow object reference of Name Service" << std::endl; throw CannotInitialize(); } if (CORBA::is_nil (ns)) { std::cerr << "ServerActivatorImpl: Name Service is nil" << std::endl; throw CannotInitialize(); } // Create the Qedo and Activators naming context CosNaming::Name current_name; current_name.length (1); current_name[0].id = CORBA::string_dup ("Qedo"); current_name[0].kind = CORBA::string_dup (""); try { ns->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // Ignore this exception } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Qedo'" << std::endl; throw CannotInitialize(); } current_name.length(2); current_name[1].id = CORBA::string_dup ("Activators"); current_name[1].kind = CORBA::string_dup (""); try { ns->bind_new_context (current_name); } catch (CosNaming::NamingContext::AlreadyBound&) { // Ignore this exception } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during binding context 'Activators'" << std::endl; throw CannotInitialize(); } // Now bind this Component Server Activator with the Name Service, use the name Qedo/Activators/<hostname> char hostname[256]; if (gethostname (hostname, 256)) { std::cerr << "ServerActivatorImpl: Cannot determine my hostname" << std::endl; throw CannotInitialize(); } std::cout << "ServerActivatorImpl: Binding Component Server Activator under Qedo/Activators/" << hostname << std::endl; current_name.length (3); current_name[2].id = CORBA::string_dup (hostname); current_name[2].kind = CORBA::string_dup (""); CORBA::Object_var my_ref = this->_this(); try { ns->bind (current_name, my_ref); } catch (CosNaming::NamingContext::AlreadyBound&) { try { ns->rebind (current_name, my_ref); } catch (CosNaming::NamingContext::InvalidName&) { std::cerr << "ServerActivatorImpl: Name Service complains about an invalid name" << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception in rebind()" << std::endl; throw CannotInitialize(); } } catch (CosNaming::NamingContext::InvalidName&) { std::cerr << "ServerActivatorImpl: Name Service complains about an invalid name" << std::endl; throw CannotInitialize(); } catch (CORBA::SystemException&) { std::cerr << "ServerActivatorImpl: CORBA system exception during bind()" << std::endl; throw CannotInitialize(); } }
static void bindObjectToName(CORBA::ORB_ptr orb, const char name[], CORBA::Object_ptr objref) { CosNaming::NamingContext_var rootContext; try { // Obtain a reference to the root context of the name service: CORBA::Object_var obj; obj = orb->resolve_initial_references("NameService"); // Narrow the reference returned. rootContext = CosNaming::NamingContext::_narrow(obj); if(CORBA::is_nil(rootContext)) { cerr << "Failed to narrow the root naming context." << endl; return; } } catch (CORBA::ORB::InvalidName& ex) { // This should not happen! cerr << "Service required is invalid [does not exist]." << endl; return; } try { CosNaming::Name contextName; contextName.length(1); contextName[0].id = (const char*) "corejava"; contextName[0].kind = (const char*) "Context"; CosNaming::NamingContext_var corejavaContext; try { // Bind the context to root. corejavaContext = rootContext->bind_new_context(contextName); } catch (CosNaming::NamingContext::AlreadyBound& ex) { // If the context already exists, this exception will be raised. In this case, just // resolve the name and assign the context to the object returned: CORBA::Object_var obj; obj = rootContext->resolve(contextName); corejavaContext = CosNaming::NamingContext::_narrow(obj); if( CORBA::is_nil(corejavaContext) ) { cerr << "Failed to narrow naming context." << endl; return; } } // Bind objref with given name to the context: CosNaming::Name objectName; objectName.length(1); objectName[0].id = name; objectName[0].kind = (const char*) "Object"; try { corejavaContext->bind(objectName, objref); } catch (CosNaming::NamingContext::AlreadyBound& ex) { corejavaContext->rebind(objectName, objref); } } catch (CORBA::COMM_FAILURE& ex) { cerr << "Caught system exception COMM_FAILURE--unable to contact the naming service." << endl; } catch (CORBA::SystemException&) { cerr << "Caught a CORBA::SystemException while using the naming service." << endl; } }