toolwrapper.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* toolwrapper.c -- a native tool wrapper for VMs that support the JNI
  2. invocation interface
  3. Copyright (C) 2006 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. #include <jni.h>
  33. #include <ltdl.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include "config.h"
  37. #ifndef JNI_VERSION_1_2
  38. # error JNI version 1.2 or greater required
  39. #endif
  40. #ifndef MAINCLASS
  41. #define MAINCLASS "Main"
  42. #endif
  43. union env_union
  44. {
  45. void *void_env;
  46. JNIEnv *jni_env;
  47. };
  48. /* Typedef for JNI_CreateJavaVM dlopen call. */
  49. typedef jint createVM (JavaVM **, void **, void *);
  50. int
  51. main (int argc, const char** argv)
  52. {
  53. union env_union tmp;
  54. JNIEnv* env;
  55. JavaVM* jvm;
  56. JavaVMInitArgs vm_args;
  57. jint result;
  58. jclass class_id;
  59. jmethodID method_id;
  60. jstring str;
  61. jclass string_class_id;
  62. jobjectArray args_array;
  63. char** non_vm_argv;
  64. int non_vm_argc;
  65. int i;
  66. int classpath_found = 0;
  67. /* Variables for JNI_CreateJavaVM dlopen call. */
  68. lt_dlhandle libjvm_handle = NULL;
  69. createVM* libjvm_create = NULL;
  70. int libjvm_error = 0;
  71. env = NULL;
  72. jvm = NULL;
  73. vm_args.nOptions = 0;
  74. vm_args.options = NULL;
  75. non_vm_argc = 0;
  76. non_vm_argv = NULL;
  77. if (argc > 1)
  78. {
  79. for (i = 1; i < argc; i++)
  80. {
  81. if (!strncmp (argv[i], "-J", 2))
  82. {
  83. if (!strncmp (argv[i], "-J-Djava.class.path=", 20))
  84. classpath_found = 1;
  85. /* A virtual machine option. */
  86. vm_args.options = (JavaVMOption*) realloc (vm_args.options, (vm_args.nOptions + 1) * sizeof (JavaVMOption));
  87. if (vm_args.options == NULL)
  88. {
  89. fprintf (stderr, TOOLNAME ": realloc failed.\n");
  90. goto destroy;
  91. }
  92. if (strlen (argv[i]) == 2)
  93. {
  94. fprintf (stderr, TOOLNAME ": the -J option must not be followed by a space.\n");
  95. goto destroy;
  96. }
  97. else
  98. vm_args.options[vm_args.nOptions++].optionString = strdup (argv[i] + 2);
  99. }
  100. else
  101. {
  102. non_vm_argv = (char**) realloc (non_vm_argv, (non_vm_argc + 1) * sizeof (char*));
  103. if (non_vm_argv == NULL)
  104. {
  105. fprintf (stderr, TOOLNAME ": realloc failed.\n");
  106. goto destroy;
  107. }
  108. non_vm_argv[non_vm_argc++] = strdup (argv[i]);
  109. }
  110. }
  111. }
  112. if (!classpath_found)
  113. {
  114. /* Set the invocation classpath. */
  115. vm_args.options = (JavaVMOption*) realloc (vm_args.options, (vm_args.nOptions + 1) * sizeof (JavaVMOption));
  116. if (vm_args.options == NULL)
  117. {
  118. fprintf (stderr, TOOLNAME ": realloc failed.\n");
  119. goto destroy;
  120. }
  121. vm_args.options[vm_args.nOptions++].optionString = "-Xbootclasspath/p:" TOOLS_ZIP;
  122. }
  123. /* Terminate vm_args.options with a NULL element. */
  124. vm_args.options = (JavaVMOption*) realloc (vm_args.options, (vm_args.nOptions + 1) * sizeof (JavaVMOption));
  125. if (vm_args.options == NULL)
  126. {
  127. fprintf (stderr, TOOLNAME ": realloc failed.\n");
  128. goto destroy;
  129. }
  130. vm_args.options[vm_args.nOptions].optionString = NULL;
  131. /* Terminate non_vm_argv with a NULL element. */
  132. non_vm_argv = (char**) realloc (non_vm_argv, (non_vm_argc + 1) * sizeof (char*));
  133. if (non_vm_argv == NULL)
  134. {
  135. fprintf (stderr, TOOLNAME ": realloc failed.\n");
  136. goto destroy;
  137. }
  138. non_vm_argv[non_vm_argc] = NULL;
  139. vm_args.version = JNI_VERSION_1_2;
  140. vm_args.ignoreUnrecognized = JNI_TRUE;
  141. /* dlopen libjvm.so */
  142. libjvm_error = lt_dlinit ();
  143. if (libjvm_error)
  144. {
  145. fprintf (stderr, TOOLNAME ": lt_dlinit failed.\n");
  146. goto destroy;
  147. }
  148. libjvm_handle = lt_dlopenext (LIBJVM);
  149. if (!libjvm_handle)
  150. {
  151. fprintf (stderr, TOOLNAME ": failed to open " LIBJVM "\n");
  152. goto destroy;
  153. }
  154. libjvm_create = (createVM*) lt_dlsym (libjvm_handle, "JNI_CreateJavaVM");
  155. if (!libjvm_create)
  156. {
  157. fprintf (stderr, TOOLNAME ": failed to load JNI_CreateJavaVM symbol from " LIBJVM "\n");
  158. goto destroy;
  159. }
  160. result = (*libjvm_create) (&jvm, &tmp.void_env, &vm_args);
  161. if (result < 0)
  162. {
  163. fprintf (stderr, TOOLNAME ": couldn't create virtual machine\n");
  164. goto destroy;
  165. }
  166. env = tmp.jni_env;
  167. string_class_id = (*env)->FindClass (env, "java/lang/String");
  168. if (string_class_id == NULL)
  169. {
  170. fprintf (stderr, TOOLNAME ": FindClass failed.\n");
  171. goto destroy;
  172. }
  173. args_array = (*env)->NewObjectArray (env, non_vm_argc, string_class_id, NULL);
  174. if (args_array == NULL)
  175. {
  176. fprintf (stderr, TOOLNAME ": NewObjectArray failed.\n");
  177. goto destroy;
  178. }
  179. for (i = 0; i < non_vm_argc; i++)
  180. {
  181. str = (*env)->NewStringUTF (env, non_vm_argv[i]);
  182. if (str == NULL)
  183. {
  184. fprintf (stderr, TOOLNAME ": NewStringUTF failed.\n");
  185. goto destroy;
  186. }
  187. (*env)->SetObjectArrayElement (env, args_array, i, str);
  188. }
  189. class_id
  190. = (*env)->FindClass (env,
  191. "gnu/classpath/tools/" TOOLPACKAGE "/" MAINCLASS);
  192. if (class_id == NULL)
  193. {
  194. fprintf (stderr, TOOLNAME ": FindClass failed.\n");
  195. goto destroy;
  196. }
  197. method_id = (*env)->GetStaticMethodID (env, class_id, "main", "([Ljava/lang/String;)V");
  198. if (method_id == NULL)
  199. {
  200. fprintf (stderr, TOOLNAME ": GetStaticMethodID failed.\n");
  201. goto destroy;
  202. }
  203. (*env)->CallStaticVoidMethod (env, class_id, method_id, args_array);
  204. destroy:
  205. if (env != NULL)
  206. {
  207. if ((*env)->ExceptionOccurred (env))
  208. (*env)->ExceptionDescribe (env);
  209. if (jvm != NULL)
  210. (*jvm)->DestroyJavaVM (jvm);
  211. }
  212. /* libltdl cleanup */
  213. if (libjvm_handle)
  214. {
  215. if (lt_dlclose (libjvm_handle) != 0)
  216. fprintf (stderr, TOOLNAME ": failed to close " LIBJVM "\n");
  217. }
  218. if (lt_dlexit () != 0)
  219. fprintf (stderr, TOOLNAME ": lt_dlexit failed.\n");
  220. return 1;
  221. }