# HG changeset patch # User Sebastien Jodogne # Date 1531390659 -7200 # Node ID 17f849b2af345ca41b5463898d10745caa6cc7db # Parent b2ff1cd2907a3723c748c3f14a5409a716de3dd6 sharing plugin initialization code diff -r b2ff1cd2907a -r 17f849b2af34 Framework/MySQL/MySQLDatabase.cpp --- a/Framework/MySQL/MySQLDatabase.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/Framework/MySQL/MySQLDatabase.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -300,18 +300,6 @@ } - static void CheckAlphanumericString(const std::string& name) - { - for (size_t i = 0; i < name.length(); i++) - { - if (!isalnum(name[i])) - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); - } - } - } - - bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction, const std::string& name) { @@ -320,7 +308,10 @@ throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); } - CheckAlphanumericString(name); + if (!IsAlphanumericString(name)) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } Query query("SELECT COUNT(*) FROM information_schema.TABLES WHERE " "(TABLE_SCHEMA = ${database}) AND (TABLE_NAME = ${table})", true); @@ -349,7 +340,10 @@ throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); } - CheckAlphanumericString(name); + if (!IsAlphanumericString(name)) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } Query query("SELECT COUNT(*) FROM information_schema.SCHEMATA " "WHERE SCHEMA_NAME = ${database}", true); @@ -464,4 +458,18 @@ { mysql_library_end(); } + + + bool MySQLDatabase::IsAlphanumericString(const std::string& s) + { + for (size_t i = 0; i < s.length(); i++) + { + if (!isalnum(s[i])) + { + return false; + } + } + + return true; + } } diff -r b2ff1cd2907a -r 17f849b2af34 Framework/MySQL/MySQLDatabase.h --- a/Framework/MySQL/MySQLDatabase.h Thu Jul 12 10:44:17 2018 +0200 +++ b/Framework/MySQL/MySQLDatabase.h Thu Jul 12 12:17:39 2018 +0200 @@ -94,5 +94,7 @@ virtual ITransaction* CreateTransaction(bool isImplicit); static void GlobalFinalization(); + + static bool IsAlphanumericString(const std::string& s); }; } diff -r b2ff1cd2907a -r 17f849b2af34 Framework/Plugins/OrthancCppDatabasePlugin.h --- a/Framework/Plugins/OrthancCppDatabasePlugin.h Thu Jul 12 10:44:17 2018 +0200 +++ b/Framework/Plugins/OrthancCppDatabasePlugin.h Thu Jul 12 12:17:39 2018 +0200 @@ -1504,8 +1504,16 @@ if (performanceWarning) { - OrthancPluginLogWarning(context, "Performance warning: The database index plugin was compiled " - "against an old version of the Orthanc SDK, consider upgrading"); + char info[1024]; + sprintf(info, + "Performance warning: The database index plugin was compiled " + "against an old version of the Orthanc SDK (%d.%d.%d): " + "Consider upgrading to version 1.4.0 of the Orthanc SDK", + ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, + ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, + ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); + + OrthancPluginLogWarning(context, info); } OrthancPluginDatabaseContext* database = diff -r b2ff1cd2907a -r 17f849b2af34 Framework/Plugins/PluginInitialization.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Plugins/PluginInitialization.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -0,0 +1,78 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2018 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 . + **/ + + +#include "PluginInitialization.h" + +#include + +namespace OrthancDatabases +{ + static bool DisplayPerformanceWarning(const std::string& shortName) + { + (void) DisplayPerformanceWarning; // Disable warning about unused function + LOG(WARNING) << "Performance warning in " << shortName << ": " + << "Non-release build, runtime debug assertions are turned on"; + return true; + } + + + bool InitializePlugin(OrthancPluginContext* context, + const std::string& shortName, + const std::string& description) + { + Orthanc::Logging::Initialize(context); + + assert(DisplayPerformanceWarning(shortName)); + + /* Check the version of the Orthanc core */ + + bool useFallback = true; + +#if defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) // Macro introduced in Orthanc 1.3.1 +# if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 4, 0) + if (OrthancPluginCheckVersionAdvanced(context, 0, 9, 5) == 0) + { + LOG(ERROR) << "Your version of Orthanc (" << context->orthancVersion + << ") must be above 0.9.5 to run this plugin"; + return false; + } + + useFallback = false; +# endif +#endif + + if (useFallback && + OrthancPluginCheckVersion(context) == 0) + { + LOG(ERROR) << "Your version of Orthanc (" + << context->orthancVersion << ") must be above " + << ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER << "." + << ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER << "." + << ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER + << " to run this plugin"; + return false; + } + + OrthancPluginSetDescription(context, description.c_str()); + + return true; + } +} diff -r b2ff1cd2907a -r 17f849b2af34 Framework/Plugins/PluginInitialization.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/Plugins/PluginInitialization.h Thu Jul 12 12:17:39 2018 +0200 @@ -0,0 +1,33 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2018 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 . + **/ + + +#pragma once + +#include + +#include + +namespace OrthancDatabases +{ + bool InitializePlugin(OrthancPluginContext* context, + const std::string& shortName, + const std::string& description); +} diff -r b2ff1cd2907a -r 17f849b2af34 MySQL/CMakeLists.txt --- a/MySQL/CMakeLists.txt Thu Jul 12 10:44:17 2018 +0200 +++ b/MySQL/CMakeLists.txt Thu Jul 12 12:17:39 2018 +0200 @@ -23,15 +23,19 @@ ) add_library(OrthancMySQLIndex SHARED + ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp + Plugins/IndexPlugin.cpp Plugins/MySQLIndex.cpp - Plugins/IndexPlugin.cpp + ${DATABASES_SOURCES} ${AUTOGENERATED_SOURCES} ) add_library(OrthancMySQLStorage SHARED + ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp Plugins/MySQLStorageArea.cpp Plugins/StoragePlugin.cpp + ${DATABASES_SOURCES} ${AUTOGENERATED_SOURCES} ) diff -r b2ff1cd2907a -r 17f849b2af34 MySQL/Plugins/IndexPlugin.cpp --- a/MySQL/Plugins/IndexPlugin.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/MySQL/Plugins/IndexPlugin.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -21,45 +21,24 @@ #include "MySQLIndex.h" #include "../../Framework/MySQL/MySQLDatabase.h" +#include "../../Framework/Plugins/PluginInitialization.h" -#include #include static std::auto_ptr backend_; -static bool DisplayPerformanceWarning() -{ - (void) DisplayPerformanceWarning; // Disable warning about unused function - LOG(WARNING) << "Performance warning in MySQL index: " - << "Non-release build, runtime debug assertions are turned on"; - return true; -} - - extern "C" { ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) { - Orthanc::Logging::Initialize(context); - - assert(DisplayPerformanceWarning()); - - /* Check the version of the Orthanc core */ - if (OrthancPluginCheckVersion(context) == 0) + if (!OrthancDatabases::InitializePlugin + (context, "MySQL index", + "Stores the Orthanc index into a MySQL database.")) { - char info[1024]; - sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", - context->orthancVersion, - ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); - OrthancPluginLogError(context, info); return -1; } - OrthancPluginSetDescription(context, "Stores the Orthanc index into a MySQL database."); - OrthancPlugins::OrthancConfiguration configuration(context); if (!configuration.IsSection("MySQL")) diff -r b2ff1cd2907a -r 17f849b2af34 MySQL/Plugins/MySQLIndex.cpp --- a/MySQL/Plugins/MySQLIndex.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/MySQL/Plugins/MySQLIndex.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -56,6 +56,11 @@ throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); } + if (!MySQLDatabase::IsAlphanumericString(parameters_.GetDatabase())) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + if (clearAll_) { MySQLDatabase::ClearDatabase(parameters_); diff -r b2ff1cd2907a -r 17f849b2af34 MySQL/Plugins/StoragePlugin.cpp --- a/MySQL/Plugins/StoragePlugin.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/MySQL/Plugins/StoragePlugin.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -21,42 +21,22 @@ #include "MySQLStorageArea.h" #include "../../Framework/MySQL/MySQLDatabase.h" +#include "../../Framework/Plugins/PluginInitialization.h" #include -static bool DisplayPerformanceWarning() -{ - (void) DisplayPerformanceWarning; // Disable warning about unused function - LOG(WARNING) << "Performance warning in MySQL storage area: " - << "Non-release build, runtime debug assertions are turned on"; - return true; -} - - extern "C" { ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) { - Orthanc::Logging::Initialize(context); - - assert(DisplayPerformanceWarning()); - - /* Check the version of the Orthanc core */ - if (OrthancPluginCheckVersion(context) == 0) + if (!OrthancDatabases::InitializePlugin + (context, "MySQL storage area", + "Stores the Orthanc storage area into a MySQL database.")) { - char info[1024]; - sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", - context->orthancVersion, - ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); - OrthancPluginLogError(context, info); return -1; } - OrthancPluginSetDescription(context, "Stores the Orthanc storage area into a MySQL database."); - OrthancPlugins::OrthancConfiguration configuration(context); if (!configuration.IsSection("MySQL")) diff -r b2ff1cd2907a -r 17f849b2af34 PostgreSQL/CMakeLists.txt --- a/PostgreSQL/CMakeLists.txt Thu Jul 12 10:44:17 2018 +0200 +++ b/PostgreSQL/CMakeLists.txt Thu Jul 12 12:17:39 2018 +0200 @@ -23,15 +23,19 @@ ) add_library(OrthancPostgreSQLIndex SHARED + ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp + Plugins/IndexPlugin.cpp Plugins/PostgreSQLIndex.cpp - Plugins/IndexPlugin.cpp + ${DATABASES_SOURCES} ${AUTOGENERATED_SOURCES} ) add_library(OrthancPostgreSQLStorage SHARED + ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp Plugins/PostgreSQLStorageArea.cpp Plugins/StoragePlugin.cpp + ${DATABASES_SOURCES} ${AUTOGENERATED_SOURCES} ) diff -r b2ff1cd2907a -r 17f849b2af34 PostgreSQL/Plugins/IndexPlugin.cpp --- a/PostgreSQL/Plugins/IndexPlugin.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/PostgreSQL/Plugins/IndexPlugin.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -20,45 +20,24 @@ #include "PostgreSQLIndex.h" +#include "../../Framework/Plugins/PluginInitialization.h" #include static std::auto_ptr backend_; - -static bool DisplayPerformanceWarning() -{ - (void) DisplayPerformanceWarning; // Disable warning about unused function - LOG(WARNING) << "Performance warning in PostgreSQL index: " - << "Non-release build, runtime debug assertions are turned on"; - return true; -} - - extern "C" { ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) { - Orthanc::Logging::Initialize(context); - - assert(DisplayPerformanceWarning()); - - /* Check the version of the Orthanc core */ - if (OrthancPluginCheckVersion(context) == 0) + if (!OrthancDatabases::InitializePlugin + (context, "PostgreSQL index", + "Stores the Orthanc index into a PostgreSQL database.")) { - char info[1024]; - sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", - context->orthancVersion, - ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); - OrthancPluginLogError(context, info); return -1; } - OrthancPluginSetDescription(context, "Stores the Orthanc index into a PostgreSQL database."); - OrthancPlugins::OrthancConfiguration configuration(context); if (!configuration.IsSection("PostgreSQL")) diff -r b2ff1cd2907a -r 17f849b2af34 PostgreSQL/Plugins/StoragePlugin.cpp --- a/PostgreSQL/Plugins/StoragePlugin.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/PostgreSQL/Plugins/StoragePlugin.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -19,44 +19,22 @@ **/ -#include "../../Framework/Plugins/StorageBackend.h" #include "PostgreSQLStorageArea.h" +#include "../../Framework/Plugins/PluginInitialization.h" #include - -static bool DisplayPerformanceWarning() -{ - (void) DisplayPerformanceWarning; // Disable warning about unused function - LOG(WARNING) << "Performance warning in PostgreSQL storage area: " - << "Non-release build, runtime debug assertions are turned on"; - return true; -} - - extern "C" { ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) { - Orthanc::Logging::Initialize(context); - - assert(DisplayPerformanceWarning()); - - /* Check the version of the Orthanc core */ - if (OrthancPluginCheckVersion(context) == 0) + if (!OrthancDatabases::InitializePlugin + (context, "PostgreSQL storage area", + "Stores the Orthanc storage area into a PostgreSQL database.")) { - char info[1024]; - sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", - context->orthancVersion, - ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); - OrthancPluginLogError(context, info); return -1; } - OrthancPluginSetDescription(context, "Stores the Orthanc storage area into a PostgreSQL database."); - OrthancPlugins::OrthancConfiguration configuration(context); if (!configuration.IsSection("PostgreSQL")) diff -r b2ff1cd2907a -r 17f849b2af34 SQLite/CMakeLists.txt --- a/SQLite/CMakeLists.txt Thu Jul 12 10:44:17 2018 +0200 +++ b/SQLite/CMakeLists.txt Thu Jul 12 12:17:39 2018 +0200 @@ -23,8 +23,10 @@ ) add_library(OrthancSQLiteIndex SHARED + ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp + Plugins/IndexPlugin.cpp Plugins/SQLiteIndex.cpp - Plugins/IndexPlugin.cpp + ${DATABASES_SOURCES} ${AUTOGENERATED_SOURCES} ) diff -r b2ff1cd2907a -r 17f849b2af34 SQLite/Plugins/IndexPlugin.cpp --- a/SQLite/Plugins/IndexPlugin.cpp Thu Jul 12 10:44:17 2018 +0200 +++ b/SQLite/Plugins/IndexPlugin.cpp Thu Jul 12 12:17:39 2018 +0200 @@ -20,45 +20,24 @@ #include "SQLiteIndex.h" +#include "../../Framework/Plugins/PluginInitialization.h" -#include #include static std::auto_ptr backend_; -static bool DisplayPerformanceWarning() -{ - (void) DisplayPerformanceWarning; // Disable warning about unused function - LOG(WARNING) << "Performance warning in SQLite index: " - << "Non-release build, runtime debug assertions are turned on"; - return true; -} - - extern "C" { ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) { - Orthanc::Logging::Initialize(context); - - assert(DisplayPerformanceWarning()); - - /* Check the version of the Orthanc core */ - if (OrthancPluginCheckVersion(context) == 0) + if (!OrthancDatabases::InitializePlugin + (context, "SQLite index", + "Stores the Orthanc index into a SQLite database.")) { - char info[1024]; - sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin", - context->orthancVersion, - ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, - ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); - OrthancPluginLogError(context, info); return -1; } - OrthancPluginSetDescription(context, "Stores the Orthanc index into a SQLite database."); - #if 0 OrthancPlugins::OrthancConfiguration configuration(context);