changeset 1424:fe384a9d3b51

OrthancPluginGetConfiguration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Jun 2015 15:32:45 +0200
parents 7b7d597a190c
children 97268448bdfc
files Core/Toolbox.cpp Core/Toolbox.h NEWS OrthancServer/OrthancInitialization.cpp OrthancServer/OrthancInitialization.h Plugins/Engine/OrthancPlugins.cpp Plugins/Include/OrthancCPlugin.h
diffstat 7 files changed, 143 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Fri Jun 26 14:44:10 2015 +0200
+++ b/Core/Toolbox.cpp	Fri Jun 26 15:32:45 2015 +0200
@@ -1137,5 +1137,65 @@
 
     return true;
   }
+
+
+  void Toolbox::CopyJsonWithoutComments(Json::Value& target,
+                                        const Json::Value& source)
+  {
+    switch (source.type())
+    {
+      case Json::nullValue:
+        target = Json::nullValue;
+        break;
+
+      case Json::intValue:
+        target = source.asInt64();
+        break;
+
+      case Json::uintValue:
+        target = source.asUInt64();
+        break;
+
+      case Json::realValue:
+        target = source.asDouble();
+        break;
+
+      case Json::stringValue:
+        target = source.asString();
+        break;
+
+      case Json::booleanValue:
+        target = source.asBool();
+        break;
+
+      case Json::arrayValue:
+      {
+        target = Json::arrayValue;
+        for (Json::Value::ArrayIndex i = 0; i < source.size(); i++)
+        {
+          Json::Value& item = target.append(Json::nullValue);
+          CopyJsonWithoutComments(item, source[i]);
+        }
+
+        break;
+      }
+
+      case Json::objectValue:
+      {
+        target = Json::objectValue;
+        Json::Value::Members members = source.getMemberNames();
+        for (Json::Value::ArrayIndex i = 0; i < members.size(); i++)
+        {
+          const std::string item = members[i];
+          CopyJsonWithoutComments(target[item], source[item]);
+        }
+
+        break;
+      }
+
+      default:
+        break;
+    }
+  }
 }
 
--- a/Core/Toolbox.h	Fri Jun 26 14:44:10 2015 +0200
+++ b/Core/Toolbox.h	Fri Jun 26 15:32:45 2015 +0200
@@ -160,5 +160,8 @@
                               const std::vector<std::string>& arguments);
 
     bool IsInteger(const std::string& str);
+
+    void CopyJsonWithoutComments(Json::Value& target,
+                                 const Json::Value& source);
   }
 }
--- a/NEWS	Fri Jun 26 14:44:10 2015 +0200
+++ b/NEWS	Fri Jun 26 15:32:45 2015 +0200
@@ -2,6 +2,7 @@
 ===============================
 
 * The configuration can be splitted into several files stored inside the same folder
+* Plugins can retrieve the configuration file directly as a JSON string
 * Fix issue 35 (Characters in PatientID string are not protected for C-Find)
 * Fix issue 37 (Hyphens trigger range query even if datatype does not support ranges)
 
--- a/OrthancServer/OrthancInitialization.cpp	Fri Jun 26 14:44:10 2015 +0200
+++ b/OrthancServer/OrthancInitialization.cpp	Fri Jun 26 15:32:45 2015 +0200
@@ -71,25 +71,31 @@
   {
     LOG(WARNING) << "Reading the configuration from: " << path;
 
-    std::string content;
-    Toolbox::ReadFile(content, path.string());
+    Json::Value config;
+
+    {
+      std::string content;
+      Toolbox::ReadFile(content, path.string());
 
-    Json::Value tmp;
-    Json::Reader reader;
-    if (!reader.parse(content, tmp) ||
-        tmp.type() != Json::objectValue)
-    {
-      LOG(ERROR) << "Bad file format for this configuration file: " << path;
-      throw OrthancException(ErrorCode_BadFileFormat);
+      Json::Value tmp;
+      Json::Reader reader;
+      if (!reader.parse(content, tmp) ||
+          tmp.type() != Json::objectValue)
+      {
+        LOG(ERROR) << "Bad file format for this configuration file: " << path;
+        throw OrthancException(ErrorCode_BadFileFormat);
+      }
+
+      Toolbox::CopyJsonWithoutComments(config, tmp);
     }
 
     if (configuration_.size() == 0)
     {
-      configuration_ = tmp;
+      configuration_ = config;
     }
     else
     {
-      Json::Value::Members members = tmp.getMemberNames();
+      Json::Value::Members members = config.getMemberNames();
       for (Json::Value::ArrayIndex i = 0; i < members.size(); i++)
       {
         if (configuration_.isMember(members[i]))
@@ -99,7 +105,7 @@
         }
         else
         {
-          configuration_[members[i]] = tmp[members[i]];
+          configuration_[members[i]] = config[members[i]];
         }
       }
     }
@@ -839,4 +845,18 @@
   {
     return CreateFilesystemStorage();
   }  
+
+
+  void Configuration::FormatConfiguration(std::string& result)
+  {
+    Json::Value config;
+
+    {
+      boost::mutex::scoped_lock lock(globalMutex_);
+      config = configuration_;
+    }
+
+    Json::StyledWriter w;
+    result = w.write(config);
+  }
 }
