Mercurial > hg > orthanc
annotate Plugins/Engine/SharedLibrary.cpp @ 976:6968356679c0 plugins
integration mainline->plugins
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 30 Jun 2014 14:55:43 +0200 |
parents | f57802f8b4dc |
children | a13ccd93df28 |
rev | line source |
---|---|
884
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Belgium |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 * |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * License, or (at your option) any later version. |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * program give permission to link the code of its release with the |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 * in the program, then also delete it here. |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 * |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 * General Public License for more details. |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 * |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 **/ |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
33 #include "SharedLibrary.h" |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
34 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
35 #include "../../Core/Toolbox.h" |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
36 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
37 #if defined(_WIN32) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
38 #include <windows.h> |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
39 #elif defined(__linux) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
40 #include <dlfcn.h> |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
41 #else |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
42 #error Support your platform here |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
43 #endif |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
44 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
45 #include <glog/logging.h> |
884
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 namespace Orthanc |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 { |
887 | 49 SharedLibrary::SharedLibrary(const std::string& path) : |
50 path_(path), | |
51 handle_(NULL) | |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
52 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
53 #if defined(_WIN32) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
54 handle_ = ::LoadLibraryA(path.c_str()); |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
55 if (handle_ == NULL) |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
56 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
57 LOG(ERROR) << "LoadLibrary(" << path << ") failed: Error " << ::GetLastError(); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
58 throw OrthancException(ErrorCode_SharedLibrary); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
59 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
60 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
61 #elif defined(__linux) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
62 handle_ = ::dlopen(path.c_str(), RTLD_NOW); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
63 if (handle_ == NULL) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
64 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
65 std::string explanation; |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
66 const char *tmp = ::dlerror(); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
67 if (tmp) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
68 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
69 explanation = ": Error " + std::string(tmp); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
70 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
71 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
72 LOG(ERROR) << "dlopen(" << path << ") failed" << explanation; |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
73 throw OrthancException(ErrorCode_SharedLibrary); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
74 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
75 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
76 #else |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
77 #error Support your platform here |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
78 #endif |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
79 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
80 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
81 SharedLibrary::~SharedLibrary() |
884
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 { |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
83 if (handle_) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
84 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
85 #if defined(_WIN32) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
86 ::FreeLibrary((HMODULE)handle_); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
87 #elif defined(__linux) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
88 ::dlclose(handle_); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
89 #else |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
90 #error Support your platform here |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
91 #endif |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
92 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
93 } |
884
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
95 |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
96 SharedLibrary::FunctionPointer SharedLibrary::GetFunctionInternal(const std::string& name) |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
97 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
98 if (!handle_) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
99 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
100 throw OrthancException(ErrorCode_InternalError); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
101 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
102 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
103 #if defined(_WIN32) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
104 return ::GetProcAddress((HMODULE)handle_, name.c_str()); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
105 #elif defined(__linux) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
106 return ::dlsym(handle_, name.c_str()); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
107 #else |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
108 #error Support your platform here |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
109 #endif |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
110 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
111 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
112 |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
113 SharedLibrary::FunctionPointer SharedLibrary::GetFunction(const std::string& name) |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
114 { |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
115 SharedLibrary::FunctionPointer result = GetFunctionInternal(name); |
885
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
116 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
117 if (result == NULL) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
118 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
119 LOG(ERROR) << "Shared library does not expose function \"" << name << "\""; |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
120 throw OrthancException(ErrorCode_SharedLibrary); |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
121 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
122 else |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
123 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
124 return result; |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
125 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
126 } |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
127 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
128 |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
129 bool SharedLibrary::HasFunction(const std::string& name) |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
130 { |
0570a8c859cb
SharedLibrary class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
884
diff
changeset
|
131 return GetFunctionInternal(name) != NULL; |
884
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 } |
cd8a69aa4093
creation of branch plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
133 } |