arpadboda commented on a change in pull request #489: MINIFICPP-740: Add ability to run NiFi processors from Java and Python URL: https://github.com/apache/nifi-minifi-cpp/pull/489#discussion_r260267974
########## File path: extensions/jni/jvm/NarClassLoader.h ########## @@ -0,0 +1,443 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef EXTENSIONS_NARCLASSLOADER_H +#define EXTENSIONS_NARCLASSLOADER_H + +#include "JavaServicer.h" +#include "JniBundle.h" +#include "../JavaException.h" +#include <string> +#include <vector> +#include <sstream> +#include <iterator> +#include <algorithm> +#include <jni.h> + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace jni { + +class NarClassLoader { + + public: + + NarClassLoader(std::shared_ptr<minifi::jni::JavaServicer> servicer, JavaClass &clazz, const std::string &dir_name, const std::string &scratch_nar_dir, const std::string &docs_dir) + : java_servicer_(servicer) { + class_ref_ = clazz; + auto env = java_servicer_->attach(); + class_loader_ = class_ref_.newInstance(env); + jmethodID mthd = env->GetMethodID(class_ref_.getReference(), "initializeNarDirectory", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)V"); + if (mthd == nullptr) { + throw std::runtime_error("Could not find method to construct JniClassLoader"); + } else { + auto dirNameStr = env->NewStringUTF(dir_name.c_str()); + auto narWriteBaseStr = env->NewStringUTF(scratch_nar_dir.c_str()); + auto docsDirStr = env->NewStringUTF(docs_dir.c_str()); + env->CallVoidMethod(class_loader_, mthd, dirNameStr, narWriteBaseStr, docsDirStr, servicer->getClassLoader()); + ThrowIf(env); + } + + // we should now have + + getBundles(); + } + + ~NarClassLoader() { + java_servicer_->attach()->DeleteGlobalRef(class_loader_); + } + + jclass getClass(const std::string &requested_name) { + auto env = java_servicer_->attach(); + jmethodID mthd = env->GetMethodID(class_ref_.getReference(), "getClass", "(Ljava/lang/String;)Ljava/lang/Class;"); + if (mthd == nullptr) { + ThrowIf(env); + } + + auto clazz_name = env->NewStringUTF(requested_name.c_str()); + + auto job = env->CallObjectMethod(class_loader_, mthd, clazz_name); + + ThrowIf(env); + + return (jclass) job; + } + + std::pair<std::string, std::string> getAnnotation(const std::string &requested_name, const std::string &method_name) { + auto env = java_servicer_->attach(); + std::string methodName, signature; + { + jmethodID mthd = env->GetMethodID(class_ref_.getReference(), "getMethod", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); + if (mthd == nullptr) { + ThrowIf(env); + } + + auto clazz_name = env->NewStringUTF(requested_name.c_str()); + auto annotation_name = env->NewStringUTF(method_name.c_str()); + + jstring obj = (jstring) env->CallObjectMethod(class_loader_, mthd, clazz_name, annotation_name); + + ThrowIf(env); + + if (obj) { + const char *str = env->GetStringUTFChars(obj, 0); + + methodName = str; + env->ReleaseStringUTFChars(obj, str); + } + } + { + + jmethodID mthd = env->GetMethodID(class_ref_.getReference(), "getSignature", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); Review comment: These blocks have so much in common, can it be a loop? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
