Menu

[r93]: / trunk / php-java-bridge / java.c  Maximize  Restore  History

Download this file

569 lines (477 with data), 13.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*-*- mode: C; tab-width:4 -*-*/
/* wait */
#include <sys/types.h>
#include <sys/wait.h>
/* miscellaneous */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include "php.h"
#include "php_globals.h"
#include "ext/standard/info.h"
#include "php_java.h"
#include "java_bridge.h"
#include "protocol.h"
ZEND_DECLARE_MODULE_GLOBALS(java)
PHP_RINIT_FUNCTION(java)
{
extern int java_connect_to_server(struct cfg*cfg TSRMLS_DC);
assert(!JG(jenv));
java_connect_to_server(&JG(cfg) TSRMLS_CC);
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(java)
{
if (JG(php_reflect)) (*JG(jenv))->DeleteGlobalRef(JG(jenv), JG(php_reflect));
if(JG(jenv)&&*JG(jenv)&&(*JG(jenv))->peer) SFCLOSE((*JG(jenv))->peer);
if(JG(jenv)&&*JG(jenv)) free(*JG(jenv));
if(JG(jenv)) free(JG(jenv));
JG(php_reflect) = NULL;
JG(jenv) = NULL;
return SUCCESS;
}
PHP_FUNCTION(java_last_exception_get)
{
jlong result = 0;
jmethodID lastEx;
if (ZEND_NUM_ARGS()!=0) WRONG_PARAM_COUNT;
result = (jlong)(long)return_value;
if(!JG(jenv)) {
php_error(E_ERROR, "java not initialized");
return;
}
lastEx = (*JG(jenv))->GetMethodID(JG(jenv), JG(reflect_class),
"lastException", "(JJ)V");
(*JG(jenv))->LastException(JG(jenv), JG(php_reflect), lastEx,
result);
}
PHP_FUNCTION(java_last_exception_clear)
{
jlong result = 0;
jmethodID clearEx;
if (ZEND_NUM_ARGS()!=0) WRONG_PARAM_COUNT;
result = (jlong)(long)return_value;
if(!JG(jenv)) {
php_error(E_ERROR, "java not initialized");
return;
}
clearEx = (*JG(jenv))->GetMethodID(JG(jenv), JG(reflect_class),
"clearException", "()V");
(*JG(jenv))->CallVoidMethod(1, JG(jenv), JG(php_reflect),
clearEx);
}
PHP_FUNCTION(java_set_library_path)
{
zval **path;
jlong result = 0;
jmethodID setJarPath;
jstring p;
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &path) == FAILURE)
WRONG_PARAM_COUNT;
convert_to_string_ex(path);
result = (jlong)(long)return_value;
if(!JG(jenv)) {
php_error(E_ERROR, "java not initialized");
return;
}
setJarPath = (*JG(jenv))->GetMethodID(JG(jenv), JG(reflect_class),
"setJarLibraryPath",
"(Ljava/lang/String;)V");
p = (*JG(jenv))->NewStringUTF(JG(jenv), Z_STRVAL_PP(path));
(*JG(jenv))->CallVoidMethod(2, JG(jenv), JG(php_reflect),
setJarPath, p);
(*JG(jenv))->DeleteLocalRef(JG(jenv), p);
}
function_entry java_functions[] = {
PHP_FE(java_last_exception_get, NULL)
PHP_FE(java_last_exception_clear, NULL)
PHP_FE(java_set_library_path, NULL)
{NULL, NULL, NULL}
};
zend_module_entry java_module_entry = {
STANDARD_MODULE_HEADER,
"java",
java_functions,
PHP_MINIT(java),
PHP_MSHUTDOWN(java),
PHP_RINIT(java),
PHP_RSHUTDOWN(java),
PHP_MINFO(java),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_JAVA
ZEND_GET_MODULE(java)
#endif
int le_jobject;
int java_ini_updated, java_ini_last_updated;
zend_class_entry *php_java_class_entry;
static PHP_INI_MH(OnIniSockname)
{
if (new_value) {
JG(cfg).sockname=new_value;
java_ini_updated|=U_SOCKNAME;
}
return SUCCESS;
}
static PHP_INI_MH(OnIniClassPath)
{
if (new_value) {
JG(cfg).classpath =new_value;
java_ini_updated|=U_CLASSPATH;
}
return SUCCESS;
}
static PHP_INI_MH(OnIniLibPath)
{
if (new_value) {
JG(cfg).ld_library_path = new_value;
java_ini_updated|=U_LIBRARY_PATH;
}
return SUCCESS;
}
static PHP_INI_MH(OnIniJava)
{
if (new_value) {
JG(cfg).java = new_value;
java_ini_updated|=U_JAVA;
}
return SUCCESS;
}
static PHP_INI_MH(OnIniJavaHome)
{
if (new_value) {
JG(cfg).java_home = new_value;
java_ini_updated|=U_JAVA_HOME;
}
return SUCCESS;
}
static PHP_INI_MH(OnIniLogLevel)
{
if (new_value) {
JG(cfg).logLevel = new_value;
java_ini_updated|=U_LOGLEVEL;
}
return SUCCESS;
}
static PHP_INI_MH(OnIniLogFile)
{
if (new_value) {
JG(cfg).logFile = new_value;
java_ini_updated|=U_LOGFILE;
}
return SUCCESS;
}
PHP_INI_BEGIN()
PHP_INI_ENTRY("java.socketname", NULL, PHP_INI_SYSTEM, OnIniSockname)
PHP_INI_ENTRY("java.classpath", NULL, PHP_INI_SYSTEM, OnIniClassPath)
PHP_INI_ENTRY("java.libpath", NULL, PHP_INI_SYSTEM, OnIniLibPath)
PHP_INI_ENTRY("java.java", NULL, PHP_INI_SYSTEM, OnIniJava)
PHP_INI_ENTRY("java.java_home", NULL, PHP_INI_SYSTEM, OnIniJavaHome)
PHP_INI_ENTRY("java.log_level", NULL, PHP_INI_SYSTEM, OnIniLogLevel)
PHP_INI_ENTRY("java.log_file", NULL, PHP_INI_SYSTEM, OnIniLogFile)
PHP_INI_END()
static void php_java_alloc_globals_ctor(zend_java_globals *java_globals TSRMLS_DC)
{
java_globals->php_reflect=0;
java_globals->jenv=0;
java_globals->reflect_class=0;
}
static void init_server()
{
extern int java_test_server(struct cfg*cfg TSRMLS_DC);
if(java_test_server(&JG(cfg) TSRMLS_CC)==FAILURE)
java_start_server(&JG(cfg));
java_test_server(&JG(cfg) TSRMLS_CC);
}
#ifdef ZEND_ENGINE_2
static void print_array(int argc, pval** argv)
{
int i;
for (i=0; i<argc; i++) {
pval*arg=argv[i];
switch (Z_TYPE_P(arg)) {
case IS_STRING:
puts(Z_STRVAL_P(arg));
break;
case IS_OBJECT:
puts("object");
break;
case IS_BOOL:
puts("bool");
break;
case IS_LONG:
puts("long");
break;
case IS_DOUBLE:
puts("double");
break;
case IS_ARRAY:
{
puts("array");
}
break;
default:
puts("bleh");
}
}
}
PHP_METHOD(java, java)
{
zval **argv;
int argc = ZEND_NUM_ARGS();
argv = (zval **) safe_emalloc(sizeof(zval *), argc, 0);
if (zend_get_parameters_array(ht, argc, argv) == FAILURE) {
php_error(E_ERROR, "Couldn't fetch arguments into array.");
RETURN_NULL();
}
puts("java called");
print_array(argc, argv);
php_java_call_function_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU,
"java",
getThis(),
argc, argv);
efree(argv);
}
PHP_METHOD(java, __call)
{
zval **xargv, **argv;
int i = 0, xargc, argc = ZEND_NUM_ARGS();
HashPosition pos;
zval **param;
argv = (zval **) safe_emalloc(sizeof(zval *), argc, 0);
if (zend_get_parameters_array(ht, argc, argv) == FAILURE) {
php_error(E_ERROR, "Couldn't fetch arguments into array.");
RETURN_NULL();
}
/* function arguments in arg#2 */
xargc = zend_hash_num_elements(Z_ARRVAL_P(argv[1]));
xargv = safe_emalloc(sizeof(zval *), xargc, 0);
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(argv[1]), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(argv[1]), (void **) &param, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(argv[1]), &pos)) {
/*zval_add_ref(param);*/
xargv[i++] = *param;
}
print_array(argc, argv);
puts("call called");
print_array(argc, argv);
php_java_call_function_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU,
Z_STRVAL(*argv[0]),
getThis(),
xargc, xargv);
efree(argv);
efree(xargv);
}
PHP_METHOD(java, __set)
{
zval **argv;
int argc = ZEND_NUM_ARGS();
argv = (zval **) safe_emalloc(sizeof(zval *), argc, 0);
if (zend_get_parameters_array(ht, argc, argv) == FAILURE) {
php_error(E_ERROR, "Couldn't fetch arguments into array.");
RETURN_NULL();
}
puts("set called");
print_array(argc, argv);
php_java_set_property_handler(Z_STRVAL(*argv[0]), getThis(), argv[1]);
efree(argv);
}
PHP_METHOD(java, __get)
{
pval presult;
zval **argv;
int argc = ZEND_NUM_ARGS();
argv = (zval **) safe_emalloc(sizeof(zval *), argc, 0);
if (zend_get_parameters_array(ht, argc, argv) == FAILURE) {
php_error(E_ERROR, "Couldn't fetch arguments into array.");
RETURN_NULL();
}
puts("get called");
print_array(argc, argv);
presult = php_java_get_property_handler(Z_STRVAL(*argv[0]), getThis());
efree(argv);
}
static function_entry java_class_functions[] = {
PHP_ME(java, java, NULL, 0)
PHP_ME(java, __call, NULL, 0)
PHP_ME(java, __get, NULL, 0)
PHP_ME(java, __set, NULL, 0)
};
static void make_lambda(zend_internal_function *f,
void (*handler)(INTERNAL_FUNCTION_PARAMETERS))
{
f->type = ZEND_INTERNAL_FUNCTION;
f->handler = handler;
f->function_name = NULL;
f->scope = NULL;
f->fn_flags = 0;
f->prototype = NULL;
f->num_args = 0;
f->arg_info = NULL;
f->pass_rest_by_reference = 0;
}
#else
static void
call_function_handler(INTERNAL_FUNCTION_PARAMETERS, zend_property_reference *property_reference)
{
pval *object = property_reference->object;
zend_overloaded_element *function_name = (zend_overloaded_element *)
property_reference->elements_list->tail->data;
char *name = Z_STRVAL(function_name->element);
int arg_count = ZEND_NUM_ARGS();
pval **arguments = (pval **) emalloc(sizeof(pval *)*arg_count);
getParametersArray(ht, arg_count, arguments);
php_java_call_function_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU,
name, object,
arg_count, arguments);
efree(arguments);
pval_destructor(&function_name->element);
}
static pval
get_property_handler(zend_property_reference *property_reference)
{
pval presult;
zend_llist_element *element;
zend_overloaded_element *property;
char *name;
TSRMLS_FETCH();
element = property_reference->elements_list->head;
property=(zend_overloaded_element *)element->data;
name = Z_STRVAL(property->element);
presult = php_java_get_property_handler(char* name, pval* object TSRMLS_CC);
pval_destructor(&property->element);
return presult;
}
static int
set_property_handler(zend_property_reference *property_reference, pval *value)
{
int result;
zend_llist_element *element;
zend_overloaded_element *property;
char *name;
TSRMLS_FETCH();
element = property_reference->elements_list->head;
property=(zend_overloaded_element *)element->data;
name = Z_STRVAL(property->element);
result = php_java_set_property_handler(name, object, value TSRMLS_CC);
pval_destructor(&property->element);
return result;
}
#endif
PHP_MINIT_FUNCTION(java)
{
#ifndef ZEND_ENGINE_2
INIT_OVERLOADED_CLASS_ENTRY(php_java_class_entry, "java", NULL,
call_function_handler,
get_property_handler,
set_property_handler);
zend_register_internal_class(&php_java_class_entry TSRMLS_CC);
#else
zend_class_entry ce;
zend_internal_function call, get, set;
make_lambda(&call, ZEND_FN(java___call));
make_lambda(&get, ZEND_FN(java___get));
make_lambda(&set, ZEND_FN(java___set));
INIT_OVERLOADED_CLASS_ENTRY(ce, "java",
java_class_functions,
(zend_function*)&call,
(zend_function*)&get,
(zend_function*)&set);
php_java_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
#endif
/* Register the resource, with destructor (arg 1) and text
description (arg 3), the other arguments are just standard
placeholders */
le_jobject = zend_register_list_destructors_ex(php_java_destructor, NULL, "java", module_number);
ZEND_INIT_MODULE_GLOBALS(java, php_java_alloc_globals_ctor, NULL);
if(REGISTER_INI_ENTRIES()==SUCCESS) {
/* set the default values for all undefined */
extern void java_init_cfg(struct cfg *cfg);
java_init_cfg(&JG(cfg));
#ifndef CFG_JAVA_SOCKET_INET
JG(cfg).saddr.sun_family = AF_UNIX;
memset(JG(cfg).saddr.sun_path, 0, sizeof JG(cfg).saddr.sun_path);
strcpy(JG(cfg).saddr.sun_path, JG(cfg).sockname);
#else
JG(cfg).saddr.sin_family = AF_INET;
JG(cfg).saddr.sin_port=htons(atoi(JG(cfg).sockname));
JG(cfg).saddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
#endif
}
init_server();
assert(!java_ini_last_updated);
java_ini_last_updated=java_ini_updated;
java_ini_updated=0;
return SUCCESS;
}
static char*get_server_args(struct cfg*cfg) {
int i;
char*s;
char*env[N_SENV];
char*args[N_SARGS];
unsigned int length = 0;
java_get_server_args(cfg, env, args);
for(i=0; i< (sizeof env)/(sizeof*env); i++) {
if(!env[i]) break;
length+=strlen(env[i])+1;
}
for(i=0; i< (sizeof args)/(sizeof*args); i++) {
size_t l;
if(!args[i]) break;
l=strlen(args[i]);
length+=(l?l:2)+1;
}
s=malloc(length+1);
assert(s);
*s=0;
for(i=0; i< (sizeof env)/(sizeof*env); i++) {
if(!env[i]) break;
strcat(s, env[i]); strcat(s, " ");
free(env[i]);
}
for(i=0; i< (sizeof args)/(sizeof*args); i++) {
if(!args[i]) break;
if(!strlen(args[i])) strcat(s,"'");
strcat(s, args[i]);
if(!strlen(args[i])) strcat(s,"'");
strcat(s, " ");
free(args[i]);
}
s[length]=0;
return s;
}
PHP_MINFO_FUNCTION(java)
{
char*s=get_server_args(&JG(cfg));
int status = java_test_server(&JG(cfg) TSRMLS_CC);
php_info_print_table_start();
php_info_print_table_row(2, "java support", "Enabled");
php_info_print_table_row(2, "java bridge", java_bridge_version);
#ifndef CFG_JAVA_SOCKET_ANON
php_info_print_table_row(2, "java command", s);
#endif
php_info_print_table_row(2, "java.libpath", JG(cfg).ld_library_path);
php_info_print_table_row(2, "java.classpath", JG(cfg).classpath);
php_info_print_table_row(2, "java.java_home", JG(cfg).java_home);
php_info_print_table_row(2, "java.java", JG(cfg).java);
if(strlen(JG(cfg).logFile)==0)
php_info_print_table_row(2, "java.log_file", "<stdout>");
else
php_info_print_table_row(2, "java.log_file", JG(cfg).logFile);
php_info_print_table_row(2, "java.log_level", JG(cfg).logLevel);
php_info_print_table_row(2, "java status", (status==SUCCESS)?"running":"not running");
php_info_print_table_end();
free(s);
}
PHP_MSHUTDOWN_FUNCTION(java)
{
extern void php_java_shutdown_library(struct cfg*cfg TSRMLS_DC);
extern void java_destroy_cfg(int, struct cfg*cfg TSRMLS_DC);
java_destroy_cfg(java_ini_last_updated, &JG(cfg) TSRMLS_CC);
java_ini_last_updated=0;
UNREGISTER_INI_ENTRIES();
php_java_shutdown_library(&JG(cfg) TSRMLS_CC);
return SUCCESS;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.