changeset 24:17f849b2af34

sharing plugin initialization code
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 12 Jul 2018 12:17:39 +0200
parents b2ff1cd2907a
children 69a94267cdea
files Framework/MySQL/MySQLDatabase.cpp Framework/MySQL/MySQLDatabase.h Framework/Plugins/OrthancCppDatabasePlugin.h Framework/Plugins/PluginInitialization.cpp Framework/Plugins/PluginInitialization.h MySQL/CMakeLists.txt MySQL/Plugins/IndexPlugin.cpp MySQL/Plugins/MySQLIndex.cpp MySQL/Plugins/StoragePlugin.cpp PostgreSQL/CMakeLists.txt PostgreSQL/Plugins/IndexPlugin.cpp PostgreSQL/Plugins/StoragePlugin.cpp SQLite/CMakeLists.txt SQLite/Plugins/IndexPlugin.cpp
diffstat 14 files changed, 183 insertions(+), 144 deletions(-) [+]
line wrap: on
line diff
--- 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;
+  }
 }
--- 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);
   };
 }
--- 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 =
--- /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 <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "PluginInitialization.h"
+
+#include <Core/Logging.h>
+
+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;
+  }
+}
--- /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 <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include <orthanc/OrthancCPlugin.h>
+
+#include <string>
+
+namespace OrthancDatabases
+{
+  bool InitializePlugin(OrthancPluginContext* context,
+                        const std::string& shortName,
+                        const std::string& description);
+}
--- 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}
   )
--- 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 <Plugins/Samples/Common/OrthancPluginCppWrapper.h>
 #include <Core/Logging.h>
 
 static std::auto_ptr<OrthancDatabases::MySQLIndex> 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"))
--- 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_);
--- 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 <Core/Logging.h>
 
 
-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"))
--- 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}
   )
--- 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 <Core/Logging.h>
 
 static std::auto_ptr<OrthancDatabases::PostgreSQLIndex> 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"))
--- 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 <Core/Logging.h>
 
-
-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"))
--- 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}
   )
--- 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 <Plugins/Samples/Common/OrthancPluginCppWrapper.h>
 #include <Core/Logging.h>
 
 static std::auto_ptr<OrthancDatabases::SQLiteIndex> 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);