comparison Odbc/Plugins/IndexPlugin.cpp @ 329:b5fb8b77ce4d

initial commit of ODBC framework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 Aug 2021 20:08:53 +0200
parents
children 674bbb9d1c83
comparison
equal deleted inserted replaced
328:6a49c495c940 329:b5fb8b77ce4d
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "OdbcIndex.h"
23
24 #include "../../Framework/Plugins/PluginInitialization.h"
25 #include "../../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
26
27 #include <Logging.h>
28
29
30 #if defined(_WIN32)
31 # warning Strings have not been tested on Windows (UTF-16 issues ahead)!
32 # include <windows.h>
33 #else
34 # include <ltdl.h>
35 # include <libltdl/lt_dlloader.h>
36 #endif
37
38
39 static const char* const KEY_ODBC = "Odbc";
40
41
42 extern "C"
43 {
44 #if !defined(_WIN32)
45 extern lt_dlvtable *dlopen_LTX_get_vtable(lt_user_data loader_data);
46 #endif
47
48
49 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
50 {
51 if (!OrthancDatabases::InitializePlugin(context, "ODBC", true))
52 {
53 return -1;
54 }
55
56 #if !defined(_WIN32)
57 lt_dlinit();
58
59 /**
60 * The following call is necessary for "libltdl" to access the
61 * "dlopen()" primitives if statically linking. Otherwise, only the
62 * "preopen" primitives are available.
63 **/
64 lt_dlloader_add(dlopen_LTX_get_vtable(NULL));
65 #endif
66
67 OrthancPlugins::OrthancConfiguration configuration;
68
69 if (!configuration.IsSection(KEY_ODBC))
70 {
71 LOG(WARNING) << "No available configuration for the ODBC index plugin";
72 return 0;
73 }
74
75 OrthancPlugins::OrthancConfiguration odbc;
76 configuration.GetSection(odbc, KEY_ODBC);
77
78 bool enable;
79 if (!odbc.LookupBooleanValue(enable, "EnableIndex") ||
80 !enable)
81 {
82 LOG(WARNING) << "The ODBC index is currently disabled, set \"EnableIndex\" "
83 << "to \"true\" in the \"" << KEY_ODBC << "\" section of the configuration file of Orthanc";
84 return 0;
85 }
86
87 try
88 {
89 const std::string connectionString = odbc.GetStringValue("IndexConnectionString", "");
90 const unsigned int countConnections = odbc.GetUnsignedIntegerValue("IndexConnectionsCount", 1);
91 const unsigned int maxConnectionRetries = odbc.GetUnsignedIntegerValue("MaxConnectionRetries", 10);
92 const unsigned int connectionRetryInterval = odbc.GetUnsignedIntegerValue("ConnectionRetryInterval", 5);
93
94 if (connectionString.empty())
95 {
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
97 "No connection string provided for the ODBC index");
98 }
99
100 std::unique_ptr<OrthancDatabases::OdbcIndex> index(new OrthancDatabases::OdbcIndex(context, connectionString));
101 index->SetMaxConnectionRetries(maxConnectionRetries);
102 index->SetConnectionRetryInterval(connectionRetryInterval);
103
104 OrthancDatabases::IndexBackend::Register(index.release(), countConnections, maxConnectionRetries);
105 }
106 catch (Orthanc::OrthancException& e)
107 {
108 LOG(ERROR) << e.What();
109 return -1;
110 }
111 catch (...)
112 {
113 LOG(ERROR) << "Native exception while initializing the plugin";
114 return -1;
115 }
116
117 return 0;
118 }
119
120
121 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
122 {
123 LOG(WARNING) << "ODBC index is finalizing";
124 OrthancDatabases::IndexBackend::Finalize();
125 }
126
127
128 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
129 {
130 return "odbc-index";
131 }
132
133
134 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
135 {
136 return ORTHANC_PLUGIN_VERSION;
137 }
138 }