annotate Plugins/Engine/PluginsManager.cpp @ 889:d2a393061eb6 plugins

typos
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 14 Jun 2014 20:12:11 +0200
parents d44b845c1c89
children f57802f8b4dc
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
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 /**
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 * gcc would complain about "ISO C++ forbids casting between
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 * pointer-to-function and pointer-to-object" without the trick
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 * below, that is known as "the POSIX.1-2003 (Technical Corrigendum
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 * 1) workaround". See the man page of "dlsym()".
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 * http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 * http://stackoverflow.com/a/14543811/881731
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 **/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 Initialize initialize;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 *(void **) (&initialize) = plugin.GetFunction("OrthancPluginInitialize");
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 assert(initialize != NULL);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 int32_t error = initialize(&context);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if (error != 0)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 LOG(ERROR) << "Error while initializing plugin " << plugin.GetPath()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 << " (code " << error << ")";
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 throw OrthancException(ErrorCode_SharedLibrary);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 static void CallFinalize(SharedLibrary& plugin)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 typedef void (*Finalize) ();
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 Finalize finalize;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 *(void **) (&finalize) = plugin.GetFunction("OrthancPluginFinalize");
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 assert(finalize != NULL);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 finalize();
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
92 static const char* CallGetName(SharedLibrary& plugin)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
93 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
94 typedef const char* (*GetName) ();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
95
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
96 GetName getName;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
97 *(void **) (&getName) = plugin.GetFunction("OrthancPluginGetName");
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
98 assert(getName != NULL);
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
99
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
100 return getName();
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
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
103
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
104 static const char* CallGetVersion(SharedLibrary& plugin)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
105 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
106 typedef const char* (*GetVersion) ();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
107
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
108 GetVersion getVersion;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
109 *(void **) (&getVersion) = plugin.GetFunction("OrthancPluginGetVersion");
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
110 assert(getVersion != NULL);
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
111
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
112 return getVersion();
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
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 static void LogError(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 LOG(ERROR) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 static void LogWarning(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 LOG(WARNING) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 static void LogInfo(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 LOG(INFO) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 static int32_t InvokeService(const char* serviceName,
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 const void* serviceParameters)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 // TODO
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 return 0;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 PluginsManager::PluginsManager()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 context_.orthancVersion = ORTHANC_VERSION;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 context_.InvokeService = InvokeService;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 context_.LogError = LogError;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 context_.LogWarning = LogWarning;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 context_.LogInfo = LogInfo;
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 PluginsManager::~PluginsManager()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 for (Plugins::iterator it = plugins_.begin(); it != plugins_.end(); it++)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
151 if (it->second != NULL)
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 {
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
153 CallFinalize(*(it->second));
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
154 delete it->second;
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
159
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
160 static bool IsOrthancPlugin(SharedLibrary& library)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
161 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
162 return (library.HasFunction("OrthancPluginInitialize") &&
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
163 library.HasFunction("OrthancPluginFinalize") &&
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
164 library.HasFunction("OrthancPluginGetName") &&
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
165 library.HasFunction("OrthancPluginGetVersion"));
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
166 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
167
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 void PluginsManager::RegisterPlugin(const std::string& path)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 std::auto_ptr<SharedLibrary> plugin(new SharedLibrary(path));
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
173 if (!IsOrthancPlugin(*plugin))
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 LOG(ERROR) << "Plugin " << plugin->GetPath()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 << " does not declare the proper entry functions";
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 throw OrthancException(ErrorCode_SharedLibrary);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
180 std::string name(CallGetName(*plugin));
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
181 if (plugins_.find(name) != plugins_.end())
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
182 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
183 LOG(ERROR) << "Plugin '" << name << "' already registered";
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
184 throw OrthancException(ErrorCode_SharedLibrary);
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
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
187 LOG(WARNING) << "Registering plugin '" << name
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
188 << "' (version " << CallGetVersion(*plugin) << ")";
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 CallInitialize(*plugin, context_);
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 plugins_[name] = plugin.release();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
193 }
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
194
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
195
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
196 void PluginsManager::ScanFolderForPlugins(const std::string& folder,
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
197 bool isRecursive)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
198 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
199 using namespace boost::filesystem;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
200
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
201 if (!exists(folder))
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
202 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
203 return;
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
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
206 LOG(INFO) << "Scanning folder " << folder << " for plugins";
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
207
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
208 directory_iterator end_it; // default construction yields past-the-end
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
209 for (directory_iterator it(folder);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
210 it != end_it;
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
211 ++it)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
212 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
213 std::string path = it->path().string();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
214
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
215 if (is_directory(it->status()))
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 if (isRecursive)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
218 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
219 ScanFolderForPlugins(path, true);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
220 }
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 else
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 if (boost::filesystem::extension(it->path()) == PLUGIN_EXTENSION)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
225 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
226 LOG(INFO) << "Found a shared library: " << it->path();
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
227
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
228 try
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
229 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
230 SharedLibrary plugin(path);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
231 if (IsOrthancPlugin(plugin))
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
232 {
889
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 888
diff changeset
233 RegisterPlugin(path);
888
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
234 }
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 catch (OrthancException&)
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
237 {
d44b845c1c89 recursive scan for plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 887
diff changeset
238 }
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 }
887
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 }