changeset 4:9032ffb3a7d5

added configuration options
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Oct 2023 08:22:23 +0200
parents c9b4ff27ad5d
children c8f19e93ff99
files Plugin/Plugin.cpp
diffstat 1 files changed, 90 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/Plugin.cpp	Wed Oct 18 18:56:58 2023 +0200
+++ b/Plugin/Plugin.cpp	Thu Oct 19 08:22:23 2023 +0200
@@ -961,6 +961,60 @@
 }
 
 
+static void ParseJson(Json::Value& target,
+                      const std::string& source)
+{
+  Json::CharReaderBuilder builder;
+  builder.settings_["collectComments"] = false;
+
+  const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
+  assert(reader.get() != NULL);
+
+  JSONCPP_STRING err;
+  if (!reader->parse(source.c_str(), source.c_str() + source.size(), &target, &err))
+  {
+    throw std::runtime_error("Cannot parse JSON: " + err);
+  }
+}
+
+
+static bool HasOption(const Json::Value& json,
+                      const std::string& key,
+                      Json::ValueType type,
+                      bool isMandatory)
+{
+  if (!json.isMember(key))
+  {
+    if (isMandatory)
+    {
+      throw std::runtime_error("Missing configuration option for the Java plugin: \"" + key + "\"");
+    }
+    else
+    {
+      return false;
+    }
+  }
+  else if (json[key].type() == type)
+  {
+    return true;
+  }
+  else
+  {
+    throw std::runtime_error("The configuration option \"" + key + "\" for the Java plugin has not the proper type");
+  }
+}
+
+
+static std::string GetMandatoryString(const Json::Value& json,
+                                      const std::string& key)
+{
+  HasOption(json, key, Json::stringValue, true);
+  assert(json.isMember(key) &&
+         json[key].type() == Json::stringValue);
+  return json[key].asString();
+}
+
+
 extern "C"
 {
   ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
@@ -980,6 +1034,8 @@
       return -1;
     }
 
+    OrthancPluginSetDescription(context, "Java plugin for Orthanc");
+
     try
     {
       {
@@ -996,7 +1052,35 @@
         }
       }
 
-      java_.reset(new JavaVirtualMachine("TODO"));
+      Json::Value globalConfiguration;
+
+      {
+        OrthancString tmp(OrthancPluginGetConfiguration(context));
+        ParseJson(globalConfiguration, tmp.GetValue());
+      }
+
+      static const std::string KEY_JAVA = "Java";
+      static const std::string KEY_ENABLED = "Enabled";
+      static const std::string KEY_CLASSPATH = "Classpath";
+      static const std::string KEY_INITIALIZATION_CLASS = "InitializationClass";
+
+      if (!HasOption(globalConfiguration, KEY_JAVA, Json::objectValue, false))
+      {
+        OrthancPluginLogInfo(context, "Java plugin is disabled");
+        return 0;
+      }
+
+      Json::Value javaConfiguration = globalConfiguration[KEY_JAVA];
+      assert(javaConfiguration.isObject());
+
+      if (HasOption(javaConfiguration, KEY_ENABLED, Json::booleanValue, false) &&
+          !javaConfiguration[KEY_ENABLED].asBool())
+      {
+        OrthancPluginLogInfo(context, "Java plugin is disabled");
+        return 0;
+      }
+
+      java_.reset(new JavaVirtualMachine(GetMandatoryString(javaConfiguration, KEY_CLASSPATH)));
 
       callbacksConfiguration_.reset(new CallbacksConfiguration);
       OrthancPluginRegisterOnChangeCallback(context_, OnChangeCallback);
@@ -1021,6 +1105,11 @@
               (void*) RegisterOnRestRequestCallback });
         env.RegisterNatives("be/uclouvain/orthanc/Callbacks", methods);
       }
+
+      if (HasOption(javaConfiguration, KEY_INITIALIZATION_CLASS, Json::stringValue, false))
+      {
+        env.FindClass(javaConfiguration[KEY_INITIALIZATION_CLASS].asString());
+      }
     }
     catch (std::runtime_error& e)
     {