--- a/OrthancServer/OrthancInitialization.h	Fri Jun 26 14:44:10 2015 +0200
+++ b/OrthancServer/OrthancInitialization.h	Fri Jun 26 15:32:45 2015 +0200
@@ -108,5 +108,7 @@
     static IDatabaseWrapper* CreateDatabaseWrapper();
 
     static IStorageArea* CreateStorageArea();
+
+    static void FormatConfiguration(std::string& result);
   };
 }
--- a/Plugins/Engine/OrthancPlugins.cpp	Fri Jun 26 14:44:10 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Fri Jun 26 15:32:45 2015 +0200
@@ -1008,6 +1008,15 @@
         return true;
       }
 
+      case _OrthancPluginService_GetConfiguration:
+      {
+        std::string s;
+        Configuration::FormatConfiguration(s);
+
+        *reinterpret_cast<const _OrthancPluginRetrieveDynamicString*>(parameters)->result = CopyString(s);
+        return true;
+      }
+
       case _OrthancPluginService_RegisterRestCallback:
         RegisterRestCallback(parameters);
         return true;
--- a/Plugins/Include/OrthancCPlugin.h	Fri Jun 26 14:44:10 2015 +0200
+++ b/Plugins/Include/OrthancCPlugin.h	Fri Jun 26 15:32:45 2015 +0200
@@ -256,6 +256,7 @@
     _OrthancPluginService_GetCommandLineArgumentsCount = 10,
     _OrthancPluginService_GetCommandLineArgument = 11,
     _OrthancPluginService_GetExpectedDatabaseVersion = 12,
+    _OrthancPluginService_GetConfiguration = 13,
 
     /* Registration of callbacks */
     _OrthancPluginService_RegisterRestCallback = 1000,
@@ -1804,12 +1805,14 @@
    * This function returns the path to the configuration file(s) that
    * was specified when starting Orthanc. Since version 0.9.1, this
    * path can refer to a folder that stores a set of configuration
-   * files.
+   * files. This function is deprecated in favor of
+   * OrthancPluginGetConfiguration().
    * 
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @return NULL in the case of an error, or a newly allocated string
    * containing the path. This string must be freed by
    * OrthancPluginFreeString().
+   * @see OrthancPluginGetConfiguration()
    **/
   ORTHANC_PLUGIN_INLINE char *OrthancPluginGetConfigurationPath(OrthancPluginContext* context)
   {
@@ -2118,6 +2121,38 @@
 
 
 
+  /**
+   * @brief Return the content of the configuration file(s).
+   *
+   * This function returns the content of the configuration that is
+   * used by Orthanc, formatted as a JSON string.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @return NULL in the case of an error, or a newly allocated string
+   * containing the configuration. This string must be freed by
+   * OrthancPluginFreeString().
+   **/
+  ORTHANC_PLUGIN_INLINE char *OrthancPluginGetConfiguration(OrthancPluginContext* context)
+  {
+    char* result;
+
+    _OrthancPluginRetrieveDynamicString params;
+    params.result = &result;
+    params.argument = NULL;
+
+    if (context->InvokeService(context, _OrthancPluginService_GetConfiguration, &params))
+    {
+      /* Error */
+      return NULL;
+    }
+    else
+    {
+      return result;
+    }
+  }
+
+
+
 #ifdef  __cplusplus
 }
 #endif