2
0

java_iface.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2013 Konstantin Mosesov
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. *
  12. * This file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include "../../str.h"
  23. #include "../../sr_module.h"
  24. #include <string.h>
  25. #include <jni.h>
  26. #include "global.h"
  27. #include "java_iface.h"
  28. #include "utils.h"
  29. #include "app_java_mod.h"
  30. #include "java_iface.h"
  31. #include "java_support.h"
  32. #include "java_native_methods.h"
  33. #include "java_msgobj.h"
  34. #include "java_sig_parser.h"
  35. /*
  36. example of java prototype: public int method_name();
  37. example of kamailio invocation: java_method_exec("method_name", "V");
  38. */
  39. int j_nst_exec_0(struct sip_msg *msgp, char *method_name, char *signature)
  40. {
  41. return java_exec(msgp, 0, 0, method_name, signature, NULL);
  42. }
  43. /*
  44. example of java prototype: public int method_name(int param);
  45. example of kamailio invocation: java_method_exec("method_name", "I", "5");
  46. */
  47. int j_nst_exec_1(struct sip_msg *msgp, char *method_name, char *signature, char *param)
  48. {
  49. return java_exec(msgp, 0, 0, method_name, signature, param);
  50. }
  51. /*
  52. example of java prototype: public synchronized int method_name();
  53. example of kamailio invocation: java_s_method_exec("method_name", "V");
  54. */
  55. int j_s_nst_exec_0(struct sip_msg *msgp, char *method_name, char *signature)
  56. {
  57. return java_exec(msgp, 0, 1, method_name, signature, NULL);
  58. }
  59. /*
  60. example of java prototype: public synchronized int method_name(int param);
  61. example of kamailio invocation: java_s_method_exec("method_name", "I", "5");
  62. */
  63. int j_s_nst_exec_1(struct sip_msg *msgp, char *method_name, char *signature, char *param)
  64. {
  65. return java_exec(msgp, 0, 1, method_name, signature, param);
  66. }
  67. /*
  68. example of java prototype: public static int method_name();
  69. example of kamailio invocation: java_staticmethod_exec("method_name", "V");
  70. */
  71. int j_st_exec_0(struct sip_msg *msgp, char *method_name, char *signature)
  72. {
  73. return java_exec(msgp, 1, 0, method_name, signature, NULL);
  74. }
  75. /*
  76. example of java prototype: public static int method_name(int param);
  77. example of kamailio invocation: java_staticmethod_exec("method_name", "I", "5");
  78. */
  79. int j_st_exec_1(struct sip_msg *msgp, char *method_name, char *signature, char *param)
  80. {
  81. return java_exec(msgp, 1, 0, method_name, signature, param);
  82. }
  83. /*
  84. example of java prototype: public static synchronized int method_name();
  85. example of kamailio invocation: java_s_staticmethod_exec("method_name", "V");
  86. */
  87. int j_s_st_exec_0(struct sip_msg *msgp, char *method_name, char *signature)
  88. {
  89. return java_exec(msgp, 1, 1, method_name, signature, NULL);
  90. }
  91. /*
  92. example of java prototype: public static synchronized int method_name(int param);
  93. example of kamailio invocation: java_s_staticmethod_exec("method_name", "I", "5");
  94. */
  95. int j_s_st_exec_1(struct sip_msg *msgp, char *method_name, char *signature, char *param)
  96. {
  97. return java_exec(msgp, 1, 1, method_name, signature, param);
  98. }
  99. int java_exec(struct sip_msg *msgp, int is_static, int is_synchronized, char *method_name, char *signature, char *param)
  100. {
  101. char *retval_sig;
  102. char *cs;
  103. size_t cslen;
  104. jint retval;
  105. int locked;
  106. jfieldID fid;
  107. jclass cls;
  108. jmethodID invk_method, invk_method_ref;
  109. jvalue *jparam;
  110. if (signature == NULL || !strcmp(signature, ""))
  111. {
  112. LM_ERR("%s: java_method_exec(): signature is empty or invalid.\n", APP_NAME);
  113. return -1;
  114. }
  115. if (param == NULL && strcmp(signature, "V"))
  116. {
  117. LM_ERR("%s: java_method_exec(): no parameter (parameter is NULL) but signature '%s' is not equals to 'V'.\n", APP_NAME, signature);
  118. return -1;
  119. }
  120. if (is_sig_allowed(signature) == 0)
  121. {
  122. LM_ERR("%s: java_method_exec(): error: signature '%s' isn't supported yet.\n", APP_NAME, signature);
  123. return -1;
  124. }
  125. if (!strcmp(signature, "V"))
  126. {
  127. signature = "";
  128. }
  129. retval_sig = "I";
  130. cslen = strlen(signature) + 2 + 1 + 1; // '(' + 'signature' + ')' + 'return signature' + null terminator
  131. cs = (char *)pkg_malloc(cslen * sizeof(char));
  132. if (!cs)
  133. {
  134. LM_ERR("%s: pkg_malloc() has failed. Can't allocate %lu bytes. Not enough memory!\n", APP_NAME, (unsigned long)cslen);
  135. return -1;
  136. }
  137. snprintf(cs, cslen, "(%s)%s", signature, retval_sig);
  138. cs[cslen] = '\0';
  139. // attach to current thread
  140. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  141. if ((*env)->ExceptionCheck(env))
  142. {
  143. handle_exception();
  144. return -1;
  145. }
  146. cls = (*env)->GetObjectClass(env, KamailioClassInstance);
  147. if ((*env)->ExceptionCheck(env))
  148. {
  149. handle_exception();
  150. (*jvm)->DetachCurrentThread(jvm);
  151. return -1;
  152. }
  153. fid = (*env)->GetFieldID(env, cls, "mop", "I");
  154. if (!fid)
  155. {
  156. handle_exception();
  157. (*jvm)->DetachCurrentThread(jvm);
  158. return -1;
  159. }
  160. msg = msgp;
  161. // find a method by signature
  162. invk_method = is_static ?
  163. (*env)->GetStaticMethodID(env, KamailioClassRef, method_name, cs) :
  164. (*env)->GetMethodID(env, KamailioClassRef, method_name, cs);
  165. if (!invk_method || (*env)->ExceptionCheck(env))
  166. {
  167. handle_exception();
  168. (*jvm)->DetachCurrentThread(jvm);
  169. return -1;
  170. }
  171. pkg_free(cs);
  172. // keep local reference to method
  173. invk_method_ref = (*env)->NewLocalRef(env, invk_method);
  174. if (!invk_method_ref || (*env)->ExceptionCheck(env))
  175. {
  176. handle_exception();
  177. (*env)->DeleteLocalRef(env, invk_method_ref);
  178. (*jvm)->DetachCurrentThread(jvm);
  179. return -1;
  180. }
  181. retval = -1;
  182. if (is_synchronized)
  183. {
  184. if ((*env)->MonitorEnter(env, invk_method_ref) != JNI_OK)
  185. {
  186. locked = 0;
  187. LM_ERR("%s: MonitorEnter() has failed! Can't synchronize!\n", APP_NAME);
  188. }
  189. else
  190. {
  191. locked = 1;
  192. }
  193. }
  194. if (param == NULL)
  195. {
  196. retval = is_static ?
  197. (int)(*env)->CallStaticIntMethod(env, KamailioClassRef, invk_method_ref) :
  198. (int)(*env)->CallIntMethod(env, KamailioClassInstanceRef, invk_method_ref);
  199. }
  200. else
  201. {
  202. jparam = get_value_by_sig_type(signature, param);
  203. if (jparam == NULL)
  204. {
  205. (*env)->DeleteLocalRef(env, invk_method_ref);
  206. (*env)->DeleteLocalRef(env, invk_method);
  207. (*jvm)->DetachCurrentThread(jvm);
  208. return -1;
  209. }
  210. retval = is_static ?
  211. (int)(*env)->CallStaticIntMethod(env, KamailioClassRef, invk_method_ref, *jparam) :
  212. (int)(*env)->CallIntMethod(env, KamailioClassInstanceRef, invk_method_ref, *jparam);
  213. }
  214. if ((*env)->ExceptionCheck(env))
  215. {
  216. LM_ERR("%s: %s(): %s() has failed. See exception below.\n", APP_NAME,
  217. (is_static ?
  218. (is_synchronized ? "java_s_staticmethod_exec" : "java_staticmethod_exec") :
  219. (is_synchronized ? "java_s_method_exec" : "java_method_exec")
  220. ),
  221. is_static ? "CallStaticIntMethod" : "CallIntMethod"
  222. );
  223. handle_exception();
  224. (*env)->DeleteLocalRef(env, invk_method_ref);
  225. (*env)->DeleteLocalRef(env, invk_method);
  226. (*jvm)->DetachCurrentThread(jvm);
  227. return -1;
  228. }
  229. if (is_synchronized && locked)
  230. {
  231. if ((*env)->MonitorExit(env, invk_method_ref) != JNI_OK)
  232. {
  233. LM_ERR("%s: MonitorExit() has failed! Can't synchronize!\n", APP_NAME);
  234. }
  235. }
  236. (*env)->DeleteLocalRef(env, invk_method_ref);
  237. (*env)->DeleteLocalRef(env, invk_method);
  238. (*jvm)->DetachCurrentThread(jvm);
  239. return retval;
  240. }