Menu

[r690]: / branches / Release-4-1-2 / php-java-bridge / java_bridge.c  Maximize  Restore  History

Download this file

439 lines (369 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
/*-*- mode: C; tab-width:4 -*-*/
/* java_bridge.c -- contains utility procedures
Copyright (C) 2003-2007 Jost Boekemeier
This file is part of the PHP/Java Bridge.
The PHP/Java Bridge ("the library") is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2, or (at your option) any later version.
The library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with the PHP/Java Bridge; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this file statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
#include "php_java.h"
#include <stdlib.h>
#include "java_bridge.h"
/* miscellaneous */
#include <stdio.h>
/* strings */
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef ZEND_ENGINE_2
#include "zend_exceptions.h"
#endif
static void writeArgument(pval* arg, short ignoreNonJava TSRMLS_DC);
static void writeArguments(int argc, pval***argv, short ignoreNonJava TSRMLS_DC);
EXT_EXTERN_MODULE_GLOBALS(EXT)
static void checkError(pval *value TSRMLS_DC)
{
#ifndef ZEND_ENGINE_2
if (Z_TYPE_P(value) == IS_EXCEPTION) {
struct cb_stack_elem *stack_elem = 0;
/* display the exception only if we do not abort a callback or if
we abort a callback and this callback is not a method. This is
consistent with PHP5 behaviour, where we use
call_user_func_array (which also reports the exception when
the callback is not a method). */
if((!JG(cb_stack)) ||
(JG(cb_stack) &&
(SUCCESS == zend_stack_top(JG(cb_stack), (void**)&stack_elem)) &&
(!stack_elem->exception||(stack_elem->exception&&
!(stack_elem->object&&*stack_elem->object)))))
php_error(E_WARNING, "%s", Z_STRVAL_P(value));
efree(Z_STRVAL_P(value));
ZVAL_FALSE(value);
};
#endif
}
static short is_type (zval *pobj TSRMLS_DC) {
#ifdef ZEND_ENGINE_2
zend_class_entry *ce = Z_OBJCE_P(pobj);
return instanceof_function(ce, EXT_GLOBAL(class_entry) TSRMLS_CC) ||
instanceof_function(ce, EXT_GLOBAL(exception_class_entry) TSRMLS_CC);
#else
extern void EXT_GLOBAL(call_function_handler4)(INTERNAL_FUNCTION_PARAMETERS, zend_property_reference *property_reference);
return pobj->type == IS_OBJECT && pobj->value.obj.ce->handle_function_call==EXT_GLOBAL(call_function_handler4);
#endif
}
#ifdef ZEND_ENGINE_2
struct java_object {
zend_object parent;
long id;
};
void EXT_GLOBAL(store_jobject)(zval*object, long id TSRMLS_DC)
{
struct java_object*jobject = (struct java_object*)zend_objects_get_address(object TSRMLS_CC);
assert(id!=0);
jobject->id = id;
}
int EXT_GLOBAL(get_jobject_from_object)(zval*object, long *id TSRMLS_DC)
{
if(is_type(object TSRMLS_CC)) {
struct java_object*jobject = (struct java_object*)zend_objects_get_address(object TSRMLS_CC);
*id = jobject->id;
return *id!=0;
}
*id=0;
return 0;
}
static void destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
{
struct java_object*jobject = ((struct java_object*)object);
if(JG(jenv)&&jobject->id) (*JG(jenv))->writeUnref(JG(jenv), jobject->id);
jobject->id=0;
}
static void free_object(zend_object *object TSRMLS_DC)
{
((struct java_object*)object)->id=0;
zend_hash_destroy(object->properties);
FREE_HASHTABLE(object->properties);
efree(object);
}
static zend_object_value objects_new(struct java_object **object, zend_class_entry *class_type TSRMLS_DC)
{
zend_object_value retval;
*object = emalloc(sizeof(struct java_object));
memset(*object, 0, sizeof(struct java_object));
(*object)->parent.ce = class_type;
retval.handle = zend_objects_store_put(*object, (zend_objects_store_dtor_t) destroy_object, (zend_objects_free_object_storage_t) free_object, NULL TSRMLS_CC);
retval.handlers = (zend_object_handlers*)&EXT_GLOBAL(handlers);
return retval;
}
zend_object_value EXT_GLOBAL(create_object)(zend_class_entry *class_type TSRMLS_DC)
{
zval tmp;
zend_object_value obj;
struct java_object *object;
obj = objects_new(&object, class_type TSRMLS_CC);
ALLOC_HASHTABLE(object->parent.properties);
zend_hash_init(object->parent.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_copy(object->parent.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
return obj;
}
zend_object_value EXT_GLOBAL(create_exception_object)(zend_class_entry *class_type TSRMLS_DC)
{
zval tmp;
zend_object_value obj;
struct java_object *object;
zend_object *temp_exception_object;
obj = objects_new(&object, class_type TSRMLS_CC);
ALLOC_HASHTABLE(object->parent.properties);
zend_hash_init(object->parent.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
/* create a standard exception object */
#if ZEND_EXTENSION_API_NO >= 220060519
tmp.value.obj= zend_exception_get_default(TSRMLS_C)->create_object(class_type TSRMLS_CC);
#else
tmp.value.obj= zend_exception_get_default()->create_object(class_type TSRMLS_CC);
#endif
temp_exception_object=zend_objects_get_address(&tmp TSRMLS_CC);
/* and copy the trace from there */
zend_hash_copy(object->parent.properties,
temp_exception_object->properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
return obj;
}
#else
int EXT_GLOBAL(get_jobject_from_object)(pval*object, long *obj TSRMLS_DC)
{
pval **handle;
int n=-1;
if(is_type(object TSRMLS_CC))
n = zend_hash_index_find(Z_OBJPROP_P(object), 0, (void**) &handle);
if(n==-1) { *obj=0; return 0; }
*obj=**(long**)handle;
return 1;
}
#endif
void EXT_GLOBAL(result) (pval* arg, short ignoreNonJava, pval*presult TSRMLS_DC) {
proxyenv *jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
(*jenv)->writeResultBegin(jenv, presult);
if(arg)
writeArgument(arg, ignoreNonJava TSRMLS_CC);
else
(*jenv)->writeObject(jenv, 0);
(*jenv)->writeResultEnd(jenv);
}
short EXT_GLOBAL(invoke)(char*name, long object, int arg_count, zval***arguments, short ignoreNonJava, pval*presult TSRMLS_DC)
{
proxyenv *jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
(*jenv)->writeInvokeBegin(jenv, object, name, 0, 'I', (void*)presult);
writeArguments(arg_count, arguments, ignoreNonJava TSRMLS_CC);
return (*jenv)->writeInvokeEnd(jenv);
}
short EXT_GLOBAL(call_function_handler)(INTERNAL_FUNCTION_PARAMETERS, char*name, enum constructor constructor, short createInstance, pval *object, int arg_count, zval***arguments)
{
long result = 0;
proxyenv *jenv;
if (constructor) {
/* construct a Java object:
First argument is the class name. Any additional arguments will
be treated as constructor parameters. */
result = (long)object;
if (ZEND_NUM_ARGS() < 1) {
php_error(E_ERROR, "Missing classname in new "/**/EXT_NAME()/**/" call");
return 0;
}
#if EXTENSION == JAVA
/* create a new vm object */
jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
if(!jenv) {ZVAL_NULL(object); return 0;}
(*jenv)->writeCreateObjectBegin(jenv, Z_STRVAL_PP(arguments[0]), Z_STRLEN_PP(arguments[0]), createInstance?'I':'C', (void*)result);
writeArguments(--arg_count, ++arguments, 0 TSRMLS_CC);
(*jenv)->writeCreateObjectEnd(jenv);
#elif EXTENSION == MONO
/* create a new vm object, prepend .cli */
char *cname, *mname;
size_t clen;
jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
if(!jenv) {ZVAL_NULL(object); return;}
cname = Z_STRVAL_PP(arguments[0]);
clen = Z_STRLEN_PP(arguments[0]);
mname = emalloc(clen+5);
assert(mname); if(!mname) {ZVAL_NULL(object); return;}
strcpy(mname, "cli.");
memcpy(mname+4,cname, clen);
mname[clen+4] = 0;
(*jenv)->writeCreateObjectBegin(jenv, mname, clen+4, createInstance?'I':'C', (void*)result);
writeArguments(--arg_count, ++arguments, 0 TSRMLS_CC);
(*jenv)->writeCreateObjectEnd(jenv);
efree(mname);
#endif
} else {
long obj;
jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
if(!jenv) {ZVAL_NULL(object); return 0;}
EXT_GLOBAL(get_jobject_from_object)(object, &obj TSRMLS_CC);
if(!obj) {
php_error(E_ERROR, "php_mod_"/**/EXT_NAME()/**/"(%d): Call object is null: The connection to the current back end doesn't exist anymore; probably the current back end has been restarted w/o restarting the front end.", 98);
ZVAL_NULL(object); return 0;
}
result = (long)return_value;
/* invoke a method on the given object */
(*jenv)->writeInvokeBegin(jenv, obj, name, 0, 'I', (void*)result);
writeArguments(arg_count, arguments, 0 TSRMLS_CC);
if(!(*jenv)->writeInvokeEnd(jenv)) return 0;
}
checkError((pval*)result TSRMLS_CC);
return 1;
}
static void writeArgument(pval* arg, short ignoreNonJava TSRMLS_DC)
{
proxyenv *jenv = JG(jenv);
long result;
switch (Z_TYPE_P(arg)) {
case IS_STRING:
(*jenv)->writeString(jenv, Z_STRVAL_P(arg), Z_STRLEN_P(arg));
break;
case IS_OBJECT:
EXT_GLOBAL(get_jobject_from_object)(arg, &result TSRMLS_CC);
if(!ignoreNonJava && !result)
php_error(E_WARNING, "Argument is not (or does not contain) Java object(s).");
(*jenv)->writeObject(jenv, result);
break;
case IS_BOOL:
(*jenv)->writeBoolean(jenv, Z_LVAL_P(arg));
break;
case IS_LONG:
(*jenv)->writeLong(jenv, Z_LVAL_P(arg));
break;
case IS_DOUBLE:
(*jenv)->writeDouble(jenv, Z_DVAL_P(arg));
break;
case IS_ARRAY:
{
zval **value;
zstr string_key;
ulong num_key;
short wrote_begin=0;
/* Iterate through hash */
zend_hash_internal_pointer_reset(Z_ARRVAL_P(arg));
while(zend_hash_get_current_data(Z_ARRVAL_P(arg), (void**)&value) == SUCCESS) {
switch (zend_hash_get_current_key(Z_ARRVAL_P(arg), &string_key, &num_key, 0)) {
case HASH_KEY_IS_STRING:
if(!wrote_begin) {
wrote_begin=1;
(*jenv)->writeCompositeBegin_h(jenv);
}
(*jenv)->writePairBegin_s(jenv, ZSTR_S(string_key), strlen(ZSTR_S(string_key)));
writeArgument(*value, ignoreNonJava TSRMLS_CC);
(*jenv)->writePairEnd(jenv);
break;
case HASH_KEY_IS_LONG:
if(!wrote_begin) {
wrote_begin=1;
(*jenv)->writeCompositeBegin_h(jenv);
}
(*jenv)->writePairBegin_n(jenv, num_key);
writeArgument(*value, ignoreNonJava TSRMLS_CC);
(*jenv)->writePairEnd(jenv);
break;
default: /* HASH_KEY_NON_EXISTANT */
if(!wrote_begin) {
wrote_begin=1;
(*jenv)->writeCompositeBegin_a(jenv);
}
(*jenv)->writePairBegin(jenv);
writeArgument(*value, ignoreNonJava TSRMLS_CC);
(*jenv)->writePairEnd(jenv);
}
zend_hash_move_forward(Z_ARRVAL_P(arg));
}
if(!wrote_begin) (*jenv)->writeCompositeBegin_a(jenv);
(*jenv)->writeCompositeEnd(jenv);
break;
}
default:
(*jenv)->writeObject(jenv, 0);
}
}
static void writeArguments(int argc, pval***argv, short ignoreNonJava TSRMLS_DC)
{
int i;
for (i=0; i<argc; i++) {
writeArgument(*argv[i], ignoreNonJava TSRMLS_CC);
}
}
/**
* get_property_handler
*/
short EXT_GLOBAL(get_property_handler)(char*name, zval *object, zval *presult)
{
long obj;
proxyenv *jenv;
TSRMLS_FETCH();
jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
if(!jenv) {ZVAL_NULL(presult); return FAILURE;}
/* get the object */
EXT_GLOBAL(get_jobject_from_object)(object, &obj TSRMLS_CC);
ZVAL_NULL(presult);
if (!obj) {
php_error(E_ERROR,
"Attempt to access a Java property on a non-Java object");
} else {
/* invoke the method */
(*jenv)->writeInvokeBegin(jenv, obj, name, 0, 'P', (void*)presult);
if(!(*jenv)->writeInvokeEnd(jenv)) return 0;
}
checkError(presult TSRMLS_CC);
return 1;
}
/**
* set_property_handler
*/
short EXT_GLOBAL(set_property_handler)(char*name, zval *object, zval *value, zval *presult)
{
long obj;
proxyenv *jenv;
TSRMLS_FETCH();
jenv = EXT_GLOBAL(connect_to_server)(TSRMLS_C);
if(!jenv) {ZVAL_NULL(presult); return FAILURE; }
/* get the object */
EXT_GLOBAL(get_jobject_from_object)(object, &obj TSRMLS_CC);
ZVAL_NULL(presult);
if (!obj) {
php_error(E_ERROR,
"Attempt to access a Java property on a non-Java object");
} else {
/* invoke the method */
(*jenv)->writeInvokeBegin(jenv, obj, name, 0, 'P', (void*)presult);
writeArgument(value, 0 TSRMLS_CC);
if(!(*jenv)->writeInvokeEnd(jenv)) return 0;
}
checkError(presult TSRMLS_CC);
return 1;
}
#ifndef PHP_WRAPPER_H
#error must include php_wrapper.h
#endif
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.