changeset 7046:0f2833c4e06e

refactoring class OrthancConfiguration
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Aug 2026 16:53:26 +0200
parents 3b85dc5a36ed
children 667acdc1b2f2
files OrthancServer/Sources/OrthancConfiguration.cpp OrthancServer/Sources/OrthancConfiguration.h OrthancServer/Sources/OrthancInitialization.cpp OrthancServer/Sources/OrthancInitialization.h OrthancServer/Sources/main.cpp OrthancServer/UnitTestsSources/UnitTestsMain.cpp
diffstat 6 files changed, 235 insertions(+), 155 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/OrthancConfiguration.cpp	Tue Aug 11 13:46:00 2026 +0200
+++ b/OrthancServer/Sources/OrthancConfiguration.cpp	Tue Aug 11 16:53:26 2026 +0200
@@ -50,45 +50,70 @@
 static const char* const CONFIG_LOADER_THREADS = "LoaderThreads";
 static const char* const CONFIG_ZIP_LOADER_THREADS = "ZipLoaderThreads"; // for backward compatibility only
 
-static Json::Value defaultConfiguration;
-
 namespace Orthanc
 {
-  static void AddFileToConfiguration(Json::Value& target,
-                                     const boost::filesystem::path& path)
+  static void ReadConfigurationFromString(Json::Value& target,
+                                          const std::string& content)
   {
     std::map<std::string, std::string> env;
     SystemToolbox::GetEnvironmentVariables(env);
-    
-    LOG(WARNING) << "Reading the configuration from: " << SystemToolbox::PathToUtf8(path);
 
-    Json::Value config;
-
-    {
-      std::string content;
-      SystemToolbox::ReadFile(content, path);
+    std::string substituted = Toolbox::SubstituteVariables(content, env);
 
-      content = Toolbox::SubstituteVariables(content, env);
-
-      Json::Value tmp;
-      if (!Toolbox::ReadJson(tmp, content) ||
-          tmp.type() != Json::objectValue)
-      {
-        throw OrthancException(ErrorCode_BadJson,
-                               "The configuration file does not follow the JSON syntax: " + SystemToolbox::PathToUtf8(path));
-      }
-
-      Toolbox::CopyJsonWithoutComments(config, tmp);
+    Json::Value tmp;
+    if (!Toolbox::ReadJson(tmp, substituted) ||
+        tmp.type() != Json::objectValue)
+    {
+      throw OrthancException(ErrorCode_BadJson, "The configuration file does not follow the JSON syntax");
     }
 
-    if (target.size() == 0)
+    Toolbox::CopyJsonWithoutComments(target, tmp);
+  }
+
+
+  static void ReadConfigurationFromFile(Json::Value& target,
+                                        const boost::filesystem::path& path)
+  {
+    LOG(WARNING) << "Reading the configuration file: " << SystemToolbox::PathToUtf8(path);
+
+    std::string content;
+    SystemToolbox::ReadFile(content, path);
+
+    ReadConfigurationFromString(target, content);
+  }
+
+
+  static void ReadDefaultConfiguration(Json::Value& target)
+  {
+#if ORTHANC_STANDALONE == 1
+    std::string content;
+    GetFileResource(content, ServerResources::CONFIGURATION_SAMPLE);
+
+    ReadConfigurationFromString(target, content);
+#else
+    // In a non-standalone build, we use the
+    // "Resources/Configuration.json" from the Orthanc source code
+
+    boost::filesystem::path p = ORTHANC_PATH;
+    p /= "Resources";
+    p /= "Configuration.json";
+
+    ReadConfigurationFromFile(target, p);
+#endif
+  }
+
+
+  static void MergeConfigurations(Json::Value& target,
+                                  const Json::Value& source)
+  {
+    if (target.type() != Json::objectValue ||
+        source.type() != Json::objectValue)
     {
-      target = config;
+      throw OrthancException(ErrorCode_InternalError);
     }
     else
     {
-      // Merge the newly-added file with the previous content of "target"
-      Json::Value::Members members = config.getMemberNames();
+      Json::Value::Members members = source.getMemberNames();
       for (Json::Value::ArrayIndex i = 0; i < members.size(); i++)
       {
         if (target.isMember(members[i]))
@@ -99,24 +124,21 @@
         }
         else
         {
-          target[members[i]] = config[members[i]];
+          target[members[i]] = source[members[i]];
         }
       }
     }
   }
 
-    
-  static void ScanFolderForConfiguration(Json::Value& target,
-                                         const boost::filesystem::path& folder)
-  {
-    using namespace boost::filesystem;
 
-    LOG(WARNING) << "Scanning folder \"" << Orthanc::SystemToolbox::PathToUtf8(folder) << "\" for configuration files";
+  static void ReadConfigurationsFromFolder(Json::Value& target,
+                                           const boost::filesystem::path& folder)
+  {
+    LOG(WARNING) << "Scanning folder for configuration files: " << Orthanc::SystemToolbox::PathToUtf8(folder);
 
-    directory_iterator end_it; // default construction yields past-the-end
-    for (directory_iterator it(folder);
-         it != end_it;
-         ++it)
+    boost::filesystem::directory_iterator end_it; // default construction yields past-the-end
+
+    for (boost::filesystem::directory_iterator it(folder); it != end_it; ++it)
     {
       if (!is_directory(it->status()))
       {
@@ -125,70 +147,34 @@
 
         if (extension == ".json")
         {
-          AddFileToConfiguration(target, it->path());
+          Json::Value config;
+          ReadConfigurationFromFile(config, it->path());
+          MergeConfigurations(target, config);
         }
       }
     }
   }
 
-    
-  static void ReadConfiguration(Json::Value& target, 
-                                const boost::filesystem::path &configurationFile)
+
+  static void ReadConfiguration(Json::Value& target,
+                                const std::list<boost::filesystem::path>& configurationPaths)
   {
-    // lazy loading of the default configuration
-    if (defaultConfiguration.empty())
-    {
-      std::string defaultConfigurationContent;
-      GetFileResource(defaultConfigurationContent, ServerResources::CONFIGURATION_SAMPLE);
-
-      Json::Value tmp;
-      if (!Toolbox::ReadJson(tmp, defaultConfigurationContent) ||
-          tmp.type() != Json::objectValue)
-      {
-        throw OrthancException(ErrorCode_InternalError, "The default configuration file does not follow the JSON syntax !!");
-      }
-
-      Toolbox::CopyJsonWithoutComments(defaultConfiguration, tmp);
-    }
-
     target = Json::objectValue;
 
-    if (!configurationFile.empty())
+    for (std::list<boost::filesystem::path>::const_iterator it = configurationPaths.begin();
+         it != configurationPaths.end(); ++it)
     {
-      if (!boost::filesystem::exists(configurationFile))
+      if (boost::filesystem::is_directory(*it))
       {
-        throw OrthancException(ErrorCode_InexistentFile,
-                               "Inexistent path to configuration: " +
-                               SystemToolbox::PathToUtf8(configurationFile));
-      }
-      
-      if (boost::filesystem::is_directory(configurationFile))
-      {
-        ScanFolderForConfiguration(target, configurationFile);
+        ReadConfigurationsFromFolder(target, *it);
       }
       else
       {
-        AddFileToConfiguration(target, configurationFile);
+        Json::Value config;
+        ReadConfigurationFromFile(config, *it);
+        MergeConfigurations(target, config);
       }
     }
-    else
-    {
-#if ORTHANC_STANDALONE == 1
-      // No default path for the standalone configuration
-      LOG(WARNING) << "Using the default Orthanc configuration";
-      return;
-
-#else
-      // In a non-standalone build, we use the
-      // "Resources/Configuration.json" from the Orthanc source code
-
-      boost::filesystem::path p = ORTHANC_PATH;
-      p /= "Resources";
-      p /= "Configuration.json";
-
-      AddFileToConfiguration(target, p);
-#endif
-    }
   }
 
 
@@ -207,6 +193,13 @@
   }
 
 
+  OrthancConfiguration::OrthancConfiguration() :
+    serverIndex_(NULL)
+  {
+    ReadDefaultConfiguration(defaultConfiguration_);
+  }
+
+
   void OrthancConfiguration::LoadModalitiesFromJson(const Json::Value& source)
   {
     modalities_.clear();
@@ -486,19 +479,20 @@
   }
 
 
-  bool OrthancConfiguration::LookupStringParameter(std::string& target,
-                                                   const std::string& parameter) const
+  static bool LookupStringParameterInternal(std::string& target,
+                                            const Json::Value& config,
+                                            const std::string& parameter)
   {
-    if (json_.isMember(parameter))
+    if (config.isMember(parameter))
     {
-      if (json_[parameter].type() != Json::stringValue)
+      if (config[parameter].type() != Json::stringValue)
       {
         throw OrthancException(ErrorCode_BadParameterType,
                                "The configuration option \"" + parameter + "\" must be a string");
       }
       else
       {
-        target = json_[parameter].asString();
+        target = config[parameter].asString();
         return true;
       }
     }
@@ -509,6 +503,13 @@
   }
 
 
+  bool OrthancConfiguration::LookupStringParameter(std::string& target,
+                                                   const std::string& parameter) const
+  {
+    return LookupStringParameterInternal(target, json_, parameter);
+  }
+
+
   std::string OrthancConfiguration::GetStringParameter(const std::string& parameter) const
   {
     std::string value;
@@ -516,30 +517,32 @@
     {
       return value;
     }
-    else if (defaultConfiguration.isMember(parameter) && defaultConfiguration[parameter].type() == Json::stringValue)
+    else if (LookupStringParameterInternal(value, defaultConfiguration_, parameter))
     {
-      return defaultConfiguration[parameter].asString();
+      return value;
     }
     else
     {
-      throw OrthancException(ErrorCode_InternalError, std::string("No or invalid default parameter found in the default configuration for '") + parameter + "'");
+      throw OrthancException(ErrorCode_InternalError,
+                             "No or invalid default parameter found in the default configuration for '" + parameter + "'");
     }
   }
 
-    
-  bool OrthancConfiguration::LookupIntegerParameter(int& target,
-                                                    const std::string& parameter) const
+
+  static bool LookupIntegerParameterInternal(int& target,
+                                             const Json::Value& config,
+                                             const std::string& parameter)
   {
-    if (json_.isMember(parameter))
+    if (config.isMember(parameter))
     {
-      if (json_[parameter].type() != Json::intValue)
+      if (config[parameter].type() != Json::intValue)
       {
         throw OrthancException(ErrorCode_BadParameterType,
                                "The configuration option \"" + parameter + "\" must be an integer");
       }
       else
       {
-        target = json_[parameter].asInt();
+        target = config[parameter].asInt();
         return true;
       }
     }
@@ -550,13 +553,41 @@
   }
 
 
-    
-  bool OrthancConfiguration::LookupUnsignedIntegerParameter(unsigned int& target,
-                                                            const std::string& parameter) const
+  bool OrthancConfiguration::LookupIntegerParameter(int& target,
+                                                    const std::string& parameter) const
+  {
+    return LookupIntegerParameterInternal(target, json_, parameter);
+  }
+
+
+  bool OrthancConfiguration::GetIntegerParameter(int& target,
+                                                 const std::string& parameter) const
   {
     int v;
+
     if (LookupIntegerParameter(v, parameter))
     {
+      return v;
+    }
+    else if (LookupIntegerParameterInternal(v, defaultConfiguration_, parameter))
+    {
+      return v;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_InternalError,
+                             "No or invalid default parameter found in the default configuration for '" + parameter + "'");
+    }
+  }
+
+    
+  static bool LookupUnsignedIntegerParameterInternal(unsigned int& target,
+                                                     const Json::Value& config,
+                                                     const std::string& parameter)
+  {
+    int v;
+    if (LookupIntegerParameterInternal(v, config, parameter))
+    {
       if (v < 0)
       {
         throw OrthancException(ErrorCode_ParameterOutOfRange,
@@ -575,6 +606,13 @@
   }
 
 
+  bool OrthancConfiguration::LookupUnsignedIntegerParameter(unsigned int& target,
+                                                            const std::string& parameter) const
+  {
+    return LookupUnsignedIntegerParameterInternal(target, json_, parameter);
+  }
+
+
   unsigned int OrthancConfiguration::GetUnsignedIntegerParameter(const std::string& parameter) const
   {
     unsigned int v;
@@ -582,23 +620,25 @@
     {
       return v;
     }
-    else if (defaultConfiguration.isMember(parameter) && defaultConfiguration[parameter].type() == Json::intValue)
+    else if (LookupUnsignedIntegerParameterInternal(v, defaultConfiguration_, parameter))
     {
-      return static_cast<unsigned int>(defaultConfiguration[parameter].asInt());
+      return v;
     }
     else
     {
-      throw OrthancException(ErrorCode_InternalError, std::string("No or invalid default parameter found in the default configuration for '") + parameter + "'");
+      throw OrthancException(ErrorCode_InternalError,
+                             "No or invalid default parameter found in the default configuration for '" + parameter + "'");
     }
   }
 
 
-  bool OrthancConfiguration::LookupBooleanParameter(bool& target,
-                                                    const std::string& parameter) const
+  static bool LookupBooleanParameterInternal(bool& target,
+                                             const Json::Value& config,
+                                             const std::string& parameter)
   {
-    if (json_.isMember(parameter))
+    if (config.isMember(parameter))
     {
-      if (json_[parameter].type() != Json::booleanValue)
+      if (config[parameter].type() != Json::booleanValue)
       {
         throw OrthancException(ErrorCode_BadParameterType,
                                "The configuration option \"" + parameter +
@@ -606,7 +646,7 @@
       }
       else
       {
-        target = json_[parameter].asBool();
+        target = config[parameter].asBool();
         return true;
       }
     }
@@ -617,6 +657,13 @@
   }
 
 
+  bool OrthancConfiguration::LookupBooleanParameter(bool& target,
+                                                    const std::string& parameter) const
+  {
+    return LookupBooleanParameterInternal(target, json_, parameter);
+  }
+
+
   bool OrthancConfiguration::GetBooleanParameter(const std::string& parameter) const
   {
     bool value;
@@ -624,52 +671,76 @@
     {
       return value;
     }
-    else if (defaultConfiguration.isMember(parameter) && defaultConfiguration[parameter].type() == Json::booleanValue)
+    else if (LookupBooleanParameterInternal(value, defaultConfiguration_, parameter))
     {
-      return defaultConfiguration[parameter].asBool();
+      return value;
     }
     else
     {
-      throw OrthancException(ErrorCode_InternalError, std::string("No or invalid default parameter found in the default configuration for '") + parameter + "'");
+      throw OrthancException(ErrorCode_InternalError,
+                             "No or invalid default parameter found in the default configuration for '" + parameter + "'");
     }
   }
 
 
-  void OrthancConfiguration::Read(const boost::filesystem::path& configurationFile)
+  std::string OrthancConfiguration::GetConfigurationAbsolutePath() const
   {
+    if (configurationPaths_.empty())
+    {
+      return "";
+    }
+    else
+    {
+      if (configurationPaths_.size() >= 2)
+      {
+        LOG(WARNING) << "A plugin has called deprecated OrthancPluginGetConfigurationPath(), the results "
+                     << "are unreliable because multiple configuration path were provided";
+      }
+
+      const boost::filesystem::path& first = configurationPaths_.front();
+
+      if (boost::filesystem::is_directory(first))
+      {
+        return boost::filesystem::absolute(first).parent_path().string();
+      }
+      else
+      {
+        return boost::filesystem::absolute(first).string();
+      }
+    }    
+  }
+
+
+  void OrthancConfiguration::Read(const std::list<boost::filesystem::path>& configurationPaths)
+  {
+    if (configurationPaths.empty())
+    {
+      LOG(WARNING) << "Using the default Orthanc configuration";
+    }
+
     // Read the content of the configuration
-    configurationFileArg_ = configurationFile;
-    ReadConfiguration(json_, configurationFile);
+    configurationPaths_ = configurationPaths;
+    ReadConfiguration(json_, configurationPaths);
 
     // Adapt the paths to the configurations
     defaultDirectory_ = boost::filesystem::current_path();
-    configurationAbsolutePath_ = "";
 
-    if (configurationFile.empty())
+    if (!configurationPaths.empty())
     {
-      if (boost::filesystem::is_directory(configurationFile))
+      const boost::filesystem::path& first = configurationPaths.front();
+
+      if (boost::filesystem::is_directory(first))
       {
-        defaultDirectory_ = configurationFile;
-        configurationAbsolutePath_ = boost::filesystem::absolute(configurationFile).parent_path().string();
+        defaultDirectory_ = first;
       }
       else
       {
-        defaultDirectory_ = boost::filesystem::path(configurationFile).parent_path();
-        configurationAbsolutePath_ = boost::filesystem::absolute(configurationFile).string();
+        defaultDirectory_ = boost::filesystem::path(first).parent_path();
       }
     }
-    else
-    {
-#if ORTHANC_STANDALONE != 1
-      // In a non-standalone build, we use the
-      // "Resources/Configuration.json" from the Orthanc source code
 
-      boost::filesystem::path p = ORTHANC_PATH;
-      p /= "Resources";
-      p /= "Configuration.json";
-      configurationAbsolutePath_ = boost::filesystem::absolute(p).string();
-#endif
-    }
+    LOG(WARNING) << "The paths in the Orthanc configuration will be interpreted relative to folder: "
+                 << SystemToolbox::PathToUtf8(defaultDirectory_);
   }
 
 
@@ -1033,7 +1104,7 @@
   bool OrthancConfiguration::HasConfigurationChanged() const
   {
     Json::Value current;
-    ReadConfiguration(current, configurationFileArg_);
+    ReadConfiguration(current, configurationPaths_);
 
     std::string a, b;
     Toolbox::WriteFastJson(a, json_);
--- a/OrthancServer/Sources/OrthancConfiguration.h	Tue Aug 11 13:46:00 2026 +0200
+++ b/OrthancServer/Sources/OrthancConfiguration.h	Tue Aug 11 16:53:26 2026 +0200
@@ -83,21 +83,18 @@
     typedef std::map<std::string, unsigned int>               JobsEngineThreadsCount;
 
     boost::shared_mutex      mutex_;
+    Json::Value              defaultConfiguration_;
     Json::Value              json_;
     boost::filesystem::path  defaultDirectory_;
-    std::string              configurationAbsolutePath_;
     FontRegistry             fontRegistry_;
-    boost::filesystem::path  configurationFileArg_;
+    std::list<boost::filesystem::path>  configurationPaths_;
     Modalities               modalities_;
     Peers                    peers_;
     JobsEngineThreadsCount   jobsEngineThreadsCount_;
     ServerIndex*             serverIndex_;
     std::set<Warnings>       disabledWarnings_;
 
-    OrthancConfiguration() :
-      serverIndex_(NULL)
-    {
-    }
+    OrthancConfiguration();
 
     void LoadModalitiesFromJson(const Json::Value& source);
     
@@ -173,17 +170,14 @@
     };
 
 
-    const std::string& GetConfigurationAbsolutePath() const
-    {
-      return configurationAbsolutePath_;
-    }
+    std::string GetConfigurationAbsolutePath() const;
 
     const FontRegistry& GetFontRegistry() const
     {
       return fontRegistry_;
     }
 
-    void Read(const boost::filesystem::path &configurationFile);
+    void Read(const std::list<boost::filesystem::path>& configurationPaths);
 
     // "SetServerIndex()" must have been called
     void LoadModalitiesAndPeers();
@@ -204,6 +198,9 @@
     bool LookupIntegerParameter(int& target,
                                 const std::string& parameter) const;
 
+    bool GetIntegerParameter(int& target,
+                             const std::string& parameter) const;
+
     bool LookupUnsignedIntegerParameter(unsigned int& target,
                                         const std::string& parameter) const;
 
--- a/OrthancServer/Sources/OrthancInitialization.cpp	Tue Aug 11 13:46:00 2026 +0200
+++ b/OrthancServer/Sources/OrthancInitialization.cpp	Tue Aug 11 16:53:26 2026 +0200
@@ -331,7 +331,7 @@
 
 
 
-  void OrthancInitialize(const boost::filesystem::path& configurationFile)
+  void OrthancInitialize(const std::list<boost::filesystem::path>& configurationPaths)
   {
     static const char* const LOCALE = "Locale";
     static const char* const PKCS11 = "Pkcs11";
@@ -348,7 +348,7 @@
     InitializeServerEnumerations();
 
     // Read the user-provided configuration
-    lock.GetConfiguration().Read(configurationFile);
+    lock.GetConfiguration().Read(configurationPaths);
 
     {
       std::string locale;
@@ -405,6 +405,12 @@
   }
 
 
+  void OrthancInitialize()
+  {
+    std::list<boost::filesystem::path> configurationPaths;
+    OrthancInitialize(configurationPaths);
+  }
+
 
   void OrthancFinalize()
   {
--- a/OrthancServer/Sources/OrthancInitialization.h	Tue Aug 11 13:46:00 2026 +0200
+++ b/OrthancServer/Sources/OrthancInitialization.h	Tue Aug 11 16:53:26 2026 +0200
@@ -30,7 +30,9 @@
 
 namespace Orthanc
 {
-  void OrthancInitialize(const boost::filesystem::path& configurationFile);
+  void OrthancInitialize(const std::list<boost::filesystem::path>& configurationPaths);
+
+  void OrthancInitialize();  // Use only the default configuration
 
   void OrthancFinalize();
 
--- a/OrthancServer/Sources/main.cpp	Tue Aug 11 13:46:00 2026 +0200
+++ b/OrthancServer/Sources/main.cpp	Tue Aug 11 16:53:26 2026 +0200
@@ -2032,7 +2032,8 @@
 
   bool upgradeDatabase = false;
   bool loadJobsFromDatabase = true;
-  boost::filesystem::path configurationFile;
+  std::list<boost::filesystem::path> configurationPaths;
+
 
   /**
    * Parse the command-line options.
@@ -2048,7 +2049,7 @@
     }
     else if (argument[0] != '-')
     {
-      if (!configurationFile.empty())
+      if (!configurationPaths.empty())
       {
         LOG(ERROR) << "More than one configuration path were provided on the command line, aborting";
         return -1;
@@ -2058,13 +2059,16 @@
         // Use the first argument that does not start with a "-" as
         // the configuration file
 
-        configurationFile = SystemToolbox::PathFromUtf8(argument);
+        boost::filesystem::path configurationFile = SystemToolbox::PathFromUtf8(argument);
+
 //        // TODO WHAT IS THE ENCODING?
 //#if defined(_WIN32)
 //        //configurationFileUtf8Str = SystemToolbox::WStringToUtf8(SystemToolbox::WStringFromCharPtr(argv[i]));
 //#else
 //        configurationFileUtf8Str = std::string(argv[i]);
 //#endif
+
+        configurationPaths.push_back(configurationFile);
       }
     }
     else if (argument == "--errors")
@@ -2329,7 +2333,7 @@
   {
     for (;;)
     {
-      OrthancInitialize(configurationFile);
+      OrthancInitialize(configurationPaths);
 
       bool restart = StartOrthanc(arguments, upgradeDatabase, loadJobsFromDatabase);
       if (restart)
--- a/OrthancServer/UnitTestsSources/UnitTestsMain.cpp	Tue Aug 11 13:46:00 2026 +0200
+++ b/OrthancServer/UnitTestsSources/UnitTestsMain.cpp	Tue Aug 11 16:53:26 2026 +0200
@@ -547,7 +547,7 @@
   SetGlobalVerbosity(Verbosity_Verbose);
   Toolbox::DetectEndianness();
   SystemToolbox::MakeDirectory(SystemToolbox::PathFromUtf8("UnitTestsResults"));
-  OrthancInitialize("");
+  OrthancInitialize();
 
   ::testing::InitGoogleTest(&argc, argv);
   int result = RUN_ALL_TESTS();