Salome HOME
Features and plugins loading mechanisms
[modules/shaper.git] / src / Model / Model_PluginManager.cxx
1 // File:        Model_PluginManager.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_PluginManager.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Plugin.h>
8 #include <Model_Feature.h>
9 #include <Event_Loop.h>
10 #include <Config_FeatureMessage.h>
11 #include <Config_ModuleReader.h>
12
13 using namespace std;
14
15 static Model_PluginManager* myImpl = new Model_PluginManager();
16
17 boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
18 {
19   if (this != myImpl) return myImpl->createFeature(theFeatureID);
20
21   LoadPluginsInfo();
22   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
23     if (myPluginObjs.find(myPlugins[theFeatureID]) == myPluginObjs.end()) {
24       // load plugin library if not yet done
25       myCurrentPluginName = myPlugins[theFeatureID];
26       loadLibrary(myCurrentPluginName);
27     }
28     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
29       return myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
30     }
31   }
32
33   return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
34 }
35
36 Model_PluginManager::Model_PluginManager()
37 {
38   myPluginsInfoLoaded = false;
39   static Event_ID aFeatureEvent = Event_Loop::eventByName("RegisterFeature");
40
41   ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
42   // register the configuration reading listener
43   Event_Loop* aLoop = Event_Loop::loop();
44   aLoop->registerListener(this, aFeatureEvent);
45 }
46
47 void Model_PluginManager::processEvent(const Event_Message* theMessage)
48 {
49   const Config_FeatureMessage* aMsg =
50     dynamic_cast<const Config_FeatureMessage*>(theMessage);
51   if (aMsg) {
52     // proccess the plugin info, load plugin
53     if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
54       myPlugins[aMsg->id()] = aMsg->pluginLibrary(); // TO DO: plugin name must be also imported from XMLs
55     }
56   }
57   // plugins information was started to load, so, it will be loaded
58   myPluginsInfoLoaded = true;
59 }
60
61 void Model_PluginManager::LoadPluginsInfo()
62 {
63   if (myPluginsInfoLoaded) // nothing to do
64     return;
65
66   // Read plugins information from XML files
67   Config_ModuleReader aXMLReader;
68   aXMLReader.setAutoImport(true);
69   aXMLReader.readAll();
70 }
71
72 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
73 {
74   myPluginObjs[myCurrentPluginName] = thePlugin;
75 }