annotate Plugins/Engine/PluginsManager.cpp @ 887:4066e6f2d134 plugins

PluginsManager
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 14 Jun 2014 19:20:38 +0200
parents
children d44b845c1c89
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>
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace Orthanc
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 static void CallInitialize(SharedLibrary& plugin,
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 const OrthancPluginContext& context)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 typedef int32_t (*Initialize) (const OrthancPluginContext*);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 /**
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 * gcc would complain about "ISO C++ forbids casting between
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 * pointer-to-function and pointer-to-object" without the trick
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 * below, that is known as "the POSIX.1-2003 (Technical Corrigendum
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 * 1) workaround". See the man page of "dlsym()".
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 * http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 * http://stackoverflow.com/a/14543811/881731
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 **/
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 Initialize initialize;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 *(void **) (&initialize) = plugin.GetFunction("OrthancPluginInitialize");
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 assert(initialize != NULL);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 int32_t error = initialize(&context);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 if (error != 0)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 LOG(ERROR) << "Error while initializing plugin " << plugin.GetPath()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 << " (code " << error << ")";
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 throw OrthancException(ErrorCode_SharedLibrary);
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
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 static void CallFinalize(SharedLibrary& plugin)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 typedef void (*Finalize) ();
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 Finalize finalize;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 *(void **) (&finalize) = plugin.GetFunction("OrthancPluginFinalize");
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 assert(finalize != NULL);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 finalize();
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
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 static void LogError(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 LOG(ERROR) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 static void LogWarning(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 LOG(WARNING) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 static void LogInfo(const char* str)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 LOG(INFO) << str;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 static int32_t InvokeService(const char* serviceName,
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 const void* serviceParameters)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 // TODO
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 return 0;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 PluginsManager::PluginsManager()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 context_.orthancVersion = ORTHANC_VERSION;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 context_.InvokeService = InvokeService;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 context_.LogError = LogError;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 context_.LogWarning = LogWarning;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 context_.LogInfo = LogInfo;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 PluginsManager::~PluginsManager()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 for (Plugins::iterator it = plugins_.begin(); it != plugins_.end(); it++)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 if (*it != NULL)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 CallFinalize(**it);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 delete *it;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
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 void PluginsManager::RegisterPlugin(const std::string& path)
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 std::auto_ptr<SharedLibrary> plugin(new SharedLibrary(path));
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 if (!plugin->HasFunction("OrthancPluginInitialize") ||
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 !plugin->HasFunction("OrthancPluginFinalize"))
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 LOG(ERROR) << "Plugin " << plugin->GetPath()
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 << " does not declare the proper entry functions";
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 throw OrthancException(ErrorCode_SharedLibrary);
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 LOG(WARNING) << "Registering plugin " << path;
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 CallInitialize(*plugin, context_);
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 plugins_.push_back(plugin.release());
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
4066e6f2d134 PluginsManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }