PageRenderTime 185ms CodeModel.GetById 2ms app.highlight 168ms RepoModel.GetById 1ms app.codeStats 1ms

/Scones/java/Java.inc

https://github.com/structureddynamics/Scones
PHP | 2084 lines | 2074 code | 1 blank | 9 comment | 268 complexity | a43dcb6247c9040fa963ebe61eea6946 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2# Java.inc -- The PHP/Java Bridge PHP library. Compiled from JavaBridge.inc.
   3# Copyright (C) 2003-2009 Jost Boekemeier.
   4# Distributed under the MIT license, see Options.inc for details.
   5# Customization examples:
   6# define ("JAVA_HOSTS", 9267); define ("JAVA_SERVLET", false);
   7# define ("JAVA_HOSTS", "127.0.0.1:8787");
   8# define ("JAVA_HOSTS", "ssl://my-secure-host.com:8443");
   9# define ("JAVA_SERVLET", "/MyWebApp/servlet.phpjavabridge");
  10# define ("JAVA_PREFER_VALUES", 1);
  11
  12if(!function_exists("java_get_base")) {
  131.0E512;
  14function java_get_base() {
  15$ar=get_required_files();
  16$arLen=sizeof($ar);
  17if($arLen>0) {
  18$thiz=$ar[$arLen-1];
  19return dirname($thiz);
  20} else {
  21return "java";
  22}
  23}
  24function java_truncate($str) {
  25if (strlen($str)>955)
  26return substr($str,0,475).'[...]'.substr($str,-475);
  27return $str;
  28}
  29class java_JavaException extends Exception {
  30function __toString() {return $this->getMessage();}
  31};
  32class java_RuntimeException extends java_JavaException {};
  33class java_IOException extends java_JavaException {};
  34class java_ConnectException extends java_IOException {};
  35class java_IllegalStateException extends java_RuntimeException {};
  36class java_IllegalArgumentException extends java_RuntimeException {
  37function __construct($ob) {
  38parent::__construct("illegal argument: ".gettype($ob));
  39}
  40};
  41function java_autoload_function5($x) {
  42$s=str_replace("_",".",$x);
  43$c=__javaproxy_Client_getClient();
  44if(!($c->invokeMethod(0,"typeExists",array($s)))) return false;
  45$i="class ${x} extends Java {".
  46"static function type(\$sub=null){if(\$sub) \$sub='\$'.\$sub; return java('${s}'.\"\$sub\");}".
  47'function __construct() {$args=func_get_args();'.
  48'array_unshift($args,'."'$s'".'); parent::__construct($args);}}';
  49eval ("$i");
  50return true;
  51}
  52function java_autoload_function($x) {
  53$idx=strrpos($x,"\\"); if (!$idx) return java_autoload_function5($x);
  54$str=str_replace("\\",".",$x);
  55$client=__javaproxy_Client_getClient();
  56if(!($client->invokeMethod(0,"typeExists",array($str)))) return false;
  57$package=substr($x,0,$idx);
  58$name=substr($x,1+$idx);
  59$instance="namespace $package; class ${name} extends \\Java {".
  60"static function type(\$sub=null){if(\$sub) \$sub='\$'.\$sub;return \\java('${str}'.\"\$sub\");}".
  61"static function __callStatic(\$procedure,\$args) {return \\java_invoke(\\java('${str}'),\$procedure,\$args);}".
  62'function __construct() {$args=func_get_args();'.
  63'array_unshift($args,'."'$str'".'); parent::__construct($args);}}';
  64eval ("$instance");
  65return true;
  66}
  67if(!defined("JAVA_DISABLE_AUTOLOAD") && function_exists("spl_autoload_register")) spl_autoload_register("java_autoload_function");
  68function java_autoload($libs=null) {
  69trigger_error('Please use <a href="http://php-java-bridge.sourceforge.net/pjb/webapp.php>tomcat or jee hot deployment</a> instead',E_USER_WARNING);
  70}
  71function java_virtual($path,$return=false) {
  72$req=java_context()->getHttpServletRequest();
  73$req=new java("php.java.servlet.VoidInputHttpServletRequest",$req);
  74$res=java_context()->getHttpServletResponse();
  75$res=new java("php.java.servlet.RemoteHttpServletResponse",$res);
  76$req->getRequestDispatcher($path)->include($req,$res);
  77if ($return) return $res->getBufferContents();
  78echo $res->getBufferContents();
  79return true;
  80}
  81function Java($name) {
  82static $classMap=array();
  83if(array_key_exists($name,$classMap)) return $classMap[$name];
  84return $classMap[$name]=new JavaClass($name);
  85}
  86function java_get_closure() {return java_closure_array(func_get_args());}
  87function java_wrap() {return java_closure_array(func_get_args());}
  88function java_get_values($arg) { return java_values($arg); }
  89function java_get_session() {return java_session_array(func_get_args());}
  90function java_get_context() {return java_context(); }
  91function java_get_server_name() { return java_server_name(); }
  92function java_isnull($value) { return is_null (java_values ($value)); }
  93function java_is_null($value) { return is_null (java_values ($value)); }
  94function java_istrue($value) { return (boolean)(java_values ($value)); }
  95function java_is_true($value) { return (boolean)(java_values ($value)); }
  96function java_isfalse($value) { return !(java_values ($value)); }
  97function java_is_false($value) { return !(java_values ($value)); }
  98function java_set_encoding($enc) { return java_set_file_encoding ($enc); }
  99function java_call_with_continuation($kontinuation=null) {
 100if (java_getHeader("X_JAVABRIDGE_INCLUDE",$_SERVER) && !java_getHeader("X_JAVABRIDGE_INCLUDE_ONLY",$_SERVER)) {
 101if (is_null($kontinuation))
 102java_context()->call(java_closure());
 103elseif (is_string($kontinuation))
 104java_context()->call(call_user_func($kontinuation));
 105elseif ($kontinuation instanceof java_JavaType)
 106java_context()->call($kontinuation);
 107else
 108java_context()->call(java_closure($kontinuation));
 109}
 110}
 111function java_defineHostFromInitialQuery($java_base) {
 112if($java_base!="java") {
 113$url=parse_url($java_base);
 114if(isset($url["scheme"]) && ($url["scheme"]=="http" || $url["scheme"]=="https")) {
 115$scheme=$url["scheme"]=="https" ? "ssl://" : "";
 116$host=$url["host"];
 117$port=$url["port"];
 118$path=$url["path"];
 119define ("JAVA_HOSTS","${scheme}${host}:${port}");
 120$dir=dirname($path);
 121define ("JAVA_SERVLET","$dir/servlet.phpjavabridge");
 122return true;
 123}
 124}
 125return false;
 126}
 127define("JAVA_PEAR_VERSION","6.2.1");
 128if(!defined("JAVA_SEND_SIZE"))
 129define("JAVA_SEND_SIZE",8192);
 130if(!defined("JAVA_RECV_SIZE"))
 131define("JAVA_RECV_SIZE",8192);
 132if(!defined("JAVA_HOSTS")) {
 133if(!java_defineHostFromInitialQuery(java_get_base())) {
 134if ($java_ini=get_cfg_var("java.hosts")) define("JAVA_HOSTS",$java_ini);
 135else define("JAVA_HOSTS","127.0.0.1:8080");
 136}
 137}
 138if(!defined("JAVA_SERVLET")) {
 139if (!(($java_ini=get_cfg_var("java.servlet"))===false)) define("JAVA_SERVLET",$java_ini);
 140else define("JAVA_SERVLET",1);
 141}
 142if(!defined("JAVA_LOG_LEVEL"))
 143if (!(($java_ini=get_cfg_var("java.log_level"))===false)) define("JAVA_LOG_LEVEL",(int)$java_ini);
 144else define("JAVA_LOG_LEVEL",null);
 145if (!defined("JAVA_PREFER_VALUES"))
 146if ($java_ini=get_cfg_var("java.prefer_values")) define("JAVA_PREFER_VALUES",$java_ini);
 147else define("JAVA_PREFER_VALUES",0);
 148class java_SimpleFactory {
 149public $client;
 150function java_SimpleFactory($client) {
 151$this->client=$client;
 152}
 153function getProxy($result,$signature,$exception,$wrap) {
 154return $result;
 155}
 156function checkResult($result) {
 157}
 158}
 159class java_ProxyFactory extends java_SimpleFactory {
 160function create($result,$signature) {
 161return new java_JavaProxy($result,$signature);
 162}
 163function createInternal($proxy) {
 164return new java_InternalJava($proxy);
 165}
 166function getProxy($result,$signature,$exception,$wrap) {
 167$proxy=$this->create($result,$signature);
 168if($wrap) $proxy=$this->createInternal($proxy);
 169return $proxy;
 170}
 171}
 172class java_ArrayProxyFactory extends java_ProxyFactory {
 173function create($result,$signature) {
 174return new java_ArrayProxy($result,$signature);
 175}
 176}
 177class java_IteratorProxyFactory extends java_ProxyFactory {
 178function create($result,$signature) {
 179return new java_IteratorProxy($result,$signature);
 180}
 181}
 182class java_ExceptionProxyFactory extends java_SimpleFactory {
 183function create($result,$signature) {
 184return new java_ExceptionProxy($result,$signature);
 185}
 186function getProxy($result,$signature,$exception,$wrap) {
 187$proxy=$this->create($result,$signature);
 188if($wrap) $proxy=new java_InternalException($proxy,$exception);
 189return $proxy;
 190}
 191}
 192class java_ThrowExceptionProxyFactory extends java_ExceptionProxyFactory {
 193function getProxy($result,$signature,$exception,$wrap) {
 194$proxy=$this->create($result,$signature);
 195$proxy=new java_InternalException($proxy,$exception);
 196return $proxy;
 197}
 198function checkResult($result) {
 199if (JAVA_PREFER_VALUES || ($result->__hasDeclaredExceptions=='T'))
 200throw $result;
 201else {
 202trigger_error("Unchecked exception detected: ".java_truncate($result->__toString()),E_USER_WARNING);
 203}
 204}
 205}
 206class java_CacheEntry {
 207public $fmt,$signature,$factory,$java;
 208public $resultVoid;
 209function java_CacheEntry($fmt,$signature,$factory,$resultVoid) {
 210$this->fmt=$fmt;
 211$this->signature=$signature;
 212$this->factory=$factory;
 213$this->resultVoid=$resultVoid;
 214}
 215}
 216class java_Arg {
 217public $client;
 218public $exception;
 219public $factory,$val;
 220public $signature;
 221function java_Arg($client) {
 222$this->client=$client;
 223$this->factory=$client->simpleFactory;
 224}
 225function linkResult(&$val) {
 226$this->val=&$val;
 227}
 228function setResult($val) {
 229$this->val=&$val;
 230}
 231function getResult($wrap) {
 232$rc=$this->factory->getProxy($this->val,$this->signature,$this->exception,$wrap);
 233$factory=$this->factory;
 234$this->factory=$this->client->simpleFactory;
 235$factory->checkResult($rc);
 236return $rc;
 237}
 238function setFactory($factory) {
 239$this->factory=$factory;
 240}
 241function setException($string) {
 242$this->exception=$string;
 243}
 244function setVoidSignature() {
 245$this->signature="@V";
 246$key=$this->client->currentCacheKey;
 247if($key && $key[0]!='~') {
 248$this->client->currentArgumentsFormat[6]="3";
 249$cacheEntry=new java_CacheEntry($this->client->currentArgumentsFormat,$this->signature,$this->factory,true);
 250$this->client->methodCache[$key]=$cacheEntry;
 251}
 252}
 253function setSignature($signature) {
 254$this->signature=$signature;
 255$key=$this->client->currentCacheKey;
 256if($key && $key[0]!='~') {
 257$cacheEntry=new java_CacheEntry($this->client->currentArgumentsFormat,$this->signature,$this->factory,false);
 258$this->client->methodCache[$key]=$cacheEntry;
 259}
 260}
 261}
 262class java_CompositeArg extends java_Arg {
 263public $parentArg;
 264public $idx;
 265public $type;
 266public $counter;
 267function java_CompositeArg($client,$type) {
 268parent::java_Arg($client);
 269$this->type=$type;
 270$this->val=array();
 271$this->counter=0;
 272}
 273function setNextIndex() {
 274$this->idx=$this->counter++;
 275}
 276function setIndex($val) {
 277$this->idx=$val;
 278}
 279function linkResult(&$val) {
 280$this->val[$this->idx]=&$val;
 281}
 282function setResult($val) {
 283$this->val[$this->idx]=$this->factory->getProxy($val,$this->signature,$this->exception,true);
 284$this->factory=$this->client->simpleFactory;
 285}
 286}
 287class java_ApplyArg extends java_CompositeArg {
 288public $m,$p,$v,$n;
 289function java_ApplyArg($client,$type,$m,$p,$v,$n) {
 290parent::java_CompositeArg($client,$type);
 291$this->m=$m;
 292$this->p=$p;
 293$this->v=$v;
 294$this->n=$n;
 295}
 296}
 297class java_Client {
 298public $RUNTIME;
 299public $result,$exception;
 300public $parser;
 301public $simpleArg,$compositeArg;
 302public $simpleFactory,
 303$proxyFactory,$iteratorProxyFacroty,
 304$arrayProxyFactory,$exceptionProxyFactory,$throwExceptionProxyFactory;
 305public $arg;
 306public $asyncCtx,$cancelProxyCreationCounter;
 307public $globalRef;
 308public $stack;
 309public $defaultCache=array(),$asyncCache=array(),$methodCache;
 310public $isAsync=0;
 311public $currentCacheKey,$currentArgumentsFormat;
 312public $cachedJavaPrototype;
 313public $sendBuffer,$preparedToSendBuffer;
 314public $inArgs;
 315function java_Client() {
 316$this->RUNTIME=array();
 317$this->RUNTIME["NOTICE"]='***USE echo java_inspect(jVal) OR print_r(java_values(jVal)) TO SEE THE CONTENTS OF THIS JAVA OBJECT!***';
 318$this->parser=new java_Parser($this);
 319$this->protocol=new java_Protocol($this);
 320$this->simpleFactory=new java_SimpleFactory($this);
 321$this->proxyFactory=new java_ProxyFactory($this);
 322$this->arrayProxyFactory=new java_ArrayProxyFactory($this);
 323$this->iteratorProxyFactory=new java_IteratorProxyFactory($this);
 324$this->exceptionProxyFactory=new java_ExceptionProxyFactory($this);
 325$this->throwExceptionProxyFactory=new java_ThrowExceptionProxyFactory($this);
 326$this->cachedJavaPrototype=new java_JavaProxyProxy($this);
 327$this->simpleArg=new java_Arg($this);
 328$this->globalRef=new java_GlobalRef();
 329$this->asyncCtx=$this->cancelProxyCreationCounter=0;
 330$this->methodCache=$this->defaultCache;
 331$this->inArgs=false;
 332}
 333function read($size) {
 334return $this->protocol->read($size);
 335}
 336function setDefaultHandler() {
 337$this->methodCache=$this->defaultCache;
 338}
 339function setAsyncHandler() {
 340$this->methodCache=$this->asyncCache;
 341}
 342function handleRequests() {
 343$tail_call=false;
 344do {
 345$this->stack=array($this->arg=$this->simpleArg);
 346$this->idx=0;
 347$this->parser->parse();
 348if((count($this->stack)) > 1) {
 349$arg=array_pop($this->stack);
 350$this->apply($arg);
 351$tail_call=true;
 352} else {
 353$tail_call=false;
 354}
 355$this->stack=null;
 356} while($tail_call);
 357return 1;
 358}
 359function getWrappedResult($wrap) {
 360return $this->simpleArg->getResult($wrap);
 361}
 362function getInternalResult() {
 363return $this->getWrappedResult(false);
 364}
 365function getResult() {
 366return $this->getWrappedResult(true);
 367}
 368function getProxyFactory($type) {
 369switch($type[0]) {
 370case 'E':
 371$factory=$this->exceptionProxyFactory;
 372break;
 373case 'C':
 374$factory=$this->iteratorProxyFactory;
 375break;
 376case 'A':
 377$factory=$this->arrayProxyFactory;
 378break;
 379default:
 380case 'O':
 381$factory=$this->proxyFactory;
 382}
 383return $factory;
 384}
 385function link(&$arg,&$newArg) {
 386$arg->linkResult($newArg->val);
 387$newArg->parentArg=$arg;
 388}
 389function getExact($str) {
 390return hexdec($str);
 391}
 392function getInexact($str) {
 393$val=null;
 394sscanf($str,"%e",$val);
 395return $val;
 396}
 397function begin($name,$st) {
 398$arg=$this->arg;
 399switch($name[0]) {
 400case 'A':
 401$object=$this->globalRef->get($this->getExact($st['v']));
 402$newArg=new java_ApplyArg($this,'A',
 403$this->parser->getData($st['m']),
 404$this->parser->getData($st['p']),
 405$object,
 406$this->getExact($st['n']));
 407$this->link($arg,$newArg);
 408array_push($this->stack,$this->arg=$newArg);
 409break;
 410case 'X':
 411$newArg=new java_CompositeArg($this,$st['t']);
 412$this->link($arg,$newArg);
 413array_push($this->stack,$this->arg=$newArg);
 414break;
 415case 'P':
 416if($arg->type=='H') {
 417$s=$st['t'];
 418if($s[0]=='N') {
 419$arg->setIndex($this->getExact($st['v']));
 420} else {
 421$arg->setIndex($this->parser->getData($st['v']));
 422}
 423} else {
 424$arg->setNextIndex();
 425}
 426break;
 427case 'S':
 428$arg->setResult($this->parser->getData($st['v']));
 429break;
 430case 'B':
 431$s=$st['v'];
 432$arg->setResult($s[0]=='T');
 433break;
 434case 'L':
 435$sign=$st['p'];
 436$val=$this->getExact($st['v']);
 437if($sign[0]=='A') $val*=-1;
 438$arg->setResult($val);
 439break;
 440case 'D':
 441$arg->setResult($this->getInexact($st['v']));
 442break;
 443case 'V':
 444if ($st['n']!='T') {
 445$arg->setVoidSignature();
 446}
 447case 'N':
 448$arg->setResult(null);
 449break;
 450case 'F':
 451break;
 452case 'O':
 453$arg->setFactory($this->getProxyFactory($st['p']));
 454$arg->setResult($this->asyncCtx=$this->getExact($st['v']));
 455if($st['n']!='T') $arg->setSignature($st['m']);
 456break;
 457case 'E':
 458$arg->setFactory($this->throwExceptionProxyFactory);
 459$arg->setException($st['m']);
 460$arg->setResult($this->asyncCtx=$this->getExact($st['v']));
 461break;
 462default:
 463$this->parser->parserError();
 464}
 465}
 466function end($name) {
 467switch($name[0]) {
 468case 'X':
 469$frame=array_pop($this->stack);
 470$this->arg=$frame->parentArg;
 471break;
 472}
 473}
 474function createParserString() {
 475return new java_ParserString();
 476}
 477function writeArg($arg) {
 478if(is_string($arg)) {
 479$this->protocol->writeString($arg);
 480} else if(is_object($arg)) {
 481if ((!$arg instanceof java_JavaType)) {
 482error_log((string)new java_IllegalArgumentException($arg));
 483trigger_error("argument '".get_class($arg)."' is not a Java object,using NULL instead",E_USER_WARNING);
 484$this->protocol->writeObject(null);
 485} else {
 486$this->protocol->writeObject($arg->__java);
 487}
 488} else if(is_null($arg)) {
 489$this->protocol->writeObject(null);
 490} else if(is_bool($arg)) {
 491$this->protocol->writeBoolean($arg);
 492} else if(is_integer($arg)) {
 493$this->protocol->writeLong($arg);
 494} else if(is_float($arg)) {
 495$this->protocol->writeDouble($arg);
 496} else if(is_array($arg)) {
 497$wrote_begin=false;
 498foreach($arg as $key=>$val) {
 499if(is_string($key)) {
 500if(!$wrote_begin) {
 501$wrote_begin=1;
 502$this->protocol->writeCompositeBegin_h();
 503}
 504$this->protocol->writePairBegin_s($key);
 505$this->writeArg($val);
 506$this->protocol->writePairEnd();
 507} else {
 508if(!$wrote_begin) {
 509$wrote_begin=1;
 510$this->protocol->writeCompositeBegin_h();
 511}
 512$this->protocol->writePairBegin_n($key);
 513$this->writeArg($val);
 514$this->protocol->writePairEnd();
 515}
 516}
 517if(!$wrote_begin) {
 518$this->protocol->writeCompositeBegin_a();
 519}
 520$this->protocol->writeCompositeEnd();
 521}
 522}
 523function writeArgs($args) {
 524$this->inArgs=true;
 525$n=count($args);
 526for($i=0; $i<$n; $i++) {
 527$this->writeArg($args[$i]);
 528}
 529$this->inArgs=false;
 530}
 531function createObject($name,$args) {
 532$this->protocol->createObjectBegin($name);
 533$this->writeArgs($args);
 534$this->protocol->createObjectEnd();
 535$val=$this->getInternalResult();
 536return $val;
 537}
 538function referenceObject($name,$args) {
 539$this->protocol->referenceBegin($name);
 540$this->writeArgs($args);
 541$this->protocol->referenceEnd();
 542$val=$this->getInternalResult();
 543return $val;
 544}
 545function getProperty($object,$property) {
 546$this->protocol->propertyAccessBegin($object,$property);
 547$this->protocol->propertyAccessEnd();
 548return $this->getResult();
 549}
 550function setProperty($object,$property,$arg) {
 551$this->protocol->propertyAccessBegin($object,$property);
 552$this->writeArg($arg);
 553$this->protocol->propertyAccessEnd();
 554$this->getResult();
 555}
 556function invokeMethod($object,$method,$args) {
 557$this->protocol->invokeBegin($object,$method);
 558$this->writeArgs($args);
 559$this->protocol->invokeEnd();
 560$val=$this->getResult();
 561return $val;
 562}
 563function unref($object) {
 564if (isset($this->protocol)) $this->protocol->writeUnref($object);
 565}
 566function apply($arg) {
 567$name=$arg->p;
 568$object=$arg->v;
 569$ob=($object==null) ? $name : array(&$object,$name);
 570$isAsync=$this->isAsync;
 571$methodCache=$this->methodCache;
 572$currentArgumentsFormat=$this->currentArgumentsFormat;
 573try {
 574$res=$arg->getResult(true);
 575if((($object==null) && !function_exists($name)) || (!($object==null) && !method_exists($object,$name))) throw new JavaException("java.lang.NoSuchMethodError","$name");
 576$res=call_user_func_array($ob,$res);
 577if (is_object($res) && (!($res instanceof java_JavaType))) {
 578trigger_error("object returned from $name() is not a Java object",E_USER_WARNING);
 579$this->protocol->invokeBegin(0,"makeClosure");
 580$this->protocol->writeULong($this->globalRef->add($res));
 581$this->protocol->invokeEnd();
 582$res=$this->getResult();
 583}
 584$this->protocol->resultBegin();
 585$this->writeArg($res);
 586$this->protocol->resultEnd();
 587} catch (JavaException $e) {
 588$trace=$e->getTraceAsString();
 589$this->protocol->resultBegin();
 590$this->protocol->writeException($e->__java,$trace);
 591$this->protocol->resultEnd();
 592} catch(Exception $ex) {
 593error_log($ex->__toString());
 594trigger_error("Unchecked exception detected in callback",E_USER_ERROR);
 595die (1);
 596}
 597$this->isAsync=$isAsync;
 598$this->methodCache=$methodCache;
 599$this->currentArgumentsFormat=$currentArgumentsFormat;
 600}
 601function cast($object,$type) {
 602switch($type[0]) {
 603case 'S': case 's':
 604return $this->invokeMethod(0,"castToString",array($object));
 605case 'B': case 'b':
 606return $this->invokeMethod(0,"castToBoolean",array($object));
 607case 'L': case 'I': case 'l': case 'i':
 608return $this->invokeMethod(0,"castToExact",array($object));
 609case 'D': case 'd': case 'F': case 'f':
 610return $this->invokeMethod(0,"castToInExact",array($object));
 611case 'N': case 'n':
 612return null;
 613case 'A': case 'a':
 614return $this->invokeMethod(0,"castToArray",array($object));
 615case 'O': case 'o':
 616return $object;
 617default:
 618throw new java_RuntimeException("$type illegal");
 619}
 620}
 621function getContext() {
 622static $cache=null;
 623if (!is_null($cache)) return $cache;
 624return $cache=$this->invokeMethod(0,"getContext",array());
 625}
 626function getSession($args) {
 627return $this->invokeMethod(0,"getSession",$args);
 628}
 629function getServerName() {
 630static $cache=null;
 631if (!is_null($cache)) return $cache;
 632return $cache=$this->protocol->getServerName();
 633}
 634}
 635function java_shutdown() {
 636global $java_initialized;
 637if (!$java_initialized) return;
 638if (session_id()) session_write_close();
 639$client=__javaproxy_Client_getClient();
 640if (!isset($client->protocol) || $client->inArgs) return;
 641if ($client->preparedToSendBuffer)
 642$client->sendBuffer.=$client->preparedToSendBuffer;
 643$client->sendBuffer.=$client->protocol->getKeepAlive();
 644$client->protocol->flush();
 645$client->protocol->keepAlive();
 646}
 647register_shutdown_function("java_shutdown");
 648class java_GlobalRef {
 649public $map;
 650function java_GlobalRef() {
 651$this->map=array();
 652}
 653function add($object) {
 654if(is_null($object)) return 0;
 655return array_push($this->map,$object);
 656}
 657function get($id) {
 658if(!$id) return 0;
 659return $this->map[--$id];
 660}
 661}
 662class java_NativeParser {
 663public $parser,$handler;
 664public $level,$event;
 665public $buf;
 666function java_NativeParser($handler) {
 667$this->handler=$handler;
 668$this->parser=xml_parser_create();
 669xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);
 670xml_set_object($this->parser,$this);
 671xml_set_element_handler($this->parser,"begin","end");
 672xml_parse($this->parser,"<F>");
 673$this->level=0;
 674}
 675function begin($parser,$name,$param) {
 676$this->event=true;
 677switch($name) {
 678case 'X': case 'A': $this->level+=1;
 679}
 680$this->handler->begin($name,$param);
 681}
 682function end($parser,$name) {
 683$this->handler->end($name);
 684switch($name) {
 685case 'X': case 'A': $this->level-=1;
 686}
 687}
 688function getData($str) {
 689return base64_decode($str);
 690}
 691function parse() {
 692do {
 693$this->event=false;
 694$buf=$this->buf=$this->handler->read(JAVA_RECV_SIZE);
 695$len=strlen($buf);
 696if(!xml_parse($this->parser,$buf,$len==0)) {
 697$this->handler->protocol->handler->shutdownBrokenConnection(
 698sprintf("protocol error: %s,%s at col %d. Check the back end log for OutOfMemoryErrors.",
 699$buf,
 700xml_error_string(xml_get_error_code($this->parser)),
 701xml_get_current_column_number($this->parser)));
 702}
 703} while(!$this->event || $this->level>0);
 704}
 705function parserError() {
 706$this->handler->protocol->handler->shutdownBrokenConnection(
 707sprintf("protocol error: %s. Check the back end log for details.",$this->buf));
 708}
 709}
 710class java_Parser {
 711public $parser;
 712function java_Parser($handler) {
 713if(function_exists("xml_parser_create")) {
 714$this->parser=new java_NativeParser($handler);
 715$handler->RUNTIME["PARSER"]="NATIVE";
 716} else {
 717$this->parser=new java_SimpleParser($handler);
 718$handler->RUNTIME["PARSER"]="SIMPLE";
 719}
 720}
 721function parse() {
 722$this->parser->parse();
 723}
 724function getData($str) {
 725return $this->parser->getData($str);
 726}
 727function parserError() {
 728$this->parser->parserError();
 729}
 730}
 731function java_getHeader($name,$array) {
 732if (array_key_exists($name,$array)) return $array[$name];
 733$name="HTTP_$name";
 734if (array_key_exists($name,$array)) return $array[$name];
 735return null;
 736}
 737function java_checkCliSapi() {
 738$sapi=substr(php_sapi_name(),0,3);
 739return ((($sapi=='cgi') && !get_cfg_var("java.session")) || ($sapi=='cli'));
 740}
 741function java_getCompatibilityOption($client) {
 742static $compatibility=null;
 743if ($compatibility) return $compatibility;
 744$compatibility=$client->RUNTIME["PARSER"]=="NATIVE"
 745? (0103-JAVA_PREFER_VALUES)
 746: (0100+JAVA_PREFER_VALUES);
 747if(is_int(JAVA_LOG_LEVEL)) {
 748$compatibility |=128 | (7 & JAVA_LOG_LEVEL)<<2;
 749}
 750$compatibility=chr ($compatibility);
 751return $compatibility;
 752}
 753class java_EmptyChannel {
 754protected $handler;
 755private $res;
 756function java_EmptyChannel($handler) {
 757$this->handler=$handler;
 758}
 759function shutdownBrokenConnection () {}
 760function fwrite($data) {
 761return $this->handler->fwrite($data);
 762}
 763function fread($size) {
 764return $this->handler->fread($size);
 765}
 766function getKeepAliveA() {
 767return "<F p=\"A\" />";
 768}
 769function getKeepAliveE() {
 770return "<F p=\"E\" />";
 771}
 772function getKeepAlive() {
 773return $this->getKeepAliveE();
 774}
 775function error() {
 776trigger_error("An unchecked exception occured during script execution. Please check the server log files for details.",E_USER_ERROR);
 777}
 778function checkA($peer) {
 779$val=$this->res[6];
 780if ($val !='A') fclose($peer);
 781if ($val !='A' && $val !='E') {
 782$this->error();
 783}
 784}
 785function checkE() {
 786$val=$this->res[6];
 787if ($val !='E') {
 788$this->error();
 789}
 790}
 791function keepAliveS() {
 792$this->res=$this->fread(10);
 793}
 794function keepAliveSC() {
 795$this->res=$this->fread(10);
 796$this->fwrite("");
 797$this->fread(JAVA_RECV_SIZE);
 798}
 799function keepAliveH() {
 800$this->res=$this->handler->read(10);
 801}
 802function keepAlive() {
 803$this->keepAliveH();
 804$this->checkE();
 805}
 806}
 807abstract class java_SocketChannel extends java_EmptyChannel {
 808public $peer,$host;
 809function java_SocketChannel($peer,$host) {
 810$this->peer=$peer;
 811$this->host=$host;
 812}
 813function fwrite($data) {
 814return fwrite($this->peer,$data);
 815}
 816function fread($size) {
 817return fread($this->peer,$size);
 818}
 819function shutdownBrokenConnection () {
 820fclose($this->peer);
 821}
 822}
 823class java_SocketChannelP extends java_SocketChannel {
 824function getKeepAlive() {return $this->getKeepAliveA();}
 825function keepAlive() { $this->keepAliveS(); $this->checkA($this->peer); }
 826}
 827class java_ChunkedSocketChannel extends java_SocketChannel {
 828function fwrite($data) {
 829$len=dechex(strlen($data));
 830return fwrite($this->peer,"${len}\r\n${data}\r\n");
 831}
 832function fread($size) {
 833$length=hexdec(fgets($this->peer,JAVA_RECV_SIZE));
 834$data="";
 835while ($length > 0) {
 836$str=fread($this->peer,$length);
 837if (feof ($this->peer)) return null;
 838$length -=strlen($str);
 839$data .=$str;
 840}
 841fgets($this->peer,3);
 842return $data;
 843}
 844function keepAlive() { $this->keepAliveSC(); $this->checkE(); fclose ($this->peer); }
 845}
 846class java_SocketHandler {
 847public $protocol,$channel;
 848function java_SocketHandler($protocol,$channel) {
 849$this->protocol=$protocol;
 850$this->channel=$channel;
 851}
 852function write($data) {
 853return $this->channel->fwrite($data);
 854}
 855function fwrite($data) {return $this->write($data);}
 856function read($size) {
 857return $this->channel->fread($size);
 858}
 859function fread($size) {return $this->read($size);}
 860function redirect() {}
 861function getKeepAlive() {
 862return $this->channel->getKeepAlive();
 863}
 864function keepAlive() {
 865$this->channel->keepAlive();
 866}
 867function dieWithBrokenConnection($msg) {
 868unset($this->protocol->client->protocol);
 869trigger_error ($msg?$msg:"unknown error: please see back end log for details",E_USER_ERROR);
 870}
 871function shutdownBrokenConnection ($msg) {
 872$this->channel->shutdownBrokenConnection();
 873$this->dieWithBrokenConnection($msg);
 874}
 875}
 876class java_SimpleHttpHandler extends java_SocketHandler {
 877public $headers,$cookies;
 878public $context,$ssl,$port;
 879public $host;
 880function createChannel() {
 881$channelName=java_getHeader("X_JAVABRIDGE_REDIRECT",$_SERVER);
 882$context=java_getHeader("X_JAVABRIDGE_CONTEXT",$_SERVER);
 883$len=strlen($context);
 884$len0=java_getCompatibilityOption($this->protocol->client);
 885$len1=chr($len&0xFF); $len>>=8;
 886$len2=chr($len&0xFF);
 887$this->channel=new java_EmptyChannel($this);
 888$this->channel=$this->getChannel($channelName);
 889$this->protocol->socketHandler=new java_SocketHandler($this->protocol,$this->channel);
 890$this->protocol->write("\177${len0}${len1}${len2}${context}");
 891$this->context=sprintf("X_JAVABRIDGE_CONTEXT: %s\r\n",$context);
 892$this->protocol->handler=$this->protocol->socketHandler;
 893$this->protocol->handler->write($this->protocol->client->sendBuffer)
 894or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
 895$this->protocol->client->sendBuffer=null;
 896$this->protocol->handler->read(1)
 897or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
 898}
 899function java_SimpleHttpHandler($protocol,$ssl,$host,$port) {
 900$this->cookies=array();
 901$this->protocol=$protocol;
 902$this->ssl=$ssl;
 903$this->host=$host;
 904$this->port=$port;
 905$this->createChannel();
 906}
 907function getCookies() {
 908$str="";
 909$first=true;
 910foreach($_COOKIE as $k=> $v) {
 911$str .=($first ? "Cookie: $k=$v":"; $k=$v");
 912$first=false;
 913}
 914if(!$first) $str .="\r\n";
 915return $str;
 916}
 917function getContextFromCgiEnvironment() {
 918$ctx=java_getHeader('X_JAVABRIDGE_CONTEXT',$_SERVER);
 919return $ctx;
 920}
 921function getContext() {
 922static $context=null;
 923if($context) return $context;
 924$ctx=$this->getContextFromCgiEnvironment();
 925$context="";
 926if($ctx) {
 927$context=sprintf("X_JAVABRIDGE_CONTEXT: %s\r\n",$ctx);
 928}
 929return $context;
 930}
 931function getWebAppInternal() {
 932$context=$this->protocol->webContext;
 933if(isset($context)) return $context;
 934return (JAVA_SERVLET=="User" &&
 935array_key_exists('PHP_SELF',$_SERVER) &&
 936array_key_exists('HTTP_HOST',$_SERVER))
 937? $_SERVER['PHP_SELF']."javabridge"
 938: null;
 939}
 940function getWebApp() {
 941$context=$this->getWebAppInternal();
 942if(is_null($context)) $context=JAVA_SERVLET;
 943if(is_null($context) || $context[0]!="/")
 944$context="/JavaBridge/JavaBridge.phpjavabridge";
 945return $context;
 946}
 947function write($data) {
 948return $this->protocol->socketHandler->write($data);
 949}
 950function doSetCookie($key,$val,$path) {
 951$path=trim($path);
 952$webapp=$this->getWebAppInternal(); if(!$webapp) $path="/";
 953setcookie($key,$val,0,$path);
 954}
 955function read($size) {
 956return $this->protocol->socketHandler->read($size);
 957}
 958function getChannel($channelName) {
 959$errstr=null; $errno=null;
 960$peer=pfsockopen($this->host,$channelName,$errno,$errstr,20);
 961if (!$peer) throw new java_IllegalStateException("No ContextServer for {$this->host}:{$channelName}. Error: $errstr ($errno)\n");
 962stream_set_timeout($peer,-1);
 963return new java_SocketChannelP($peer,$this->host);
 964}
 965function keepAlive() {
 966parent::keepAlive();
 967}
 968function redirect() {}
 969}
 970class java_SimpleHttpTunnelHandler extends java_SimpleHttpHandler {
 971public $socket;
 972protected $hasContentLength=false;
 973function createSimpleChannel () {
 974$this->channel=new java_EmptyChannel($this);
 975}
 976function createChannel() {
 977$this->createSimpleChannel();
 978}
 979function shutdownBrokenConnection ($msg) {
 980fclose($this->socket);
 981$this->dieWithBrokenConnection($msg);
 982}
 983function checkSocket($socket,&$errno,&$errstr) {
 984if (!$socket) {
 985$msg="Could not connect to the JEE server {$this->ssl}{$this->host}:{$this->port}. Please start it.";
 986$msg.=java_checkCliSapi()
 987?" Or define('JAVA_HOSTS',9267); define('JAVA_SERVLET',false); before including 'Java.inc' and try again. Error message: $errstr ($errno)\n"
 988:" Error message: $errstr ($errno)\n";
 989throw new java_ConnectException($msg);
 990}
 991}
 992function open() {
 993$errno=null; $errstr=null;
 994$socket=fsockopen("{$this->ssl}{$this->host}",$this->port,$errno,$errstr,20);
 995$this->checkSocket($socket,$errno,$errstr);
 996stream_set_timeout($socket,-1);
 997$this->socket=$socket;
 998}
 999function fread($size) {
1000$length=hexdec(fgets($this->socket,JAVA_RECV_SIZE));
1001$data="";
1002while ($length > 0) {
1003$str=fread($this->socket,$length);
1004if (feof ($this->socket)) return null;
1005$length -=strlen($str);
1006$data .=$str;
1007}
1008fgets($this->socket,3);
1009return $data;
1010}
1011function fwrite($data) {
1012$len=dechex(strlen($data));
1013return fwrite($this->socket,"${len}\r\n${data}\r\n");
1014}
1015function close() {
1016fwrite($this->socket,"0\r\n\r\n");
1017fgets($this->socket,JAVA_RECV_SIZE);
1018fgets($this->socket,3);
1019fclose($this->socket);
1020}
1021function java_SimpleHttpTunnelHandler($protocol,$ssl,$host,$port) {
1022parent::java_SimpleHttpHandler($protocol,$ssl,$host,$port);
1023$this->open();
1024}
1025function read($size) {
1026if(is_null($this->headers)) $this->parseHeaders();
1027if (isset($this->headers["http_error"])) {
1028if (isset($this->headers["transfer_chunked"])) {
1029$str=$this->fread(JAVA_RECV_SIZE);
1030} elseif (isset($this->headers['content_length'])) {
1031$len=$this->headers['content_length'];
1032for($str=fread($this->socket,$len); strlen($str)<$len; $str.=fread($this->socket,$len-strlen($str)))
1033if (feof ($this->socket)) break;
1034} else {
1035$str=fread($this->socket,JAVA_RECV_SIZE);
1036}
1037$this->shutdownBrokenConnection($str);
1038}
1039return $this->fread(JAVA_RECV_SIZE);
1040}
1041function getBodyFor ($compat,$data) {
1042$len=dechex(2+strlen($data));
1043return "Cache-Control: no-cache\r\nPragma: no-cache\r\nTransfer-Encoding: chunked\r\n\r\n${len}\r\n\177${compat}${data}\r\n";
1044}
1045function write($data) {
1046$compat=java_getCompatibilityOption($this->protocol->client);
1047$this->headers=null;
1048$socket=$this->socket;
1049$webapp=$this->getWebApp();
1050$cookies=$this->getCookies();
1051$context=$this->getContext();
1052$res="PUT ";
1053$res .=$webapp;
1054$res .=" HTTP/1.1\r\n";
1055$res .="Host: {$this->host}:{$this->port}\r\n";
1056$res .=$context;
1057$res .=$cookies;
1058$res .=$this->getBodyFor($compat,$data);
1059$count=fwrite($socket,$res) or $this->shutdownBrokenConnection("Broken connection handle");
1060fflush($socket) or $this->shutdownBrokenConnection("Broken connection handle");
1061return $count;
1062}
1063function parseHeaders() {
1064$this->headers=array();
1065$line=trim(fgets($this->socket,JAVA_RECV_SIZE));
1066$ar=explode (" ",$line);
1067$code=((int)$ar[1]);
1068if ($code !=200) $this->headers["http_error"]=$code;
1069while (($str=trim(fgets($this->socket,JAVA_RECV_SIZE)))) {
1070if($str[0]=='X') {
1071if(!strncasecmp("X_JAVABRIDGE_REDIRECT",$str,21)) {
1072$this->headers["redirect"]=trim(substr($str,22));
1073} else if(!strncasecmp("X_JAVABRIDGE_CONTEXT",$str,20)) {
1074$this->headers["context"]=trim(substr($str,21));
1075}
1076} else if($str[0]=='S') {
1077if(!strncasecmp("SET-COOKIE",$str,10)) {
1078$str=substr($str,12);
1079$this->cookies[]=$str;
1080$ar=explode(";",$str);
1081$cookie=explode("=",$ar[0]);
1082$path="";
1083if(isset($ar[1])) $p=explode("=",$ar[1]);
1084if(isset($p)) $path=$p[1];
1085$this->doSetCookie($cookie[0],$cookie[1],$path);
1086}
1087} else if($str[0]=='C') {
1088if(!strncasecmp("CONTENT-LENGTH",$str,14)) {
1089$this->headers["content_length"]=trim(substr($str,15));
1090$this->hasContentLength=true;
1091} else if(!strncasecmp("CONNECTION",$str,10) && !strncasecmp("close",trim(substr($str,11)),5)) {
1092$this->headers["connection_close"]=true;
1093}
1094} else if($str[0]=='T') {
1095if(!strncasecmp("TRANSFER-ENCODING",$str,17) && !strncasecmp("chunked",trim(substr($str,18)),7)) {
1096$this->headers["transfer_chunked"]=true;
1097}
1098}
1099}
1100}
1101function getSimpleChannel() {
1102return new java_ChunkedSocketChannel($this->socket,$this->protocol,$this->host);
1103}
1104function redirect() {
1105$this->isRedirect=isset($this->headers["redirect"]);
1106if ($this->isRedirect)
1107$channelName=$this->headers["redirect"];
1108$context=$this->headers["context"];
1109$len=strlen($context);
1110$len0=chr(0xFF);
1111$len1=chr($len&0xFF); $len>>=8;
1112$len2=chr($len&0xFF);
1113if ($this->isRedirect) {
1114$this->protocol->socketHandler=new java_SocketHandler($this->protocol,$this->getChannel($channelName));
1115$this->protocol->write("\177${len0}${len1}${len2}${context}");
1116$this->context=sprintf("X_JAVABRIDGE_CONTEXT: %s\r\n",$context);
1117$this->close ();
1118$this->protocol->handler=$this->protocol->socketHandler;
1119$this->protocol->handler->write($this->protocol->client->sendBuffer)
1120or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
1121$this->protocol->client->sendBuffer=null;
1122$this->protocol->handler->read(1)
1123or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
1124} else {
1125$this->protocol->handler=$this->protocol->socketHandler=new java_SocketHandler($this->protocol,$this->getSimpleChannel());
1126}
1127}
1128}
1129class java_HttpTunnelHandler extends java_SimpleHttpTunnelHandler {
1130function fread($size) {
1131if ($this->hasContentLength)
1132return fread($this->socket,$this->headers["content_length"]);
1133else
1134return parent::fread($size);
1135}
1136function fwrite($data) {
1137if ($this->hasContentLength)
1138return fwrite($this->socket,$data);
1139else
1140return parent::fwrite($data);
1141}
1142function close() {
1143if ($this->hasContentLength) {
1144fwrite($this->socket,"0\r\n\r\n");
1145fclose($this->socket);
1146} else {
1147parent::fclose($this->socket);
1148}
1149}
1150}
1151class java_Protocol {
1152public $client;
1153public $webContext;
1154public $serverName;
1155function getOverrideHosts() {
1156if(array_key_exists('X_JAVABRIDGE_OVERRIDE_HOSTS',$_ENV)) {
1157$override=$_ENV['X_JAVABRIDGE_OVERRIDE_HOSTS'];
1158if(!is_null($override) && $override!='/') return $override;
1159}
1160return
1161java_getHeader('X_JAVABRIDGE_OVERRIDE_HOSTS_REDIRECT',$_SERVER);
1162}
1163static function getHost() {
1164static $host=null;
1165if(is_null($host)) {
1166$hosts=explode(";",JAVA_HOSTS);
1167$host=explode(":",$hosts[0]);
1168while(count ($host) < 3) array_unshift($host,"");
1169if (substr($host[1],0,2)=="//") $host[1]=substr($host[1],2);
1170}
1171return $host;
1172}
1173function createHttpHandler() {
1174$overrideHosts=$this->getOverrideHosts();
1175$ssl="";
1176if($overrideHosts) {
1177$s=$overrideHosts;
1178if((strlen($s)>2) && ($s[1]==':')) {
1179if($s[0]=='s')
1180$ssl="ssl://";
1181$s=substr($s,2);
1182}
1183$webCtx=strpos($s,"//");
1184if($webCtx)
1185$host=substr($s,0,$webCtx);
1186else
1187$host=$s;
1188$idx=strpos($host,':');
1189if($idx) {
1190if($webCtx)
1191$port=substr($host,$idx+1,$webCtx);
1192else
1193$port=substr($host,$idx+1);
1194$host=substr($host,0,$idx);
1195} else {
1196$port="8080";
1197}
1198if($webCtx) $webCtx=substr($s,$webCtx+1);
1199$this->webContext=$webCtx;
1200} else {
1201$hostVec=java_Protocol::getHost();
1202if ($ssl=$hostVec[0]) $ssl .="://";
1203$host=$hostVec[1];
1204$port=$hostVec[2];
1205}
1206$this->serverName="${ssl}${host}:$port";
1207if ((array_key_exists("X_JAVABRIDGE_REDIRECT",$_SERVER)) ||
1208(array_key_exists("HTTP_X_JAVABRIDGE_REDIRECT",$_SERVER)))
1209return new java_SimpleHttpHandler($this,$ssl,$host,$port);
1210return new java_HttpTunnelHandler($this,$ssl,$host,$port);
1211}
1212function createSimpleHandler($name,$again=true) {
1213$channelName=$name;
1214$errno=null; $errstr=null;
1215if(is_numeric($channelName)) {
1216$peer=@pfsockopen($host="127.0.0.1",$channelName,$errno,$errstr,5);
1217} else {
1218$type=$channelName[0];
1219list($host,$channelName)=explode(":",$channelName);
1220$peer=pfsockopen($host,$channelName,$errno,$errstr,20);
1221if (!$peer)
1222throw new java_ConnectException("No Java server at $host:$channelName. Error message: $errstr ($errno)");
1223}
1224if (!$peer) {
1225$java=file_exists(ini_get("extension_dir")."/JavaBridge.jar")?ini_get("extension_dir")."/JavaBridge.jar":(java_get_base()."/JavaBridge.jar");
1226if (!file_exists($java))
1227throw new java_IOException("Could not find $java in ".getcwd().". Download it from http://sf.net/projects/php-java-bridge/files/Binary%20package/php-java-bridge_".JAVA_PEAR_VERSION."/exploded/JavaBridge.jar/download and try again.");
1228$java_cmd="java -Dphp.java.bridge.daemon=true -jar \"${java}\" INET_LOCAL:$channelName 0";
1229if (!$again)
1230throw new java_ConnectException("No Java back end! Please run it with: $java_cmd. Error message: $errstr ($errno)");
1231if (!java_checkCliSapi())
1232trigger_error("This PHP SAPI requires a JEE or SERVLET back end. Start it,define ('JAVA_SERVLET',true); define('JAVA_HOSTS',...); and try again.",E_USER_ERROR);
1233system ($java_cmd);
1234return $this->createSimpleHandler($name,false);
1235}
1236stream_set_timeout($peer,-1);
1237$handler=new java_SocketHandler($this,new java_SocketChannelP($peer,$host));
1238$compatibility=java_getCompatibilityOption($this->client);
1239$this->write("\177$compatibility");
1240$this->serverName="127.0.0.1:$channelName";
1241return $handler;
1242}
1243function java_get_simple_channel() {
1244return (JAVA_HOSTS&&(!JAVA_SERVLET||(JAVA_SERVLET=="Off"))) ? JAVA_HOSTS : null;
1245}
1246function createHandler() {
1247if(!java_getHeader('X_JAVABRIDGE_OVERRIDE_HOSTS',$_SERVER)&&
1248((function_exists("java_get_default_channel")&&($defaultChannel=java_get_default_channel())) ||
1249($defaultChannel=$this->java_get_simple_channel())) ) {
1250return $this->createSimpleHandler($defaultChannel);
1251} else {
1252return $this->createHttpHandler();
1253}
1254}
1255function java_Protocol ($client) {
1256$this->client=$client;
1257$this->handler=$this->createHandler();
1258}
1259function redirect() {
1260$this->handler->redirect();
1261}
1262function read($size) {
1263return $this->handler->read($size);
1264}
1265function sendData() {
1266$this->handler->write($this->client->sendBuffer);
1267$this->client->sendBuffer=null;
1268}
1269function flush() {
1270$this->sendData();
1271}
1272function getKeepAlive() {
1273return $this->handler->getKeepAlive();
1274}
1275function keepAlive() {
1276$this->handler->keepAlive();
1277}
1278function handle() {
1279$this->client->handleRequests();
1280}
1281function write($data) {
1282$this->client->sendBuffer.=$data;
1283}
1284function finish() {
1285$this->flush();
1286$this->handle();
1287$this->redirect();
1288}
1289function referenceBegin($name) {
1290$this->client->sendBuffer.=$this->client->preparedToSendBuffer;
1291$this->client->preparedToSendBuffer=null;
1292$signature=sprintf("<H p=\"1\" v=\"%s\">",$name);
1293$this->write($signature);
1294$signature[6]="2";
1295$this->client->currentArgumentsFormat=$signature;
1296}
1297function referenceEnd() {
1298$this->client->currentArgumentsFormat.=$format="</H>";
1299$this->write($format);
1300$this->finish();
1301$this->client->currentCacheKey=null;
1302}
1303function createObjectBegin($name) {
1304$this->client->sendBuffer.=$this->client->preparedToSendBuffer;
1305$this->client->preparedToSendBuffer=null;
1306$signature=sprintf("<K p=\"1\" v=\"%s\">",$name);
1307$this->write($signature);
1308$signature[6]="2";
1309$this->client->currentArgumentsFormat=$signature;
1310}
1311function createObjectEnd() {
1312$this->client->currentArgumentsFormat.=$format="</K>";
1313$this->write($format);
1314$this->finish();
1315$this->client->currentCacheKey=null;
1316}
1317function propertyAccessBegin($object,$method) {
1318$this->client->sendBuffer.=$this->client->preparedToSendBuffer;
1319$this->client->preparedToSendBuffer=null;
1320$this->write(sprintf("<G p=\"1\" v=\"%x\" m=\"%s\">",$object,$method));
1321$this->client->currentArgumentsFormat="<G p=\"2\" v=\"%x\" m=\"${method}\">";
1322}
1323function propertyAccessEnd() {
1324$this->client->currentArgumentsFormat.=$format="</G>";
1325$this->write($format);
1326$this->finish();
1327$this->client->currentCacheKey=null;
1328}
1329function invokeBegin($object,$method) {
1330$this->client->sendBuffer.=$this->client->preparedToSendBuffer;
1331$this->client->preparedToSendBuffer=null;
1332$this->write(sprintf("<Y p=\"1\" v=\"%x\" m=\"%s\">",$object,$method));
1333$this->client->currentArgumentsFormat="<Y p=\"2\" v=\"%x\" m=\"${method}\">";
1334}
1335function invokeEnd() {
1336$this->client->currentArgumentsFormat.=$format="</Y>";
1337$this->write($format);
1338$this->finish();
1339$this->client->currentCacheKey=null;
1340}
1341function resultBegin() {
1342$this->client->sendBuffer.=$this->client->preparedToSendBuffer;
1343$this->client->preparedToSendBuffer=null;
1344$this->write("<R>");
1345}
1346function resultEnd() {
1347$this->client->currentCacheKey=null;
1348$this->write("</R>");
1349$this->flush();
1350}
1351function writeString($name) {
1352$this->client->currentArgumentsFormat.=$format="<S v=\"%s\"/>";
1353$this->write(sprintf($format,htmlspecialchars($name,ENT_COMPAT)));
1354}
1355function writeBoolean($boolean) {
1356$this->client->currentArgumentsFormat.=$format="<T v=\"%s\"/>";
1357$this->write(sprintf($format,$boolean));
1358}
1359function writeLong($l) {
1360$this->client->currentArgumentsFormat.="<J v=\"%d\"/>";
1361if($l<0) {
1362$this->write(sprintf("<L v=\"%x\" p=\"A\"/>",-$l));
1363} else {
1364$this->write(sprintf("<L v=\"%x\" p=\"O\"/>",$l));
1365}
1366}
1367function writeULong($l) {
1368$this->client->currentArgumentsFormat.=$format="<L v=\"%x\" p=\"O\"/>";
1369$this->write(sprintf($format,$l));
1370}
1371function writeDouble($d) {
1372$this->client->currentArgumentsFormat.=$format="<D v=\"%.14e\"/>";
1373$this->write(sprintf($format,$d));
1374}
1375function writeObject($object) {
1376$this->client->currentArgumentsFormat.=$format="<O v=\"%x\"/>";
1377$this->write(sprintf($format,$object));
1378}
1379function writeException($object,$str) {
1380$this->write(sprintf("<E v=\"%x\" m=\"%s\"/>",$object,htmlspecialchars($str,ENT_COMPAT)));
1381}
1382function writeCompositeBegin_a() {
1383$this->write("<X t=\"A\">");
1384}
1385function writeCompositeBegin_h() {
1386$this->write("<X t=\"H\">");
1387}
1388function writeCompositeEnd() {
1389$this->write("</X>");
1390}
1391function writePairBegin_s($key) {
1392$this->write(sprintf("<P t=\"S\" v=\"%s\">",htmlspecialchars($key,ENT_COMPAT)));
1393}
1394function writePairBegin_n($key) {
1395$this->write(sprintf("<P t=\"N\" v=\"%x\">",$key));
1396}
1397function writePairBegin() {
1398$this->write("<P>");
1399}
1400function writePairEnd() {
1401$this->write("</P>");
1402}
1403function writeUnref($object) {
1404$this->client->sendBuffer.=$this->client->preparedToSendBuffer;
1405$this->client->preparedToSendBuffer=null;
1406$this->write(sprintf("<U v=\"%x\"/>",$object));
1407}
1408function getServerName() {
1409return $this->serverName;
1410}
1411}
1412class java_ParserString {
1413public $string,$off,$length;
1414function toString() {
1415return $this->getString();
1416}
1417function getString() {
1418return substr($this->string,$this->off,$this->length);
1419}
1420}
1421class java_ParserTag {
1422public $n,$strings;
1423function java_ParserTag() {
1424$this->strings=array();
1425$this->n=0;
1426}
1427}
1428class java_SimpleParser {
1429public $SLEN=256;
1430public $handler;
1431public $tag,$buf,$len,$s;
1432public $type;
1433function java_SimpleParser($handler) {
1434$this->handler=$handler;
1435$this->tag=array(new java_ParserTag(),new java_ParserTag(),new java_ParserTag());
1436$this->len=$this->SLEN;
1437$this->s=str_repeat(" ",$this->SLEN);
1438$this->type=$this->VOJD;
1439}
1440public $BEGIN=0,$KEY=1,$VAL=2,$ENTITY=3,$VOJD=5,$END=6;
1441public $level=0,$eor=0; public $in_dquote,$eot=false;
1442public $pos=0,$c=0,$i=0,$i0=0,$e;
1443function RESET() {
1444$this->type=$this->VOJD;
1445$this->level=0;
1446$this->eor=0;
1447$this->in_dquote=false;
1448$this->i=0;
1449$this->i0=0;
1450}
1451function APPEND($c) {
1452if($this->i>=$this->len-1) {
1453$this->s=str_repeat($this->s,2);
1454$this->len*=2;
1455}
1456$this->s[$this->i++]=$c;
1457}
1458function CALL_BEGIN() {
1459$pt=&$this->tag[1]->strings;
1460$st=&$this->tag[2]->strings;
1461$t=&$this->tag[0]->strings[0];
1462$name=$t->string[$t->off];
1463$n=$this->tag[2]->n;
1464$ar=array();
1465for($i=0; $i<$n; $i++) {
1466$ar[$pt[$i]->getString()]=$st[$i]->getString();
1467}
1468$this->handler->begin($name,$ar);
1469}
1470function CALL_END() {
1471$t=&$this->tag[0]->strings[0];
1472$name=$t->string[$t->off];
1473$this->handler->end($name);
1474}
1475function PUSH($t) {
1476$str=&$this->tag[$t]->strings;
1477$n=&$this->tag[$t]->n;
1478$this->s[$this->i]='|';
1479if(!isset($str[$n])){$h=$this->handler; $str[$n]=$h->createParserString();}
1480$str[$n]->string=&$this->s;
1481$str[$n]->off=$this->i0;
1482$str[$n]->length=$this->i-$this->i0;
1483++$this->tag[$t]->n;
1484$this->APPEND('|');
1485$this->i0=$this->i;
1486}
1487function parse() {
1488while($this->eor==0) {
1489if($this->c>=$this->pos) {
1490$this->buf=$this->handler->read(JAVA_RECV_SIZE);
1491if(is_null($this->buf) || strlen($this->buf)==0)
1492$this->handler->protocol->handler->shutdownBrokenConnection("protocol error. Check the back end log for OutOfMemoryErrors.");
1493$this->pos=strlen($this->buf);
1494if($this->pos==0) break;
1495$this->c=0;
1496}
1497switch(($ch=$this->buf[$this->c]))
1498{
1499case '<': if($this->in_dquote) {$this->APPEND($ch); break;}
1500$this->level+=1;
1501$this->type=$this->BEGIN;
1502break;
1503case '\t': case '\f': case '\n': case '\r': case ' ': if($this->in_dquote) {$this->APPEND($ch); break;}
1504if($this->type==$this->BEGIN) {
1505$this->PUSH($this->type);
1506$this->type=$this->KEY;
1507}
1508break;
1509case '=': if($this->in_dquote) {$this->APPEND($ch); break;}
1510$this->PUSH($this->type);
1511$this->type=$this->VAL;
1512break;
1513case '/': if($this->in_dquote) {$this->APPEND($ch); break;}
1514if($this->type==$this->BEGIN) { $this->type=$this->END; $this->level-=1; }
1515$this->level-=1;
1516$this->eot=true;
1517break;
1518case '>': if($this->in_dquote) {$this->APPEND($ch); break;}
1519if($this->type==$this->END){
1520$this->PUSH($this->BEGIN);
1521$this->CALL_END();
1522} else {
1523if($this->type==$this->VAL) $this->PUSH($this->type);
1524$this->CALL_BEGIN();
1525}
1526$this->tag[0]->n=$this->tag[1]->n=$this->tag[2]->n=0; $this->i0=$this->i=0;
1527$this->type=$this->VOJD;
1528if($this->level==0) $this->eor=1;
1529break;
1530case ';':
1531if($this->type==$this->ENTITY) {
1532switch ($this->s[$this->e+1]) {
1533case 'l': $this->s[$this->e]='<'; $this->i=$this->e+1; break;
1534case 'g': $this->s[$this->e]='>'; $this->i=$this->e+1; break;
1535case 'a': $this->s[$this->e]=($this->s[$this->e+2]=='m'?'&':'\''); $this->i=$this->e+1; break;
1536case 'q': $this->s[$this->e]='"'; $this->i=$this->e+1; break;
1537default: $this->APPEND($ch);
1538}
1539$this->type=$this->VAL;
1540} else {
1541$this->APPEND($ch);
1542}
1543break;
1544case '&':
1545$this->type=$this->ENTITY;
1546$this->e=$this->i;
1547$this->APPEND($ch);
1548break;
1549case '"':
1550$this->in_dquote=!$this->in_dquote;
1551if(!$this->in_dquote && $this->type==$this->VAL) {
1552$this->PUSH($this->type);
1553$this->type=$this->KEY;
1554}
1555break;
1556default:
1557$this->APPEND($ch);
1558}
1559$this->c+=1;
1560}
1561$this->RESET();
1562}
1563function getData($str) {
1564return $str;
1565}
1566function parserError() {
1567$this->handler->protocol->handler->shutdownBrokenConnection(
1568sprintf("protocol error: %s. Check the back end log for details.",$this->s));
1569}
1570}
1571interface java_JavaType {};
1572$java_initialized=false;
1573function __javaproxy_Client_getClient() {
1574static $client=null;
1575if(!is_null($client)) return $client;
1576if (function_exists("java_create_client")) $client=java_create_client();
1577else {
1578global $java_initialized;
1579$client=new java_Client();
1580$java_initialized=true;
1581}
1582return $client;
1583}
1584function java_last_exception_get() {
1585$client=__javaproxy_Client_getClient();
1586return $client->invokeMethod(0,"getLastException",array());
1587}
1588function java_last_exception_clear() {
1589$client=__javaproxy_Client_getClient();
1590$client->invokeMethod(0,"clearLastException",array());
1591}
1592function java_values_internal($object) {
1593if(!$object instanceof java_JavaType) return $object;
1594$client=__javaproxy_Client_getClient();
1595return $client->invokeMethod(0,"getValues",array($object));
1596}
1597function java_invoke($object,$method,$args) {
1598$client=__javaproxy_Client_getClient();
1599$id=($object==null) ? 0 : $object->__java;
1600return $client->invokeMethod($id,$method,$args);
1601}
1602function java_unwrap ($object) {
1603if(!$object instanceof java_JavaType) throw new java_IllegalArgumentException($object);
1604$client=__javaproxy_Client_getClient();
1605return $client->globalRef->get($client->invokeMethod(0,"unwrapClosure",array($object)));
1606}
1607function java_values($object) {
1608return java_values_internal($object);
1609}
1610function java_inspect_internal($object) {
1611if(!$object instanceof java_JavaType) throw new java_IllegalArgumentException($object);
1612$client=__javaproxy_Client_getClient();
1613return $client->invokeMethod(0,"inspect",array($object));
1614}
1615function java_inspect($object) {
1616return java_inspect_internal($object);
1617}
1618function java_set_file_encoding($enc) {
1619$client=__javaproxy_Client_getClient();
1620return $client->invokeMethod(0,"setFileEncoding",array($enc));
1621}
1622function java_instanceof_internal($ob,$clazz) {
1623if(!$ob instanceof java_JavaType) throw new java_IllegalArgumentException($ob);
1624if(!$clazz instanceof java_JavaType) throw new java_IllegalArgumentException($clazz);
1625$client=__javaproxy_Client_getClient();
1626return $client->invokeMethod(0,"instanceOf",array($ob,$clazz));
1627}
1628function java_instanceof($ob,$clazz) {
1629return java_instanceof_internal($ob,$clazz);
1630}
1631function java_cast_internal($object,$type) {
1632if(!$object instanceof java_JavaType) {
1633switch($type[0]) {
1634case 'S': case 's':
1635return (string)$object;
1636case 'B': case 'b':
1637return (boolean)$object;
1638case 'L': case 'I': case 'l': case 'i':
1639return (integer)$object;
1640case 'D': case 'd': case 'F': case 'f':
1641return (float) $object;
1642case 'N': case 'n':
1643return null;
1644case 'A': case 'a':
1645return (array)$object;
1646case 'O': case 'o':
1647return (object)$object;
1648}
1649}
1650return $object->__cast($type);
1651}
1652function java_cast($object,$type) {
1653return java_cast_internal($object,$type);
1654}
1655function java_require($arg) {
1656trigger_error('java_require() not supported anymore. Please use <a href="http://php-java-bridge.sourceforge.net/pjb/webapp.php>tomcat or jee hot deployment</a> instead',E_USER_WARNING);
1657}
1658function java_get_lifetime ()
1659{
1660$session_max_lifetime=ini_get("session.gc_maxlifetime");
1661return $session_max_lifetime ? (int)$session_max_lifetime : 1440;
1662}
1663function java_session_array($args) {
1664$client=__javaproxy_Client_getClient();
1665if(!isset($args[0])) $args[0]=null;
1666if(!isset($args[1]))
1667$args[1]=0;
1668elseif ($args[1]===true)
1669$args[1]=1;
1670else
1671$args[1]=2;
1672if(!isset($args[2])) {
1673$args[2]=java_get_lifetime ();
1674}
1675return $client->getSession($args);
1676}
1677function java_session() {
1678return java_session_array(func_get_args());
1679}
1680function java_server_name() {
1681try {
1682$client=__javaproxy_Client_getClient();
1683return $client->getServerNa

Large files files are truncated, but you can click here to view the full file