comparison Plugin/JavaVirtualMachine.cpp @ 5:c8f19e93ff99

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Oct 2023 08:49:07 +0200
parents
children 1c407ba1d311
comparison
equal deleted inserted replaced
4:9032ffb3a7d5 5:c8f19e93ff99
1 /**
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * Java plugin for Orthanc
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium
9 *
10 * This program is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **/
23
24
25 #include "JavaVirtualMachine.h"
26
27 #include <cassert>
28 #include <stdexcept>
29 #include <vector>
30
31
32 JavaVirtualMachine::JavaVirtualMachine(const std::string& classPath)
33 {
34 std::string classPathOption = "-Djava.class.path=" + classPath;
35
36 std::vector<JavaVMOption> options;
37
38 options.resize(1);
39 options[0].optionString = const_cast<char*>(classPathOption.c_str());
40 options[0].extraInfo = NULL;
41
42 #if !defined(NDEBUG)
43 // Debug mode
44 {
45 JavaVMOption option;
46 option.optionString = const_cast<char*>("-Xcheck:jni");
47 option.extraInfo = NULL;
48 options.push_back(option);
49 }
50 #endif
51
52 JavaVMInitArgs vm_args;
53 vm_args.version = JNI_VERSION_1_6;
54 vm_args.nOptions = options.size();
55 vm_args.options = (options.empty() ? NULL : &options[0]);
56 vm_args.ignoreUnrecognized = false;
57
58 JNIEnv* env = NULL;
59 jint res = JNI_CreateJavaVM(&jvm_, (void **) &env, &vm_args);
60 if (res != JNI_OK ||
61 jvm_ == NULL ||
62 env == NULL)
63 {
64 throw std::runtime_error("Cannot create the JVM");
65 }
66 }
67
68
69 JavaVirtualMachine::~JavaVirtualMachine()
70 {
71 jvm_->DestroyJavaVM();
72 }
73
74
75 JavaVM& JavaVirtualMachine::GetValue()
76 {
77 assert(jvm_ != NULL);
78 return *jvm_;
79 }