[Resource-Container] Java SDK and Sample applications
[contrib/iotivity.git] / service / resource-container / android / service / src / main / jni / util / JavaExceptions.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "JavaExceptions.h"
22
23 #include "JNIEnvWrapper.h"
24 #include "Verify.h"
25
26 #include "RCSException.h"
27
28 namespace
29 {
30     jclass g_cls_PlatformException;
31
32     jmethodID g_ctor_PlatformException;
33 }
34
35 void initJavaExceptions(JNIEnvWrapper *env)
36 {
37     g_cls_PlatformException = env->FindClassAsGlobalRef(EXC_NAME_PLATFORM);
38     g_ctor_PlatformException = env->GetConstructorID(g_cls_PlatformException,
39                                "(" AS_SIG(CLS_NAME_STRING) "I)V");
40 }
41
42 void clearJavaExceptions(JNIEnvWrapper *env)
43 {
44     env->DeleteGlobalRef(g_cls_PlatformException);
45 }
46
47 void throwPlatformException(JNIEnv *env, const OIC::Service::RCSPlatformException &e)
48 {
49     auto msg = newStringObject(env, e.getReason());
50     VERIFY_NO_EXC(env);
51
52     auto exObj = env->NewObject(g_cls_PlatformException, g_ctor_PlatformException,
53                                 msg, e.getReasonCode());
54     VERIFY_NO_EXC(env);
55
56     env->Throw(static_cast< jthrowable >(exObj));
57 }