java_iface.c 7.8 KB

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