1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * Config_ModuleReader.cpp
6 * Created on: Mar 20, 2014
12 #include <Config_Keywords.h>
13 #include <Config_Common.h>
14 #include <Config_ModuleReader.h>
15 #include <Config_FeatureReader.h>
16 #include <Events_Error.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
21 // Have to be included before std headers
30 #pragma warning(disable : 4996) // for getenv
35 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
36 std::set<std::string> Config_ModuleReader::myDependencyModules;
38 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
39 : Config_XMLReader(PLUGIN_FILE),
40 myEventGenerated(theEventGenerated)
44 Config_ModuleReader::~Config_ModuleReader()
48 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
50 return myFeaturesInFiles;
53 const std::set<std::string>& Config_ModuleReader::modulePluginFiles() const
59 * Get module name from plugins.xml
62 std::string Config_ModuleReader::getModuleName()
64 xmlNodePtr aRoot = findRoot();
65 return getProperty(aRoot, PLUGINS_MODULE);
68 void Config_ModuleReader::processNode(xmlNodePtr theNode)
70 if (isNode(theNode, NODE_PLUGIN, NULL)) {
71 if (!hasRequiredModules(theNode))
73 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
74 if (!aPluginConf.empty()) myPluginFiles.insert(aPluginConf);
75 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
76 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
77 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
79 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
80 std::list<std::string>::iterator it = aFeatures.begin();
81 for (; it != aFeatures.end(); it++) {
82 myFeaturesInFiles[*it] = aPluginConf;
87 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
89 return isNode(theNode, NODE_PLUGINS, NULL);
92 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
94 std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
95 if(aRequiredModule.empty())
97 std::set<std::string>::iterator it = myDependencyModules.begin();
98 for ( ; it != myDependencyModules.end(); it++ ) {
99 if (*it == aRequiredModule) return true;
104 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
105 const std::string& thePluginXmlConf)
107 if (thePluginXmlConf.empty()) { //probably a third party library
108 loadLibrary(thePluginLibrary);
109 return std::list<std::string>();
112 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
116 return aReader.features();
119 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
120 const std::string& aPluginScript,
121 const std::string& aPluginConf)
123 PluginType aType = Config_ModuleReader::Binary;
124 std::string aPluginName;
125 if (!aPluginLibrary.empty()) {
126 aPluginName = aPluginLibrary;
127 if (aPluginConf.empty()) {
128 aType = Config_ModuleReader::Intrenal;
130 } else if (!aPluginScript.empty()) {
131 aPluginName = aPluginScript;
132 aType = Config_ModuleReader::Python;
134 if(!aPluginName.empty()) {
135 myPluginTypes[aPluginName] = aType;
137 addDependencyModule(aPluginName);
141 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
143 PluginType aType = Config_ModuleReader::Binary;
144 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
145 aType = myPluginTypes.at(thePluginName);
148 case Config_ModuleReader::Python:
149 loadScript(thePluginName);
151 case Config_ModuleReader::Binary:
152 case Config_ModuleReader::Intrenal:
154 loadLibrary(thePluginName);
159 void Config_ModuleReader::loadScript(const std::string& theFileName)
161 /* aquire python thread */
162 PyGILState_STATE gstate = PyGILState_Ensure();
164 PyObject* module = PyImport_ImportModule(theFileName.c_str());
166 std::string anErrorMsg = "An error occured while importing " + theFileName;
167 //Get detailed error message:
168 if (PyErr_Occurred()) {
169 PyObject *pstr, *ptype, *pvalue, *ptraceback;
170 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
171 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
172 pstr = PyObject_Str(pvalue);
173 std::string aPyError = std::string(PyString_AsString(pstr));
174 if (!aPyError.empty()) {
175 anErrorMsg += ":\n" + aPyError;
180 Py_XDECREF(ptraceback);
182 Events_Error::send(anErrorMsg);
185 /* release python thread */
186 PyGILState_Release(gstate);
189 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
191 std::string aFileName = library(theLibName);
192 if (aFileName.empty())
196 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
198 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
200 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
201 std::string anErrorMsg = "Failed to load " + aFileName;
203 DWORD dwLastError = ::GetLastError();
204 LPSTR messageBuffer = NULL;
205 size_t size = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
206 FORMAT_MESSAGE_FROM_SYSTEM |
207 FORMAT_MESSAGE_IGNORE_INSERTS,
210 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
211 (LPSTR)&messageBuffer, 0, NULL);
212 anErrorMsg += ": " + std::string(messageBuffer, size);
214 anErrorMsg += ": " + std::string(dlerror());
216 std::cerr << anErrorMsg << std::endl;
217 Events_Error::send(anErrorMsg);
221 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
223 myDependencyModules.insert(normalize(theModuleName));