1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
23 #include <Config_Keywords.h>
24 #include <Config_Common.h>
25 #include <Config_ModuleReader.h>
26 #include <Config_FeatureReader.h>
27 #include <Events_InfoMessage.h>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
32 // Have to be included before std headers
41 #pragma warning(disable : 4996) // for getenv
46 std::map<std::string, Config_ModuleReader::PluginType> Config_ModuleReader::myPluginTypes;
47 std::set<std::string> Config_ModuleReader::myDependencyModules;
49 Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated)
50 : Config_XMLReader(PLUGIN_FILE),
51 myEventGenerated(theEventGenerated)
55 Config_ModuleReader::~Config_ModuleReader()
59 const std::map<std::string, std::string>& Config_ModuleReader::featuresInFiles() const
61 return myFeaturesInFiles;
64 const std::set<std::string>& Config_ModuleReader::modulePluginFiles() const
70 * Get module name from plugins.xml
73 std::string Config_ModuleReader::getModuleName()
75 xmlNodePtr aRoot = findRoot();
76 return getProperty(aRoot, PLUGINS_MODULE);
80 void Config_ModuleReader::addFeature(const std::string& theFeatureName,
81 const std::string& thePluginConfig)
83 if (myFeaturesInFiles.count(theFeatureName)) {
84 std::string anErrorMsg = "Can not register feature '%1' in plugin '%2'."
85 " There is a feature with the same ID.";
86 Events_InfoMessage("Config_ModuleReader", anErrorMsg)
87 .arg(theFeatureName).arg(thePluginConfig).send();
91 myFeaturesInFiles[theFeatureName] = thePluginConfig;
94 void Config_ModuleReader::processNode(xmlNodePtr theNode)
96 if (isNode(theNode, NODE_PLUGIN, NULL)) {
97 if (!hasRequiredModules(theNode))
99 std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG);
100 if (!aPluginConf.empty())
101 myPluginFiles.insert(aPluginConf);
102 std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY);
103 std::string aPluginScript = getProperty(theNode, PLUGIN_SCRIPT);
104 std::string aPluginName = addPlugin(aPluginLibrary, aPluginScript, aPluginConf);
106 std::list<std::string> aFeatures = importPlugin(aPluginName, aPluginConf);
107 std::list<std::string>::iterator it = aFeatures.begin();
108 for (; it != aFeatures.end(); it++) {
109 addFeature(*it, aPluginConf);
114 bool Config_ModuleReader::processChildren(xmlNodePtr theNode)
116 return isNode(theNode, NODE_PLUGINS, NULL);
119 bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const
121 std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY));
122 if(aRequiredModule.empty())
124 std::set<std::string>::iterator it = myDependencyModules.begin();
125 for ( ; it != myDependencyModules.end(); it++ ) {
126 if (*it == aRequiredModule) return true;
131 std::list<std::string> Config_ModuleReader::importPlugin(const std::string& thePluginLibrary,
132 const std::string& thePluginXmlConf)
134 if (thePluginXmlConf.empty()) { //probably a third party library
135 loadLibrary(thePluginLibrary);
136 return std::list<std::string>();
139 Config_FeatureReader aReader = Config_FeatureReader(thePluginXmlConf,
143 return aReader.features();
146 std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
147 const std::string& aPluginScript,
148 const std::string& aPluginConf)
150 PluginType aType = Config_ModuleReader::Binary;
151 std::string aPluginName;
152 if (!aPluginLibrary.empty()) {
153 aPluginName = aPluginLibrary;
154 if (aPluginConf.empty()) {
155 aType = Config_ModuleReader::Intrenal;
157 } else if (!aPluginScript.empty()) {
158 aPluginName = aPluginScript;
159 aType = Config_ModuleReader::Python;
161 if(!aPluginName.empty()) {
162 myPluginTypes[aPluginName] = aType;
164 addDependencyModule(aPluginName);
168 void Config_ModuleReader::loadPlugin(const std::string& thePluginName)
170 PluginType aType = Config_ModuleReader::Binary;
171 if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
172 aType = myPluginTypes.at(thePluginName);
175 case Config_ModuleReader::Python:
176 loadScript(thePluginName);
178 case Config_ModuleReader::Binary:
179 case Config_ModuleReader::Intrenal:
181 loadLibrary(thePluginName);
186 void Config_ModuleReader::loadScript(const std::string& theFileName, bool theSendErr)
188 /* acquire python thread */
189 PyGILState_STATE gstate = PyGILState_Ensure();
191 PyObject* module = PyImport_ImportModule(theFileName.c_str());
193 std::string anErrorMsg = "An error occurred while importing " + theFileName;
194 //Get detailed error message:
195 if (PyErr_Occurred()) {
196 PyObject *pstr, *ptype, *pvalue, *ptraceback;
197 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
198 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
199 pstr = PyObject_Str(pvalue);
200 std::string aPyError = std::string(PyString_AsString(pstr));
201 if (!aPyError.empty()) {
202 anErrorMsg += ":\n" + aPyError;
207 Py_XDECREF(ptraceback);
210 Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
213 /* release python thread */
214 PyGILState_Release(gstate);
217 void Config_ModuleReader::loadLibrary(const std::string& theLibName)
219 std::string aFileName = library(theLibName);
220 if (aFileName.empty())
224 HINSTANCE aModLib = ::LoadLibrary(aFileName.c_str());
226 void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY | RTLD_GLOBAL );
228 if(!aModLib && theLibName != "DFBrowser") { // don't show error for internal debugging tool
229 std::string anErrorMsg = "Failed to load " + aFileName;
231 DWORD dwLastError = ::GetLastError();
232 LPSTR messageBuffer = NULL;
233 size_t size = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
234 FORMAT_MESSAGE_FROM_SYSTEM |
235 FORMAT_MESSAGE_IGNORE_INSERTS,
238 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
239 (LPSTR)&messageBuffer, 0, NULL);
240 anErrorMsg += ": " + std::string(messageBuffer, size);
242 anErrorMsg += ": " + std::string(dlerror());
244 std::cerr << anErrorMsg << std::endl;
245 Events_InfoMessage("Config_ModuleReader", anErrorMsg).send();
249 void Config_ModuleReader::addDependencyModule(const std::string& theModuleName)
251 myDependencyModules.insert(normalize(theModuleName));