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_InfoMessage.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);
69 void Config_ModuleReader::addFeature(const std::string& theFeatureName,
70 const std::string& thePluginConfig)
72 if (myFeaturesInFiles.count(theFeatureName)) {
73 std::string anErrorMsg = "Can not register feature '%1' in plugin '%2'."
74 " There is a feature with the same ID.";
75 Events_InfoMessage("Config_ModuleReader", anErrorMsg)
76 .arg(theFeatureName).arg(thePluginConfig).send();
80 myFeaturesInFiles[theFeatureName] = thePluginConfig;
83 void Config_ModuleReader::processNode(xmlNodePtr theNode)
85 if (isNode(theNode, NODE_PLUGIN, NULL)) {
86 if (!hasRequiredModules(theNode))
88 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
89 if (!aPluginConf.empty())
90 myPluginFiles.insert(aPluginConf);
91 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
92 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
93 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
95 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
96 std::list<std::string>::iterator it = aFeatures.begin();
97 for (; it != aFeatures.end(); it++) {
98 addFeature(*it, aPluginConf);
103 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
105 return isNode(theNode, NODE_PLUGINS, NULL);
108 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
110 std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
111 if(aRequiredModule.empty())
113 std::set<std::string>::iterator it = myDependencyModules.begin();
114 for ( ; it != myDependencyModules.end(); it++ ) {
115 if (*it == aRequiredModule) return true;
120 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
121 const std::string& thePluginXmlConf)
123 if (thePluginXmlConf.empty()) { //probably a third party library
124 loadLibrary(thePluginLibrary);
125 return std::list<std::string>();
128 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
132 return aReader.features();
135 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
136 const std::string& aPluginScript,
137 const std::string& aPluginConf)
139 PluginType aType = Config_ModuleReader::Binary;
140 std::string aPluginName;
141 if (!aPluginLibrary.empty()) {
142 aPluginName = aPluginLibrary;
143 if (aPluginConf.empty()) {
144 aType = Config_ModuleReader::Intrenal;
146 } else if (!aPluginScript.empty()) {
147 aPluginName = aPluginScript;
148 aType = Config_ModuleReader::Python;
150 if(!aPluginName.empty()) {
151 myPluginTypes[aPluginName] = aType;
153 addDependencyModule(aPluginName);
157 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
159 PluginType aType = Config_ModuleReader::Binary;
160 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
161 aType = myPluginTypes.at(thePluginName);
164 case Config_ModuleReader::Python:
165 loadScript(thePluginName);
167 case Config_ModuleReader::Binary:
168 case Config_ModuleReader::Intrenal:
170 loadLibrary(thePluginName);
175 void Config_ModuleReader::loadScript(const std::string& theFileName)
177 /* acquire python thread */
178 PyGILState_STATE gstate = PyGILState_Ensure();
180 PyObject* module = PyImport_ImportModule(theFileName.c_str());
182 std::string anErrorMsg = "An error occurred while importing " + theFileName;
183 //Get detailed error message:
184 if (PyErr_Occurred()) {
185 PyObject *pstr, *ptype, *pvalue, *ptraceback;
186 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
187 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
188 pstr = PyObject_Str(pvalue);
189 std::string aPyError = std::string(PyString_AsString(pstr));
190 if (!aPyError.empty()) {
191 anErrorMsg += ":\n" + aPyError;
196 Py_XDECREF(ptraceback);
198 Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
201 /* release python thread */
202 PyGILState_Release(gstate);
205 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
207 std::string aFileName = library(theLibName);
208 if (aFileName.empty())
212 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
214 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
216 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
217 std::string anErrorMsg = "Failed to load " + aFileName;
219 DWORD dwLastError = ::GetLastError();
220 LPSTR messageBuffer = NULL;
221 size_t size = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
222 FORMAT_MESSAGE_FROM_SYSTEM |
223 FORMAT_MESSAGE_IGNORE_INSERTS,
226 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
227 (LPSTR)&messageBuffer, 0, NULL);
228 anErrorMsg += ": " + std::string(messageBuffer, size);
230 anErrorMsg += ": " + std::string(dlerror());
232 std::cerr << anErrorMsg << std::endl;
233 Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
237 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
239 myDependencyModules.insert(normalize(theModuleName));