2 * Config_ModuleReader.cpp
4 * Created on: Mar 20, 2014
8 #include <Config_Keywords.h>
9 #include <Config_Common.h>
10 #include <Config_ModuleReader.h>
11 #include <Config_FeatureReader.h>
12 #include <Events_Error.h>
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
17 // Have to be included before std headers
29 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
31 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
32 : Config_XMLReader(PLUGIN_FILE),
33 myEventGenerated(theEventGenerated)
37 Config_ModuleReader::~Config_ModuleReader()
41 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
43 return myFeaturesInFiles;
47 * Get module name from plugins.xml
50 std::string Config_ModuleReader::getModuleName()
52 xmlNodePtr aRoot = findRoot();
53 return getProperty(aRoot, PLUGINS_MODULE);
59 void Config_ModuleReader::processNode(xmlNodePtr theNode)
61 if (isNode(theNode, NODE_PLUGIN, NULL)) {
62 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
63 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
64 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
65 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
67 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
68 std::list<std::string>::iterator it = aFeatures.begin();
69 for (; it != aFeatures.end(); it++) {
70 myFeaturesInFiles[*it] = aPluginConf;
75 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
77 return isNode(theNode, NODE_PLUGINS, NULL);
80 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
81 const std::string& thePluginXmlConf)
83 if (thePluginXmlConf.empty()) { //probably a third party library
84 loadLibrary(thePluginLibrary);
85 return std::list<std::string>();
88 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
92 return aReader.features();
95 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
96 const std::string& aPluginScript,
97 const std::string& aPluginConf)
99 PluginType aType = Config_ModuleReader::Binary;
100 std::string aPluginName;
101 if (!aPluginLibrary.empty()) {
102 aPluginName = aPluginLibrary;
103 if (aPluginConf.empty()) {
104 aType = Config_ModuleReader::Intrenal;
106 } else if (!aPluginScript.empty()) {
107 aPluginName = aPluginScript;
108 aType = Config_ModuleReader::Python;
110 if(!aPluginName.empty()) {
111 myPluginTypes[aPluginName] = aType;
117 void Config_ModuleReader::loadPlugin(const std::string thePluginName)
119 PluginType aType = Config_ModuleReader::Binary;
120 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
121 aType = myPluginTypes.at(thePluginName);
124 case Config_ModuleReader::Python:
125 loadScript(thePluginName);
127 case Config_ModuleReader::Binary:
128 case Config_ModuleReader::Intrenal:
130 loadLibrary(thePluginName);
135 void Config_ModuleReader::loadScript(const std::string theFileName)
137 /* aquire python thread */
138 PyGILState_STATE gstate = PyGILState_Ensure();
139 PyObject* module = PyImport_ImportModule(theFileName.c_str());
142 std::string anErrorMsg = "An error occured while importing " + theFileName;
143 //Get detailed error message:
144 if (PyErr_Occurred()) {
145 PyObject *ptype, *pvalue, *ptraceback;
146 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
147 std::string aPyError = std::string(PyString_AsString(pvalue));
148 if (!aPyError.empty()) {
149 anErrorMsg += ":\n" + aPyError;
153 Py_XDECREF(ptraceback);
155 Events_Error::send(anErrorMsg);
158 /* release python thread */
159 PyGILState_Release(gstate);
162 void Config_ModuleReader::loadLibrary(const std::string theLibName)
164 std::string aFileName = library(theLibName);
165 if (aFileName.empty())
169 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
171 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
173 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
174 std::string anErrorMsg = "Failed to load " + aFileName;
176 anErrorMsg += ": " + std::string(dlerror());
178 Events_Error::send(anErrorMsg);