You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

528 lines
15 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.0 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license.html. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sam Ruby (rubys@us.ibm.com) |
  16. +----------------------------------------------------------------------+
  17. */
  18. /*
  19. * This module implements Zend OO syntax overloading support for Java
  20. * components using JNI and reflection.
  21. */
  22. #include "dl/phpdl.h"
  23. #include "php.h"
  24. #include "zend_compile.h"
  25. #include "php_ini.h"
  26. #include "php_globals.h"
  27. #include <jni.h>
  28. #include <stdio.h>
  29. #define IS_EXCEPTION 86
  30. /***************************************************************************/
  31. #ifndef KAFFE
  32. #ifndef JNI_11
  33. #ifndef JNI_12
  34. #ifdef JNI_VERSION_1_2
  35. #define JNI_12
  36. #else
  37. #define JNI_11
  38. #endif
  39. #endif
  40. #endif
  41. #endif
  42. #if WIN32|WINNT
  43. #ifdef JNI_12
  44. #pragma comment(lib,"jvm.lib")
  45. #else
  46. #pragma comment(lib,"javai.lib")
  47. #endif
  48. #else
  49. static void *javadl = 0;
  50. #endif
  51. /***************************************************************************/
  52. static int le_jobject = 0;
  53. static char *javalib = 0;
  54. static char *classpath = 0;
  55. static char *libpath = 0;
  56. static char *javahome = 0;
  57. static int iniUpdated = 0;
  58. static JavaVM *jvm = 0;
  59. static JNIEnv *jenv = 0;
  60. static jclass php_reflect;
  61. static zend_class_entry java_class_entry;
  62. static PHP_INI_MH(OnIniUpdate) {
  63. if (new_value) *(char**)mh_arg1 = new_value;
  64. iniUpdated=1;
  65. return SUCCESS;
  66. }
  67. PHP_INI_BEGIN()
  68. PHP_INI_ENTRY1("java.class.path",
  69. NULL, PHP_INI_ALL, OnIniUpdate, &classpath)
  70. PHP_INI_ENTRY1("java.home",
  71. NULL, PHP_INI_ALL, OnIniUpdate, &javahome)
  72. PHP_INI_ENTRY1("java.library",
  73. NULL, PHP_INI_ALL, OnIniUpdate, &javalib)
  74. PHP_INI_ENTRY1("java.library.path",
  75. NULL, PHP_INI_ALL, OnIniUpdate, &libpath)
  76. PHP_INI_END()
  77. /***************************************************************************/
  78. /*
  79. * Destroy a Java Virtual Machine.
  80. */
  81. void jvm_destroy() {
  82. if (php_reflect) (*jenv)->DeleteGlobalRef(jenv, php_reflect);
  83. if (jvm) (*jvm)->DestroyJavaVM(jvm);
  84. #if !(WIN32||WINNT)
  85. if (javadl) dlclose(javadl);
  86. #endif
  87. php_reflect = 0;
  88. jvm = 0;
  89. jenv = 0;
  90. }
  91. /*
  92. * Create a Java Virtual Machine.
  93. * - class.path, home, and library.path are read out of the INI file
  94. * - appropriate (pre 1.1, JDK 1.1, and JDK 1.2) initialization is performed
  95. * - net.php.reflect class file is located
  96. */
  97. #ifdef JNI_12
  98. static void addJVMOption(JavaVMInitArgs *vm_args, char *name, char *value) {
  99. char *option = (char*) malloc(strlen(name) + strlen(value) + 1);
  100. strcpy(option, name);
  101. strcat(option, value);
  102. vm_args->options[vm_args->nOptions++].optionString = option;
  103. }
  104. #endif
  105. static int jvm_create() {
  106. int rc;
  107. jclass local_php_reflect;
  108. jthrowable error;
  109. #ifdef JNI_11
  110. JDK1_1InitArgs vm_args;
  111. #else
  112. JavaVMInitArgs vm_args;
  113. #ifdef JNI_12
  114. JavaVMOption options[3];
  115. #endif
  116. #endif
  117. iniUpdated=0;
  118. if (!classpath) classpath = getenv("CLASSPATH");
  119. #if !(WIN32||WINNT)
  120. if (!libpath) libpath = getenv("LD_LIBRARY_PATH");
  121. if (javalib) {
  122. javadl = dlopen(javalib, RTLD_GLOBAL | RTLD_LAZY);
  123. if (!javadl) {
  124. php_error(E_ERROR, "Unable to create Java Virtual Machine");
  125. return -1;
  126. }
  127. }
  128. #endif
  129. #ifdef JNI_12
  130. vm_args.version = JNI_VERSION_1_2;
  131. vm_args.ignoreUnrecognized = JNI_FALSE;
  132. vm_args.options = options;
  133. vm_args.nOptions = 0;
  134. if (classpath) addJVMOption(&vm_args, "-Djava.class.path=", classpath);
  135. if (javahome) addJVMOption(&vm_args, "-Djava.home=", javahome);
  136. if (libpath) addJVMOption(&vm_args, "-Djava.library.path=", libpath);
  137. rc = JNI_CreateJavaVM(&jvm, (void**)&jenv, &vm_args);
  138. #else
  139. vm_args.version=0x00010001;
  140. JNI_GetDefaultJavaVMInitArgs(&vm_args);
  141. if (!classpath) classpath = "";
  142. vm_args.classpath = classpath;
  143. #ifdef KAFFE
  144. vm_args.classhome = javahome;
  145. vm_args.libraryhome = libpath;
  146. #endif
  147. rc = JNI_CreateJavaVM(&jvm, &jenv, &vm_args);
  148. #endif
  149. if (rc) {
  150. php_error(E_ERROR, "Unable to create Java Virtual Machine");
  151. return rc;
  152. }
  153. local_php_reflect = (*jenv)->FindClass(jenv, "net/php/reflect");
  154. error = (*jenv)->ExceptionOccurred(jenv);
  155. if (error) {
  156. jclass errClass = (*jenv)->GetObjectClass(jenv, error);
  157. jmethodID toString = (*jenv)->GetMethodID(jenv, errClass, "toString",
  158. "()Ljava/lang/String;");
  159. jobject errString = (*jenv)->CallObjectMethod(jenv, error, toString);
  160. const char *errAsUTF = (*jenv)->GetStringUTFChars(jenv, errString, 0);
  161. php_error(E_ERROR, "%s", errAsUTF);
  162. (*jenv)->ReleaseStringUTFChars(jenv, error, errAsUTF);
  163. (*jenv)->ExceptionClear(jenv);
  164. jvm_destroy();
  165. return -1;
  166. }
  167. php_reflect = (*jenv)->NewGlobalRef(jenv, local_php_reflect);
  168. return rc;
  169. }
  170. /***************************************************************************/
  171. static jobjectArray _java_makeArray(int argc, pval** argv) {
  172. jclass objectClass = (*jenv)->FindClass(jenv, "java/lang/Object");
  173. jobjectArray result = (*jenv)->NewObjectArray(jenv, argc, objectClass, 0);
  174. jobject arg;
  175. jmethodID makeArg;
  176. int i;
  177. pval **handle;
  178. int type;
  179. for (i=0; i<argc; i++) {
  180. switch (argv[i]->type) {
  181. case IS_STRING:
  182. arg=(*jenv)->NewStringUTF(jenv,argv[i]->value.str.val);
  183. break;
  184. case IS_OBJECT:
  185. zend_hash_index_find(argv[i]->value.obj.properties, 0, (void*)&handle);
  186. arg = zend_list_find((*handle)->value.lval, &type);
  187. break;
  188. case IS_BOOL:
  189. makeArg = (*jenv)->GetStaticMethodID(jenv, php_reflect, "MakeArg",
  190. "(Z)Ljava/lang/Object;");
  191. arg = (*jenv)->CallStaticObjectMethod(jenv, php_reflect, makeArg,
  192. (jboolean)(argv[i]->value.lval));
  193. break;
  194. case IS_LONG:
  195. makeArg = (*jenv)->GetStaticMethodID(jenv, php_reflect, "MakeArg",
  196. "(J)Ljava/lang/Object;");
  197. arg = (*jenv)->CallStaticObjectMethod(jenv, php_reflect, makeArg,
  198. (jlong)(argv[i]->value.lval));
  199. break;
  200. case IS_DOUBLE:
  201. makeArg = (*jenv)->GetStaticMethodID(jenv, php_reflect, "MakeArg",
  202. "(D)Ljava/lang/Object;");
  203. arg = (*jenv)->CallStaticObjectMethod(jenv, php_reflect, makeArg,
  204. (jdouble)(argv[i]->value.dval));
  205. break;
  206. default:
  207. arg=0;
  208. }
  209. (*jenv)->SetObjectArrayElement(jenv, result, i, arg);
  210. if (argv[i]->type != IS_OBJECT)
  211. (*jenv)->DeleteLocalRef(jenv, arg);
  212. }
  213. return result;
  214. }
  215. static int checkError(pval *value) {
  216. if (value->type == IS_EXCEPTION) {
  217. php_error(E_WARNING, "%s", value->value.str.val);
  218. efree(value->value.str.val);
  219. var_reset(value);
  220. return 1;
  221. };
  222. return 0;
  223. }
  224. /***************************************************************************/
  225. /*
  226. * Invoke a method on an object. If method name is "java", create a new
  227. * object instead.
  228. */
  229. void java_call_function_handler
  230. (INTERNAL_FUNCTION_PARAMETERS, zend_property_reference *property_reference)
  231. {
  232. pval *object = property_reference->object;
  233. zend_overloaded_element *function_name = (zend_overloaded_element *)
  234. property_reference->elements_list.tail->data;
  235. int arg_count = ARG_COUNT(ht);
  236. jlong result = 0;
  237. pval **arguments = (pval **) emalloc(sizeof(pval *)*arg_count);
  238. getParametersArray(ht, arg_count, arguments);
  239. if (iniUpdated && jenv) jvm_destroy();
  240. if (!jenv) jvm_create();
  241. if (!jenv) return;
  242. if (!strcmp("java",function_name->element.value.str.val)) {
  243. /* construct a Java object:
  244. First argument is the class name. Any additional arguments will
  245. be treated as constructor parameters. */
  246. jmethodID co = (*jenv)->GetStaticMethodID(jenv, php_reflect, "CreateObject",
  247. "(Ljava/lang/String;[Ljava/lang/Object;J)V");
  248. jstring className=(*jenv)->NewStringUTF(jenv, arguments[0]->value.str.val);
  249. (pval*)(long)result = object;
  250. (*jenv)->CallStaticVoidMethod(jenv, php_reflect, co,
  251. className, _java_makeArray(arg_count-1, arguments+1), result);
  252. (*jenv)->DeleteLocalRef(jenv, className);
  253. } else {
  254. pval **handle;
  255. int type;
  256. jobject obj;
  257. jstring method;
  258. /* invoke a method on the given object */
  259. jmethodID invoke = (*jenv)->GetStaticMethodID(jenv, php_reflect, "Invoke",
  260. "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;J)V");
  261. zend_hash_index_find(object->value.obj.properties, 0, (void**) &handle);
  262. obj = zend_list_find((*handle)->value.lval, &type);
  263. method = (*jenv)->NewStringUTF(jenv, function_name->element.value.str.val);
  264. (pval*)(long)result = return_value;
  265. (*jenv)->CallStaticVoidMethod(jenv, php_reflect, invoke,
  266. obj, method, _java_makeArray(arg_count, arguments), result);
  267. (*jenv)->DeleteLocalRef(jenv, method);
  268. }
  269. efree(arguments);
  270. pval_destructor(&function_name->element);
  271. checkError((pval*)(long)result);
  272. }
  273. /***************************************************************************/
  274. static pval _java_getset_property
  275. (zend_property_reference *property_reference, jobjectArray value)
  276. {
  277. pval presult;
  278. jlong result = 0;
  279. pval **pobject;
  280. jobject obj;
  281. int type;
  282. /* get the property name */
  283. zend_llist_element *element = property_reference->elements_list.head;
  284. zend_overloaded_element *property=(zend_overloaded_element *)element->data;
  285. jstring propName =
  286. (*jenv)->NewStringUTF(jenv, property->element.value.str.val);
  287. /* get the object */
  288. zend_hash_index_find(property_reference->object->value.obj.properties,
  289. 0, (void **) &pobject);
  290. obj = zend_list_find((*pobject)->value.lval,&type);
  291. (pval*)(long)result = &presult;
  292. var_uninit(&presult);
  293. if (!obj || (type!=le_jobject)) {
  294. php_error(E_ERROR,
  295. "Attempt to access a Java property on a non-Java object");
  296. } else {
  297. /* invoke the method */
  298. jmethodID gsp = (*jenv)->GetStaticMethodID(jenv, php_reflect, "GetSetProp",
  299. "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;J)V");
  300. (*jenv)->CallStaticVoidMethod
  301. (jenv, php_reflect, gsp, obj, propName, value, result);
  302. }
  303. (*jenv)->DeleteLocalRef(jenv, propName);
  304. pval_destructor(&property->element);
  305. return presult;
  306. }
  307. pval java_get_property_handler
  308. (zend_property_reference *property_reference)
  309. {
  310. pval presult = _java_getset_property(property_reference, 0);
  311. checkError(&presult);
  312. return presult;
  313. }
  314. int java_set_property_handler
  315. (zend_property_reference *property_reference, pval *value)
  316. {
  317. pval presult = _java_getset_property
  318. (property_reference, _java_makeArray(1, &value));
  319. return checkError(&presult) ? FAILURE : SUCCESS;
  320. }
  321. /***************************************************************************/
  322. static void _php_java_destructor(void *jobject) {
  323. if (jenv) (*jenv)->DeleteGlobalRef(jenv, jobject);
  324. }
  325. PHP_MINIT_FUNCTION(java) {
  326. INIT_OVERLOADED_CLASS_ENTRY(java_class_entry, "java", NULL,
  327. java_call_function_handler,
  328. java_get_property_handler,
  329. java_set_property_handler);
  330. register_internal_class(&java_class_entry);
  331. le_jobject = register_list_destructors(_php_java_destructor,NULL);
  332. REGISTER_INI_ENTRIES();
  333. return SUCCESS;
  334. }
  335. PHP_MSHUTDOWN_FUNCTION(java) {
  336. UNREGISTER_INI_ENTRIES();
  337. if (jvm) jvm_destroy();
  338. return SUCCESS;
  339. }
  340. function_entry java_functions[] = {
  341. {NULL, NULL, NULL}
  342. };
  343. static PHP_MINFO_FUNCTION(java) {
  344. DISPLAY_INI_ENTRIES();
  345. }
  346. zend_module_entry java_module_entry = {
  347. "java",
  348. java_functions,
  349. PHP_MINIT(java),
  350. PHP_MSHUTDOWN(java),
  351. NULL,
  352. NULL,
  353. PHP_MINFO(java),
  354. STANDARD_MODULE_PROPERTIES
  355. };
  356. DLEXPORT zend_module_entry *get_module(void) { return &java_module_entry; }
  357. /***************************************************************************/
  358. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromString
  359. (JNIEnv *jenv, jclass self, jlong result, jstring value)
  360. {
  361. const char *valueAsUTF = (*jenv)->GetStringUTFChars(jenv, value, 0);
  362. pval *presult = (pval*)(long)result;
  363. presult->type=IS_STRING;
  364. presult->value.str.len=strlen(valueAsUTF);
  365. presult->value.str.val=emalloc(presult->value.str.len+1);
  366. strcpy(presult->value.str.val, valueAsUTF);
  367. (*jenv)->ReleaseStringUTFChars(jenv, value, valueAsUTF);
  368. }
  369. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromLong
  370. (JNIEnv *jenv, jclass self, jlong result, jlong value)
  371. {
  372. pval *presult = (pval*)(long)result;
  373. presult->type=IS_LONG;
  374. presult->value.lval=(long)value;
  375. }
  376. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromDouble
  377. (JNIEnv *jenv, jclass self, jlong result, jdouble value)
  378. {
  379. pval *presult = (pval*)(long)result;
  380. presult->type=IS_DOUBLE;
  381. presult->value.dval=value;
  382. }
  383. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromBoolean
  384. (JNIEnv *jenv, jclass self, jlong result, jboolean value)
  385. {
  386. pval *presult = (pval*)(long)result;
  387. presult->type=IS_BOOL;
  388. presult->value.lval=value;
  389. }
  390. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromObject
  391. (JNIEnv *jenv, jclass self, jlong result, jobject value)
  392. {
  393. /* wrapper the java object in a pval object */
  394. pval *presult = (pval*)(long)result;
  395. pval *handle;
  396. if (presult->type != IS_OBJECT) {
  397. presult->type=IS_OBJECT;
  398. presult->value.obj.ce=&java_class_entry;
  399. presult->value.obj.properties = (HashTable *) emalloc(sizeof(HashTable));
  400. presult->is_ref=1;
  401. presult->refcount=1;
  402. zend_hash_init(presult->value.obj.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  403. };
  404. ALLOC_ZVAL(handle);
  405. handle->type = IS_LONG;
  406. handle->value.lval =
  407. zend_list_insert((*jenv)->NewGlobalRef(jenv,value), le_jobject);
  408. pval_copy_constructor(handle);
  409. INIT_PZVAL(handle);
  410. zend_hash_index_update(presult->value.obj.properties, 0,
  411. &handle, sizeof(pval *), NULL);
  412. }
  413. JNIEXPORT void JNICALL Java_net_php_reflect_setException
  414. (JNIEnv *jenv, jclass self, jlong result, jstring value)
  415. {
  416. pval *presult = (pval*)(long)result;
  417. Java_net_php_reflect_setResultFromString(jenv, self, result, value);
  418. presult->type=IS_EXCEPTION;
  419. }
  420. JNIEXPORT void JNICALL Java_net_php_reflect_setEnv
  421. (JNIEnv *newJenv, jclass self)
  422. {
  423. iniUpdated=0;
  424. jenv=newJenv;
  425. if (!self) self = (*jenv)->FindClass(jenv, "net/php/reflect");
  426. php_reflect = (*jenv)->NewGlobalRef(jenv, self);
  427. }