annotate Plugins/Engine/PluginsManager.cpp @ 893:f57802f8b4dc plugins

plugins for windows
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Jun 2014 16:14:56 +0200
parents d2a393061eb6
children 690aeb4cb899
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
4066e6f2d134 PluginsManager
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
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "PluginsManager.h"
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <glog/logging.h>
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <cassert>
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <memory>
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
38 #include <boost/filesystem.hpp>
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
39
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
40 #ifdef WIN32
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
41 #define PLUGIN_EXTENSION ".dll"
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
42 #elif defined(__linux)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
43 #define PLUGIN_EXTENSION ".so"
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
44 #else
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
45 #error Support your platform here
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
46 #endif
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
47
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 namespace Orthanc
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 static void CallInitialize(SharedLibrary& plugin,
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 const OrthancPluginContext& context)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 typedef int32_t (*Initialize) (const OrthancPluginContext*);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
56 #if defined(_WIN32)
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
57 Initialize initialize = (Initialize) plugin.GetFunction("OrthancPluginInitialize");
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
58 #else
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 /**
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 * gcc would complain about "ISO C++ forbids casting between
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 * pointer-to-function and pointer-to-object" without the trick
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 * below, that is known as "the POSIX.1-2003 (Technical Corrigendum
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 * 1) workaround". See the man page of "dlsym()".
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 * http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 * http://stackoverflow.com/a/14543811/881731
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 **/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 Initialize initialize;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 *(void **) (&initialize) = plugin.GetFunction("OrthancPluginInitialize");
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
70 #endif
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
71
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 assert(initialize != NULL);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 int32_t error = initialize(&context);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if (error != 0)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 LOG(ERROR) << "Error while initializing plugin " << plugin.GetPath()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 << " (code " << error << ")";
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 throw OrthancException(ErrorCode_SharedLibrary);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 static void CallFinalize(SharedLibrary& plugin)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 typedef void (*Finalize) ();
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
88 #if defined(_WIN32)
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
89 Finalize finalize = (Finalize) plugin.GetFunction("OrthancPluginFinalize");
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
90 #else
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 Finalize finalize;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 *(void **) (&finalize) = plugin.GetFunction("OrthancPluginFinalize");
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
93 #endif
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
94
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 assert(finalize != NULL);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 finalize();
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
100 static const char* CallGetName(SharedLibrary& plugin)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
101 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
102 typedef const char* (*GetName) ();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
103
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
104 #if defined(_WIN32)
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
105 GetName getName = (GetName) plugin.GetFunction("OrthancPluginGetName");
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
106 #else
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
107 GetName getName;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
108 *(void **) (&getName) = plugin.GetFunction("OrthancPluginGetName");
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
109 #endif
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
110
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
111 assert(getName != NULL);
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
112 return getName();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
113 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
114
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
115
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
116 static const char* CallGetVersion(SharedLibrary& plugin)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
117 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
118 typedef const char* (*GetVersion) ();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
119
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
120 #if defined(_WIN32)
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
121 GetVersion getVersion = (GetVersion) plugin.GetFunction("OrthancPluginGetVersion");
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
122 #else
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
123 GetVersion getVersion;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
124 *(void **) (&getVersion) = plugin.GetFunction("OrthancPluginGetVersion");
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
125 #endif
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
126
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
127 assert(getVersion != NULL);
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
128 return getVersion();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
129 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
130
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
131
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 static void LogError(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 LOG(ERROR) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 static void LogWarning(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 LOG(WARNING) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 static void LogInfo(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 LOG(INFO) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 static int32_t InvokeService(const char* serviceName,
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 const void* serviceParameters)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 // TODO
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 return 0;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 PluginsManager::PluginsManager()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 context_.orthancVersion = ORTHANC_VERSION;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 context_.InvokeService = InvokeService;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 context_.LogError = LogError;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 context_.LogWarning = LogWarning;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 context_.LogInfo = LogInfo;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 PluginsManager::~PluginsManager()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 for (Plugins::iterator it = plugins_.begin(); it != plugins_.end(); it++)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
167 if (it->second != NULL)
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 {
893
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
169 LOG(WARNING) << "Unregistering plugin '" << CallGetName(*it->second)
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
170 << "' (version " << CallGetVersion(*it->second) << ")";
f57802f8b4dc plugins for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 889
diff changeset
171
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
172 CallFinalize(*(it->second));
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
173 delete it->second;
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
178
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
179 static bool IsOrthancPlugin(SharedLibrary& library)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
180 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
181 return (library.HasFunction("OrthancPluginInitialize") &&
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
182 library.HasFunction("OrthancPluginFinalize") &&
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
183 library.HasFunction("OrthancPluginGetName") &&
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
184 library.HasFunction("OrthancPluginGetVersion"));
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
185 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
186
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 void PluginsManager::RegisterPlugin(const std::string& path)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 std::auto_ptr<SharedLibrary> plugin(new SharedLibrary(path));
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
192 if (!IsOrthancPlugin(*plugin))
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 LOG(ERROR) << "Plugin " << plugin->GetPath()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 << " does not declare the proper entry functions";
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 throw OrthancException(ErrorCode_SharedLibrary);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
199 std::string name(CallGetName(*plugin));
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
200 if (plugins_.find(name) != plugins_.end())
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
201 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
202 LOG(ERROR) << "Plugin '" << name << "' already registered";
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
203 throw OrthancException(ErrorCode_SharedLibrary);
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
204 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
205
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
206 LOG(WARNING) << "Registering plugin '" << name
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
207 << "' (version " << CallGetVersion(*plugin) << ")";
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 CallInitialize(*plugin, context_);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
211 plugins_[name] = plugin.release();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
212 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
213
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
214
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
215 void PluginsManager::ScanFolderForPlugins(const std::string& folder,
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
216 bool isRecursive)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
217 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
218 using namespace boost::filesystem;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
219
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
220 if (!exists(folder))
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
221 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
222 return;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
223 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
224
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
225 LOG(INFO) << "Scanning folder " << folder << " for plugins";
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
226
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
227 directory_iterator end_it; // default construction yields past-the-end
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
228 for (directory_iterator it(folder);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
229 it != end_it;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
230 ++it)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
231 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
232 std::string path = it->path().string();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
233
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
234 if (is_directory(it->status()))
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
235 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
236 if (isRecursive)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
237 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
238 ScanFolderForPlugins(path, true);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
239 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
240 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
241 else
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
242 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
243 if (boost::filesystem::extension(it->path()) == PLUGIN_EXTENSION)
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(INFO) << "Found a shared library: " << it->path();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
246
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
247 try
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
248 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
249 SharedLibrary plugin(path);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
250 if (IsOrthancPlugin(plugin))
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
251 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
252 RegisterPlugin(path);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
253 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
254 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
255 catch (OrthancException&)
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 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
259 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
260 }
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }