replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / android / notification-service / src / main / jni / common / JniNotificationCommon.cpp
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 #include "JniNotificationCommon.h"\r
22 #include <cstddef>\r
23 #include "JniUtils.h"\r
24 \r
25 static jclass g_cls_NSException = NULL;\r
26 static jmethodID g_mid_NSException_ctor = NULL;\r
27 static jmethodID g_mid_NSException_setNativeExceptionLocation = NULL;\r
28 \r
29 static const char *NSResultToChar(const int nsresult)\r
30 {\r
31     switch (nsresult)\r
32     {\r
33         case JNI_NS_OK:\r
34             return "OK";\r
35         case JNI_NS_ERROR:\r
36             return "ERROR";\r
37         case JNI_NS_SUCCESS:\r
38             return "SUCCESS";\r
39         case JNI_NS_FAIL:\r
40             return "FAIL";\r
41         case JNI_NS_ALLOW:\r
42             return "ALLOW";\r
43         case JNI_NS_DENY:\r
44             return "DENY";\r
45         case JNI_EXCEPTION:\r
46             return "JNI_EXCEPTION";\r
47         case JNI_NO_NATIVE_POINTER:\r
48             return "JNI_NO_NATIVE_POINTER";\r
49         case JNI_INVALID_VALUE:\r
50             return "JNI_INVALID_VALUE";\r
51         case NATIVE_EXCEPTION:\r
52             return "NATIVE_EXCEPTION";\r
53         default:\r
54             return "";\r
55     }\r
56 }\r
57 \r
58 \r
59 jobject getNSException(JNIEnv *env, const char *file, const char *functionName,\r
60                        const int line, const int code, const char *message)\r
61 {\r
62     NS_LOGE ("Failed : %s" , message );\r
63     const char *codeChar = NSResultToChar(code);\r
64     if (codeChar[0] == '\0')\r
65     {\r
66         codeChar = NSResultToChar(JNI_INVALID_VALUE);\r
67     }\r
68     jobject exception = env->NewObject(\r
69                             g_cls_NSException,\r
70                             g_mid_NSException_ctor,\r
71                             env->NewStringUTF(codeChar),\r
72                             env->NewStringUTF(message));\r
73     if (!exception)\r
74     {\r
75         return NULL;\r
76     }\r
77     env->CallVoidMethod(\r
78         exception,\r
79         g_mid_NSException_setNativeExceptionLocation,\r
80         env->NewStringUTF(file),\r
81         env->NewStringUTF(functionName),\r
82         line);\r
83     if (env->ExceptionCheck())\r
84     {\r
85         return NULL;\r
86     }\r
87     return exception;\r
88 }\r
89 \r
90 void throwNSException(JNIEnv *env, jobject exception)\r
91 {\r
92     env->Throw((jthrowable)exception);\r
93 }\r
94 \r
95 int NSExceptionInit(JNIEnv *env)\r
96 {\r
97     if (!env)\r
98     {\r
99         NS_LOGE ("JNIEnv is null");\r
100         return JNI_ERR;\r
101     }\r
102 \r
103     //OcException\r
104     jclass localNSException = env->FindClass(\r
105                                   "org/iotivity/service/ns/common/NSException");\r
106     if (!localNSException)\r
107     {\r
108         NS_LOGE ("Failed to get local NSException");\r
109         return JNI_ERR;\r
110     }\r
111     g_cls_NSException = (jclass)env->NewGlobalRef(localNSException);\r
112 \r
113 \r
114     g_mid_NSException_ctor = env->GetMethodID(g_cls_NSException,\r
115                              "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");\r
116     if (!g_mid_NSException_ctor)\r
117     {\r
118         NS_LOGE ("Failed to Get MethodID");\r
119         return JNI_ERR;\r
120     }\r
121 \r
122     g_mid_NSException_setNativeExceptionLocation = env->GetMethodID(g_cls_NSException,\r
123             "setNativeExceptionLocation",\r
124             "(Ljava/lang/String;Ljava/lang/String;I)V");\r
125     if (!g_mid_NSException_setNativeExceptionLocation)\r
126     {\r
127         NS_LOGE ("Failed to Get MethodID");\r
128         return JNI_ERR;\r
129     }\r
130 \r
131     env->DeleteLocalRef(localNSException);\r
132 \r
133     return JNI_CURRENT_VERSION;\r
134 }\r