1 // Copyright (C) 2018-2021 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 email : webmaster.salome@opencascade.com
20 #include "ObserverAsPlugin.hxx"
21 #include "Dispatcher.hxx"
22 #include "Exception.hxx"
31 void *HandleOnLoadedPlugin=nullptr;
32 void (*DefineCustomObservers)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *)=nullptr;
33 void (*CleanUpObservers) ()=nullptr;
35 void YACS::ENGINE::LoadObserversPluginIfAny(ComposedNode *rootNode, Executor *executor)
37 static const char SYMBOLE_NAME_1[]="DefineCustomObservers";
38 static const char SYMBOLE_NAME_2[]="CleanUpObservers";
40 Dispatcher *disp(Dispatcher::getDispatcher());
42 throw YACS::Exception("Internal error ! No dispatcher !");
43 char *yacsDriverPluginPath(getenv("YACS_DRIVER_PLUGIN_PATH"));
44 if(!yacsDriverPluginPath)
46 void *handle(dlopen(yacsDriverPluginPath, RTLD_LAZY | RTLD_GLOBAL));
49 std::string message(dlerror());
50 std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" defined by the YACS_DRIVER_PLUGIN_PATH env var : " << message;
51 throw YACS::Exception(oss.str());
53 DefineCustomObservers=(void (*)(YACS::ENGINE::Dispatcher *, YACS::ENGINE::ComposedNode *, YACS::ENGINE::Executor *))(dlsym(handle,SYMBOLE_NAME_1));
54 if(!DefineCustomObservers)
56 std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_1 << " does not exists !";
57 throw YACS::Exception(oss.str());
59 CleanUpObservers=(void (*)())(dlsym(handle,SYMBOLE_NAME_2));
62 std::ostringstream oss; oss << "Error during load of \"" << yacsDriverPluginPath << "\" ! Library has been correctly loaded but symbol " << SYMBOLE_NAME_2 << " does not exists !";
63 throw YACS::Exception(oss.str());
65 HandleOnLoadedPlugin=handle;
66 DefineCustomObservers(disp,rootNode,executor);
70 void YACS::ENGINE::UnLoadObserversPluginIfAny()
73 if(HandleOnLoadedPlugin)
76 dlclose(HandleOnLoadedPlugin);