diff OrthancServer/main.cpp @ 2941:e292798f9980

OrthancConfiguration::SetServerIndex()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2018 15:16:32 +0100
parents 4767d36679ed
children 2e751f615e03
line wrap: on
line diff
--- 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;
 }