comparison Plugin/Plugin.cpp @ 4:9032ffb3a7d5

added configuration options
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Oct 2023 08:22:23 +0200
parents 3ecef5782f2c
children c8f19e93ff99
comparison
equal deleted inserted replaced
3:c9b4ff27ad5d 4:9032ffb3a7d5
959 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_Plugin); 959 JavaEnvironment::ThrowException(env, OrthancPluginErrorCode_Plugin);
960 } 960 }
961 } 961 }
962 962
963 963
964 static void ParseJson(Json::Value& target,
965 const std::string& source)
966 {
967 Json::CharReaderBuilder builder;
968 builder.settings_["collectComments"] = false;
969
970 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
971 assert(reader.get() != NULL);
972
973 JSONCPP_STRING err;
974 if (!reader->parse(source.c_str(), source.c_str() + source.size(), &target, &err))
975 {
976 throw std::runtime_error("Cannot parse JSON: " + err);
977 }
978 }
979
980
981 static bool HasOption(const Json::Value& json,
982 const std::string& key,
983 Json::ValueType type,
984 bool isMandatory)
985 {
986 if (!json.isMember(key))
987 {
988 if (isMandatory)
989 {
990 throw std::runtime_error("Missing configuration option for the Java plugin: \"" + key + "\"");
991 }
992 else
993 {
994 return false;
995 }
996 }
997 else if (json[key].type() == type)
998 {
999 return true;
1000 }
1001 else
1002 {
1003 throw std::runtime_error("The configuration option \"" + key + "\" for the Java plugin has not the proper type");
1004 }
1005 }
1006
1007
1008 static std::string GetMandatoryString(const Json::Value& json,
1009 const std::string& key)
1010 {
1011 HasOption(json, key, Json::stringValue, true);
1012 assert(json.isMember(key) &&
1013 json[key].type() == Json::stringValue);
1014 return json[key].asString();
1015 }
1016
1017
964 extern "C" 1018 extern "C"
965 { 1019 {
966 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) 1020 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
967 { 1021 {
968 context_ = context; 1022 context_ = context;
978 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); 1032 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
979 OrthancPluginLogError(context, info); 1033 OrthancPluginLogError(context, info);
980 return -1; 1034 return -1;
981 } 1035 }
982 1036
1037 OrthancPluginSetDescription(context, "Java plugin for Orthanc");
1038
983 try 1039 try
984 { 1040 {
985 { 1041 {
986 // Sanity check to ensure that the compiler has created different callback functions 1042 // Sanity check to ensure that the compiler has created different callback functions
987 std::set<intptr_t> c; 1043 std::set<intptr_t> c;
994 { 1050 {
995 throw std::runtime_error("The Java plugin has not been properly compiled"); 1051 throw std::runtime_error("The Java plugin has not been properly compiled");
996 } 1052 }
997 } 1053 }
998 1054
999 java_.reset(new JavaVirtualMachine("TODO")); 1055 Json::Value globalConfiguration;
1056
1057 {
1058 OrthancString tmp(OrthancPluginGetConfiguration(context));
1059 ParseJson(globalConfiguration, tmp.GetValue());
1060 }
1061
1062 static const std::string KEY_JAVA = "Java";
1063 static const std::string KEY_ENABLED = "Enabled";
1064 static const std::string KEY_CLASSPATH = "Classpath";
1065 static const std::string KEY_INITIALIZATION_CLASS = "InitializationClass";
1066
1067 if (!HasOption(globalConfiguration, KEY_JAVA, Json::objectValue, false))
1068 {
1069 OrthancPluginLogInfo(context, "Java plugin is disabled");
1070 return 0;
1071 }
1072
1073 Json::Value javaConfiguration = globalConfiguration[KEY_JAVA];
1074 assert(javaConfiguration.isObject());
1075
1076 if (HasOption(javaConfiguration, KEY_ENABLED, Json::booleanValue, false) &&
1077 !javaConfiguration[KEY_ENABLED].asBool())
1078 {
1079 OrthancPluginLogInfo(context, "Java plugin is disabled");
1080 return 0;
1081 }
1082
1083 java_.reset(new JavaVirtualMachine(GetMandatoryString(javaConfiguration, KEY_CLASSPATH)));
1000 1084
1001 callbacksConfiguration_.reset(new CallbacksConfiguration); 1085 callbacksConfiguration_.reset(new CallbacksConfiguration);
1002 OrthancPluginRegisterOnChangeCallback(context_, OnChangeCallback); 1086 OrthancPluginRegisterOnChangeCallback(context_, OnChangeCallback);
1003 1087
1004 JavaEnvironment env(*java_); 1088 JavaEnvironment env(*java_);
1019 const_cast<char*>("register"), 1103 const_cast<char*>("register"),
1020 const_cast<char*>("(Ljava/lang/String;Lbe/uclouvain/orthanc/Callbacks$OnRestRequest;)V"), 1104 const_cast<char*>("(Ljava/lang/String;Lbe/uclouvain/orthanc/Callbacks$OnRestRequest;)V"),
1021 (void*) RegisterOnRestRequestCallback }); 1105 (void*) RegisterOnRestRequestCallback });
1022 env.RegisterNatives("be/uclouvain/orthanc/Callbacks", methods); 1106 env.RegisterNatives("be/uclouvain/orthanc/Callbacks", methods);
1023 } 1107 }
1108
1109 if (HasOption(javaConfiguration, KEY_INITIALIZATION_CLASS, Json::stringValue, false))
1110 {
1111 env.FindClass(javaConfiguration[KEY_INITIALIZATION_CLASS].asString());
1112 }
1024 } 1113 }
1025 catch (std::runtime_error& e) 1114 catch (std::runtime_error& e)
1026 { 1115 {
1027 OrthancPluginLogError(context, e.what()); 1116 OrthancPluginLogError(context, e.what());
1028 return -1; 1117 return -1;