# HG changeset patch # User Sebastien Jodogne # Date 1435325565 -7200 # Node ID fe384a9d3b51f20dc29fbac5e744750eeb50ea57 # Parent 7b7d597a190cd254978713c060752e59a43faca6 OrthancPluginGetConfiguration diff -r 7b7d597a190c -r fe384a9d3b51 Core/Toolbox.cpp --- 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; + } + } } diff -r 7b7d597a190c -r fe384a9d3b51 Core/Toolbox.h --- 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& arguments); bool IsInteger(const std::string& str); + + void CopyJsonWithoutComments(Json::Value& target, + const Json::Value& source); } } diff -r 7b7d597a190c -r fe384a9d3b51 NEWS --- 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) diff -r 7b7d597a190c -r fe384a9d3b51 OrthancServer/OrthancInitialization.cpp --- 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); + } } diff -r 7b7d597a190c -r fe384a9d3b51 OrthancServer/OrthancInitialization.h --- 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); }; } diff -r 7b7d597a190c -r fe384a9d3b51 Plugins/Engine/OrthancPlugins.cpp --- 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(parameters)->result = CopyString(s); + return true; + } + case _OrthancPluginService_RegisterRestCallback: RegisterRestCallback(parameters); return true; diff -r 7b7d597a190c -r fe384a9d3b51 Plugins/Include/OrthancCPlugin.h --- 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, ¶ms)) + { + /* Error */ + return NULL; + } + else + { + return result; + } + } + + + #ifdef __cplusplus } #endif