1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
20 #include "jni_easy_setup_jvm.h"
22 #include "jni_easy_setup.h"
26 * @brief This class provides functions for initializing the Java class path and Java class.
38 const char *szClassPath;
43 JClassMap(const char *path) :
51 * @brief This class provides functins for initializing the Java Class path and Java Class
64 const char *szClassPath;
69 JObjectMap(const char *path) :
75 static JClassMap gJClassMapArray[] = { JClassMap(
76 EASY_SETUP_SERVICE_NATIVE_API_CLASS_PATH), JClassMap(
77 EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH) };
79 static JObjectMap gJObjectMapArray[] = { JObjectMap(
80 EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH) };
82 static JNINativeMethod gEasySetupMethodTable[] = {
83 { "InitEasySetup", "()V", (void *) JNIInitEasySetup },
84 { "TerminateEasySetup", "()V", (void *) JNITerminateEasySetup },
85 { "ProvisionEnrollee", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
86 (void *) JNIProvisionEnrollee },
87 { "StopEnrolleeProvisioning", "(I)V", (void *) JNIStopEnrolleeProvisioning }, };
89 static int gEasySetupMethodTableSize = sizeof(gEasySetupMethodTable)
90 / sizeof(gEasySetupMethodTable[0]);
92 int InitializeJClassMapArray(JNIEnv *env) {
93 LOGI("InitializeJClassMapArray: Enter");
95 unsigned int nLen = sizeof(gJClassMapArray) / sizeof(JClassMap);
97 for (unsigned int i = 0; i < nLen; i++) {
98 jclass classRef = env->FindClass(gJClassMapArray[i].szClassPath);
99 if (NULL == classRef) {
100 LOGE("FindClass failed for [%s]", gJClassMapArray[i].szClassPath);
103 gJClassMapArray[i].classRef = (jclass) env->NewGlobalRef(classRef);
104 env->DeleteLocalRef(classRef);
107 LOGI("InitializeJClassMapArray: Exit");
111 jclass GetJClass(const char *szClassPath) {
112 unsigned int nLen = sizeof(gJClassMapArray) / sizeof(JClassMap);
114 jclass classRef = NULL;
116 for (unsigned int i = 0; i < nLen; i++) {
117 if (0 == strcmp(gJClassMapArray[i].szClassPath, szClassPath)) {
118 classRef = gJClassMapArray[i].classRef;
126 void DeleteClassMapArray(JNIEnv *env) {
127 LOGI("DeleteClassMapArray: Enter");
129 unsigned int nLen = sizeof(gJClassMapArray) / sizeof(JClassMap);
130 for (unsigned int i = 0; i < nLen; i++) {
131 if (NULL != gJClassMapArray[i].classRef) {
132 env->DeleteGlobalRef(gJClassMapArray[i].classRef);
133 gJClassMapArray[i].classRef = NULL;
137 LOGI("DeleteClassMapArray: Exit");
140 int InitializeJObjectMapArray(JNIEnv *env) {
141 LOGI("InitializeJObjectMapArray: Enter");
143 unsigned int nLen = sizeof(gJObjectMapArray) / sizeof(JObjectMap);
145 for (unsigned int i = 0; i < nLen; i++) {
146 jclass classRef = env->FindClass(gJObjectMapArray[i].szClassPath);
147 if (NULL == classRef) {
148 LOGE("InitializeJObjectMapArray: FindClass failed for [%s]",
149 gJObjectMapArray[i].szClassPath);
153 std::string methodSignature = "()L";
154 methodSignature.append(gJObjectMapArray[i].szClassPath);
155 methodSignature.append(";");
157 // Get the object form "getInstance"
158 jmethodID methodid = env->GetStaticMethodID(classRef, "getInstance",
159 methodSignature.c_str());
160 if (NULL == methodid) {
161 LOGE("InitializeJObjectMapArray: GetStaticMethodID failed for [%s]",
162 gJObjectMapArray[i].szClassPath);
166 // Get the singleton object
167 jobject objectRef = (jobject) env->CallStaticObjectMethod(classRef,
169 if (NULL == objectRef) {
171 "InitializeJObjectMapArray: CallStaticObjectMethod failed for [%s]",
172 gJObjectMapArray[i].szClassPath);
176 gJObjectMapArray[i].object = (jobject) env->NewGlobalRef(objectRef);
177 env->DeleteLocalRef(classRef);
180 LOGI("InitializeJObjectMapArray: Exit");
184 jobject GetJObjectInstance(const char *szClassPath) {
185 unsigned int nLen = sizeof(gJObjectMapArray) / sizeof(JObjectMap);
187 jobject object = NULL;
189 for (unsigned int i = 0; i < nLen; i++) {
190 if (0 == strcmp(gJObjectMapArray[i].szClassPath, szClassPath)) {
191 object = gJObjectMapArray[i].object;
199 void DeleteObjectMapArray(JNIEnv *env) {
200 LOGI("DeleteObjectMapArray: Enter");
202 unsigned int nLen = sizeof(gJObjectMapArray) / sizeof(JObjectMap);
203 for (unsigned int i = 0; i < nLen; i++) {
204 if (NULL != gJObjectMapArray[i].object) {
205 env->DeleteGlobalRef(gJObjectMapArray[i].object);
206 gJObjectMapArray[i].object = NULL;
210 LOGI("DeleteObjectMapArray: Exit");
213 JavaVM *EasySetupJVM::m_jvm = NULL;
214 std::mutex EasySetupJVM::m_currentThreadMutex;
215 JNIEnv *EasySetupJVM::getEnv() {
216 std::unique_lock < std::mutex > scoped_lock(m_currentThreadMutex);
219 LOGE("Failed to get JVM");
224 jint ret = m_jvm->GetEnv((void **) &env, JNI_CURRENT_VERSION);
229 if (0 > m_jvm->AttachCurrentThread(&env, NULL)) {
230 LOGE("Failed to attach current thread to env");
235 LOGE("JNI version not supported");
238 LOGE("Failed to get the environment");
243 void EasySetupJVM::releaseEnv() {
244 std::unique_lock < std::mutex > scoped_lock(m_currentThreadMutex);
247 LOGE("Failed to release JVM");
251 m_jvm->DetachCurrentThread();
257 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
258 LOGD("JNI_OnLoad: Enter");
261 LOGE("JNI_OnLoad: vm is invalid");
266 if (JNI_OK != vm->GetEnv((void **) &env, JNI_CURRENT_VERSION)) {
267 LOGE("JNI_OnLoad: Version check is failed!");
271 if (0 != InitializeJClassMapArray(env)) {
272 LOGE("JNI_OnLoad: Initialize JClass Array failed!");
276 if (0 != InitializeJObjectMapArray(env)) {
277 LOGE("JNI_OnLoad: Initialize JObject Array failed!");
281 jclass easySetupClassRef = GetJClass(
282 EASY_SETUP_SERVICE_NATIVE_API_CLASS_PATH);
283 if (NULL == easySetupClassRef) {
284 LOGE("JNI_OnLoad: GetJClass gEasySetupClass failed !");
287 env->RegisterNatives(easySetupClassRef, gEasySetupMethodTable,
288 gEasySetupMethodTableSize);
290 EasySetupJVM::m_jvm = vm;
292 LOGI("JNI_OnLoad: Exit");
293 return JNI_CURRENT_VERSION;
296 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
298 LOGD("JNI_OnUnload: Enter");
301 if (JNI_OK != vm->GetEnv((void **)&env, JNI_CURRENT_VERSION))
303 LOGE("JNI_OnLoad: Version check is failed!");
307 // delete all class references
308 DeleteClassMapArray(env);
310 // delete all jobject
311 DeleteObjectMapArray(env);
313 LOGD("JNI_OnUnload: Exit");