changeset 2941:e292798f9980

OrthancConfiguration::SetServerIndex()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2018 15:16:32 +0100
parents 4767d36679ed
children 9f6716008066 f395460af74d
files OrthancServer/OrthancConfiguration.cpp OrthancServer/OrthancConfiguration.h OrthancServer/main.cpp
diffstat 3 files changed, 70 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancConfiguration.cpp	Thu Nov 29 11:47:00 2018 +0100
+++ b/OrthancServer/OrthancConfiguration.cpp	Thu Nov 29 15:16:32 2018 +0100
@@ -40,6 +40,8 @@
 #include "../Core/SystemToolbox.h"
 #include "../Core/Toolbox.h"
 
+#include "ServerIndex.h"
+
 namespace Orthanc
 {
   static void AddFileToConfiguration(Json::Value& target,
@@ -717,4 +719,16 @@
 
     return a != b;
   }
+
+
+  void OrthancConfiguration::SetServerIndex(ServerIndex& index)
+  {
+    serverIndex_ = &index;
+  }
+
+
+  void OrthancConfiguration::ResetServerIndex()
+  {
+    serverIndex_ = NULL;
+  }
 }
--- a/OrthancServer/OrthancConfiguration.h	Thu Nov 29 11:47:00 2018 +0100
+++ b/OrthancServer/OrthancConfiguration.h	Thu Nov 29 15:16:32 2018 +0100
@@ -46,6 +46,7 @@
 namespace Orthanc
 {
   class MongooseServer;
+  class ServerIndex;
   
   class OrthancConfiguration : public boost::noncopyable
   {
@@ -56,6 +57,7 @@
     std::string              configurationAbsolutePath_;
     FontRegistry             fontRegistry_;
     const char*              configurationFileArg_;
+    ServerIndex*             serverIndex_;
 
     OrthancConfiguration() :
       configurationFileArg_(NULL)
@@ -201,5 +203,9 @@
     void SetDefaultEncoding(Encoding encoding);
 
     bool HasConfigurationChanged() const;
+
+    void SetServerIndex(ServerIndex& index);
+
+    void ResetServerIndex();
   };
 }
--- a/OrthancServer/main.cpp	Thu Nov 29 11:47:00 2018 +0100
+++ b/OrthancServer/main.cpp	Thu Nov 29 15:16:32 2018 +0100
@@ -1031,6 +1031,54 @@
 }
 
 
+
+namespace
+{
+  class ServerContextConfigurator : public boost::noncopyable
+  {
+  private:
+    ServerContext&   context_;
+    OrthancPlugins*  plugins_;
+
+  public:
+    ServerContextConfigurator(ServerContext& context,
+                              OrthancPlugins* plugins) :
+      context_(context),
+      plugins_(plugins)
+    {
+      {
+        OrthancConfiguration::WriterLock lock;
+        lock.GetConfiguration().SetServerIndex(context.GetIndex());
+      }
+
+#if ORTHANC_ENABLE_PLUGINS == 1
+      if (plugins_ != NULL)
+      {
+        plugins_->SetServerContext(context_);
+        context_.SetPlugins(*plugins_);
+      }
+#endif
+    }
+
+    ~ServerContextConfigurator()
+    {
+      {
+        OrthancConfiguration::WriterLock lock;
+        lock.GetConfiguration().ResetServerIndex();
+      }
+
+#if ORTHANC_ENABLE_PLUGINS == 1
+      if (plugins_ != NULL)
+      {
+        plugins_->ResetServerContext();
+        context_.ResetPlugins();
+      }
+#endif
+    }
+  };
+}
+
+
 static bool ConfigureServerContext(IDatabaseWrapper& database,
                                    IStorageArea& storageArea,
                                    OrthancPlugins *plugins,
@@ -1081,41 +1129,10 @@
       (lock.GetConfiguration().GetUnsignedIntegerParameter("JobsHistorySize", 10));
   }
 
-
-#if ORTHANC_ENABLE_PLUGINS == 1
-  if (plugins)
   {
-    plugins->SetServerContext(context);
-    context.SetPlugins(*plugins);
-  }
-#endif
-
-  bool restart = false;
-  ErrorCode error = ErrorCode_Success;
-
-  try
-  {
-    restart = ConfigureHttpHandler(context, plugins, loadJobsFromDatabase);
+    ServerContextConfigurator configurator(context, plugins);
+    return ConfigureHttpHandler(context, plugins, loadJobsFromDatabase);
   }
-  catch (OrthancException& e)
-  {
-    error = e.GetErrorCode();
-  }
-
-#if ORTHANC_ENABLE_PLUGINS == 1
-  if (plugins)
-  {
-    plugins->ResetServerContext();
-    context.ResetPlugins();
-  }
-#endif
-
-  if (error != ErrorCode_Success)
-  {
-    throw OrthancException(error);
-  }
-
-  return restart;
 }