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