Mercurial > hg > orthanc-databases
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Odbc/Plugins/IndexPlugin.cpp Tue Aug 10 20:08:53 2021 +0200 @@ -0,0 +1,138 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2021 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + **/ + + +#include "OdbcIndex.h" + +#include "../../Framework/Plugins/PluginInitialization.h" +#include "../../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" + +#include <Logging.h> + + +#if defined(_WIN32) +# warning Strings have not been tested on Windows (UTF-16 issues ahead)! +# include <windows.h> +#else +# include <ltdl.h> +# include <libltdl/lt_dlloader.h> +#endif + + +static const char* const KEY_ODBC = "Odbc"; + + +extern "C" +{ +#if !defined(_WIN32) + extern lt_dlvtable *dlopen_LTX_get_vtable(lt_user_data loader_data); +#endif + + + ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) + { + if (!OrthancDatabases::InitializePlugin(context, "ODBC", true)) + { + return -1; + } + +#if !defined(_WIN32) + lt_dlinit(); + + /** + * The following call is necessary for "libltdl" to access the + * "dlopen()" primitives if statically linking. Otherwise, only the + * "preopen" primitives are available. + **/ + lt_dlloader_add(dlopen_LTX_get_vtable(NULL)); +#endif + + OrthancPlugins::OrthancConfiguration configuration; + + if (!configuration.IsSection(KEY_ODBC)) + { + LOG(WARNING) << "No available configuration for the ODBC index plugin"; + return 0; + } + + OrthancPlugins::OrthancConfiguration odbc; + configuration.GetSection(odbc, KEY_ODBC); + + bool enable; + if (!odbc.LookupBooleanValue(enable, "EnableIndex") || + !enable) + { + LOG(WARNING) << "The ODBC index is currently disabled, set \"EnableIndex\" " + << "to \"true\" in the \"" << KEY_ODBC << "\" section of the configuration file of Orthanc"; + return 0; + } + + try + { + const std::string connectionString = odbc.GetStringValue("IndexConnectionString", ""); + const unsigned int countConnections = odbc.GetUnsignedIntegerValue("IndexConnectionsCount", 1); + const unsigned int maxConnectionRetries = odbc.GetUnsignedIntegerValue("MaxConnectionRetries", 10); + const unsigned int connectionRetryInterval = odbc.GetUnsignedIntegerValue("ConnectionRetryInterval", 5); + + if (connectionString.empty()) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, + "No connection string provided for the ODBC index"); + } + + std::unique_ptr<OrthancDatabases::OdbcIndex> index(new OrthancDatabases::OdbcIndex(context, connectionString)); + index->SetMaxConnectionRetries(maxConnectionRetries); + index->SetConnectionRetryInterval(connectionRetryInterval); + + OrthancDatabases::IndexBackend::Register(index.release(), countConnections, maxConnectionRetries); + } + catch (Orthanc::OrthancException& e) + { + LOG(ERROR) << e.What(); + return -1; + } + catch (...) + { + LOG(ERROR) << "Native exception while initializing the plugin"; + return -1; + } + + return 0; + } + + + ORTHANC_PLUGINS_API void OrthancPluginFinalize() + { + LOG(WARNING) << "ODBC index is finalizing"; + OrthancDatabases::IndexBackend::Finalize(); + } + + + ORTHANC_PLUGINS_API const char* OrthancPluginGetName() + { + return "odbc-index"; + } + + + ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() + { + return ORTHANC_PLUGIN_VERSION; + } +}