1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * Config_ModuleReader.cpp
6 * Created on: Mar 20, 2014
10 #include <Config_Keywords.h>
11 #include <Config_Common.h>
12 #include <Config_ModuleReader.h>
13 #include <Config_FeatureReader.h>
14 #include <Events_Error.h>
16 #include <libxml/parser.h>
17 #include <libxml/tree.h>
19 // Have to be included before std headers
28 #pragma warning(disable : 4996) // for getenv
33 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
34 std::set<std::string> Config_ModuleReader::myDependencyModules;
36 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
37 : Config_XMLReader(PLUGIN_FILE),
38 myEventGenerated(theEventGenerated)
42 Config_ModuleReader::~Config_ModuleReader()
46 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
48 return myFeaturesInFiles;
52 * Get module name from plugins.xml
55 std::string Config_ModuleReader::getModuleName()
57 xmlNodePtr aRoot = findRoot();
58 return getProperty(aRoot, PLUGINS_MODULE);
64 void Config_ModuleReader::processNode(xmlNodePtr theNode)
66 if (isNode(theNode, NODE_PLUGIN, NULL)) {
67 if (!hasRequiredModules(theNode))
69 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
70 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
71 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
72 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
74 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
75 std::list<std::string>::iterator it = aFeatures.begin();
76 for (; it != aFeatures.end(); it++) {
77 myFeaturesInFiles[*it] = aPluginConf;
82 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
84 return isNode(theNode, NODE_PLUGINS, NULL);
87 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
89 std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
90 if(aRequiredModule.empty())
92 std::set<std::string>::iterator it = myDependencyModules.begin();
93 for ( ; it != myDependencyModules.end(); it++ ) {
94 if (*it == aRequiredModule) return true;
99 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
100 const std::string& thePluginXmlConf)
102 if (thePluginXmlConf.empty()) { //probably a third party library
103 loadLibrary(thePluginLibrary);
104 return std::list<std::string>();
107 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
111 return aReader.features();
114 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
115 const std::string& aPluginScript,
116 const std::string& aPluginConf)
118 PluginType aType = Config_ModuleReader::Binary;
119 std::string aPluginName;
120 if (!aPluginLibrary.empty()) {
121 aPluginName = aPluginLibrary;
122 if (aPluginConf.empty()) {
123 aType = Config_ModuleReader::Intrenal;
125 } else if (!aPluginScript.empty()) {
126 aPluginName = aPluginScript;
127 aType = Config_ModuleReader::Python;
129 if(!aPluginName.empty()) {
130 myPluginTypes[aPluginName] = aType;
132 addDependencyModule(aPluginName);
136 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
138 PluginType aType = Config_ModuleReader::Binary;
139 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
140 aType = myPluginTypes.at(thePluginName);
143 case Config_ModuleReader::Python:
144 loadScript(thePluginName);
146 case Config_ModuleReader::Binary:
147 case Config_ModuleReader::Intrenal:
149 loadLibrary(thePluginName);
154 void Config_ModuleReader::loadScript(const std::string& theFileName)
156 /* aquire python thread */
157 PyGILState_STATE gstate = PyGILState_Ensure();
159 PyObject* module = PyImport_ImportModule(theFileName.c_str());
161 std::string anErrorMsg = "An error occured while importing " + theFileName;
162 //Get detailed error message:
163 if (PyErr_Occurred()) {
164 PyObject *pstr, *ptype, *pvalue, *ptraceback;
165 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
166 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
167 pstr = PyObject_Str(pvalue);
168 std::string aPyError = std::string(PyString_AsString(pstr));
169 if (!aPyError.empty()) {
170 anErrorMsg += ":\n" + aPyError;
175 Py_XDECREF(ptraceback);
177 Events_Error::send(anErrorMsg);
180 /* release python thread */
181 PyGILState_Release(gstate);
184 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
186 std::string aFileName = library(theLibName);
187 if (aFileName.empty())
191 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
193 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
195 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
196 std::string anErrorMsg = "Failed to load " + aFileName;
198 anErrorMsg += ": " + std::string(dlerror());
200 Events_Error::send(anErrorMsg);
204 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
206 myDependencyModules.insert(normalize(theModuleName));