Menu

[r585]: / trunk / php-java-bridge / server / META-INF / java / Client.inc  Maximize  Restore  History

Download this file

687 lines (634 with data), 18.8 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<?php /*-*- mode: php; tab-width:4 -*-*/
/* java_Client.php -- parser callbacks for the PHP/Java Bridge.
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. */
require_once("${JAVA_BASE}/JavaProxy.inc");
require_once("${JAVA_BASE}/Parser.inc");
require_once("${JAVA_BASE}/Protocol.inc");
require_once("${JAVA_BASE}/GlobalRef.inc");
/**
* @access private
*/
class java_SimpleFactory {
var $client;
function java_SimpleFactory($client) {
$this->client = $client;
}
function getProxy($result, $signature, $wrap) {
return $result;
}
function checkResult($result) {}
}
/**
* @access private
*/
class java_ProxyFactory extends java_SimpleFactory {
function java_ProxyFactory($client) {
parent::java_SimpleFactory($client);
}
function create($result, $signature) {
return new java_JavaProxy($result, $signature);
}
function createInternal($proxy) {
return new java_InternalJava($proxy);
}
function getProxy($result, $signature, $wrap) {
$proxy = $this->create($result, $signature);
if($wrap) $proxy = $this->createInternal($proxy);
return $proxy;
}
}
/**
* @access private
*/
class java_ArrayProxyFactory extends java_ProxyFactory {
function java_ArrayProxyFactory($client) {
parent::java_ProxyFactory($client);
}
function create($result, $signature) {
return new java_ArrayProxy($result, $signature);
}
}
/**
* @access private
*/
class java_IteratorProxyFactory extends java_ProxyFactory {
function java_IteratorProxyFactory($client) {
parent::java_ProxyFactory($client);
}
function create($result, $signature) {
return new java_IteratorProxy($result, $signature);
}
}
/**
* @access private
*/
class java_ExceptionProxyFactory extends java_SimpleFactory {
function java_ExceptionProxyFactory($client) {
parent::java_SimpleFactory($client);
}
function create($result, $signature) {
static $count = 0;
return new java_ExceptionProxy($result, $signature);
}
function getProxy($result, $signature, $wrap) {
$proxy = $this->create($result, $signature);
if($wrap) $proxy = new java_InternalException($proxy);
return $proxy;
}
}
/**
* @access private
*/
class java_ThrowExceptionProxyFactory extends java_ExceptionProxyFactory {
function java_ThrowExceptionProxyFactory($client) {
parent::java_ExceptionProxyFactory($client);
}
function getProxy($result, $signature, $wrap) {
$proxy = $this->create($result, $signature);
// don't check for $wrap, which may be wrong (type Java instead of
// JavaException) when the user has managed to create an exception
// from a Java constructor, e.g.: new Java("java.lang.String",
// null). Since we'll discard the possibly wrong type anyway, we
// can create a fresh proxy without any further checks:
$proxy = new java_InternalException($proxy);
return $proxy;
}
function checkResult($result) {
throw $result;
}
}
/**
* @access private
*/
class java_CacheEntry {
var $fmt, $signature, $factory, $java;
var $resultVoid;
function java_CacheEntry($fmt, $signature, $factory, $resultVoid) {
$this->fmt = $fmt;
$this->signature = $signature;
$this->factory = $factory;
$this->resultVoid = $resultVoid;
}
}
/**
* @access private
*/
class java_Arg {
var $client;
var $exception; // string representation for php4
var $factory, $val;
var $signature; // class type
function java_Arg($client) {
$this->client = $client;
$this->factory = $client->simpleFactory;
}
function linkResult(&$val) {
$this->val = &$val;
}
function setResult($val) {
$this->val = &$val;
}
function getResult($wrap) {
$rc = $this->factory->getProxy($this->val, $this->signature, $wrap);
$factory = $this->factory;
$this->factory = $this->client->simpleFactory;
$factory->checkResult($rc);
return $rc;
}
function setFactory($factory) {
$this->factory = $factory;
}
function setException($string) {
$this->exception = $string;
}
function setVoidSignature() {
$this->signature = "@N";
// update cache
$key = $this->client->currentCacheKey;
$this->client->currentArgumentsFormat[6]="3";
if(JAVA_DEBUG) {
echo "ignore further results:"; echo "\n";
}
if(JAVA_DEBUG) {
echo "updating cache $key, argformat: {$this->client->currentArgumentsFormat}, classType: {$this->signature}\n";
}
$cacheEntry = new java_CacheEntry($this->client->currentArgumentsFormat, $this->signature, $this->factory, true);
$this->client->methodCache[$key]=$cacheEntry;
}
function setSignature($signature) {
$this->signature = $signature;
// update cache
$key = $this->client->currentCacheKey;
if($key && $key[0]!='~') { // don't cache array(...) or non-java objects
if(JAVA_DEBUG) {
echo "updating cache $key, argformat: {$this->client->currentArgumentsFormat}, classType: {$this->signature}\n";
}
$cacheEntry = new java_CacheEntry($this->client->currentArgumentsFormat, $this->signature, $this->factory, false);
$this->client->methodCache[$key]=$cacheEntry;
}
}
}
/**
* @access private
*/
class java_CompositeArg extends java_Arg {
var $parentArg;
var $idx; // position within $val;
var $type; // for A and X
var $counter;
function java_CompositeArg($client, $type) {
parent::java_Arg($client);
$this->type = $type;
$this->val = array();
$this->counter = 0;
}
function setNextIndex() {
$this->idx = $this->counter++;
}
function setIndex($val) {
$this->idx = $val;
}
function linkResult(&$val) {
$this->val[$this->idx]=&$val;
}
function setResult($val) {
$this->val[$this->idx]=$this->factory->getProxy($val, $this->signature, true);
$this->factory = $this->client->simpleFactory;
}
}
/**
* @access private
*/
class java_ApplyArg extends java_CompositeArg {
var $m, $p, $v, $n; // see PROTOCOL.TXT
function java_ApplyArg($client, $type, $m, $p, $v, $n) {
parent::java_CompositeArg($client, $type);
$this->m = $m;
$this->p = $p;
$this->v = $v;
$this->n = $n;
}
}
/**@deprecated*/
/**
* @access private
*/
class java_Handler {
var $client;
function java_Handler($client) {
$this->client = $client;
}
function flush() {}
function sendData() {
$this->client->protocol->sendData();
}
function handleRequests() {
$client = $this->client;
do {
$tail_call = false;
$this->client->stack=array($this->client->arg=$this->client->simpleArg);
$client->idx = 0;
$client->parser->parse();
/* pull off A, if any */
if((count($client->stack)) > 1) {
$arg = array_pop($client->stack);
$client->apply($arg);
$tail_call = 1; // we don't expect a result
} else {
$tail_call = 0;
}
$client->stack=null;
} while($tail_call);
return 1;
}
}
/**@deprecated*/
/**
* @access private
*/
class java_AsyncHandler extends java_Handler {
var $arg;
function java_AsyncHandler($client) {
$this->client = $client;
$this->arg = $client->simpleArg;
}
function flush() {
$this->client->protocol->sendData();
}
function sendData() {
$this->client->protocol->sendAsyncData();
}
function handleRequests() {
$this->arg->setFactory($this->client->proxyFactory);
$this->arg->setResult(++$this->client->asyncCtx);
}
}
/**
* @access private
*/
class java_Client /* implements IDocHandler */ {
var $RUNTIME;
const RECV_SIZE=8192;
const SEND_SIZE=8192;
var $result, $exception;
var $parser;
var $simpleArg, $compositeArg;
var $simpleFactory,
$proxyFactory, $iteratorProxyFacroty,
$arrayProxyFactory, $exceptionProxyFactory, $throwExceptionProxyFactory;
var $arg;
var $asyncCtx;
var $globalRef;
var $stack;
// the cache
var $methodCache = array(), $currentCacheKey, $currentArgumentsFormat;
var $cachedJavaPrototype;
// send buffer
var $sendBuffer, $preparedToSendBuffer;
/**@deprecated*/
var $defaultHandler, $asyncHandler, $handler;
function java_Client() {
$this->RUNTIME = array();
// named pipes are enabled if shared memory is accessible
if(JAVA_PIPE_DIR && function_exists("posix_mkfifo"))
$this->RUNTIME['PIPE_DIR']=JAVA_PIPE_DIR;
else
$this->RUNTIME['PIPE_DIR']=null;
$this->parser = new java_Parser($this);
$this->protocol = new java_Protocol($this);
$this->simpleFactory = new java_SimpleFactory($this);
$this->proxyFactory = new java_ProxyFactory($this);
$this->arrayProxyFactory = new java_ArrayProxyFactory($this);
$this->iteratorProxyFactory = new java_IteratorProxyFactory($this);
$this->exceptionProxyFactory = new java_ExceptionProxyFactory($this);
$this->throwExceptionProxyFactory = new java_ThrowExceptionProxyFactory($this);
$this->cachedJavaPrototype=new java_JavaProxyProxy($this);
$this->simpleArg = new java_Arg($this);
$this->globalRef = new java_GlobalRef($this);
$this->asyncCtx = 0;
/**@deprecated*/
$this->handler = $this->defaultHandler = new java_Handler($this);
$this->asyncHandler = new java_AsyncHandler($this);
}
function __destruct() {
if(JAVA_DEBUG) echo "the client destroyed\n";
$this->protocol->flush();
$this->protocol->keepAlive();
}
function read($size) {
return $this->protocol->read($size);
}
function setDefaultHandler() {
$this->handler->flush();
$this->handler = $this->defaultHandler;
}
function setAsyncHandler() {
$this->handler = $this->asyncHandler;
}
function isAsync() {
return $this->handler === $this->asyncHandler;
}
function handleRequests() {
if(!JAVA_CACHE_ENABLED) { $this->handler->handleRequests(); return 1;}
do {
$tail_call = false;
$this->stack=array($this->arg=$this->simpleArg);
$this->idx = 0;
$this->parser->parse();
/* pull off A, if any */
if((count($this->stack)) > 1) {
$arg = array_pop($this->stack);
$this->apply($arg);
$tail_call = 1; // we don't expect a result
} else {
$tail_call = 0;
}
$this->stack=null;
} while($tail_call);
return 1;
}
function getWrappedResult($wrap) {
return $this->simpleArg->getResult($wrap);
}
function getInternalResult() {
return $this->getWrappedResult(false);
}
function getResult() {
return $this->getWrappedResult(true);
}
function getProxyFactory($type) {
switch($type[0]) {
case 'E':
$factory = $this->exceptionProxyFactory;
break;
case 'C':
$factory = $this->iteratorProxyFactory;
break;
case 'A':
$factory = $this->arrayProxyFactory;
break;
case 'O':
$factory = $this->proxyFactory;
}
return $factory;
}
function link(&$arg, &$newArg) {
$arg->linkResult($newArg->val);
$newArg->parentArg = $arg;
}
function getExact($str) {
return hexdec($str);
}
function getInexact($str) {
sscanf($str, "%e", $val);
return $val;
}
function begin($name, $st) {
$arg = $this->arg;
switch($name[0]) {
case 'A': /* receive apply args as normal array */
$object = $this->globalRef->get($this->getExact($st['v']));
$newArg = new java_ApplyArg($this, 'A',
$this->parser->getData($st['m']),
$this->parser->getData($st['p']),
$object,
$this->getExact($st['n']));
$this->link($arg, $newArg);
array_push($this->stack, $this->arg = $newArg);
break;
case 'X':
$newArg = new java_CompositeArg($this, $st['t']);
$this->link($arg, $newArg);
array_push($this->stack, $this->arg = $newArg);
break;
case 'P':
if($arg->type=='H') { /* hash table */
$s = $st['t'];
if($s[0]=='N') { /* number */
$arg->setIndex($this->getExact($st['v']));
} else {
$arg->setIndex($this->parser->getData($st['v']));
}
} else { /* array */
$arg->setNextIndex();
}
break;
case 'S':
$arg->setResult($this->parser->getData($st['v']));
break;
case 'B':
$s=$st['v'];
$arg->setResult($s[0]=='T');
break;
case 'L': // unsigned long
$sign = $st['p'];
$val = $this->getExact($st['v']);
if($sign[0]=='A') $val*=-1;
$arg->setResult($val);
break;
case 'D':
$arg->setResult($this->getInexact($st['v']));
break;
case 'V':
if(JAVA_DEBUG) {echo "setresult VOID:"; echo "\n";}
/* fall through */
case 'N': // FIXME
$arg->setVoidSignature();
$arg->setResult(null);
break;
case 'F':
break;
case 'O':
$arg->setFactory($this->getProxyFactory($st['p']));
$arg->setResult($this->asyncCtx=$this->getExact($st['v']));
$arg->setSignature($st['m']);
if(JAVA_DEBUG) {echo "setresult object:"; echo sprintf("%x", $this->asyncCtx); echo "\n";}
break;
case 'E':
$arg->setFactory($this->throwExceptionProxyFactory);
$arg->setResult($this->asyncCtx=$this->getExact($st['v']));
if(JAVA_DEBUG) {echo "setresult exception:"; echo sprintf("%x", $this->asyncCtx); echo "\n";}
$arg->setException($st['m']);
break;
default:
$this->parser->parserError();
}
}
function end($name) {
switch($name[0]) {
case 'X':
$frame = array_pop($this->stack);
$this->arg = $frame->parentArg;
break;
}
}
function createParserString() {
return new java_ParserString();
}
function writeArg($arg) {
if(is_string($arg)) {
$this->protocol->writeString($arg);
} else if(is_object($arg)) {
$this->protocol->writeObject($arg->__java);
return $arg;
} else if(is_null($arg)) {
$this->protocol->writeObject(null);
} else if(is_bool($arg)) {
$this->protocol->writeBoolean($arg);
} else if(is_integer($arg)) {
$this->protocol->writeLong($arg);
} else if(is_float($arg)) {
$this->protocol->writeDouble($arg);
} else if(is_array($arg)) {
$wrote_begin=false;
foreach($arg as $key=>$val) {
if(is_string($key)) {
if(!$wrote_begin) {
$wrote_begin=1;
$this->protocol->writeCompositeBegin_h();
}
$this->protocol->writePairBegin_s($key);
$this->writeArg($val);
$this->protocol->writePairEnd();
} else {
if(!$wrote_begin) {
$wrote_begin=1;
$this->protocol->writeCompositeBegin_h();
}
$this->protocol->writePairBegin_n($key);
$this->writeArg($val);
$this->protocol->writePairEnd();
}
}
if(!$wrote_begin) {
$this->protocol->writeCompositeBegin_a();
}
$this->protocol->writeCompositeEnd();
}
return null;
}
function writeArgs($args) {
$n = count($args);
for($i=0; $i<$n; $i++) {
$this->writeArg($args[$i]);
}
}
function createObject($name, $args) {
$this->protocol->createObjectBegin($name);
$this->writeArgs($args);
$this->protocol->createObjectEnd();
$val = $this->getInternalResult();
return $val;
}
function referenceObject($name, $args) {
$this->protocol->referenceBegin($name);
$this->writeArgs($args);
$this->protocol->referenceEnd();
$val = $this->getInternalResult();
return $val;
}
function getProperty($object, $property) {
$this->protocol->propertyAccessBegin($object, $property);
$this->protocol->propertyAccessEnd();
return $this->getResult();
}
function setProperty($object, $property, $arg) {
$this->protocol->propertyAccessBegin($object, $property);
$this->writeArg($arg);
$this->protocol->propertyAccessEnd();
$this->getResult();
}
function invokeMethod($object, $method, $args) {
$this->protocol->invokeBegin($object, $method);
$this->writeArgs($args);
$this->protocol->invokeEnd();
$val = $this->getResult();
return $val;
}
function unref($object) {
$this->protocol->writeUnref($object);
}
function apply($arg) {
$name = $arg->p;
$object = $arg->v;
$ob = $object ? array(&$object, $name) : $name;
try {
$res = $arg->getResult(true);
if((!$object && !function_exists($name)) || ($object && !method_exists($object, $name))) throw new JavaException("java.lang.NoSuchMethodException", "$name");
$res = call_user_func_array($ob, $res);
$this->protocol->resultBegin();
$this->writeArg($res);
$this->protocol->resultEnd();
} catch (JavaException $e) {
$trace = $e->getTraceAsString();
$this->protocol->resultBegin();
$this->protocol->writeException($e->__java, $trace);
$this->protocol->resultEnd();
} catch(Exception $ex) {
$e = new Java("java.lang.Exception", $ex->getMessage);
$t = new Java("java.lang.reflect.UndeclaredThrowableException",$e);
$trace = $ex->getTraceAsString();
$this->protocol->resultBegin();
$this->protocol->writeException($t->__java, $trace);
$this->protocol->resultEnd();
}
}
function cast($object, $type) {
switch($type[0]) {
case 'S': case 's':
return $this->invokeMethod(0, "castToString", array($object));
case 'B': case 'b':
return $this->invokeMethod(0, "castToBoolean", array($object));
case 'L': case 'I': case 'l': case 'i':
return $this->invokeMethod(0, "castToExact", array($object));
case 'D': case 'd': case 'F': case 'f':
return $this->invokeMethod(0, "castToInExact", array($object));
case 'N': case 'n':
return null;
case 'A': case 'a':
return $this->invokeMethod(0, "castToArray", array($object));
case 'O': case 'o': // eh?
return $object;
default:
throw new java_RuntimeException("$type illegal");
}
}
function getContext() {
return $this->invokeMethod(0, "getContext", array());
}
function getSession($args) {
return $this->invokeMethod(0, "getSession", $args);
}
function getServerName() {
return $this->protocol->getServerName();
}
}
?>