Exemplo n.º 1
0
CORBA::Object_ptr
DAnCE_TargetManager_Module::init (CORBA::ORB_ptr orb,
                                  int argc,
                                  ACE_TCHAR *argv[])
{
  DANCE_TRACE ("DAnCE_TargetManager_Module::init");

  try
    {
      if (CORBA::is_nil(orb))
        {
          DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR, (LM_ERROR, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                       ACE_TEXT ("Attempted to create Target Manager with a nil orb.\n")));
          return CORBA::Object::_nil();
        }
      else
        {
          this->orb_ = CORBA::ORB::_duplicate (orb);
        }

      if (ACE_OS::strcmp(orb->id(), this->orb_->id()) != 0)
        {
          DANCE_TRACE_LOG (DANCE_LOG_TRACE, (LM_TRACE, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                                             ACE_TEXT ("Resetting TM's orb.\n")));
          this->orb_ = CORBA::ORB::_duplicate (orb);
          this->domain_nc_ = CosNaming::NamingContext::_nil();
        }

      if (!this->parse_args (argc, argv))
        {
          return CORBA::Object::_nil ();
        }

      this->create_poas ();

      if (this->options_.domain_nc_)
        {
          try
            {
              DANCE_TRACE_LOG (DANCE_LOG_TRACE, (LM_TRACE, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                           ACE_TEXT ("Resolving DomainNC.\n")));
              CORBA::Object_var domain_obj = this->orb_->string_to_object (this->options_.domain_nc_);
              if (!CORBA::is_nil (domain_obj.in ()))
                {
                  this->domain_nc_ = CosNaming::NamingContext::_narrow (domain_obj.in());
                  if (CORBA::is_nil (this->domain_nc_.in ()))
                    {
                      DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR, (LM_ERROR,DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                                    ACE_TEXT ("Narrow to NamingContext return nil for DomainNC.\n")));
                      return CORBA::Object::_nil ();
                    }
                }
            }
          catch (CORBA::Exception&)
            {
              DANCE_DEBUG (DANCE_LOG_ERROR,
                           (LM_WARNING, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                             ACE_TEXT ("DomainNC context not found!\n")));
            }
        }


      DANCE_TRACE_LOG (DANCE_LOG_TRACE, (LM_TRACE, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                    ACE_TEXT ("Initializing the IOR Table\n")));
      // Initialize IOR table
      CORBA::Object_var table_object = orb->resolve_initial_references ("IORTable");

      IORTable::Table_var adapter = IORTable::Table::_narrow (table_object.in ());

      if (CORBA::is_nil (adapter.in ()))
        {
          DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR, (LM_ERROR, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                         ACE_TEXT ("Unable to RIR the IORTable.\n")));
          return CORBA::Object::_nil ();
        }


      //Creating repository manager servant
      DAnCE::TargetManagerDaemon_i * rm = new DAnCE::TargetManagerDaemon_i (orb);

      PortableServer::ServantBase_var safe_svt (rm);

      ACE_CString repository_manager_oid;

      if (this->options_.name_ == 0)
        {
          repository_manager_oid = "TargetManager";
        }
      else
        {
          repository_manager_oid = ACE_TEXT_ALWAYS_CHAR (this->options_.name_);
          repository_manager_oid += ".TargetManager";
        }

      // Registering servant in poa
      PortableServer::ObjectId_var oid =
        PortableServer::string_to_ObjectId (repository_manager_oid.c_str());
      this->rm_poa_->activate_object_with_id (oid, rm);

      // Getting repository manager ior
      CORBA::Object_var nm_obj = this->rm_poa_->id_to_reference (oid.in ());
      CORBA::String_var ior = orb->object_to_string (nm_obj.in ());

      // Binding ior to IOR Table
      adapter->bind (repository_manager_oid.c_str (), ior.in ());

      // Binding repository manager to DomainNC
      if (!CORBA::is_nil (this->domain_nc_.in ()))
        {
          ACE_CString ns_name;
          if (this->options_.name_ == 0)
            {
              ns_name = "TargetManager";
            }
          else
            {
              ns_name = ACE_TEXT_ALWAYS_CHAR (this->options_.name_);
            }

          DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                       (LM_TRACE, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                       ACE_TEXT ("Registering NM in NC as \"%C\".\n"), ns_name.c_str ()));
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (ns_name.c_str ());
          name[0].kind = CORBA::string_dup ("TargetManager");
          this->domain_nc_->rebind (name, nm_obj.in ());
        }

      // Writing ior to file
      if (0 != this->options_.ior_file_)
        {
          DANCE_DEBUG (DANCE_LOG_MINOR_EVENT, (LM_TRACE,  DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                        ACE_TEXT ("Writing RM IOR %C to file %C.\n"), this->options_.ior_file_, ior.in ()));
          if (!DAnCE::Target_Manager::write_IOR (this->options_.ior_file_, ior.in ()))
            DANCE_ERROR (DANCE_LOG_ERROR, (LM_ERROR, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                          ACE_TEXT ("Error: Unable to write IOR to file %C\n"),
                          this->options_.ior_file_));
        }

      // Activate POA manager
      PortableServer::POAManager_var mgr = this->root_poa_->the_POAManager ();
      mgr->activate ();

      // Finishing Deployment part
      DANCE_DEBUG (DANCE_LOG_MAJOR_EVENT, (LM_NOTICE, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                    ACE_TEXT ("DAnCE_TargetManager is running...\n")));

      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO, (LM_DEBUG, DLINFO ACE_TEXT ("DAnCE_TargetManager_Module::init - ")
                    ACE_TEXT ("TargetManager IOR: %s\n"), ior.in ()));

      return nm_obj._retn ();
    }
  catch (const CORBA::Exception& ex)
    {
      DANCE_ERROR (DANCE_LOG_TERMINAL_ERROR,
                   (LM_EMERGENCY,
                    ACE_TEXT ("Caught CORBA Exception: %C\n"),
                    ex._info ().c_str ()));
      return CORBA::Object::_nil ();
    }
}
Exemplo n.º 2
0
CORBA::Object_ptr
DAnCE_NodeManager_Module::init (CORBA::ORB_ptr orb,
                                int argc,
                                ACE_TCHAR *argv[])
{
  try
    {
      if (CORBA::is_nil(orb))
        {
          DANCE_ERROR (DANCE_LOG_EMERGENCY,
                       (LM_ERROR, DLINFO
                        ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT("Attempted to create Node Manager with a nil orb.\n")));
          return CORBA::Object::_nil();
        }

      if (CORBA::is_nil(this->orb_.in()))
        {
          DANCE_TRACE_LOG (DANCE_LOG_DETAILED_TRACE,
                           (LM_TRACE, DLINFO ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                            ACE_TEXT("NM's orb is nil.\n")));
          this->orb_ = CORBA::ORB::_duplicate (orb);
        }
      else if (ACE_OS::strcmp(orb->id(), this->orb_->id()) != 0)
        {
          DANCE_DEBUG (DANCE_LOG_DETAILED_TRACE,
                       (LM_TRACE, DLINFO
                        ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT("Resetting NM's orb.\n")));
          this->orb_ = CORBA::ORB::_duplicate (orb);
          this->domain_nc_ = CosNaming::NamingContext::_nil();
        }

      if (!this->parse_args (argc, argv))
        {
          DANCE_ERROR (DANCE_LOG_EMERGENCY,
                       (LM_ERROR, DLINFO
                        ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT("Unable to parse the commandline arguments.\n")));
          return CORBA::Object::_nil ();
        }

      this->register_value_factories ();
      this->create_poas ();

      if (this->options_.domain_nc_)
        {
          try
            {
              DANCE_TRACE_LOG (DANCE_LOG_TRACE,
                               (LM_TRACE, DLINFO ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                                ACE_TEXT("Resolving DomainNC.\n")));
              CORBA::Object_var domain_obj = this->orb_->string_to_object (this->options_.domain_nc_);
              if (!CORBA::is_nil (domain_obj.in ()))
                {
                  this->domain_nc_ = CosNaming::NamingContext::_narrow (domain_obj.in());
                  if (CORBA::is_nil (this->domain_nc_.in ()))
                    {
                      DANCE_ERROR (DANCE_LOG_EMERGENCY,
                                   (LM_ERROR, DLINFO
                                    ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                                    ACE_TEXT("Narrow to NamingContext return nil for DomainNC.\n")));
                      return CORBA::Object::_nil ();
                    }
                }
            }
          catch (const CORBA::Exception&)
            {
              DANCE_ERROR (DANCE_LOG_WARNING, (LM_WARNING, DLINFO
                                               ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                                               ACE_TEXT("DomainNC context not found!\n")));
            }
        }

      DANCE_TRACE_LOG (DANCE_LOG_TRACE,
                       (LM_TRACE, DLINFO
                        ACE_TEXT("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT("Resolving plan NS naming context.\n")));

      CosNaming::NamingContext_var rebind_nc;
      if (this->options_.rebind_plan_ns_)
        {
          if (!this->options_.create_plan_ns_)
            {
              DANCE_ERROR (DANCE_LOG_EMERGENCY,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                            ACE_TEXT ("Rebind_plan_ns option is enabled but Create_plan_ns is not.\n")));
              return CORBA::Object::_nil();
            }
          CORBA::Object_var tmp_obj;
          if (0 != this->options_.rebind_plan_ns_ior_)
            {
              tmp_obj = orb->string_to_object (this->options_.rebind_plan_ns_ior_);
              rebind_nc = CosNaming::NamingContext::_narrow (tmp_obj);
            }
          else
            {
              if (CORBA::is_nil (this->domain_nc_.in ()))
                {
                  DANCE_ERROR (DANCE_LOG_EMERGENCY,
                               (LM_ERROR, DLINFO
                                ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                                ACE_TEXT ("The rebind plan is enabled but neither NC ")
                                ACE_TEXT ("nor DomainNC are not supplied. No rebinding will be done.\n")
                                ACE_TEXT ("Use the \"-rebind-plan-ns NC ior\" ")
                                ACE_TEXT ("or \"-ORBInitRef DomainNC\" option.\n")));
                  return CORBA::Object::_nil ();
                }
              rebind_nc = CosNaming::NamingContext::_duplicate (this->domain_nc_.in ());
            }
        }

      // Initialize IOR table
      CORBA::Object_var table_object = orb->resolve_initial_references ("IORTable");

      IORTable::Table_var adapter = IORTable::Table::_narrow (table_object.in ());

      if (CORBA::is_nil (adapter.in ()))
        {
          DANCE_ERROR (DANCE_LOG_EMERGENCY,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT ("Unable to RIR the IORTable.\n")));
          return CORBA::Object::_nil ();
        }

      // Creating in process nameservice
      CosNaming::NamingContext_var plan_nc;
      if (this->options_.create_plan_ns_)
        {
          DANCE_DEBUG (DANCE_LOG_TRACE,
                       (LM_TRACE, DLINFO
                        ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT ("Resolving plan-specific naming context.\n")));
          CORBA::Object_var naming_obj;
          if (0 != this->options_.create_plan_ns_ior_)
            {
              naming_obj = orb->string_to_object (this->options_.create_plan_ns_ior_);
              plan_nc = CosNaming::NamingContext::_narrow (naming_obj.in());
              if (CORBA::is_nil (plan_nc.in()))
                {
                  DANCE_ERROR (DANCE_LOG_EMERGENCY,
                               (LM_ERROR, DLINFO
                                ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                                ACE_TEXT ("Failed trying to narrow naming context ")
                                ACE_TEXT ("for dance creating plan NC.\n")
                                ACE_TEXT ("Use the \"--create-plan-ns NC ior\" ")
                                ACE_TEXT ("or \"--process-ns\" option.\n")));
                  return CORBA::Object::_nil ();
                }
            }
          else if (this->options_.process_ns_)
            {
              naming_obj = orb->resolve_initial_references ("NameService");
              if (0 != this->options_.process_ns_file_)
                {
                  CORBA::String_var ior = orb->object_to_string (naming_obj.in ());
                  DAnCE::Node_Manager::write_IOR (this->options_.process_ns_file_, ior.in ());
                }
              plan_nc = CosNaming::NamingContext::_narrow (naming_obj.in ());
              if (CORBA::is_nil (plan_nc.in ()))
                {
                  DANCE_ERROR (DANCE_LOG_EMERGENCY,
                               (LM_ERROR, DLINFO
                                ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                                ACE_TEXT ("Fails trying to narrow naming context ")
                                ACE_TEXT ("for dance creating plan NC.\n")
                                ACE_TEXT ("Use the \"--create-plan-ns NC ior\" ")
                                ACE_TEXT ("or \"--process-ns\" option.\n")));
                  return CORBA::Object::_nil ();
                }
            }
          else if (CORBA::is_nil (this->domain_nc_.in ()))
            {
              DANCE_ERROR (DANCE_LOG_EMERGENCY,
                           (LM_ERROR, DLINFO
                            ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                            ACE_TEXT ("The create plan is enabled but neither NC ior ")
                            ACE_TEXT ("nor --process-ns or DomainNC option are not supplied.\n")
                            ACE_TEXT ("Use the \"--create-plan-ns NC ior\", \"--process-ns\" ")
                            ACE_TEXT ("or \"-ORBInitRef DomainNC\" option.\n")));
              return CORBA::Object::_nil ();
            }
          else
            {
              plan_nc = CosNaming::NamingContext::_duplicate (this->domain_nc_.in ());
            }
        }

      // Parsing Node name and node manager ior file name
      ACE_TString node_name = this->options_.node_managers_[0].c_str ();
      ACE_TString node_file;
      size_t npos = node_name.find ('=');
      if (ACE_CString::npos != npos)
        {
          node_file = node_name.substring (npos + 1, node_name.length() - npos + 1);
          node_name = node_name.substring (0, npos);
        }

      // load service modules for artifact installation service
      load_artifact_installation_modules (argc, argv);

      // Creating in process artifact installation service
      DAnCE::ArtifactInstallation_Impl* installer = 0;
      ACE_NEW_RETURN (installer,
                      DAnCE::ArtifactInstallation_Impl (),
                      CORBA::Object::_nil ());
      PortableServer::Servant_var<DAnCE::ArtifactInstallation_Impl> safe_installer (installer);

      ACE_CString installer_oid_str = ACE_TEXT_ALWAYS_CHAR ((node_name + ACE_TEXT (".ArtifactInstaller")).c_str ());

      // Registering servant in poa
      PortableServer::ObjectId_var installer_oid =
        PortableServer::string_to_ObjectId (installer_oid_str.c_str());
      this->nm_poa_->activate_object_with_id (installer_oid, safe_installer._retn ());

      CORBA::Object_var installer_object = this->nm_poa_->id_to_reference (installer_oid.in ());
      this->installer_ = DAnCE::ArtifactInstallation::_narrow (installer_object.in ());

      // Getting node manager ior
      CORBA::String_var installer_ior = orb->object_to_string (installer_object.in ());

      // Binding ior to IOR Table
      adapter->bind (installer_oid_str.c_str (), installer_ior.in ());

      // Make sure that we have only one Node Manager
      if (this->options_.node_managers_.size () != 1)
        {
          DANCE_ERROR (DANCE_LOG_EMERGENCY,
                       (LM_ERROR, DLINFO
                        ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT ("For now only one node manager creation is supported.\n")));
          return CORBA::Object::_nil ();
        }

      DANCE_DEBUG (DANCE_LOG_MINOR_EVENT,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                    ACE_TEXT ("DAnCE_NodeManager::run_main - creating NodeManager for node %C\n"),
                    this->options_.node_managers_[0].c_str()));

      //Creating node manager servant
      DAnCE::NodeManager_Impl * nm = 0;
      if (this->nm_map_.find (node_name, nm) == -1)
        {
          DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                       (LM_TRACE, DLINFO
                        ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT ("Allocating new NodeManager servant instance for NodeManager\n")));
          int size = 64;
          DAnCE::Utility::PROPERTY_MAP properties (size);
          this->create_nm_properties (properties);

          ACE_NEW_RETURN (nm,
                          DAnCE::NodeManager_Impl (orb,
                                                   this->root_poa_.in (),
                                                   this->installer_.in (),
                                                   ACE_TEXT_ALWAYS_CHAR (node_name.c_str()),
                                                   this->options_.node_config_,
                                                   properties),
                          CORBA::Object::_nil ());
          DANCE_TRACE_LOG (DANCE_LOG_TRACE,
                           (LM_TRACE, DLINFO
                            ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                            ACE_TEXT ("New NodeManager servant instance for NodeManager allocated.\n")));
          this->nm_map_.bind (node_name, nm);
        }

      ACE_CString node_manager_oid = ACE_TEXT_ALWAYS_CHAR ((node_name + ACE_TEXT (".NodeManager")).c_str ());

      // Registering servant in poa
      PortableServer::ObjectId_var oid =
        PortableServer::string_to_ObjectId (node_manager_oid.c_str());
      this->nm_poa_->activate_object_with_id (oid, nm);

      // Getting node manager ior
      CORBA::Object_var nm_obj = this->nm_poa_->id_to_reference (oid.in ());
      CORBA::String_var ior = orb->object_to_string (nm_obj.in ());

      // Binding ior to IOR Table
      adapter->bind (node_manager_oid.c_str (), ior.in ());

      // Binding node manager to DomainNC
      if (!CORBA::is_nil (this->domain_nc_.in ()))
        {
          DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                       (LM_TRACE, DLINFO
                        ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT ("Registering NM in NC as \"%C.NodeManager\".\n"), node_name.c_str ()));
          CosNaming::Name name (1);
          name.length (1);
          name[0].id = CORBA::string_dup (ACE_TEXT_ALWAYS_CHAR (node_name.c_str ()));
          name[0].kind = CORBA::string_dup ("NodeManager");
          this->domain_nc_->rebind (name, nm_obj.in ());
        }

      // Writing ior to file
      if (0 != node_file.length ())
        {
          DANCE_DEBUG (DANCE_LOG_EVENT_TRACE,
                       (LM_TRACE,  DLINFO
                        ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                        ACE_TEXT ("Writing node IOR %C to file %C.\n"), node_file.c_str (), ior.in ()));
          if (!DAnCE::Node_Manager::write_IOR (node_file.c_str (), ior.in ()))
            DANCE_ERROR (DANCE_LOG_NONFATAL_ERROR,
                         (LM_ERROR, DLINFO
                          ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                          ACE_TEXT ("Error: Unable to write IOR to file %C\n"),
                          node_file.c_str ()));
        }

      // Activate POA manager
      PortableServer::POAManager_var mgr = this->root_poa_->the_POAManager ();
      mgr->activate ();

      // Finishing Deployment part
      DANCE_DEBUG (DANCE_LOG_MAJOR_EVENT,
                   (LM_NOTICE, DLINFO
                    ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                    ACE_TEXT ("DAnCE_NodeManager is running...\n")));

      DANCE_DEBUG (DANCE_LOG_MAJOR_DEBUG_INFO,
                   (LM_DEBUG, DLINFO
                    ACE_TEXT ("DAnCE_NodeManager_Module::init - ")
                    ACE_TEXT ("NodeManager IOR: %C\n"), ior.in ()));

      return nm_obj._retn ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("DAnCE_NodeManager::main\t\n");
      return CORBA::Object::_nil ();
    }
}