| 38 | Jvm* Jvm::instance = nullptr; |
| 39 | |
| 40 | Try<Jvm*> Jvm::create( |
| 41 | const std::vector<std::string>& _options, |
| 42 | JNI::Version version, |
| 43 | bool exceptions) |
| 44 | { |
| 45 | // TODO(benh): Make this thread-safe. |
| 46 | if (instance != nullptr) { |
| 47 | return Error("Java Virtual Machine already created"); |
| 48 | } |
| 49 | |
| 50 | JavaVMInitArgs vmArgs; |
| 51 | vmArgs.version = version; |
| 52 | vmArgs.ignoreUnrecognized = false; |
| 53 | |
| 54 | std::vector<std::string> options = _options; |
| 55 | |
| 56 | #ifdef __APPLE__ |
| 57 | // The Apple JNI implementation requires the AWT thread to not |
| 58 | // be the main application thread. Enabling headless mode |
| 59 | // circumvents the issue. Further details: |
| 60 | // https://issues.apache.org/jira/browse/MESOS-524 |
| 61 | // http://www.oracle.com/technetwork/articles/javase/headless-136834.html |
| 62 | if (std::find(options.begin(), options.end(), "-Djava.awt.headless=true") |
| 63 | == options.end()) { |
| 64 | options.push_back("-Djava.awt.headless=true"); |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | JavaVM* jvm = nullptr; |
| 69 | JNIEnv* env = nullptr; |
| 70 | Option<std::string> libJvmPath = os::getenv("JAVA_JVM_LIBRARY"); |
| 71 | |
| 72 | if (libJvmPath.isNone()) { |
| 73 | libJvmPath = mesos::internal::build::JAVA_JVM_LIBRARY; |
| 74 | } |
| 75 | |
| 76 | static DynamicLibrary* libJvm = new DynamicLibrary(); |
| 77 | Try<Nothing> openResult = libJvm->open(libJvmPath.get()); |
| 78 | |
| 79 | if (openResult.isError()) { |
| 80 | return Error(openResult.error()); |
| 81 | } |
| 82 | |
| 83 | Try<void*> symbol = libJvm->loadSymbol("JNI_CreateJavaVM"); |
| 84 | |
| 85 | if (symbol.isError()) { |
| 86 | libJvm->close(); |
| 87 | return Error(symbol.error()); |
| 88 | } |
| 89 | |
| 90 | std::vector<JavaVMOption> opts(options.size()); |
| 91 | for (size_t i = 0; i < options.size(); i++) { |
| 92 | opts[i].optionString = const_cast<char*>(options[i].c_str()); |
| 93 | } |
| 94 | |
| 95 | vmArgs.nOptions = opts.size(); |
| 96 | if (!opts.empty()) { |
| 97 | vmArgs.options = &opts[0]; |
no test coverage detected