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);
61 void Config_ModuleReader::processNode(xmlNodePtr theNode)
63 if (isNode(theNode, NODE_PLUGIN, NULL)) {
64 if (!hasRequiredModules(theNode))
66 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
67 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
68 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
69 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
71 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
72 std::list<std::string>::iterator it = aFeatures.begin();
73 for (; it != aFeatures.end(); it++) {
74 myFeaturesInFiles[*it] = aPluginConf;
79 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
81 return isNode(theNode, NODE_PLUGINS, NULL);
84 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
86 std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
87 if(aRequiredModule.empty())
89 std::set<std::string>::iterator it = myDependencyModules.begin();
90 for ( ; it != myDependencyModules.end(); it++ ) {
91 if (*it == aRequiredModule) return true;
96 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
97 const std::string& thePluginXmlConf)
99 if (thePluginXmlConf.empty()) { //probably a third party library
100 loadLibrary(thePluginLibrary);
101 return std::list<std::string>();
104 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
108 return aReader.features();
111 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
112 const std::string& aPluginScript,
113 const std::string& aPluginConf)
115 PluginType aType = Config_ModuleReader::Binary;
116 std::string aPluginName;
117 if (!aPluginLibrary.empty()) {
118 aPluginName = aPluginLibrary;
119 if (aPluginConf.empty()) {
120 aType = Config_ModuleReader::Intrenal;
122 } else if (!aPluginScript.empty()) {
123 aPluginName = aPluginScript;
124 aType = Config_ModuleReader::Python;
126 if(!aPluginName.empty()) {
127 myPluginTypes[aPluginName] = aType;
129 addDependencyModule(aPluginName);
133 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
135 PluginType aType = Config_ModuleReader::Binary;
136 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
137 aType = myPluginTypes.at(thePluginName);
140 case Config_ModuleReader::Python:
141 loadScript(thePluginName);
143 case Config_ModuleReader::Binary:
144 case Config_ModuleReader::Intrenal:
146 loadLibrary(thePluginName);
151 void Config_ModuleReader::loadScript(const std::string& theFileName)
153 /* aquire python thread */
154 PyGILState_STATE gstate = PyGILState_Ensure();
156 PyObject* module = PyImport_ImportModule(theFileName.c_str());
158 std::string anErrorMsg = "An error occured while importing " + theFileName;
159 //Get detailed error message:
160 if (PyErr_Occurred()) {
161 PyObject *pstr, *ptype, *pvalue, *ptraceback;
162 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
163 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
164 pstr = PyObject_Str(pvalue);
165 std::string aPyError = std::string(PyString_AsString(pstr));
166 if (!aPyError.empty()) {
167 anErrorMsg += ":\n" + aPyError;
172 Py_XDECREF(ptraceback);
174 Events_Error::send(anErrorMsg);
177 /* release python thread */
178 PyGILState_Release(gstate);
181 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
183 std::string aFileName = library(theLibName);
184 if (aFileName.empty())
188 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
190 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
192 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
193 std::string anErrorMsg = "Failed to load " + aFileName;
195 anErrorMsg += ": " + std::string(dlerror());
197 std::cerr << anErrorMsg << std::endl;
198 Events_Error::send(anErrorMsg);
202 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
204 myDependencyModules.insert(normalize(theModuleName));