From f4d8b4a6d49a0d2a428e90f5df75f592fcd10766 Mon Sep 17 00:00:00 2001 From: ribes Date: Wed, 1 Apr 2009 14:22:28 +0000 Subject: [PATCH] Adding a new env var: USER_CATALOG_RESOURCES_FILE that permits to have a user Catalog resources file --- src/ResourcesManager/ResourcesManager.cxx | 134 +++++++++++------- src/ResourcesManager/ResourcesManager.hxx | 10 +- .../SALOME_ResourcesCatalog_Handler.cxx | 2 +- .../SALOME_ResourcesCatalog_Parser.hxx | 1 + 4 files changed, 91 insertions(+), 56 deletions(-) diff --git a/src/ResourcesManager/ResourcesManager.cxx b/src/ResourcesManager/ResourcesManager.cxx index 5bcf6d843..5b068ea31 100644 --- a/src/ResourcesManager/ResourcesManager.cxx +++ b/src/ResourcesManager/ResourcesManager.cxx @@ -45,9 +45,9 @@ using namespace std; //============================================================================= ResourcesManager_cpp:: -ResourcesManager_cpp(const char *xmlFilePath) : - _path_resources(xmlFilePath) +ResourcesManager_cpp(const char *xmlFilePath) { + _path_resources.push_back(xmlFilePath); #if defined(_DEBUG_) || defined(_DEBUG) cerr << "ResourcesManager_cpp constructor" << endl; #endif @@ -69,25 +69,33 @@ ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException) #if defined(_DEBUG_) || defined(_DEBUG) cerr << "ResourcesManager_cpp constructor" << endl; #endif - _isAppliSalomeDefined = (getenv("APPLI") != 0); - if(!getenv("KERNEL_ROOT_DIR")) - throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!!"); - if (_isAppliSalomeDefined) + std::string default_file(""); + if (getenv("APPLI") != 0) { - _path_resources = getenv("HOME"); - _path_resources += "/"; - _path_resources += getenv("APPLI"); - _path_resources += "/CatalogResources.xml"; + default_file += getenv("HOME"); + default_file += "/"; + default_file += getenv("APPLI"); + default_file += "/CatalogResources.xml"; + _path_resources.push_back(default_file); } - else { - _path_resources = getenv("KERNEL_ROOT_DIR"); - _path_resources += "/share/salome/resources/kernel/CatalogResources.xml"; + if(!getenv("KERNEL_ROOT_DIR")) + throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!! -> cannot load a CatalogResources.xml"); + default_file = getenv("KERNEL_ROOT_DIR"); + default_file += "/share/salome/resources/kernel/CatalogResources.xml"; + _path_resources.push_back(default_file); } - ParseXmlFile(); + if (getenv("USER_CATALOG_RESOURCES_FILE") != 0) + { + std::string user_file(""); + user_file = getenv("USER_CATALOG_RESOURCES_FILE"); + _path_resources.push_back(user_file); + } + + ParseXmlFiles(); #if defined(_DEBUG_) || defined(_DEBUG) cerr << "ResourcesManager_cpp constructor end"; #endif @@ -126,7 +134,7 @@ ResourcesManager_cpp::GetFittingResources(const machineParams& params, { vector vec; - ParseXmlFile(); + ParseXmlFiles(); const char *hostname = params.hostname.c_str(); #if defined(_DEBUG_) || defined(_DEBUG) @@ -283,19 +291,19 @@ void ResourcesManager_cpp::DeleteResourceInCatalog(const char *hostname) */ //============================================================================= -void ResourcesManager_cpp::WriteInXmlFile() +void ResourcesManager_cpp::WriteInXmlFile(std::string & xml_file) { - const char* aFilePath = _path_resources.c_str(); - +#if defined(_DEBUG_) || defined(_DEBUG) + std::cerr << "WriteInXmlFile : start" << std::endl; +#endif + const char* aFilePath = xml_file.c_str(); FILE* aFile = fopen(aFilePath, "w"); if (aFile == NULL) - { -#if defined(_DEBUG_) || defined(_DEBUG) - cerr << "Error opening file !" << endl; -#endif - return; - } + { + std::cerr << "Error opening file in WriteInXmlFile : " << xml_file << std::endl; + return; + } xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0"); xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog"); @@ -305,20 +313,15 @@ void ResourcesManager_cpp::WriteInXmlFile() handler->PrepareDocToXmlFile(aDoc); delete handler; -#if defined(_DEBUG_) || defined(_DEBUG) int isOk = xmlSaveFile(aFilePath, aDoc); - if (!isOk) cerr << "Error while XML file saving." << endl; -#else - xmlSaveFile(aFilePath, aDoc); -#endif + if (!isOk) + std::cerr << "Error while XML file saving : " << xml_file << std::endl; // Free the document xmlFreeDoc(aDoc); - fclose(aFile); - #if defined(_DEBUG_) || defined(_DEBUG) - cerr << "WRITING DONE!" << endl; + std::cerr << "WriteInXmlFile : WRITING DONE!" << std::endl; #endif } @@ -328,37 +331,66 @@ void ResourcesManager_cpp::WriteInXmlFile() */ //============================================================================= -const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFile() +const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles() { - SALOME_ResourcesCatalog_Handler* handler = - new SALOME_ResourcesCatalog_Handler(_resourcesList, _resourcesBatchList); + // Nettoyage des variables (est-ce vraiment utile ?) + _resourcesList.clear(); + _resourcesBatchList.clear(); - const char* aFilePath = _path_resources.c_str(); - FILE* aFile = fopen(aFilePath, "r"); - - if (aFile != NULL) + // On parse tous les fichiers + for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it) + { + MapOfParserResourcesType _resourcesList_tmp; + MapOfParserResourcesType _resourcesBatchList_tmp; + SALOME_ResourcesCatalog_Handler* handler = + new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp, _resourcesBatchList_tmp); + const char* aFilePath = (*_path_resources_it).c_str(); + FILE* aFile = fopen(aFilePath, "r"); + + if (aFile != NULL) { xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0); - if (aDoc != NULL) + { handler->ProcessXmlDocument(aDoc); -#if defined(_DEBUG_) || defined(_DEBUG) + + // adding new resources to the file + for (MapOfParserResourcesType_it i = _resourcesList_tmp.begin(); i != _resourcesList_tmp.end(); ++i) + { + MapOfParserResourcesType_it j = _resourcesList.find(i->first); + if (j == _resourcesList.end()) + { + _resourcesList[i->first] = i->second; + } + else + { + std::cerr << "ParseXmlFiles Warning, to resource with the same name was found, taking the first declaration : " << i->first << std::endl; + } + } + for (MapOfParserResourcesType_it i = _resourcesBatchList_tmp.begin(); i != _resourcesBatchList_tmp.end(); ++i) + { + MapOfParserResourcesType_it j = _resourcesBatchList.find(i->first); + if (j == _resourcesBatchList.end()) + { + _resourcesBatchList[i->first] = i->second; + } + else + { + std::cerr << "ParseXmlFiles Warning, to resource with the same name was found, taking the first declaration : " << i->first << std::endl; + } + } + } else - cerr << "ResourcesManager_cpp: could not parse file "<< aFilePath << endl; -#endif - + std::cerr << "ResourcesManager_cpp: could not parse file " << aFilePath << std::endl; // Free the document xmlFreeDoc(aDoc); - fclose(aFile); } -#if defined(_DEBUG_) || defined(_DEBUG) - else - cerr << "ResourcesManager_cpp: file "< #include #include +#include #include "SALOME_ResourcesCatalog_Parser.hxx" #include "SALOME_ResourcesCatalog_Handler.hxx" #include "SALOME_LoadRateManager.hxx" @@ -82,9 +83,9 @@ class RESOURCESMANAGER_EXPORT ResourcesManager_cpp void DeleteResourceInCatalog(const char *hostname); - void WriteInXmlFile(); + void WriteInXmlFile(std::string & xml_file); - const MapOfParserResourcesType& ParseXmlFile(); + const MapOfParserResourcesType& ParseXmlFiles(); const MapOfParserResourcesType& GetList() const; @@ -101,13 +102,14 @@ class RESOURCESMANAGER_EXPORT ResourcesManager_cpp throw(ResourcesException); //! will contain the path to the ressources catalog - std::string _path_resources; + std::list _path_resources; + std::list::iterator _path_resources_it; //! will contain the informations on the data type catalog(after parsing) MapOfParserResourcesType _resourcesList; //! will contain the informations on the data type catalog(after parsing) - MapOfParserResourcesType _resourcesBatchList; +MapOfParserResourcesType _resourcesBatchList; SALOME_LoadRateManager _dynamicResourcesSelecter; diff --git a/src/ResourcesManager/SALOME_ResourcesCatalog_Handler.cxx b/src/ResourcesManager/SALOME_ResourcesCatalog_Handler.cxx index a80bc83bf..1032e8fa4 100755 --- a/src/ResourcesManager/SALOME_ResourcesCatalog_Handler.cxx +++ b/src/ResourcesManager/SALOME_ResourcesCatalog_Handler.cxx @@ -106,7 +106,7 @@ void SALOME_ResourcesCatalog_Handler::ProcessXmlDocument(xmlDocPtr theDoc) xmlNodePtr aCurNode = xmlDocGetRootElement(theDoc); aCurNode = aCurNode->xmlChildrenNode; - + // Processing the document nodes while(aCurNode != NULL) { diff --git a/src/ResourcesManager/SALOME_ResourcesCatalog_Parser.hxx b/src/ResourcesManager/SALOME_ResourcesCatalog_Parser.hxx index 2d723b580..b8767a6da 100755 --- a/src/ResourcesManager/SALOME_ResourcesCatalog_Parser.hxx +++ b/src/ResourcesManager/SALOME_ResourcesCatalog_Parser.hxx @@ -92,5 +92,6 @@ struct RESOURCESMANAGER_EXPORT ParserResourcesType }; typedef std::map MapOfParserResourcesType; +typedef std::map::iterator MapOfParserResourcesType_it; #endif //SALOME_RESOURCES_CATALOG_PARSER -- 2.39.2