Mercurial > hg > orthanc
annotate Plugins/Engine/PluginsManager.cpp @ 1594:2bac60a4f584
OrthancPluginSendHttpStatus
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Aug 2015 12:56:48 +0200 |
parents | 357c4bb15701 |
children | 0a2ad4a6858f |
rev | line source |
---|---|
887 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1272
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1272
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
887 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "PluginsManager.h" | |
34 | |
894 | 35 #include "../../Core/Toolbox.h" |
897
bafc9d592632
REST callbacks are working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
896
diff
changeset
|
36 #include "../../Core/HttpServer/HttpOutput.h" |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1337
diff
changeset
|
37 #include "../../Core/Logging.h" |
894 | 38 |
887 | 39 #include <cassert> |
40 #include <memory> | |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
41 #include <boost/filesystem.hpp> |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
42 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
43 #ifdef WIN32 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
44 #define PLUGIN_EXTENSION ".dll" |
1337 | 45 #elif defined(__linux) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
46 #define PLUGIN_EXTENSION ".so" |
1026 | 47 #elif defined(__APPLE__) && defined(__MACH__) |
48 #define PLUGIN_EXTENSION ".dylib" | |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
49 #else |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
50 #error Support your platform here |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
51 #endif |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
52 |
887 | 53 |
54 namespace Orthanc | |
55 { | |
56 static void CallInitialize(SharedLibrary& plugin, | |
57 const OrthancPluginContext& context) | |
58 { | |
59 typedef int32_t (*Initialize) (const OrthancPluginContext*); | |
60 | |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
61 #if defined(_WIN32) |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
62 Initialize initialize = (Initialize) plugin.GetFunction("OrthancPluginInitialize"); |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
63 #else |
887 | 64 /** |
65 * gcc would complain about "ISO C++ forbids casting between | |
66 * pointer-to-function and pointer-to-object" without the trick | |
67 * below, that is known as "the POSIX.1-2003 (Technical Corrigendum | |
68 * 1) workaround". See the man page of "dlsym()". | |
69 * http://www.trilithium.com/johan/2004/12/problem-with-dlsym/ | |
70 * http://stackoverflow.com/a/14543811/881731 | |
71 **/ | |
72 | |
73 Initialize initialize; | |
74 *(void **) (&initialize) = plugin.GetFunction("OrthancPluginInitialize"); | |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
75 #endif |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
76 |
887 | 77 assert(initialize != NULL); |
78 int32_t error = initialize(&context); | |
79 | |
80 if (error != 0) | |
81 { | |
82 LOG(ERROR) << "Error while initializing plugin " << plugin.GetPath() | |
83 << " (code " << error << ")"; | |
84 throw OrthancException(ErrorCode_SharedLibrary); | |
85 } | |
86 } | |
87 | |
88 | |
89 static void CallFinalize(SharedLibrary& plugin) | |
90 { | |
91 typedef void (*Finalize) (); | |
92 | |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
93 #if defined(_WIN32) |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
94 Finalize finalize = (Finalize) plugin.GetFunction("OrthancPluginFinalize"); |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
95 #else |
887 | 96 Finalize finalize; |
97 *(void **) (&finalize) = plugin.GetFunction("OrthancPluginFinalize"); | |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
98 #endif |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
99 |
887 | 100 assert(finalize != NULL); |
101 finalize(); | |
102 } | |
103 | |
104 | |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
105 static const char* CallGetName(SharedLibrary& plugin) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
106 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
107 typedef const char* (*GetName) (); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
108 |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
109 #if defined(_WIN32) |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
110 GetName getName = (GetName) plugin.GetFunction("OrthancPluginGetName"); |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
111 #else |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
112 GetName getName; |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
113 *(void **) (&getName) = plugin.GetFunction("OrthancPluginGetName"); |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
114 #endif |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
115 |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
116 assert(getName != NULL); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
117 return getName(); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
118 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
119 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
120 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
121 static const char* CallGetVersion(SharedLibrary& plugin) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
122 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
123 typedef const char* (*GetVersion) (); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
124 |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
125 #if defined(_WIN32) |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
126 GetVersion getVersion = (GetVersion) plugin.GetFunction("OrthancPluginGetVersion"); |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
127 #else |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
128 GetVersion getVersion; |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
129 *(void **) (&getVersion) = plugin.GetFunction("OrthancPluginGetVersion"); |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
130 #endif |
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
131 |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
132 assert(getVersion != NULL); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
133 return getVersion(); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
134 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
135 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
136 |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
137 OrthancPluginErrorCode PluginsManager::InvokeService(OrthancPluginContext* context, |
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
138 _OrthancPluginService service, |
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
139 const void* params) |
887 | 140 { |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
141 switch (service) |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
142 { |
907 | 143 case _OrthancPluginService_LogError: |
899 | 144 LOG(ERROR) << reinterpret_cast<const char*>(params); |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
145 return OrthancPluginErrorCode_Success; |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
146 |
907 | 147 case _OrthancPluginService_LogWarning: |
899 | 148 LOG(WARNING) << reinterpret_cast<const char*>(params); |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
149 return OrthancPluginErrorCode_Success; |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
150 |
907 | 151 case _OrthancPluginService_LogInfo: |
899 | 152 LOG(INFO) << reinterpret_cast<const char*>(params); |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
153 return OrthancPluginErrorCode_Success; |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
154 |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
155 default: |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
156 break; |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
157 } |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
158 |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
159 PluginsManager* that = reinterpret_cast<PluginsManager*>(context->pluginsManager); |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
160 |
899 | 161 for (std::list<IPluginServiceProvider*>::iterator |
162 it = that->serviceProviders_.begin(); | |
944
c068671d12a9
fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
907
diff
changeset
|
163 it != that->serviceProviders_.end(); ++it) |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
164 { |
899 | 165 try |
166 { | |
167 if ((*it)->InvokeService(service, params)) | |
168 { | |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
169 return OrthancPluginErrorCode_Success; |
899 | 170 } |
171 } | |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
172 catch (OrthancException& e) |
899 | 173 { |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
174 // This service provider has failed |
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
175 LOG(ERROR) << "Exception while invoking a plugin service: " << e.What(); |
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
176 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); |
899 | 177 } |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
178 } |
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
179 |
1581
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
180 LOG(ERROR) << "Plugin invoking unknown service: " << service; |
357c4bb15701
Plugins have access to explicit error codes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
181 return OrthancPluginErrorCode_UnknownPluginService; |
887 | 182 } |
183 | |
184 | |
899 | 185 PluginsManager::PluginsManager() |
887 | 186 { |
894 | 187 memset(&context_, 0, sizeof(context_)); |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
188 context_.pluginsManager = this; |
887 | 189 context_.orthancVersion = ORTHANC_VERSION; |
904
2732b5f57d9c
sample to forward dicom data
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
901
diff
changeset
|
190 context_.Free = ::free; |
898
7000fc86fe62
improved plugin api
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
897
diff
changeset
|
191 context_.InvokeService = InvokeService; |
887 | 192 } |
193 | |
194 PluginsManager::~PluginsManager() | |
195 { | |
944
c068671d12a9
fixes thanks to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
907
diff
changeset
|
196 for (Plugins::iterator it = plugins_.begin(); it != plugins_.end(); ++it) |
887 | 197 { |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
198 if (it->second != NULL) |
887 | 199 { |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
200 LOG(WARNING) << "Unregistering plugin '" << it->first |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
201 << "' (version " << it->second->GetVersion() << ")"; |
893
f57802f8b4dc
plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
889
diff
changeset
|
202 |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
203 CallFinalize(it->second->GetLibrary()); |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
204 delete it->second; |
887 | 205 } |
206 } | |
207 } | |
208 | |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
209 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
210 static bool IsOrthancPlugin(SharedLibrary& library) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
211 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
212 return (library.HasFunction("OrthancPluginInitialize") && |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
213 library.HasFunction("OrthancPluginFinalize") && |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
214 library.HasFunction("OrthancPluginGetName") && |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
215 library.HasFunction("OrthancPluginGetVersion")); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
216 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
217 |
887 | 218 |
219 void PluginsManager::RegisterPlugin(const std::string& path) | |
220 { | |
1272
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
221 if (!boost::filesystem::exists(path)) |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
222 { |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
223 LOG(ERROR) << "Inexistent path to plugins: " << path; |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
224 return; |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
225 } |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
226 |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
227 if (boost::filesystem::is_directory(path)) |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
228 { |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
229 ScanFolderForPlugins(path, false); |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
230 return; |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
231 } |
7442097b41c9
Scan of folders for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1232
diff
changeset
|
232 |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
233 std::auto_ptr<Plugin> plugin(new Plugin(path)); |
887 | 234 |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
235 if (!IsOrthancPlugin(plugin->GetLibrary())) |
887 | 236 { |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
237 LOG(ERROR) << "Plugin " << plugin->GetLibrary().GetPath() |
887 | 238 << " does not declare the proper entry functions"; |
239 throw OrthancException(ErrorCode_SharedLibrary); | |
240 } | |
241 | |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
242 std::string name(CallGetName(plugin->GetLibrary())); |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
243 if (plugins_.find(name) != plugins_.end()) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
244 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
245 LOG(ERROR) << "Plugin '" << name << "' already registered"; |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
246 throw OrthancException(ErrorCode_SharedLibrary); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
247 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
248 |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
249 plugin->SetVersion(CallGetVersion(plugin->GetLibrary())); |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
250 LOG(WARNING) << "Registering plugin '" << name |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
251 << "' (version " << plugin->GetVersion() << ")"; |
887 | 252 |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
253 CallInitialize(plugin->GetLibrary(), context_); |
887 | 254 |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
255 plugins_[name] = plugin.release(); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
256 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
257 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
258 |
889 | 259 void PluginsManager::ScanFolderForPlugins(const std::string& folder, |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
260 bool isRecursive) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
261 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
262 using namespace boost::filesystem; |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
263 |
889 | 264 if (!exists(folder)) |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
265 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
266 return; |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
267 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
268 |
889 | 269 LOG(INFO) << "Scanning folder " << folder << " for plugins"; |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
270 |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
271 directory_iterator end_it; // default construction yields past-the-end |
889 | 272 for (directory_iterator it(folder); |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
273 it != end_it; |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
274 ++it) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
275 { |
889 | 276 std::string path = it->path().string(); |
277 | |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
278 if (is_directory(it->status())) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
279 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
280 if (isRecursive) |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
281 { |
889 | 282 ScanFolderForPlugins(path, true); |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
283 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
284 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
285 else |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
286 { |
1323
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
287 std::string extension = boost::filesystem::extension(it->path()); |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
288 Toolbox::ToLowerCase(extension); |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
289 |
5a92665dee23
Sample plugin: Serve folders
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
290 if (extension == PLUGIN_EXTENSION) |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
291 { |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
292 LOG(INFO) << "Found a shared library: " << it->path(); |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
293 |
1297 | 294 SharedLibrary plugin(path); |
295 if (IsOrthancPlugin(plugin)) | |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
296 { |
1297 | 297 RegisterPlugin(path); |
888
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
298 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
299 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
300 } |
d44b845c1c89
recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
887
diff
changeset
|
301 } |
887 | 302 } |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
303 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
304 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
305 void PluginsManager::ListPlugins(std::list<std::string>& result) const |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
306 { |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
307 result.clear(); |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
308 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
309 for (Plugins::const_iterator it = plugins_.begin(); |
1304 | 310 it != plugins_.end(); ++it) |
1232
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
311 { |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
312 result.push_back(it->first); |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
313 } |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
314 } |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
315 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
316 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
317 bool PluginsManager::HasPlugin(const std::string& name) const |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
318 { |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
319 return plugins_.find(name) != plugins_.end(); |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
320 } |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
321 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
322 |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
323 const std::string& PluginsManager::GetPluginVersion(const std::string& name) const |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
324 { |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
325 Plugins::const_iterator it = plugins_.find(name); |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
326 if (it == plugins_.end()) |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
327 { |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
328 throw OrthancException(ErrorCode_ParameterOutOfRange); |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
329 } |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
330 else |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
331 { |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
332 return it->second->GetVersion(); |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
333 } |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
334 } |
f1c01451a8ee
Introspection of plugins, Plugins can extend Orthanc Explorer with custom JavaScript
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1200
diff
changeset
|
335 |
887 | 336 } |