src: applied clang-format
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "JavaEnvironment.h"
|
||||
#include "ApplicationSettings.h"
|
||||
#include "JavaEnvironment.h"
|
||||
#include "logging.h"
|
||||
#include "utilityLibrary.h"
|
||||
#include "utilityWindows.h"
|
||||
@@ -22,12 +22,13 @@ void JavaEnvironmentFactory::createInstance(std::string classPath, std::string&
|
||||
{
|
||||
LOG_ERROR("java classpath cannot be changed!");
|
||||
// todo: implement destroying the old factory instance and create a new one.
|
||||
// may be not so easy... there can only be one java env per process (which can never be destroyed) -.-
|
||||
// may be not so easy... there can only be one java env per process (which can never be
|
||||
// destroyed) -.-
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::function<jint (JavaVM**, void**, void*)> createInstanceFunction;
|
||||
std::function<jint(JavaVM**, void**, void*)> createInstanceFunction;
|
||||
const FilePath javaPath = ApplicationSettings::getInstance()->getJavaPath();
|
||||
|
||||
if (javaPath.empty())
|
||||
@@ -37,10 +38,7 @@ void JavaEnvironmentFactory::createInstance(std::string classPath, std::string&
|
||||
}
|
||||
|
||||
createInstanceFunction = utility::loadFunctionFromLibrary<jint, JavaVM**, void**, void*>(
|
||||
javaPath,
|
||||
"JNI_CreateJavaVM",
|
||||
errorString
|
||||
);
|
||||
javaPath, "JNI_CreateJavaVM", errorString);
|
||||
|
||||
if (!createInstanceFunction && !errorString.empty())
|
||||
{
|
||||
@@ -51,52 +49,54 @@ void JavaEnvironmentFactory::createInstance(std::string classPath, std::string&
|
||||
|
||||
const int optionCount = 2;
|
||||
|
||||
JavaVM* jvm = nullptr; // Pointer to the JVM (Java Virtual Machine)
|
||||
JNIEnv* env = nullptr; // Pointer to native interface
|
||||
JavaVM* jvm = nullptr; // Pointer to the JVM (Java Virtual Machine)
|
||||
JNIEnv* env = nullptr; // Pointer to native interface
|
||||
|
||||
JavaVMInitArgs vm_args; // Initialization arguments
|
||||
JavaVMOption* options = new JavaVMOption[optionCount]; // JVM invocation options
|
||||
JavaVMInitArgs vm_args; // Initialization arguments
|
||||
JavaVMOption* options = new JavaVMOption[optionCount]; // JVM invocation options
|
||||
std::string classPathOption = "-Djava.class.path=" + classPath;
|
||||
options[0].optionString = const_cast<char*>(classPathOption.c_str());
|
||||
options[1].optionString = const_cast<char*>("-Xms64m");
|
||||
|
||||
// use these options to enable profiling in VisualVM
|
||||
//options[2].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote");
|
||||
//options[3].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote.port=9010");
|
||||
//options[4].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote.local.only=false");
|
||||
//options[5].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote.authenticate=false");
|
||||
//options[6].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote.ssl=false");
|
||||
// options[2].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote");
|
||||
// options[3].optionString = const_cast<char*>("-Dcom.sun.management.jmxremote.port=9010");
|
||||
// options[4].optionString =
|
||||
// const_cast<char*>("-Dcom.sun.management.jmxremote.local.only=false"); options[5].optionString
|
||||
// = const_cast<char*>("-Dcom.sun.management.jmxremote.authenticate=false"); options[6].optionString
|
||||
// = const_cast<char*>("-Dcom.sun.management.jmxremote.ssl=false");
|
||||
|
||||
vm_args.version = JNI_VERSION_1_8;
|
||||
vm_args.nOptions = optionCount;
|
||||
vm_args.options = options;
|
||||
vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
|
||||
vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
|
||||
|
||||
jint rc = createInstanceFunction(&jvm, (void**)&env, &vm_args);
|
||||
|
||||
delete [] options;
|
||||
delete[] options;
|
||||
|
||||
if (rc != JNI_OK)
|
||||
{
|
||||
if(rc == JNI_EVERSION)
|
||||
if (rc == JNI_EVERSION)
|
||||
{
|
||||
errorString = "JVM is oudated and doesn't meet requirements";
|
||||
}
|
||||
else if(rc == JNI_ENOMEM)
|
||||
else if (rc == JNI_ENOMEM)
|
||||
{
|
||||
errorString = "not enough memory for JVM";
|
||||
}
|
||||
else if(rc == JNI_EINVAL)
|
||||
else if (rc == JNI_EINVAL)
|
||||
{
|
||||
errorString = "invalid argument for launching JVM";
|
||||
}
|
||||
else if(rc == JNI_EEXIST)
|
||||
else if (rc == JNI_EEXIST)
|
||||
{
|
||||
errorString = "the process can only launch one JVM an not more";
|
||||
}
|
||||
else
|
||||
{
|
||||
errorString = "could not create the JVM instance (error code " + std::to_string(rc) + ")";
|
||||
errorString = "could not create the JVM instance (error code " + std::to_string(rc) +
|
||||
")";
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -113,7 +113,7 @@ std::shared_ptr<JavaEnvironmentFactory> JavaEnvironmentFactory::getInstance()
|
||||
|
||||
JavaEnvironmentFactory::~JavaEnvironmentFactory()
|
||||
{
|
||||
// todo: what if there are threads running using the jvm?? log something!
|
||||
// todo: what if there are threads running using the jvm?? log something!
|
||||
m_jvm->DestroyJavaVM();
|
||||
}
|
||||
|
||||
@@ -126,7 +126,8 @@ std::shared_ptr<JavaEnvironment> JavaEnvironmentFactory::createEnvironment()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_threadIdToEnvAndUserCountMutex);
|
||||
|
||||
std::map<std::thread::id, std::pair<JNIEnv*, int>>::const_iterator it = m_threadIdToEnvAndUserCount.find(currentThreadId);
|
||||
std::map<std::thread::id, std::pair<JNIEnv*, int>>::const_iterator it =
|
||||
m_threadIdToEnvAndUserCount.find(currentThreadId);
|
||||
if (it != m_threadIdToEnvAndUserCount.end())
|
||||
{
|
||||
env = it->second.first;
|
||||
@@ -134,7 +135,8 @@ std::shared_ptr<JavaEnvironment> JavaEnvironmentFactory::createEnvironment()
|
||||
else
|
||||
{
|
||||
m_jvm->AttachCurrentThread((void**)&env, NULL);
|
||||
m_threadIdToEnvAndUserCount.insert(std::make_pair(currentThreadId, std::make_pair(env, 0)));
|
||||
m_threadIdToEnvAndUserCount.insert(
|
||||
std::make_pair(currentThreadId, std::make_pair(env, 0)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,17 +147,15 @@ std::shared_ptr<JavaEnvironmentFactory> JavaEnvironmentFactory::s_instance;
|
||||
|
||||
std::string JavaEnvironmentFactory::s_classPath;
|
||||
|
||||
JavaEnvironmentFactory::JavaEnvironmentFactory(JavaVM* jvm)
|
||||
: m_jvm(jvm)
|
||||
{
|
||||
}
|
||||
JavaEnvironmentFactory::JavaEnvironmentFactory(JavaVM* jvm): m_jvm(jvm) {}
|
||||
|
||||
void JavaEnvironmentFactory::registerEnvironment()
|
||||
{
|
||||
std::thread::id currentThreadId = std::this_thread::get_id();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_threadIdToEnvAndUserCountMutex);
|
||||
std::map<std::thread::id, std::pair<JNIEnv*, int>>::iterator it = m_threadIdToEnvAndUserCount.find(currentThreadId);
|
||||
std::map<std::thread::id, std::pair<JNIEnv*, int>>::iterator it =
|
||||
m_threadIdToEnvAndUserCount.find(currentThreadId);
|
||||
if (it != m_threadIdToEnvAndUserCount.end())
|
||||
{
|
||||
it->second.second++;
|
||||
@@ -172,12 +172,13 @@ void JavaEnvironmentFactory::unregisterEnvironment()
|
||||
std::thread::id currentThreadId = std::this_thread::get_id();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_threadIdToEnvAndUserCountMutex);
|
||||
std::map<std::thread::id, std::pair<JNIEnv*, int>>::iterator it = m_threadIdToEnvAndUserCount.find(currentThreadId);
|
||||
std::map<std::thread::id, std::pair<JNIEnv*, int>>::iterator it =
|
||||
m_threadIdToEnvAndUserCount.find(currentThreadId);
|
||||
if (it != m_threadIdToEnvAndUserCount.end())
|
||||
{
|
||||
it->second.second--;
|
||||
if (it->second.second == 0)
|
||||
{ // TODO: currently this happens quite often. do something about that.
|
||||
{ // TODO: currently this happens quite often. do something about that.
|
||||
m_jvm->DetachCurrentThread();
|
||||
m_threadIdToEnvAndUserCount.erase(it);
|
||||
}
|
||||
@@ -188,5 +189,3 @@ void JavaEnvironmentFactory::unregisterEnvironment()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user