Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / bases / DynLibLoaderGNU.cxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "DynLibLoaderGNU.hxx"
21 #include <iostream>
22 #include <dlfcn.h>
23 #include "Exception.hxx"
24
25 using namespace YACS::BASES;
26
27 const char DynLibLoaderGNU::_extForDynLib[]=".so";
28
29 DynLibLoaderGNU::DynLibLoaderGNU(const std::string& libNameWithoutExtension):_libName(libNameWithoutExtension),
30                                                                              _handleOnLoadedLib(0)
31 {
32 }
33
34 DynLibLoaderGNU::~DynLibLoaderGNU()
35 {
36   if(_handleOnLoadedLib)
37     dlclose(_handleOnLoadedLib);
38 }
39
40 bool DynLibLoaderGNU::isLibFileFindable() const
41 {
42   return true;
43 }
44
45 std::string DynLibLoaderGNU::getLibNameWithoutExt() const
46 {
47   return _libName;
48 }
49
50 /*!
51   Append a directory with name \b dirName to the searching paths.
52   \return If append succeeds 0 is returned.
53   If the directory does not exists 1 is returned.
54   If the addition of directory causes some troubles due to seach paths name 2 is returned.
55  */
56 int DynLibLoaderGNU::appendDirInSearchPath(const std::string& dirName)
57 {
58   return 0;
59 }
60
61 /*!
62   Removes a directory with name \b dirName from the searching paths.
63   \return If removal succeeds 0 is returned.
64   If the directory does not exists 1 is returned.
65   If the path were not already in existing paths 2 is returned.
66  */
67 int DynLibLoaderGNU::removeDirInSearchPath(const std::string& dirName)
68 {
69   return 0;
70 }
71
72 void *DynLibLoaderGNU::getHandleOnSymbolWithName(const std::string& symbName, bool stopOnError)
73 {
74   if(!_handleOnLoadedLib)
75     if(!isLibFileFindable())
76       {
77         std::cerr << "Dynamic library with name " << symbName << _extForDynLib;
78         std::cerr << " not existing in paths specified" << std::endl;
79         return 0;
80       }
81     else
82       loadLib();
83   return resolveSymb(symbName, stopOnError);
84 }
85
86 bool DynLibLoaderGNU::load()
87 {
88   std::string fullLibName(_libName);
89   fullLibName+=_extForDynLib;
90   dlerror();
91   _handleOnLoadedLib=dlopen(fullLibName.c_str(),RTLD_LAZY | RTLD_GLOBAL);
92   char *message=dlerror();
93   if (message != NULL)
94     {
95       std::string error = "Error while trying to load library with name " + fullLibName +
96                           " with the following internal message: " + message;
97       throw YACS::Exception(error);
98     }
99   return _handleOnLoadedLib != NULL;
100 }
101
102 bool DynLibLoaderGNU::unload()
103 {
104   if (_handleOnLoadedLib) 
105     {
106       dlclose(_handleOnLoadedLib);
107       _handleOnLoadedLib = NULL;
108     }
109 }
110
111 bool DynLibLoaderGNU::reload()
112 {
113   unload();
114   return load();
115 }
116
117 void *DynLibLoaderGNU::resolveSymb(const std::string& symbName, bool stopOnError)
118 {
119   dlerror();
120   void *ret=dlsym(_handleOnLoadedLib,symbName.c_str());
121   char *message=dlerror();
122   if(stopOnError && (NULL != message))
123     {
124       std::string error="Error detected on symbol ";
125       error=error+symbName +" search in library with name "+_libName+_extForDynLib+" with the following internal message "+message;
126       throw YACS::Exception(error);
127     }
128   else
129     return ret;
130 }
131
132 const char *DynLibLoaderGNU::getExtensionForDynLib()
133 {
134   return _extForDynLib+1;
135 }
136