1 /******************************************************************
3 * Copyright 2016 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 ******************************************************************/
21 #include "JniSecurityStatusListener.h"
22 #include "JniRemoteEnrollee.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
26 using namespace OIC::Service;
28 JniSecurityStatusListener::JniSecurityStatusListener(JNIEnv *env, jobject jListener,
29 JniRemoteEnrollee *owner)
30 : m_ownerResource(owner)
32 m_jwListener = env->NewWeakGlobalRef(jListener);
35 JniSecurityStatusListener::~JniSecurityStatusListener()
37 ES_LOGI("~JniSecurityStatusListener()");
41 JNIEnv *env = GetESJNIEnv(ret);
42 if (NULL == env) return;
43 env->DeleteWeakGlobalRef(m_jwListener);
44 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
47 void JniSecurityStatusListener::secProvisionStatusCallback(
48 std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
51 ES_LOGI("JniSecurityStatusListener::secProvisionStatusCallback enter");
54 JNIEnv *env = GetESJNIEnv(ret);
55 if (NULL == env) return;
56 jobject jListener = env->NewLocalRef(m_jwListener);
59 checkExAndRemoveListener(env);
60 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
64 jclass clsL = env->GetObjectClass(jListener);
67 checkExAndRemoveListener(env);
68 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
72 jmethodID midL = env->GetMethodID(clsL, "onProgress",
73 "(Lorg/iotivity/service/easysetup/mediator/"
74 "SecurityProvisioningStatus;"
79 checkExAndRemoveListener(env);
80 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
84 ESResult esResult = secProvisioningStatus->getESResult();
86 //create the java object
87 jobject jSecurityProvisioningStatus = NULL;
88 jSecurityProvisioningStatus = env->NewObject(g_cls_SecurityProvisioningStatus,
89 g_mid_SecurityProvisioningStatus_ctor,
91 env->NewStringUTF(secProvisioningStatus->getDeviceUUID().c_str()));
93 ES_LOGE("JniSecurityStatusListener::onSecurityProvisioningStatus UUID : %s",
94 secProvisioningStatus->getDeviceUUID().c_str());
96 if (!jSecurityProvisioningStatus)
98 ES_LOGE("JniSecurityStatusListener::onSecurityProvisioningStatus Unable to create the java object");
102 env->CallVoidMethod(jListener, midL, jSecurityProvisioningStatus);
104 if (env->ExceptionCheck())
106 ES_LOGE("Java exception is thrown");
107 checkExAndRemoveListener(env);
108 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
112 if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread();
115 void JniSecurityStatusListener::checkExAndRemoveListener(JNIEnv *env)
117 if (env->ExceptionCheck())
119 jthrowable ex = env->ExceptionOccurred();
120 env->ExceptionClear();
121 m_ownerResource->removeStatusListener<JniSecurityStatusListener>(env, m_jwListener);
122 env->Throw((jthrowable)ex);
126 m_ownerResource->removeStatusListener<JniSecurityStatusListener>(env, m_jwListener);