Source for file Client.inc
Documentation is available at Client.inc
<?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
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
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");
class java_SimpleFactory {
function java_SimpleFactory($client) {
function getProxy($result, $signature, $wrap) {
if (false) { $signature =
$signature; $wrap =
$wrap; }
function checkResult($result) {
if (false) $result =
$result;
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);
class java_ArrayProxyFactory extends java_ProxyFactory {
function java_ArrayProxyFactory($client) {
parent::java_ProxyFactory($client);
function create($result, $signature) {
return new java_ArrayProxy($result, $signature);
class java_IteratorProxyFactory extends java_ProxyFactory {
function java_IteratorProxyFactory($client) {
parent::java_ProxyFactory($client);
function create($result, $signature) {
return new java_IteratorProxy($result, $signature);
class java_ExceptionProxyFactory extends java_SimpleFactory {
function java_ExceptionProxyFactory($client) {
parent::java_SimpleFactory($client);
function create($result, $signature) {
return new java_ExceptionProxy($result, $signature);
function getProxy($result, $signature, $wrap) {
$proxy =
$this->create($result, $signature);
if($wrap) $proxy =
new java_InternalException($proxy);
class java_ThrowExceptionProxyFactory extends java_ExceptionProxyFactory {
function java_ThrowExceptionProxyFactory($client) {
parent::java_ExceptionProxyFactory($client);
function getProxy($result, $signature, $wrap) {
if (false) $wrap =
$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);
function checkResult($result) {
public $fmt, $signature, $factory, $java;
function java_CacheEntry($fmt, $signature, $factory, $resultVoid) {
$this->signature =
$signature;
$this->factory =
$factory;
$this->resultVoid =
$resultVoid;
public $exception; // string representation for php4
public $signature; // class type
function java_Arg($client) {
$this->factory =
$client->simpleFactory;
function linkResult(&$val) {
function setResult($val) {
function getResult($wrap) {
$rc =
$this->factory->getProxy($this->val, $this->signature, $wrap);
$factory =
$this->factory;
$this->factory =
$this->client->simpleFactory;
$factory->checkResult($rc);
function setFactory($factory) {
$this->factory =
$factory;
function setException($string) {
$this->exception =
$string;
function setVoidSignature() {
$key =
$this->client->currentCacheKey;
if($key &&
$key[0]!=
'~') { // don't cache array(...) or non-java objects
$this->client->currentArgumentsFormat[6]=
"3";
echo
"ignore further results:"; echo
"\n";
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;
$key = $this->client->currentCacheKey;
if($key && $key[0]!='~') { // don't cache array(...) or non-java objects
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {
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;
class java_CompositeArg extends java_Arg {
public $idx; // position within $val;
public $type; // for A and X
function java_CompositeArg($client, $type) {
parent::java_Arg($client);
function setNextIndex() {
$this->idx = $this->counter++;
function setIndex($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;
class java_ApplyArg extends java_CompositeArg {
public $m, $p, $v, $n; // see PROTOCOL.TXT
function java_ApplyArg($client, $type, $m, $p, $v, $n) {
parent::java_CompositeArg($client, $type);
class java_Client /* implements IDocHandler */ {
public $result, $exception;
public $simpleArg, $compositeArg;
$proxyFactory, $iteratorProxyFacroty,
$arrayProxyFactory, $exceptionProxyFactory, $throwExceptionProxyFactory;
public $asyncCtx, $cancelProxyCreationCounter;
public $defaultCache = array(), $asyncCache = array(), $methodCache;
public $currentCacheKey, $currentArgumentsFormat;
public $cachedJavaPrototype;
public $sendBuffer, $preparedToSendBuffer;
$this->RUNTIME = array();
// named pipes are enabled if shared memory is accessible
if(JAVA_PIPE_DIR && function_exists("posix_mkfifo"))
$this->RUNTIME['PIPE_DIR']=<a href="../Java/_Options.inc.html#defineJAVA_PIPE_DIR">JAVA_PIPE_DIR</a>;
$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 = $this->cancelProxyCreationCounter = 0;
$this->methodCache = $this->defaultCache;
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) echo "the client destroyed\n";
if ($this->sendBuffer) $this->protocol->flush();
$this->protocol->keepAlive();
return $this->protocol->read($size);
function setDefaultHandler() {
$this->methodCache = $this->defaultCache;
function setAsyncHandler() {
$this->methodCache = $this->asyncCache;
function handleRequests() {
$this->stack=array($this->arg=$this->simpleArg);
if((count($this->stack)) > 1) {
$tail_call = 1; // we don't expect a result
function getWrappedResult($wrap) {
return $this->simpleArg->getResult($wrap);
function getInternalResult() {
return $this->getWrappedResult(false);
return $this->getWrappedResult(true);
function getProxyFactory($type) {
$factory = $this->exceptionProxyFactory;
$factory = $this->iteratorProxyFactory;
$factory = $this->arrayProxyFactory;
$factory = $this->proxyFactory;
function link(&$arg, &$newArg) {
$arg->linkResult($newArg->val);
$newArg->parentArg = $arg;
function getExact($str) {
function getInexact($str) {
function begin($name, $st) {
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']),
$this->getExact($st['n']));
$this->link($arg, $newArg);
$newArg = new java_CompositeArg($this, $st['t']);
$this->link($arg, $newArg);
if($arg->type=='H') { /* hash table */
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult prepare hash:"; echo sprintf("%s", $st['t']); echo "\n";}
if($s[0]=='N') { /* number */
$arg->setIndex($this->getExact($st['v']));
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult array: index:"; echo sprintf("%s", $st['v']); echo "\n";}
$arg->setIndex($this->parser->getData($st['v']));
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult hash: index:"; echo sprintf("%s", $this->parser->getData($st['v'])); echo "\n";}
$arg->setResult($this->parser->getData($st['v']));
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult string:"; echo sprintf("%s", $this->parser->getData($st['v'])); echo "\n";}
$arg->setResult($s[0]=='T');
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult bool:"; echo sprintf("%s", $st['v']); echo "\n";}
case 'L': // unsigned long
$val = $this->getExact($st['v']);
if($sign[0]=='A') $val*=-1;
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult long:"; echo sprintf("%s, sign: %s", $st['v'], $st['p']); echo "\n";}
$arg->setResult($this->getInexact($st['v']));
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult double:"; echo sprintf("%s", $st['v']); echo "\n";}
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult VOID:"; echo "\n";}
$arg->setVoidSignature();
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult null\n"; }
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "comm. end\n"; }
$arg->setFactory($this->getProxyFactory($st['p']));
$arg->setResult($this->asyncCtx=$this->getExact($st['v']));
if($st['n']!='T') $arg->setSignature($st['m']);
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult object:"; echo sprintf("%x", $this->asyncCtx); echo "\n";}
$arg->setFactory($this->throwExceptionProxyFactory);
$arg->setResult($this->asyncCtx=$this->getExact($st['v']));
if(<a href="../Java/_Options.inc.html#defineJAVA_DEBUG">JAVA_DEBUG</a>) {echo "setresult exception:"; echo sprintf("%x", $this->asyncCtx); echo "\n";}
$arg->setException($st['m']);
$this->parser->parserError();
$this->arg = $frame->parentArg;
function createParserString() {
return new java_ParserString();
function writeArg($arg) {
$this->protocol->writeString($arg);
if (!$arg instanceof <a href="../Java/java_JavaType.html">java_JavaType</a>) throw new java_IllegalArgumentException($arg);
$this->protocol->writeObject($arg->__java);
$this->protocol->writeObject(null);
$this->protocol->writeBoolean($arg);
$this->protocol->writeLong($arg);
$this->protocol->writeDouble($arg);
foreach($arg as $key=>$val) {
$this->protocol->writeCompositeBegin_h();
$this->protocol->writePairBegin_s($key);
$this->protocol->writePairEnd();
$this->protocol->writeCompositeBegin_h();
$this->protocol->writePairBegin_n($key);
$this->protocol->writePairEnd();
$this->protocol->writeCompositeBegin_a();
$this->protocol->writeCompositeEnd();
function writeArgs($args) {
$this->writeArg($args[$i]);
function createObject($name, $args) {
$this->protocol->createObjectBegin($name);
$this->protocol->createObjectEnd();
$val = $this->getInternalResult();
function referenceObject($name, $args) {
$this->protocol->referenceBegin($name);
$this->protocol->referenceEnd();
$val = $this->getInternalResult();
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->protocol->propertyAccessEnd();
function invokeMethod($object, $method, $args) {
$this->protocol->invokeBegin($object, $method);
$this->protocol->invokeEnd();
$val = $this->getResult();
function unref($object) {
$this->protocol->writeUnref($object);
$ob = $object ? array(&$object, $name) : $name;
// save the current state
$isAsync = $this->isAsync;
$methodCache = $this->methodCache;
$currentArgumentsFormat = $this->currentArgumentsFormat;
// the currentCacheKey is destroyed when the result is received
// so that apply calls are not cached
$res = $arg->getResult(true);
if((!$object && !function_exists($name)) || ($object && !method_exists($object, $name))) throw new <a href="../Java/JavaException.html">JavaException</a>("java.lang.NoSuchMethodException", "$name");
$res = call_user_func_array($ob, $res);
$this->protocol->resultBegin();
$this->protocol->resultEnd();
} catch (JavaException $e) {
$trace = $e->getTraceAsString();
$this->protocol->resultBegin();
$this->protocol->writeException($e->__java, $trace);
$this->protocol->resultEnd();
$this->isAsync = $isAsync;
$this->methodCache = $methodCache;
$this->currentArgumentsFormat = $currentArgumentsFormat;
function cast($object, $type) {
return $this->invokeMethod(0, "castToString", array($object));
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));
return $this->invokeMethod(0, "castToArray", array($object));
case 'O': case 'o': // eh?
throw new java_RuntimeException("$type illegal");
return $this->invokeMethod(0, "getContext", array());
function getSession($args) {
return $this->invokeMethod(0, "getSession", $args);
function getServerName() {
return $this->protocol->getServerName();
Documentation generated on Sun, 16 Mar 2008 19:11:39 +0100 by phpDocumentor 1.4.0a2