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>
28 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
30 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
31 : Config_XMLReader(PLUGIN_FILE),
32 myEventGenerated(theEventGenerated)
36 Config_ModuleReader::~Config_ModuleReader()
40 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
42 return myFeaturesInFiles;
46 * Get module name from plugins.xml
49 std::string Config_ModuleReader::getModuleName()
51 xmlNodePtr aRoot = findRoot();
52 return getProperty(aRoot, PLUGINS_MODULE);
58 void Config_ModuleReader::processNode(xmlNodePtr theNode)
60 if (isNode(theNode, NODE_PLUGIN, NULL)) {
61 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
62 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
63 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
64 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
66 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
67 std::list<std::string>::iterator it = aFeatures.begin();
68 for (; it != aFeatures.end(); it++) {
69 myFeaturesInFiles[*it] = aPluginConf;
74 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
76 return isNode(theNode, NODE_PLUGINS, NULL);
79 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
80 const std::string& thePluginXmlConf)
82 if (thePluginXmlConf.empty()) { //probably a third party library
83 loadLibrary(thePluginLibrary);
84 return std::list<std::string>();
87 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
91 return aReader.features();
94 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
95 const std::string& aPluginScript,
96 const std::string& aPluginConf)
98 PluginType aType = PluginType::Binary;
99 std::string aPluginName;
100 if (!aPluginLibrary.empty()) {
101 aPluginName = aPluginLibrary;
102 if (aPluginConf.empty()) {
103 aType = PluginType::Intrenal;
105 } else if (!aPluginScript.empty()) {
106 aPluginName = aPluginScript;
107 aType = PluginType::Python;
109 if(!aPluginName.empty()) {
110 myPluginTypes[aPluginName] = aType;
116 void Config_ModuleReader::loadPlugin(const std::string thePluginName)
118 PluginType aType = PluginType::Binary;
119 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
120 aType = myPluginTypes.at(thePluginName);
123 case PluginType::Python:
124 loadScript(thePluginName);
126 case PluginType::Binary:
127 case PluginType::Intrenal:
129 loadLibrary(thePluginName);
134 void Config_ModuleReader::loadScript(const std::string theFileName)
136 std::string aPythonFile = theFileName + ".py";
137 PyGILState_STATE gstate;
139 /* aquire python thread */
140 gstate = PyGILState_Ensure();
142 PyObject* module = PyImport_ImportModule(aPythonFile.c_str());
144 /* release python thread */
145 PyGILState_Release(gstate);
148 void Config_ModuleReader::loadLibrary(const std::string theLibName)
150 std::string aFileName = library(theLibName);
151 if (aFileName.empty())
155 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
157 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
159 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
160 std::string anErrorMsg = "Failed to load " + aFileName;
162 anErrorMsg += ": " + std::string(dlerror());
164 Events_Error::send(anErrorMsg);