2
0

java_mod.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <libgen.h>
  25. #include "../../str.h"
  26. #include "../../sr_module.h"
  27. #include <jni.h>
  28. #include "global.h"
  29. #include "utils.h"
  30. #include "java_mod.h"
  31. #include "java_iface.h"
  32. #include "java_support.h"
  33. #include "java_native_methods.h"
  34. MODULE_VERSION
  35. static char* class_name = "Kamailio";
  36. static char* child_init_mname = "child_init";
  37. static char* java_options_str = "-Djava.compiler=NONE";
  38. static int mod_init(void);
  39. static int child_init(int rank);
  40. static void mod_destroy(void);
  41. /** module parameters */
  42. static param_export_t params[] = {
  43. {"class_name", PARAM_STRING, &class_name },
  44. {"child_init_method", PARAM_STRING, &child_init_mname }, /* Unused parameter? */
  45. {"java_options", PARAM_STRING, &java_options_str },
  46. {"force_cmd_exec", INT_PARAM, &force_cmd_exec },
  47. {0,0,0}
  48. };
  49. /*
  50. * Exported functions
  51. */
  52. static cmd_export_t cmds[] = {
  53. { "java_method_exec", (cmd_function)j_nst_exec_0, 2, NULL, 0, ANY_ROUTE },
  54. { "java_method_exec", (cmd_function)j_nst_exec_1, 3, NULL, 0, ANY_ROUTE },
  55. { "java_s_method_exec", (cmd_function)j_s_nst_exec_0, 2, NULL, 0, ANY_ROUTE },
  56. { "java_s_method_exec", (cmd_function)j_s_nst_exec_1, 3, NULL, 0, ANY_ROUTE },
  57. { "java_staticmethod_exec", (cmd_function)j_st_exec_0, 2, NULL, 0, ANY_ROUTE },
  58. { "java_staticmethod_exec", (cmd_function)j_st_exec_1, 3, NULL, 0, ANY_ROUTE },
  59. { "java_s_staticmethod_exec", (cmd_function)j_s_st_exec_0, 2, NULL, 0, ANY_ROUTE },
  60. { "java_s_staticmethod_exec", (cmd_function)j_s_st_exec_1, 3, NULL, 0, ANY_ROUTE },
  61. { 0, 0, 0, 0, 0, 0 }
  62. };
  63. /** module exports */
  64. struct module_exports exports = {
  65. APP_NAME, /* module name */
  66. // RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
  67. DEFAULT_DLFLAGS, /* dlopen flags */
  68. cmds, /* exported functions */
  69. params, /* exported parameters */
  70. 0, /* exported statistics */
  71. 0, /* exported MI functions */
  72. 0, /* exported pseudo-variables */
  73. 0, /* extra processes */
  74. mod_init, /* module initialization function */
  75. (response_function) NULL, /* response handling function */
  76. (destroy_function) mod_destroy, /* destroy function */
  77. child_init /* per-child init function */
  78. };
  79. static int mod_init(void)
  80. {
  81. JavaVMInitArgs vm_args;
  82. jint res;
  83. JavaVMOption *options;
  84. char **opts;
  85. int nOptions;
  86. if (force_cmd_exec < 0 || force_cmd_exec > 1)
  87. {
  88. LM_ERR("Parameter force_cmd_exec should be either 0 or 1\n");
  89. return -1;
  90. }
  91. if (force_cmd_exec)
  92. {
  93. LM_NOTICE("%s: Parameter force_cmd_exec may cause a memory leaks if used from embedded languages\n", APP_NAME);
  94. }
  95. options = (JavaVMOption *)pkg_malloc(sizeof(JavaVMOption));
  96. if (!options)
  97. {
  98. LM_ERR("pkg_malloc() failed: Couldn't initialize Java VM: Not enough memory\n");
  99. return -1;
  100. }
  101. memset(options, 0, sizeof(JavaVMOption));
  102. LM_INFO("Initializing Java VM with options: %s\n", java_options_str);
  103. opts = split(java_options_str, " ");
  104. for (nOptions=0; opts[nOptions] != NULL; nOptions++)
  105. {
  106. options[nOptions].optionString = opts[nOptions];
  107. }
  108. /* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
  109. vm_args.version = JNI_VERSION_1_2;
  110. vm_args.nOptions = nOptions;
  111. vm_args.ignoreUnrecognized = JNI_FALSE;
  112. vm_args.options = options;
  113. res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
  114. if (res < 0)
  115. {
  116. handle_VM_init_failure(res);
  117. return -1;
  118. }
  119. LM_INFO("%s: Java VM initialization OK\n", APP_NAME);
  120. // attach to current thread
  121. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  122. if ((*env)->ExceptionCheck(env))
  123. {
  124. handle_exception();
  125. return -1;
  126. }
  127. KamailioClass = (*env)->FindClass(env, class_name);
  128. if (!KamailioClass || (*env)->ExceptionCheck(env))
  129. {
  130. handle_exception();
  131. (*jvm)->DetachCurrentThread(jvm);
  132. return -1;
  133. }
  134. KamailioClassRef = (*env)->NewGlobalRef(env, KamailioClass);
  135. if (!KamailioClassRef || (*env)->ExceptionCheck(env))
  136. {
  137. handle_exception();
  138. (*jvm)->DetachCurrentThread(jvm);
  139. return -1;
  140. }
  141. KamailioID = (*env)->GetMethodID(env, KamailioClass, "<init>", "()V");
  142. if (!KamailioID || (*env)->ExceptionCheck(env))
  143. {
  144. handle_exception();
  145. (*jvm)->DetachCurrentThread(jvm);
  146. return -1;
  147. }
  148. // calling constructor
  149. KamailioClassInstance = (*env)->NewObject(env, KamailioClass, KamailioID);
  150. if (!KamailioClassInstance || (*env)->ExceptionCheck(env))
  151. {
  152. handle_exception();
  153. (*jvm)->DetachCurrentThread(jvm);
  154. return -1;
  155. }
  156. // keep a reference to kamailio class instance
  157. KamailioClassInstanceRef = (*env)->NewGlobalRef(env, KamailioClassInstance);
  158. if (!KamailioClassInstanceRef || (*env)->ExceptionCheck(env))
  159. {
  160. handle_exception();
  161. (*jvm)->DetachCurrentThread(jvm);
  162. return -1;
  163. }
  164. LM_INFO("%s: module initialization OK\n", APP_NAME);
  165. if (jvm != NULL)
  166. (*jvm)->DetachCurrentThread(jvm);
  167. return 0;
  168. }
  169. static int child_init(int rank)
  170. {
  171. int retval;
  172. jmethodID child_init_id;
  173. // attach to current thread
  174. (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
  175. if ((*env)->ExceptionCheck(env))
  176. {
  177. handle_exception();
  178. return -1;
  179. }
  180. child_init_id = (*env)->GetMethodID(env, KamailioClass, "child_init", "(I)I");
  181. if ((*env)->ExceptionCheck(env))
  182. {
  183. handle_exception();
  184. (*jvm)->DetachCurrentThread(jvm);
  185. return -1;
  186. }
  187. retval = (int)(*env)->CallIntMethod(env, KamailioClassInstanceRef, child_init_id, rank);
  188. if ((*env)->ExceptionCheck(env))
  189. {
  190. handle_exception();
  191. (*jvm)->DetachCurrentThread(jvm);
  192. return -1;
  193. }
  194. (*env)->DeleteLocalRef(env, child_init_id);
  195. (*jvm)->DetachCurrentThread(jvm);
  196. msg = NULL;
  197. return retval;
  198. }
  199. static void mod_destroy(void)
  200. {
  201. if (env != NULL)
  202. {
  203. (*env)->DeleteGlobalRef(env, KamailioClassInstanceRef);
  204. (*env)->DeleteGlobalRef(env, KamailioClassRef);
  205. }
  206. if (jvm != NULL)
  207. {
  208. (*jvm)->DetachCurrentThread(jvm);
  209. (*jvm)->DestroyJavaVM(jvm);
  210. }
  211. if (msg)
  212. {
  213. pkg_free(msg);
  214. }
  215. }