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.

537 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.01 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. #ifdef PHP_WIN32
  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 *classpath = 0;
  54. static char *libpath = 0;
  55. static char *javahome = 0;
  56. static int iniUpdated = 0;
  57. static JavaVM *jvm = 0;
  58. static JNIEnv *jenv = 0;
  59. static jclass php_reflect;
  60. static zend_class_entry java_class_entry;
  61. static PHP_INI_MH(OnIniUpdate) {
  62. if (new_value) *(char**)mh_arg1 = new_value;
  63. iniUpdated=1;
  64. return SUCCESS;
  65. }
  66. PHP_INI_BEGIN()
  67. PHP_INI_ENTRY1("java.class.path",
  68. NULL, PHP_INI_ALL, OnIniUpdate, &classpath)
  69. PHP_INI_ENTRY1("java.home",
  70. NULL, PHP_INI_ALL, OnIniUpdate, &javahome)
  71. PHP_INI_ENTRY1("java.library.path",
  72. NULL, PHP_INI_ALL, OnIniUpdate, &libpath)
  73. PHP_INI_END()
  74. /***************************************************************************/
  75. /*
  76. * Destroy a Java Virtual Machine.
  77. */
  78. void jvm_destroy() {
  79. if (php_reflect) (*jenv)->DeleteGlobalRef(jenv, php_reflect);
  80. if (jvm) {
  81. (*jvm)->DetachCurrentThread(jvm);
  82. (*jvm)->DestroyJavaVM(jvm);
  83. jvm = 0;
  84. }
  85. #ifndef PHP_WIN32
  86. if (javadl) dlclose(javadl);
  87. #endif
  88. php_reflect = 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. #ifndef PHP_WIN32
  120. if (!libpath) libpath = getenv("LD_LIBRARY_PATH");
  121. #endif
  122. #ifdef JNI_12
  123. vm_args.version = JNI_VERSION_1_2;
  124. vm_args.ignoreUnrecognized = JNI_FALSE;
  125. vm_args.options = options;
  126. vm_args.nOptions = 0;
  127. if (classpath) addJVMOption(&vm_args, "-Djava.class.path=", classpath);
  128. if (javahome) addJVMOption(&vm_args, "-Djava.home=", javahome);
  129. if (libpath) addJVMOption(&vm_args, "-Djava.library.path=", libpath);
  130. rc = JNI_CreateJavaVM(&jvm, (void**)&jenv, &vm_args);
  131. #else
  132. vm_args.version=0x00010001;
  133. JNI_GetDefaultJavaVMInitArgs(&vm_args);
  134. if (!classpath) classpath = "";
  135. vm_args.classpath = classpath;
  136. #ifdef KAFFE
  137. vm_args.classhome = javahome;
  138. vm_args.libraryhome = libpath;
  139. #endif
  140. rc = JNI_CreateJavaVM(&jvm, &jenv, &vm_args);
  141. #endif
  142. if (rc) {
  143. php_error(E_ERROR, "Unable to create Java Virtual Machine");
  144. return rc;
  145. }
  146. local_php_reflect = (*jenv)->FindClass(jenv, "net/php/reflect");
  147. error = (*jenv)->ExceptionOccurred(jenv);
  148. if (error) {
  149. jclass errClass = (*jenv)->GetObjectClass(jenv, error);
  150. jmethodID toString = (*jenv)->GetMethodID(jenv, errClass, "toString",
  151. "()Ljava/lang/String;");
  152. jobject errString = (*jenv)->CallObjectMethod(jenv, error, toString);
  153. const char *errAsUTF = (*jenv)->GetStringUTFChars(jenv, errString, 0);
  154. php_error(E_ERROR, "%s", errAsUTF);
  155. (*jenv)->ReleaseStringUTFChars(jenv, error, errAsUTF);
  156. (*jenv)->ExceptionClear(jenv);
  157. jvm_destroy();
  158. return -1;
  159. }
  160. php_reflect = (*jenv)->NewGlobalRef(jenv, local_php_reflect);
  161. return rc;
  162. }
  163. /***************************************************************************/
  164. static jobjectArray _java_makeArray(int argc, pval** argv) {
  165. jclass objectClass = (*jenv)->FindClass(jenv, "java/lang/Object");
  166. jobjectArray result = (*jenv)->NewObjectArray(jenv, argc, objectClass, 0);
  167. jobject arg;
  168. jmethodID makeArg;
  169. int i;
  170. pval **handle;
  171. int type;
  172. for (i=0; i<argc; i++) {
  173. switch (argv[i]->type) {
  174. case IS_STRING:
  175. arg=(*jenv)->NewStringUTF(jenv,argv[i]->value.str.val);
  176. break;
  177. case IS_OBJECT:
  178. zend_hash_index_find(argv[i]->value.obj.properties, 0, (void*)&handle);
  179. arg = zend_list_find((*handle)->value.lval, &type);
  180. break;
  181. case IS_BOOL:
  182. makeArg = (*jenv)->GetStaticMethodID(jenv, php_reflect, "MakeArg",
  183. "(Z)Ljava/lang/Object;");
  184. arg = (*jenv)->CallStaticObjectMethod(jenv, php_reflect, makeArg,
  185. (jboolean)(argv[i]->value.lval));
  186. break;
  187. case IS_LONG:
  188. makeArg = (*jenv)->GetStaticMethodID(jenv, php_reflect, "MakeArg",
  189. "(J)Ljava/lang/Object;");
  190. arg = (*jenv)->CallStaticObjectMethod(jenv, php_reflect, makeArg,
  191. (jlong)(argv[i]->value.lval));
  192. break;
  193. case IS_DOUBLE:
  194. makeArg = (*jenv)->GetStaticMethodID(jenv, php_reflect, "MakeArg",
  195. "(D)Ljava/lang/Object;");
  196. arg = (*jenv)->CallStaticObjectMethod(jenv, php_reflect, makeArg,
  197. (jdouble)(argv[i]->value.dval));
  198. break;
  199. default:
  200. arg=0;
  201. }
  202. (*jenv)->SetObjectArrayElement(jenv, result, i, arg);
  203. if (argv[i]->type != IS_OBJECT)
  204. (*jenv)->DeleteLocalRef(jenv, arg);
  205. }
  206. return result;
  207. }
  208. static int checkError(pval *value) {
  209. if (value->type == IS_EXCEPTION) {
  210. php_error(E_WARNING, "%s", value->value.str.val);
  211. efree(value->value.str.val);
  212. var_reset(value);
  213. return 1;
  214. };
  215. return 0;
  216. }
  217. /***************************************************************************/
  218. /*
  219. * Invoke a method on an object. If method name is "java", create a new
  220. * object instead.
  221. */
  222. void java_call_function_handler
  223. (INTERNAL_FUNCTION_PARAMETERS, zend_property_reference *property_reference)
  224. {
  225. pval *object = property_reference->object;
  226. zend_overloaded_element *function_name = (zend_overloaded_element *)
  227. property_reference->elements_list.tail->data;
  228. int arg_count = ARG_COUNT(ht);
  229. jlong result = 0;
  230. pval **arguments = (pval **) emalloc(sizeof(pval *)*arg_count);
  231. getParametersArray(ht, arg_count, arguments);
  232. if (iniUpdated && jenv) jvm_destroy();
  233. if (!jenv) jvm_create();
  234. if (!jenv) return;
  235. if (!strcmp("java",function_name->element.value.str.val)) {
  236. /* construct a Java object:
  237. First argument is the class name. Any additional arguments will
  238. be treated as constructor parameters. */
  239. jmethodID co = (*jenv)->GetStaticMethodID(jenv, php_reflect, "CreateObject",
  240. "(Ljava/lang/String;[Ljava/lang/Object;J)V");
  241. jstring className=(*jenv)->NewStringUTF(jenv, arguments[0]->value.str.val);
  242. (pval*)(long)result = object;
  243. (*jenv)->CallStaticVoidMethod(jenv, php_reflect, co,
  244. className, _java_makeArray(arg_count-1, arguments+1), result);
  245. (*jenv)->DeleteLocalRef(jenv, className);
  246. } else {
  247. pval **handle;
  248. int type;
  249. jobject obj;
  250. jstring method;
  251. /* invoke a method on the given object */
  252. jmethodID invoke = (*jenv)->GetStaticMethodID(jenv, php_reflect, "Invoke",
  253. "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;J)V");
  254. zend_hash_index_find(object->value.obj.properties, 0, (void**) &handle);
  255. obj = zend_list_find((*handle)->value.lval, &type);
  256. method = (*jenv)->NewStringUTF(jenv, function_name->element.value.str.val);
  257. (pval*)(long)result = return_value;
  258. (*jenv)->CallStaticVoidMethod(jenv, php_reflect, invoke,
  259. obj, method, _java_makeArray(arg_count, arguments), result);
  260. (*jenv)->DeleteLocalRef(jenv, method);
  261. }
  262. efree(arguments);
  263. pval_destructor(&function_name->element);
  264. checkError((pval*)(long)result);
  265. }
  266. /***************************************************************************/
  267. static pval _java_getset_property
  268. (zend_property_reference *property_reference, jobjectArray value)
  269. {
  270. pval presult;
  271. jlong result = 0;
  272. pval **pobject;
  273. jobject obj;
  274. int type;
  275. /* get the property name */
  276. zend_llist_element *element = property_reference->elements_list.head;
  277. zend_overloaded_element *property=(zend_overloaded_element *)element->data;
  278. jstring propName =
  279. (*jenv)->NewStringUTF(jenv, property->element.value.str.val);
  280. /* get the object */
  281. zend_hash_index_find(property_reference->object->value.obj.properties,
  282. 0, (void **) &pobject);
  283. obj = zend_list_find((*pobject)->value.lval,&type);
  284. (pval*)(long)result = &presult;
  285. var_uninit(&presult);
  286. if (!obj || (type!=le_jobject)) {
  287. php_error(E_ERROR,
  288. "Attempt to access a Java property on a non-Java object");
  289. } else {
  290. /* invoke the method */
  291. jmethodID gsp = (*jenv)->GetStaticMethodID(jenv, php_reflect, "GetSetProp",
  292. "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;J)V");
  293. (*jenv)->CallStaticVoidMethod
  294. (jenv, php_reflect, gsp, obj, propName, value, result);
  295. }
  296. (*jenv)->DeleteLocalRef(jenv, propName);
  297. pval_destructor(&property->element);
  298. return presult;
  299. }
  300. pval java_get_property_handler
  301. (zend_property_reference *property_reference)
  302. {
  303. pval presult = _java_getset_property(property_reference, 0);
  304. checkError(&presult);
  305. return presult;
  306. }
  307. int java_set_property_handler
  308. (zend_property_reference *property_reference, pval *value)
  309. {
  310. pval presult = _java_getset_property
  311. (property_reference, _java_makeArray(1, &value));
  312. return checkError(&presult) ? FAILURE : SUCCESS;
  313. }
  314. /***************************************************************************/
  315. static void _php_java_destructor(void *jobject) {
  316. if (jenv) (*jenv)->DeleteGlobalRef(jenv, jobject);
  317. }
  318. PHP_MINIT_FUNCTION(java) {
  319. INIT_OVERLOADED_CLASS_ENTRY(java_class_entry, "java", NULL,
  320. java_call_function_handler,
  321. java_get_property_handler,
  322. java_set_property_handler);
  323. register_internal_class(&java_class_entry);
  324. le_jobject = register_list_destructors(_php_java_destructor,NULL);
  325. REGISTER_INI_ENTRIES();
  326. return SUCCESS;
  327. }
  328. PHP_MSHUTDOWN_FUNCTION(java) {
  329. UNREGISTER_INI_ENTRIES();
  330. if (jvm) jvm_destroy();
  331. return SUCCESS;
  332. }
  333. function_entry java_functions[] = {
  334. {NULL, NULL, NULL}
  335. };
  336. static PHP_MINFO_FUNCTION(java) {
  337. DISPLAY_INI_ENTRIES();
  338. }
  339. zend_module_entry java_module_entry = {
  340. "java",
  341. java_functions,
  342. PHP_MINIT(java),
  343. PHP_MSHUTDOWN(java),
  344. NULL,
  345. NULL,
  346. PHP_MINFO(java),
  347. STANDARD_MODULE_PROPERTIES
  348. };
  349. DLEXPORT zend_module_entry *get_module(void) { return &java_module_entry; }
  350. /***************************************************************************/
  351. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromString
  352. (JNIEnv *jenv, jclass self, jlong result, jstring value)
  353. {
  354. const char *valueAsUTF = (*jenv)->GetStringUTFChars(jenv, value, 0);
  355. pval *presult = (pval*)(long)result;
  356. presult->type=IS_STRING;
  357. presult->value.str.len=strlen(valueAsUTF);
  358. presult->value.str.val=emalloc(presult->value.str.len+1);
  359. strcpy(presult->value.str.val, valueAsUTF);
  360. (*jenv)->ReleaseStringUTFChars(jenv, value, valueAsUTF);
  361. }
  362. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromLong
  363. (JNIEnv *jenv, jclass self, jlong result, jlong value)
  364. {
  365. pval *presult = (pval*)(long)result;
  366. presult->type=IS_LONG;
  367. presult->value.lval=(long)value;
  368. }
  369. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromDouble
  370. (JNIEnv *jenv, jclass self, jlong result, jdouble value)
  371. {
  372. pval *presult = (pval*)(long)result;
  373. presult->type=IS_DOUBLE;
  374. presult->value.dval=value;
  375. }
  376. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromBoolean
  377. (JNIEnv *jenv, jclass self, jlong result, jboolean value)
  378. {
  379. pval *presult = (pval*)(long)result;
  380. presult->type=IS_BOOL;
  381. presult->value.lval=value;
  382. }
  383. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromObject
  384. (JNIEnv *jenv, jclass self, jlong result, jobject value)
  385. {
  386. /* wrapper the java object in a pval object */
  387. pval *presult = (pval*)(long)result;
  388. pval *handle;
  389. if (presult->type != IS_OBJECT) {
  390. presult->type=IS_OBJECT;
  391. presult->value.obj.ce=&java_class_entry;
  392. presult->value.obj.properties = (HashTable *) emalloc(sizeof(HashTable));
  393. presult->is_ref=1;
  394. presult->refcount=1;
  395. zend_hash_init(presult->value.obj.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  396. };
  397. ALLOC_ZVAL(handle);
  398. handle->type = IS_LONG;
  399. handle->value.lval =
  400. zend_list_insert((*jenv)->NewGlobalRef(jenv,value), le_jobject);
  401. pval_copy_constructor(handle);
  402. INIT_PZVAL(handle);
  403. zend_hash_index_update(presult->value.obj.properties, 0,
  404. &handle, sizeof(pval *), NULL);
  405. }
  406. JNIEXPORT void JNICALL Java_net_php_reflect_setResultFromArray
  407. (JNIEnv *jenv, jclass self, jlong result)
  408. {
  409. array_init( (pval*)(long)result );
  410. }
  411. JNIEXPORT jlong JNICALL Java_net_php_reflect_nextElement
  412. (JNIEnv *jenv, jclass self, jlong array)
  413. {
  414. pval *result;
  415. pval *handle = (pval*)(long)array;
  416. ALLOC_ZVAL(result);
  417. zend_hash_next_index_insert(handle->value.ht, &result, sizeof(zval *), NULL);
  418. return (jlong)(long)result;
  419. }
  420. JNIEXPORT void JNICALL Java_net_php_reflect_setException
  421. (JNIEnv *jenv, jclass self, jlong result, jstring value)
  422. {
  423. pval *presult = (pval*)(long)result;
  424. Java_net_php_reflect_setResultFromString(jenv, self, result, value);
  425. presult->type=IS_EXCEPTION;
  426. }
  427. JNIEXPORT void JNICALL Java_net_php_reflect_setEnv
  428. (JNIEnv *newJenv, jclass self)
  429. {
  430. iniUpdated=0;
  431. jenv=newJenv;
  432. if (!self) self = (*jenv)->FindClass(jenv, "net/php/reflect");
  433. php_reflect = (*jenv)->NewGlobalRef(jenv, self);
  434. }