Advertisement
Guest User

Untitled

a guest
Dec 6th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 63.01 KB | None | 0 0
  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.  
  12. if(!function_exists("java_get_base")) {
  13. 1.0E512;
  14. function java_get_base() {
  15. $ar=get_required_files();
  16. $arLen=sizeof($ar);
  17. if($arLen>0) {
  18. $thiz=$ar[$arLen-1];
  19. return dirname($thiz);
  20. } else {
  21. return "java";
  22. }
  23. }
  24. function java_truncate($str) {
  25. if (strlen($str)>955)
  26. return substr($str,0,475).'[...]'.substr($str,-475);
  27. return $str;
  28. }
  29. class java_JavaException extends Exception {
  30. function __toString() {return $this->getMessage();}
  31. };
  32. class java_RuntimeException extends java_JavaException {};
  33. class java_IOException extends java_JavaException {};
  34. class java_ConnectException extends java_IOException {};
  35. class java_IllegalStateException extends java_RuntimeException {};
  36. class java_IllegalArgumentException extends java_RuntimeException {
  37. function __construct($ob) {
  38. parent::__construct("illegal argument: ".gettype($ob));
  39. }
  40. };
  41. function java_autoload_function5($x) {
  42. $s=str_replace("_",".",$x);
  43. $c=__javaproxy_Client_getClient();
  44. if(!($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);}}';
  49. eval ("$i");
  50. return true;
  51. }
  52. function 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();
  56. if(!($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);}}';
  64. eval ("$instance");
  65. return true;
  66. }
  67. if(!defined("JAVA_DISABLE_AUTOLOAD") && function_exists("spl_autoload_register")) spl_autoload_register("java_autoload_function");
  68. function java_autoload($libs=null) {
  69. trigger_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. }
  71. function 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);
  77. if ($return) return $res->getBufferContents();
  78. echo $res->getBufferContents();
  79. return true;
  80. }
  81. function Java($name) {
  82. static $classMap=array();
  83. if(array_key_exists($name,$classMap)) return $classMap[$name];
  84. return $classMap[$name]=new JavaClass($name);
  85. }
  86. function java_get_closure() {return java_closure_array(func_get_args());}
  87. function java_wrap() {return java_closure_array(func_get_args());}
  88. function java_get_values($arg) { return java_values($arg); }
  89. function java_get_session() {return java_session_array(func_get_args());}
  90. function java_get_context() {return java_context(); }
  91. function java_get_server_name() { return java_server_name(); }
  92. function java_isnull($value) { return is_null (java_values ($value)); }
  93. function java_is_null($value) { return is_null (java_values ($value)); }
  94. function java_istrue($value) { return (boolean)(java_values ($value)); }
  95. function java_is_true($value) { return (boolean)(java_values ($value)); }
  96. function java_isfalse($value) { return !(java_values ($value)); }
  97. function java_is_false($value) { return !(java_values ($value)); }
  98. function java_set_encoding($enc) { return java_set_file_encoding ($enc); }
  99. function java_call_with_continuation($kontinuation=null) {
  100. if (java_getHeader("X_JAVABRIDGE_INCLUDE",$_SERVER) && !java_getHeader("X_JAVABRIDGE_INCLUDE_ONLY",$_SERVER)) {
  101. if (is_null($kontinuation))
  102. java_context()->call(java_closure());
  103. elseif (is_string($kontinuation))
  104. java_context()->call(call_user_func($kontinuation));
  105. elseif ($kontinuation instanceof java_JavaType)
  106. java_context()->call($kontinuation);
  107. else
  108. java_context()->call(java_closure($kontinuation));
  109. }
  110. }
  111. function java_defineHostFromInitialQuery($java_base) {
  112. if($java_base!="java") {
  113. $url=parse_url($java_base);
  114. if(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"];
  119. define ("JAVA_HOSTS","${scheme}${host}:${port}");
  120. $dir=dirname($path);
  121. define ("JAVA_SERVLET","$dir/servlet.phpjavabridge");
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. define("JAVA_PEAR_VERSION","6.2.1");
  128. if(!defined("JAVA_SEND_SIZE"))
  129. define("JAVA_SEND_SIZE",8192);
  130. if(!defined("JAVA_RECV_SIZE"))
  131. define("JAVA_RECV_SIZE",8192);
  132. if(!defined("JAVA_HOSTS")) {
  133. if(!java_defineHostFromInitialQuery(java_get_base())) {
  134. if ($java_ini=get_cfg_var("java.hosts")) define("JAVA_HOSTS",$java_ini);
  135. else define("JAVA_HOSTS","127.0.0.1:8080");
  136. }
  137. }
  138. if(!defined("JAVA_SERVLET")) {
  139. if (!(($java_ini=get_cfg_var("java.servlet"))===false)) define("JAVA_SERVLET",$java_ini);
  140. else define("JAVA_SERVLET",1);
  141. }
  142. if(!defined("JAVA_LOG_LEVEL"))
  143. if (!(($java_ini=get_cfg_var("java.log_level"))===false)) define("JAVA_LOG_LEVEL",(int)$java_ini);
  144. else define("JAVA_LOG_LEVEL",null);
  145. if (!defined("JAVA_PREFER_VALUES"))
  146. if ($java_ini=get_cfg_var("java.prefer_values")) define("JAVA_PREFER_VALUES",$java_ini);
  147. else define("JAVA_PREFER_VALUES",0);
  148. class java_SimpleFactory {
  149. public $client;
  150. function java_SimpleFactory($client) {
  151. $this->client=$client;
  152. }
  153. function getProxy($result,$signature,$exception,$wrap) {
  154. return $result;
  155. }
  156. function checkResult($result) {
  157. }
  158. }
  159. class java_ProxyFactory extends java_SimpleFactory {
  160. function create($result,$signature) {
  161. return new java_JavaProxy($result,$signature);
  162. }
  163. function createInternal($proxy) {
  164. return new java_InternalJava($proxy);
  165. }
  166. function getProxy($result,$signature,$exception,$wrap) {
  167. $proxy=$this->create($result,$signature);
  168. if($wrap) $proxy=$this->createInternal($proxy);
  169. return $proxy;
  170. }
  171. }
  172. class java_ArrayProxyFactory extends java_ProxyFactory {
  173. function create($result,$signature) {
  174. return new java_ArrayProxy($result,$signature);
  175. }
  176. }
  177. class java_IteratorProxyFactory extends java_ProxyFactory {
  178. function create($result,$signature) {
  179. return new java_IteratorProxy($result,$signature);
  180. }
  181. }
  182. class java_ExceptionProxyFactory extends java_SimpleFactory {
  183. function create($result,$signature) {
  184. return new java_ExceptionProxy($result,$signature);
  185. }
  186. function getProxy($result,$signature,$exception,$wrap) {
  187. $proxy=$this->create($result,$signature);
  188. if($wrap) $proxy=new java_InternalException($proxy,$exception);
  189. return $proxy;
  190. }
  191. }
  192. class java_ThrowExceptionProxyFactory extends java_ExceptionProxyFactory {
  193. function getProxy($result,$signature,$exception,$wrap) {
  194. $proxy=$this->create($result,$signature);
  195. $proxy=new java_InternalException($proxy,$exception);
  196. return $proxy;
  197. }
  198. function checkResult($result) {
  199. if (JAVA_PREFER_VALUES || ($result->__hasDeclaredExceptions=='T'))
  200. throw $result;
  201. else {
  202. trigger_error("Unchecked exception detected: ".java_truncate($result->__toString()),E_USER_WARNING);
  203. }
  204. }
  205. }
  206. class java_CacheEntry {
  207. public $fmt,$signature,$factory,$java;
  208. public $resultVoid;
  209. function java_CacheEntry($fmt,$signature,$factory,$resultVoid) {
  210. $this->fmt=$fmt;
  211. $this->signature=$signature;
  212. $this->factory=$factory;
  213. $this->resultVoid=$resultVoid;
  214. }
  215. }
  216. class java_Arg {
  217. public $client;
  218. public $exception;
  219. public $factory,$val;
  220. public $signature;
  221. function java_Arg($client) {
  222. $this->client=$client;
  223. $this->factory=$client->simpleFactory;
  224. }
  225. function linkResult(&$val) {
  226. $this->val=&$val;
  227. }
  228. function setResult($val) {
  229. $this->val=&$val;
  230. }
  231. function 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);
  236. return $rc;
  237. }
  238. function setFactory($factory) {
  239. $this->factory=$factory;
  240. }
  241. function setException($string) {
  242. $this->exception=$string;
  243. }
  244. function setVoidSignature() {
  245. $this->signature="@V";
  246. $key=$this->client->currentCacheKey;
  247. if($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. }
  253. function setSignature($signature) {
  254. $this->signature=$signature;
  255. $key=$this->client->currentCacheKey;
  256. if($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. }
  262. class java_CompositeArg extends java_Arg {
  263. public $parentArg;
  264. public $idx;
  265. public $type;
  266. public $counter;
  267. function java_CompositeArg($client,$type) {
  268. parent::java_Arg($client);
  269. $this->type=$type;
  270. $this->val=array();
  271. $this->counter=0;
  272. }
  273. function setNextIndex() {
  274. $this->idx=$this->counter++;
  275. }
  276. function setIndex($val) {
  277. $this->idx=$val;
  278. }
  279. function linkResult(&$val) {
  280. $this->val[$this->idx]=&$val;
  281. }
  282. function setResult($val) {
  283. $this->val[$this->idx]=$this->factory->getProxy($val,$this->signature,$this->exception,true);
  284. $this->factory=$this->client->simpleFactory;
  285. }
  286. }
  287. class java_ApplyArg extends java_CompositeArg {
  288. public $m,$p,$v,$n;
  289. function java_ApplyArg($client,$type,$m,$p,$v,$n) {
  290. parent::java_CompositeArg($client,$type);
  291. $this->m=$m;
  292. $this->p=$p;
  293. $this->v=$v;
  294. $this->n=$n;
  295. }
  296. }
  297. class java_Client {
  298. public $RUNTIME;
  299. public $result,$exception;
  300. public $parser;
  301. public $simpleArg,$compositeArg;
  302. public $simpleFactory,
  303. $proxyFactory,$iteratorProxyFacroty,
  304. $arrayProxyFactory,$exceptionProxyFactory,$throwExceptionProxyFactory;
  305. public $arg;
  306. public $asyncCtx,$cancelProxyCreationCounter;
  307. public $globalRef;
  308. public $stack;
  309. public $defaultCache=array(),$asyncCache=array(),$methodCache;
  310. public $isAsync=0;
  311. public $currentCacheKey,$currentArgumentsFormat;
  312. public $cachedJavaPrototype;
  313. public $sendBuffer,$preparedToSendBuffer;
  314. public $inArgs;
  315. function 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. }
  333. function read($size) {
  334. return $this->protocol->read($size);
  335. }
  336. function setDefaultHandler() {
  337. $this->methodCache=$this->defaultCache;
  338. }
  339. function setAsyncHandler() {
  340. $this->methodCache=$this->asyncCache;
  341. }
  342. function handleRequests() {
  343. $tail_call=false;
  344. do {
  345. $this->stack=array($this->arg=$this->simpleArg);
  346. $this->idx=0;
  347. $this->parser->parse();
  348. if((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);
  357. return 1;
  358. }
  359. function getWrappedResult($wrap) {
  360. return $this->simpleArg->getResult($wrap);
  361. }
  362. function getInternalResult() {
  363. return $this->getWrappedResult(false);
  364. }
  365. function getResult() {
  366. return $this->getWrappedResult(true);
  367. }
  368. function getProxyFactory($type) {
  369. switch($type[0]) {
  370. case 'E':
  371. $factory=$this->exceptionProxyFactory;
  372. break;
  373. case 'C':
  374. $factory=$this->iteratorProxyFactory;
  375. break;
  376. case 'A':
  377. $factory=$this->arrayProxyFactory;
  378. break;
  379. default:
  380. case 'O':
  381. $factory=$this->proxyFactory;
  382. }
  383. return $factory;
  384. }
  385. function link(&$arg,&$newArg) {
  386. $arg->linkResult($newArg->val);
  387. $newArg->parentArg=$arg;
  388. }
  389. function getExact($str) {
  390. return hexdec($str);
  391. }
  392. function getInexact($str) {
  393. $val=null;
  394. sscanf($str,"%e",$val);
  395. return $val;
  396. }
  397. function begin($name,$st) {
  398. $arg=$this->arg;
  399. switch($name[0]) {
  400. case '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);
  408. array_push($this->stack,$this->arg=$newArg);
  409. break;
  410. case 'X':
  411. $newArg=new java_CompositeArg($this,$st['t']);
  412. $this->link($arg,$newArg);
  413. array_push($this->stack,$this->arg=$newArg);
  414. break;
  415. case 'P':
  416. if($arg->type=='H') {
  417. $s=$st['t'];
  418. if($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. }
  426. break;
  427. case 'S':
  428. $arg->setResult($this->parser->getData($st['v']));
  429. break;
  430. case 'B':
  431. $s=$st['v'];
  432. $arg->setResult($s[0]=='T');
  433. break;
  434. case 'L':
  435. $sign=$st['p'];
  436. $val=$this->getExact($st['v']);
  437. if($sign[0]=='A') $val*=-1;
  438. $arg->setResult($val);
  439. break;
  440. case 'D':
  441. $arg->setResult($this->getInexact($st['v']));
  442. break;
  443. case 'V':
  444. if ($st['n']!='T') {
  445. $arg->setVoidSignature();
  446. }
  447. case 'N':
  448. $arg->setResult(null);
  449. break;
  450. case 'F':
  451. break;
  452. case 'O':
  453. $arg->setFactory($this->getProxyFactory($st['p']));
  454. $arg->setResult($this->asyncCtx=$this->getExact($st['v']));
  455. if($st['n']!='T') $arg->setSignature($st['m']);
  456. break;
  457. case 'E':
  458. $arg->setFactory($this->throwExceptionProxyFactory);
  459. $arg->setException($st['m']);
  460. $arg->setResult($this->asyncCtx=$this->getExact($st['v']));
  461. break;
  462. default:
  463. $this->parser->parserError();
  464. }
  465. }
  466. function end($name) {
  467. switch($name[0]) {
  468. case 'X':
  469. $frame=array_pop($this->stack);
  470. $this->arg=$frame->parentArg;
  471. break;
  472. }
  473. }
  474. function createParserString() {
  475. return new java_ParserString();
  476. }
  477. function writeArg($arg) {
  478. if(is_string($arg)) {
  479. $this->protocol->writeString($arg);
  480. } else if(is_object($arg)) {
  481. if ((!$arg instanceof java_JavaType)) {
  482. error_log((string)new java_IllegalArgumentException($arg));
  483. trigger_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;
  498. foreach($arg as $key=>$val) {
  499. if(is_string($key)) {
  500. if(!$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 {
  508. if(!$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. }
  517. if(!$wrote_begin) {
  518. $this->protocol->writeCompositeBegin_a();
  519. }
  520. $this->protocol->writeCompositeEnd();
  521. }
  522. }
  523. function writeArgs($args) {
  524. $this->inArgs=true;
  525. $n=count($args);
  526. for($i=0; $i<$n; $i++) {
  527. $this->writeArg($args[$i]);
  528. }
  529. $this->inArgs=false;
  530. }
  531. function createObject($name,$args) {
  532. $this->protocol->createObjectBegin($name);
  533. $this->writeArgs($args);
  534. $this->protocol->createObjectEnd();
  535. $val=$this->getInternalResult();
  536. return $val;
  537. }
  538. function referenceObject($name,$args) {
  539. $this->protocol->referenceBegin($name);
  540. $this->writeArgs($args);
  541. $this->protocol->referenceEnd();
  542. $val=$this->getInternalResult();
  543. return $val;
  544. }
  545. function getProperty($object,$property) {
  546. $this->protocol->propertyAccessBegin($object,$property);
  547. $this->protocol->propertyAccessEnd();
  548. return $this->getResult();
  549. }
  550. function setProperty($object,$property,$arg) {
  551. $this->protocol->propertyAccessBegin($object,$property);
  552. $this->writeArg($arg);
  553. $this->protocol->propertyAccessEnd();
  554. $this->getResult();
  555. }
  556. function invokeMethod($object,$method,$args) {
  557. $this->protocol->invokeBegin($object,$method);
  558. $this->writeArgs($args);
  559. $this->protocol->invokeEnd();
  560. $val=$this->getResult();
  561. return $val;
  562. }
  563. function unref($object) {
  564. if (isset($this->protocol)) $this->protocol->writeUnref($object);
  565. }
  566. function 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;
  573. try {
  574. $res=$arg->getResult(true);
  575. if((($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);
  577. if (is_object($res) && (!($res instanceof java_JavaType))) {
  578. trigger_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) {
  593. error_log($ex->__toString());
  594. trigger_error("Unchecked exception detected in callback",E_USER_ERROR);
  595. die (1);
  596. }
  597. $this->isAsync=$isAsync;
  598. $this->methodCache=$methodCache;
  599. $this->currentArgumentsFormat=$currentArgumentsFormat;
  600. }
  601. function cast($object,$type) {
  602. switch($type[0]) {
  603. case 'S': case 's':
  604. return $this->invokeMethod(0,"castToString",array($object));
  605. case 'B': case 'b':
  606. return $this->invokeMethod(0,"castToBoolean",array($object));
  607. case 'L': case 'I': case 'l': case 'i':
  608. return $this->invokeMethod(0,"castToExact",array($object));
  609. case 'D': case 'd': case 'F': case 'f':
  610. return $this->invokeMethod(0,"castToInExact",array($object));
  611. case 'N': case 'n':
  612. return null;
  613. case 'A': case 'a':
  614. return $this->invokeMethod(0,"castToArray",array($object));
  615. case 'O': case 'o':
  616. return $object;
  617. default:
  618. throw new java_RuntimeException("$type illegal");
  619. }
  620. }
  621. function getContext() {
  622. static $cache=null;
  623. if (!is_null($cache)) return $cache;
  624. return $cache=$this->invokeMethod(0,"getContext",array());
  625. }
  626. function getSession($args) {
  627. return $this->invokeMethod(0,"getSession",$args);
  628. }
  629. function getServerName() {
  630. static $cache=null;
  631. if (!is_null($cache)) return $cache;
  632. return $cache=$this->protocol->getServerName();
  633. }
  634. }
  635. function java_shutdown() {
  636. global $java_initialized;
  637. if (!$java_initialized) return;
  638. if (session_id()) session_write_close();
  639. $client=__javaproxy_Client_getClient();
  640. if (!isset($client->protocol) || $client->inArgs) return;
  641. if ($client->preparedToSendBuffer)
  642. $client->sendBuffer.=$client->preparedToSendBuffer;
  643. $client->sendBuffer.=$client->protocol->getKeepAlive();
  644. $client->protocol->flush();
  645. $client->protocol->keepAlive();
  646. }
  647. register_shutdown_function("java_shutdown");
  648. class java_GlobalRef {
  649. public $map;
  650. function java_GlobalRef() {
  651. $this->map=array();
  652. }
  653. function add($object) {
  654. if(is_null($object)) return 0;
  655. return array_push($this->map,$object);
  656. }
  657. function get($id) {
  658. if(!$id) return 0;
  659. return $this->map[--$id];
  660. }
  661. }
  662. class java_NativeParser {
  663. public $parser,$handler;
  664. public $level,$event;
  665. public $buf;
  666. function java_NativeParser($handler) {
  667. $this->handler=$handler;
  668. $this->parser=xml_parser_create();
  669. xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);
  670. xml_set_object($this->parser,$this);
  671. xml_set_element_handler($this->parser,"begin","end");
  672. xml_parse($this->parser,"<F>");
  673. $this->level=0;
  674. }
  675. function begin($parser,$name,$param) {
  676. $this->event=true;
  677. switch($name) {
  678. case 'X': case 'A': $this->level+=1;
  679. }
  680. $this->handler->begin($name,$param);
  681. }
  682. function end($parser,$name) {
  683. $this->handler->end($name);
  684. switch($name) {
  685. case 'X': case 'A': $this->level-=1;
  686. }
  687. }
  688. function getData($str) {
  689. return base64_decode($str);
  690. }
  691. function parse() {
  692. do {
  693. $this->event=false;
  694. $buf=$this->buf=$this->handler->read(JAVA_RECV_SIZE);
  695. $len=strlen($buf);
  696. if(!xml_parse($this->parser,$buf,$len==0)) {
  697. $this->handler->protocol->handler->shutdownBrokenConnection(
  698. sprintf("protocol error: %s,%s at col %d. Check the back end log for OutOfMemoryErrors.",
  699. $buf,
  700. xml_error_string(xml_get_error_code($this->parser)),
  701. xml_get_current_column_number($this->parser)));
  702. }
  703. } while(!$this->event || $this->level>0);
  704. }
  705. function parserError() {
  706. $this->handler->protocol->handler->shutdownBrokenConnection(
  707. sprintf("protocol error: %s. Check the back end log for details.",$this->buf));
  708. }
  709. }
  710. class java_Parser {
  711. public $parser;
  712. function java_Parser($handler) {
  713. if(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. }
  721. function parse() {
  722. $this->parser->parse();
  723. }
  724. function getData($str) {
  725. return $this->parser->getData($str);
  726. }
  727. function parserError() {
  728. $this->parser->parserError();
  729. }
  730. }
  731. function java_getHeader($name,$array) {
  732. if (array_key_exists($name,$array)) return $array[$name];
  733. $name="HTTP_$name";
  734. if (array_key_exists($name,$array)) return $array[$name];
  735. return null;
  736. }
  737. function java_checkCliSapi() {
  738. $sapi=substr(php_sapi_name(),0,3);
  739. return ((($sapi=='cgi') && !get_cfg_var("java.session")) || ($sapi=='cli'));
  740. }
  741. function java_getCompatibilityOption($client) {
  742. static $compatibility=null;
  743. if ($compatibility) return $compatibility;
  744. $compatibility=$client->RUNTIME["PARSER"]=="NATIVE"
  745. ? (0103-JAVA_PREFER_VALUES)
  746. : (0100+JAVA_PREFER_VALUES);
  747. if(is_int(JAVA_LOG_LEVEL)) {
  748. $compatibility |=128 | (7 & JAVA_LOG_LEVEL)<<2;
  749. }
  750. $compatibility=chr ($compatibility);
  751. return $compatibility;
  752. }
  753. class java_EmptyChannel {
  754. protected $handler;
  755. private $res;
  756. function java_EmptyChannel($handler) {
  757. $this->handler=$handler;
  758. }
  759. function shutdownBrokenConnection () {}
  760. function fwrite($data) {
  761. return $this->handler->fwrite($data);
  762. }
  763. function fread($size) {
  764. return $this->handler->fread($size);
  765. }
  766. function getKeepAliveA() {
  767. return "<F p=\"A\" />";
  768. }
  769. function getKeepAliveE() {
  770. return "<F p=\"E\" />";
  771. }
  772. function getKeepAlive() {
  773. return $this->getKeepAliveE();
  774. }
  775. function error() {
  776. trigger_error("An unchecked exception occured during script execution. Please check the server log files for details.",E_USER_ERROR);
  777. }
  778. function checkA($peer) {
  779. $val=$this->res[6];
  780. if ($val !='A') fclose($peer);
  781. if ($val !='A' && $val !='E') {
  782. $this->error();
  783. }
  784. }
  785. function checkE() {
  786. $val=$this->res[6];
  787. if ($val !='E') {
  788. $this->error();
  789. }
  790. }
  791. function keepAliveS() {
  792. $this->res=$this->fread(10);
  793. }
  794. function keepAliveSC() {
  795. $this->res=$this->fread(10);
  796. $this->fwrite("");
  797. $this->fread(JAVA_RECV_SIZE);
  798. }
  799. function keepAliveH() {
  800. $this->res=$this->handler->read(10);
  801. }
  802. function keepAlive() {
  803. $this->keepAliveH();
  804. $this->checkE();
  805. }
  806. }
  807. abstract class java_SocketChannel extends java_EmptyChannel {
  808. public $peer,$host;
  809. function java_SocketChannel($peer,$host) {
  810. $this->peer=$peer;
  811. $this->host=$host;
  812. }
  813. function fwrite($data) {
  814. return fwrite($this->peer,$data);
  815. }
  816. function fread($size) {
  817. return fread($this->peer,$size);
  818. }
  819. function shutdownBrokenConnection () {
  820. fclose($this->peer);
  821. }
  822. }
  823. class java_SocketChannelP extends java_SocketChannel {
  824. function getKeepAlive() {return $this->getKeepAliveA();}
  825. function keepAlive() { $this->keepAliveS(); $this->checkA($this->peer); }
  826. }
  827. class java_ChunkedSocketChannel extends java_SocketChannel {
  828. function fwrite($data) {
  829. $len=dechex(strlen($data));
  830. return fwrite($this->peer,"${len}\r\n${data}\r\n");
  831. }
  832. function fread($size) {
  833. $length=hexdec(fgets($this->peer,JAVA_RECV_SIZE));
  834. $data="";
  835. while ($length > 0) {
  836. $str=fread($this->peer,$length);
  837. if (feof ($this->peer)) return null;
  838. $length -=strlen($str);
  839. $data .=$str;
  840. }
  841. fgets($this->peer,3);
  842. return $data;
  843. }
  844. function keepAlive() { $this->keepAliveSC(); $this->checkE(); fclose ($this->peer); }
  845. }
  846. class java_SocketHandler {
  847. public $protocol,$channel;
  848. function java_SocketHandler($protocol,$channel) {
  849. $this->protocol=$protocol;
  850. $this->channel=$channel;
  851. }
  852. function write($data) {
  853. return $this->channel->fwrite($data);
  854. }
  855. function fwrite($data) {return $this->write($data);}
  856. function read($size) {
  857. return $this->channel->fread($size);
  858. }
  859. function fread($size) {return $this->read($size);}
  860. function redirect() {}
  861. function getKeepAlive() {
  862. return $this->channel->getKeepAlive();
  863. }
  864. function keepAlive() {
  865. $this->channel->keepAlive();
  866. }
  867. function dieWithBrokenConnection($msg) {
  868. unset($this->protocol->client->protocol);
  869. trigger_error ($msg?$msg:"unknown error: please see back end log for details",E_USER_ERROR);
  870. }
  871. function shutdownBrokenConnection ($msg) {
  872. $this->channel->shutdownBrokenConnection();
  873. $this->dieWithBrokenConnection($msg);
  874. }
  875. }
  876. class java_SimpleHttpHandler extends java_SocketHandler {
  877. public $headers,$cookies;
  878. public $context,$ssl,$port;
  879. public $host;
  880. function 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)
  894. or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
  895. $this->protocol->client->sendBuffer=null;
  896. $this->protocol->handler->read(1)
  897. or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
  898. }
  899. function 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. }
  907. function getCookies() {
  908. $str="";
  909. $first=true;
  910. foreach($_COOKIE as $k=> $v) {
  911. $str .=($first ? "Cookie: $k=$v":"; $k=$v");
  912. $first=false;
  913. }
  914. if(!$first) $str .="\r\n";
  915. return $str;
  916. }
  917. function getContextFromCgiEnvironment() {
  918. $ctx=java_getHeader('X_JAVABRIDGE_CONTEXT',$_SERVER);
  919. return $ctx;
  920. }
  921. function getContext() {
  922. static $context=null;
  923. if($context) return $context;
  924. $ctx=$this->getContextFromCgiEnvironment();
  925. $context="";
  926. if($ctx) {
  927. $context=sprintf("X_JAVABRIDGE_CONTEXT: %s\r\n",$ctx);
  928. }
  929. return $context;
  930. }
  931. function getWebAppInternal() {
  932. $context=$this->protocol->webContext;
  933. if(isset($context)) return $context;
  934. return (JAVA_SERVLET=="User" &&
  935. array_key_exists('PHP_SELF',$_SERVER) &&
  936. array_key_exists('HTTP_HOST',$_SERVER))
  937. ? $_SERVER['PHP_SELF']."javabridge"
  938. : null;
  939. }
  940. function getWebApp() {
  941. $context=$this->getWebAppInternal();
  942. if(is_null($context)) $context=JAVA_SERVLET;
  943. if(is_null($context) || $context[0]!="/")
  944. $context="/JavaBridge/JavaBridge.phpjavabridge";
  945. return $context;
  946. }
  947. function write($data) {
  948. return $this->protocol->socketHandler->write($data);
  949. }
  950. function doSetCookie($key,$val,$path) {
  951. $path=trim($path);
  952. $webapp=$this->getWebAppInternal(); if(!$webapp) $path="/";
  953. setcookie($key,$val,0,$path);
  954. }
  955. function read($size) {
  956. return $this->protocol->socketHandler->read($size);
  957. }
  958. function getChannel($channelName) {
  959. $errstr=null; $errno=null;
  960. $peer=pfsockopen($this->host,$channelName,$errno,$errstr,20);
  961. if (!$peer) throw new java_IllegalStateException("No ContextServer for {$this->host}:{$channelName}. Error: $errstr ($errno)\n");
  962. stream_set_timeout($peer,-1);
  963. return new java_SocketChannelP($peer,$this->host);
  964. }
  965. function keepAlive() {
  966. parent::keepAlive();
  967. }
  968. function redirect() {}
  969. }
  970. class java_SimpleHttpTunnelHandler extends java_SimpleHttpHandler {
  971. public $socket;
  972. protected $hasContentLength=false;
  973. function createSimpleChannel () {
  974. $this->channel=new java_EmptyChannel($this);
  975. }
  976. function createChannel() {
  977. $this->createSimpleChannel();
  978. }
  979. function shutdownBrokenConnection ($msg) {
  980. fclose($this->socket);
  981. $this->dieWithBrokenConnection($msg);
  982. }
  983. function checkSocket($socket,&$errno,&$errstr) {
  984. if (!$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";
  989. throw new java_ConnectException($msg);
  990. }
  991. }
  992. function open() {
  993. $errno=null; $errstr=null;
  994. $socket=fsockopen("{$this->ssl}{$this->host}",$this->port,$errno,$errstr,20);
  995. $this->checkSocket($socket,$errno,$errstr);
  996. stream_set_timeout($socket,-1);
  997. $this->socket=$socket;
  998. }
  999. function fread($size) {
  1000. $length=hexdec(fgets($this->socket,JAVA_RECV_SIZE));
  1001. $data="";
  1002. while ($length > 0) {
  1003. $str=fread($this->socket,$length);
  1004. if (feof ($this->socket)) return null;
  1005. $length -=strlen($str);
  1006. $data .=$str;
  1007. }
  1008. fgets($this->socket,3);
  1009. return $data;
  1010. }
  1011. function fwrite($data) {
  1012. $len=dechex(strlen($data));
  1013. return fwrite($this->socket,"${len}\r\n${data}\r\n");
  1014. }
  1015. function close() {
  1016. fwrite($this->socket,"0\r\n\r\n");
  1017. fgets($this->socket,JAVA_RECV_SIZE);
  1018. fgets($this->socket,3);
  1019. fclose($this->socket);
  1020. }
  1021. function java_SimpleHttpTunnelHandler($protocol,$ssl,$host,$port) {
  1022. parent::java_SimpleHttpHandler($protocol,$ssl,$host,$port);
  1023. $this->open();
  1024. }
  1025. function read($size) {
  1026. if(is_null($this->headers)) $this->parseHeaders();
  1027. if (isset($this->headers["http_error"])) {
  1028. if (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'];
  1032. for($str=fread($this->socket,$len); strlen($str)<$len; $str.=fread($this->socket,$len-strlen($str)))
  1033. if (feof ($this->socket)) break;
  1034. } else {
  1035. $str=fread($this->socket,JAVA_RECV_SIZE);
  1036. }
  1037. $this->shutdownBrokenConnection($str);
  1038. }
  1039. return $this->fread(JAVA_RECV_SIZE);
  1040. }
  1041. function getBodyFor ($compat,$data) {
  1042. $len=dechex(2+strlen($data));
  1043. return "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. }
  1045. function 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");
  1060. fflush($socket) or $this->shutdownBrokenConnection("Broken connection handle");
  1061. return $count;
  1062. }
  1063. function parseHeaders() {
  1064. $this->headers=array();
  1065. $line=trim(fgets($this->socket,JAVA_RECV_SIZE));
  1066. $ar=explode (" ",$line);
  1067. $code=((int)$ar[1]);
  1068. if ($code !=200) $this->headers["http_error"]=$code;
  1069. while (($str=trim(fgets($this->socket,JAVA_RECV_SIZE)))) {
  1070. if($str[0]=='X') {
  1071. if(!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') {
  1077. if(!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="";
  1083. if(isset($ar[1])) $p=explode("=",$ar[1]);
  1084. if(isset($p)) $path=$p[1];
  1085. $this->doSetCookie($cookie[0],$cookie[1],$path);
  1086. }
  1087. } else if($str[0]=='C') {
  1088. if(!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') {
  1095. if(!strncasecmp("TRANSFER-ENCODING",$str,17) && !strncasecmp("chunked",trim(substr($str,18)),7)) {
  1096. $this->headers["transfer_chunked"]=true;
  1097. }
  1098. }
  1099. }
  1100. }
  1101. function getSimpleChannel() {
  1102. return new java_ChunkedSocketChannel($this->socket,$this->protocol,$this->host);
  1103. }
  1104. function redirect() {
  1105. $this->isRedirect=isset($this->headers["redirect"]);
  1106. if ($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);
  1113. if ($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)
  1120. or $this->protocol->handler->shutdownBrokenConnection("Broken local connection handle");
  1121. $this->protocol->client->sendBuffer=null;
  1122. $this->protocol->handler->read(1)
  1123. or $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. }
  1129. class java_HttpTunnelHandler extends java_SimpleHttpTunnelHandler {
  1130. function fread($size) {
  1131. if ($this->hasContentLength)
  1132. return fread($this->socket,$this->headers["content_length"]);
  1133. else
  1134. return parent::fread($size);
  1135. }
  1136. function fwrite($data) {
  1137. if ($this->hasContentLength)
  1138. return fwrite($this->socket,$data);
  1139. else
  1140. return parent::fwrite($data);
  1141. }
  1142. function close() {
  1143. if ($this->hasContentLength) {
  1144. fwrite($this->socket,"0\r\n\r\n");
  1145. fclose($this->socket);
  1146. } else {
  1147. parent::fclose($this->socket);
  1148. }
  1149. }
  1150. }
  1151. class java_Protocol {
  1152. public $client;
  1153. public $webContext;
  1154. public $serverName;
  1155. function getOverrideHosts() {
  1156. if(array_key_exists('X_JAVABRIDGE_OVERRIDE_HOSTS',$_ENV)) {
  1157. $override=$_ENV['X_JAVABRIDGE_OVERRIDE_HOSTS'];
  1158. if(!is_null($override) && $override!='/') return $override;
  1159. }
  1160. return
  1161. java_getHeader('X_JAVABRIDGE_OVERRIDE_HOSTS_REDIRECT',$_SERVER);
  1162. }
  1163. static function getHost() {
  1164. static $host=null;
  1165. if(is_null($host)) {
  1166. $hosts=explode(";",JAVA_HOSTS);
  1167. $host=explode(":",$hosts[0]);
  1168. while(count ($host) < 3) array_unshift($host,"");
  1169. if (substr($host[1],0,2)=="//") $host[1]=substr($host[1],2);
  1170. }
  1171. return $host;
  1172. }
  1173. function createHttpHandler() {
  1174. $overrideHosts=$this->getOverrideHosts();
  1175. $ssl="";
  1176. if($overrideHosts) {
  1177. $s=$overrideHosts;
  1178. if((strlen($s)>2) && ($s[1]==':')) {
  1179. if($s[0]=='s')
  1180. $ssl="ssl://";
  1181. $s=substr($s,2);
  1182. }
  1183. $webCtx=strpos($s,"//");
  1184. if($webCtx)
  1185. $host=substr($s,0,$webCtx);
  1186. else
  1187. $host=$s;
  1188. $idx=strpos($host,':');
  1189. if($idx) {
  1190. if($webCtx)
  1191. $port=substr($host,$idx+1,$webCtx);
  1192. else
  1193. $port=substr($host,$idx+1);
  1194. $host=substr($host,0,$idx);
  1195. } else {
  1196. $port="8080";
  1197. }
  1198. if($webCtx) $webCtx=substr($s,$webCtx+1);
  1199. $this->webContext=$webCtx;
  1200. } else {
  1201. $hostVec=java_Protocol::getHost();
  1202. if ($ssl=$hostVec[0]) $ssl .="://";
  1203. $host=$hostVec[1];
  1204. $port=$hostVec[2];
  1205. }
  1206. $this->serverName="${ssl}${host}:$port";
  1207. if ((array_key_exists("X_JAVABRIDGE_REDIRECT",$_SERVER)) ||
  1208. (array_key_exists("HTTP_X_JAVABRIDGE_REDIRECT",$_SERVER)))
  1209. return new java_SimpleHttpHandler($this,$ssl,$host,$port);
  1210. return new java_HttpTunnelHandler($this,$ssl,$host,$port);
  1211. }
  1212. function createSimpleHandler($name,$again=true) {
  1213. $channelName=$name;
  1214. $errno=null; $errstr=null;
  1215. if(is_numeric($channelName)) {
  1216. $peer=@pfsockopen($host="127.0.0.1",$channelName,$errno,$errstr,5);
  1217. } else {
  1218. $type=$channelName[0];
  1219. list($host,$channelName)=explode(":",$channelName);
  1220. $peer=pfsockopen($host,$channelName,$errno,$errstr,20);
  1221. if (!$peer)
  1222. throw new java_ConnectException("No Java server at $host:$channelName. Error message: $errstr ($errno)");
  1223. }
  1224. if (!$peer) {
  1225. $java=file_exists(ini_get("extension_dir")."/JavaBridge.jar")?ini_get("extension_dir")."/JavaBridge.jar":(java_get_base()."/JavaBridge.jar");
  1226. if (!file_exists($java))
  1227. throw 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";
  1229. if (!$again)
  1230. throw new java_ConnectException("No Java back end! Please run it with: $java_cmd. Error message: $errstr ($errno)");
  1231. if (!java_checkCliSapi())
  1232. trigger_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);
  1233. system ($java_cmd);
  1234. return $this->createSimpleHandler($name,false);
  1235. }
  1236. stream_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";
  1241. return $handler;
  1242. }
  1243. function java_get_simple_channel() {
  1244. return (JAVA_HOSTS&&(!JAVA_SERVLET||(JAVA_SERVLET=="Off"))) ? JAVA_HOSTS : null;
  1245. }
  1246. function createHandler() {
  1247. if(!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())) ) {
  1250. return $this->createSimpleHandler($defaultChannel);
  1251. } else {
  1252. return $this->createHttpHandler();
  1253. }
  1254. }
  1255. function java_Protocol ($client) {
  1256. $this->client=$client;
  1257. $this->handler=$this->createHandler();
  1258. }
  1259. function redirect() {
  1260. $this->handler->redirect();
  1261. }
  1262. function read($size) {
  1263. return $this->handler->read($size);
  1264. }
  1265. function sendData() {
  1266. $this->handler->write($this->client->sendBuffer);
  1267. $this->client->sendBuffer=null;
  1268. }
  1269. function flush() {
  1270. $this->sendData();
  1271. }
  1272. function getKeepAlive() {
  1273. return $this->handler->getKeepAlive();
  1274. }
  1275. function keepAlive() {
  1276. $this->handler->keepAlive();
  1277. }
  1278. function handle() {
  1279. $this->client->handleRequests();
  1280. }
  1281. function write($data) {
  1282. $this->client->sendBuffer.=$data;
  1283. }
  1284. function finish() {
  1285. $this->flush();
  1286. $this->handle();
  1287. $this->redirect();
  1288. }
  1289. function 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. }
  1297. function referenceEnd() {
  1298. $this->client->currentArgumentsFormat.=$format="</H>";
  1299. $this->write($format);
  1300. $this->finish();
  1301. $this->client->currentCacheKey=null;
  1302. }
  1303. function 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. }
  1311. function createObjectEnd() {
  1312. $this->client->currentArgumentsFormat.=$format="</K>";
  1313. $this->write($format);
  1314. $this->finish();
  1315. $this->client->currentCacheKey=null;
  1316. }
  1317. function 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. }
  1323. function propertyAccessEnd() {
  1324. $this->client->currentArgumentsFormat.=$format="</G>";
  1325. $this->write($format);
  1326. $this->finish();
  1327. $this->client->currentCacheKey=null;
  1328. }
  1329. function 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. }
  1335. function invokeEnd() {
  1336. $this->client->currentArgumentsFormat.=$format="</Y>";
  1337. $this->write($format);
  1338. $this->finish();
  1339. $this->client->currentCacheKey=null;
  1340. }
  1341. function resultBegin() {
  1342. $this->client->sendBuffer.=$this->client->preparedToSendBuffer;
  1343. $this->client->preparedToSendBuffer=null;
  1344. $this->write("<R>");
  1345. }
  1346. function resultEnd() {
  1347. $this->client->currentCacheKey=null;
  1348. $this->write("</R>");
  1349. $this->flush();
  1350. }
  1351. function writeString($name) {
  1352. $this->client->currentArgumentsFormat.=$format="<S v=\"%s\"/>";
  1353. $this->write(sprintf($format,htmlspecialchars($name,ENT_COMPAT)));
  1354. }
  1355. function writeBoolean($boolean) {
  1356. $this->client->currentArgumentsFormat.=$format="<T v=\"%s\"/>";
  1357. $this->write(sprintf($format,$boolean));
  1358. }
  1359. function writeLong($l) {
  1360. $this->client->currentArgumentsFormat.="<J v=\"%d\"/>";
  1361. if($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. }
  1367. function writeULong($l) {
  1368. $this->client->currentArgumentsFormat.=$format="<L v=\"%x\" p=\"O\"/>";
  1369. $this->write(sprintf($format,$l));
  1370. }
  1371. function writeDouble($d) {
  1372. $this->client->currentArgumentsFormat.=$format="<D v=\"%.14e\"/>";
  1373. $this->write(sprintf($format,$d));
  1374. }
  1375. function writeObject($object) {
  1376. $this->client->currentArgumentsFormat.=$format="<O v=\"%x\"/>";
  1377. $this->write(sprintf($format,$object));
  1378. }
  1379. function writeException($object,$str) {
  1380. $this->write(sprintf("<E v=\"%x\" m=\"%s\"/>",$object,htmlspecialchars($str,ENT_COMPAT)));
  1381. }
  1382. function writeCompositeBegin_a() {
  1383. $this->write("<X t=\"A\">");
  1384. }
  1385. function writeCompositeBegin_h() {
  1386. $this->write("<X t=\"H\">");
  1387. }
  1388. function writeCompositeEnd() {
  1389. $this->write("</X>");
  1390. }
  1391. function writePairBegin_s($key) {
  1392. $this->write(sprintf("<P t=\"S\" v=\"%s\">",htmlspecialchars($key,ENT_COMPAT)));
  1393. }
  1394. function writePairBegin_n($key) {
  1395. $this->write(sprintf("<P t=\"N\" v=\"%x\">",$key));
  1396. }
  1397. function writePairBegin() {
  1398. $this->write("<P>");
  1399. }
  1400. function writePairEnd() {
  1401. $this->write("</P>");
  1402. }
  1403. function writeUnref($object) {
  1404. $this->client->sendBuffer.=$this->client->preparedToSendBuffer;
  1405. $this->client->preparedToSendBuffer=null;
  1406. $this->write(sprintf("<U v=\"%x\"/>",$object));
  1407. }
  1408. function getServerName() {
  1409. return $this->serverName;
  1410. }
  1411. }
  1412. class java_ParserString {
  1413. public $string,$off,$length;
  1414. function toString() {
  1415. return $this->getString();
  1416. }
  1417. function getString() {
  1418. return substr($this->string,$this->off,$this->length);
  1419. }
  1420. }
  1421. class java_ParserTag {
  1422. public $n,$strings;
  1423. function java_ParserTag() {
  1424. $this->strings=array();
  1425. $this->n=0;
  1426. }
  1427. }
  1428. class java_SimpleParser {
  1429. public $SLEN=256;
  1430. public $handler;
  1431. public $tag,$buf,$len,$s;
  1432. public $type;
  1433. function 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. }
  1440. public $BEGIN=0,$KEY=1,$VAL=2,$ENTITY=3,$VOJD=5,$END=6;
  1441. public $level=0,$eor=0; public $in_dquote,$eot=false;
  1442. public $pos=0,$c=0,$i=0,$i0=0,$e;
  1443. function 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. }
  1451. function APPEND($c) {
  1452. if($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. }
  1458. function 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();
  1465. for($i=0; $i<$n; $i++) {
  1466. $ar[$pt[$i]->getString()]=$st[$i]->getString();
  1467. }
  1468. $this->handler->begin($name,$ar);
  1469. }
  1470. function CALL_END() {
  1471. $t=&$this->tag[0]->strings[0];
  1472. $name=$t->string[$t->off];
  1473. $this->handler->end($name);
  1474. }
  1475. function PUSH($t) {
  1476. $str=&$this->tag[$t]->strings;
  1477. $n=&$this->tag[$t]->n;
  1478. $this->s[$this->i]='|';
  1479. if(!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. }
  1487. function parse() {
  1488. while($this->eor==0) {
  1489. if($this->c>=$this->pos) {
  1490. $this->buf=$this->handler->read(JAVA_RECV_SIZE);
  1491. if(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);
  1494. if($this->pos==0) break;
  1495. $this->c=0;
  1496. }
  1497. switch(($ch=$this->buf[$this->c]))
  1498. {
  1499. case '<': if($this->in_dquote) {$this->APPEND($ch); break;}
  1500. $this->level+=1;
  1501. $this->type=$this->BEGIN;
  1502. break;
  1503. case '\t': case '\f': case '\n': case '\r': case ' ': if($this->in_dquote) {$this->APPEND($ch); break;}
  1504. if($this->type==$this->BEGIN) {
  1505. $this->PUSH($this->type);
  1506. $this->type=$this->KEY;
  1507. }
  1508. break;
  1509. case '=': if($this->in_dquote) {$this->APPEND($ch); break;}
  1510. $this->PUSH($this->type);
  1511. $this->type=$this->VAL;
  1512. break;
  1513. case '/': if($this->in_dquote) {$this->APPEND($ch); break;}
  1514. if($this->type==$this->BEGIN) { $this->type=$this->END; $this->level-=1; }
  1515. $this->level-=1;
  1516. $this->eot=true;
  1517. break;
  1518. case '>': if($this->in_dquote) {$this->APPEND($ch); break;}
  1519. if($this->type==$this->END){
  1520. $this->PUSH($this->BEGIN);
  1521. $this->CALL_END();
  1522. } else {
  1523. if($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;
  1528. if($this->level==0) $this->eor=1;
  1529. break;
  1530. case ';':
  1531. if($this->type==$this->ENTITY) {
  1532. switch ($this->s[$this->e+1]) {
  1533. case 'l': $this->s[$this->e]='<'; $this->i=$this->e+1; break;
  1534. case 'g': $this->s[$this->e]='>'; $this->i=$this->e+1; break;
  1535. case 'a': $this->s[$this->e]=($this->s[$this->e+2]=='m'?'&':'\''); $this->i=$this->e+1; break;
  1536. case 'q': $this->s[$this->e]='"'; $this->i=$this->e+1; break;
  1537. default: $this->APPEND($ch);
  1538. }
  1539. $this->type=$this->VAL;
  1540. } else {
  1541. $this->APPEND($ch);
  1542. }
  1543. break;
  1544. case '&':
  1545. $this->type=$this->ENTITY;
  1546. $this->e=$this->i;
  1547. $this->APPEND($ch);
  1548. break;
  1549. case '"':
  1550. $this->in_dquote=!$this->in_dquote;
  1551. if(!$this->in_dquote && $this->type==$this->VAL) {
  1552. $this->PUSH($this->type);
  1553. $this->type=$this->KEY;
  1554. }
  1555. break;
  1556. default:
  1557. $this->APPEND($ch);
  1558. }
  1559. $this->c+=1;
  1560. }
  1561. $this->RESET();
  1562. }
  1563. function getData($str) {
  1564. return $str;
  1565. }
  1566. function parserError() {
  1567. $this->handler->protocol->handler->shutdownBrokenConnection(
  1568. sprintf("protocol error: %s. Check the back end log for details.",$this->s));
  1569. }
  1570. }
  1571. interface java_JavaType {};
  1572. $java_initialized=false;
  1573. function __javaproxy_Client_getClient() {
  1574. static $client=null;
  1575. if(!is_null($client)) return $client;
  1576. if (function_exists("java_create_client")) $client=java_create_client();
  1577. else {
  1578. global $java_initialized;
  1579. $client=new java_Client();
  1580. $java_initialized=true;
  1581. }
  1582. return $client;
  1583. }
  1584. function java_last_exception_get() {
  1585. $client=__javaproxy_Client_getClient();
  1586. return $client->invokeMethod(0,"getLastException",array());
  1587. }
  1588. function java_last_exception_clear() {
  1589. $client=__javaproxy_Client_getClient();
  1590. $client->invokeMethod(0,"clearLastException",array());
  1591. }
  1592. function java_values_internal($object) {
  1593. if(!$object instanceof java_JavaType) return $object;
  1594. $client=__javaproxy_Client_getClient();
  1595. return $client->invokeMethod(0,"getValues",array($object));
  1596. }
  1597. function java_invoke($object,$method,$args) {
  1598. $client=__javaproxy_Client_getClient();
  1599. $id=($object==null) ? 0 : $object->__java;
  1600. return $client->invokeMethod($id,$method,$args);
  1601. }
  1602. function java_unwrap ($object) {
  1603. if(!$object instanceof java_JavaType) throw new java_IllegalArgumentException($object);
  1604. $client=__javaproxy_Client_getClient();
  1605. return $client->globalRef->get($client->invokeMethod(0,"unwrapClosure",array($object)));
  1606. }
  1607. function java_values($object) {
  1608. return java_values_internal($object);
  1609. }
  1610. function java_inspect_internal($object) {
  1611. if(!$object instanceof java_JavaType) throw new java_IllegalArgumentException($object);
  1612. $client=__javaproxy_Client_getClient();
  1613. return $client->invokeMethod(0,"inspect",array($object));
  1614. }
  1615. function java_inspect($object) {
  1616. return java_inspect_internal($object);
  1617. }
  1618. function java_set_file_encoding($enc) {
  1619. $client=__javaproxy_Client_getClient();
  1620. return $client->invokeMethod(0,"setFileEncoding",array($enc));
  1621. }
  1622. function java_instanceof_internal($ob,$clazz) {
  1623. if(!$ob instanceof java_JavaType) throw new java_IllegalArgumentException($ob);
  1624. if(!$clazz instanceof java_JavaType) throw new java_IllegalArgumentException($clazz);
  1625. $client=__javaproxy_Client_getClient();
  1626. return $client->invokeMethod(0,"instanceOf",array($ob,$clazz));
  1627. }
  1628. function java_instanceof($ob,$clazz) {
  1629. return java_instanceof_internal($ob,$clazz);
  1630. }
  1631. function java_cast_internal($object,$type) {
  1632. if(!$object instanceof java_JavaType) {
  1633. switch($type[0]) {
  1634. case 'S': case 's':
  1635. return (string)$object;
  1636. case 'B': case 'b':
  1637. return (boolean)$object;
  1638. case 'L': case 'I': case 'l': case 'i':
  1639. return (integer)$object;
  1640. case 'D': case 'd': case 'F': case 'f':
  1641. return (float) $object;
  1642. case 'N': case 'n':
  1643. return null;
  1644. case 'A': case 'a':
  1645. return (array)$object;
  1646. case 'O': case 'o':
  1647. return (object)$object;
  1648. }
  1649. }
  1650. return $object->__cast($type);
  1651. }
  1652. function java_cast($object,$type) {
  1653. return java_cast_internal($object,$type);
  1654. }
  1655. function java_require($arg) {
  1656. trigger_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. }
  1658. function java_get_lifetime ()
  1659. {
  1660. $session_max_lifetime=ini_get("session.gc_maxlifetime");
  1661. return $session_max_lifetime ? (int)$session_max_lifetime : 1440;
  1662. }
  1663. function java_session_array($args) {
  1664. $client=__javaproxy_Client_getClient();
  1665. if(!isset($args[0])) $args[0]=null;
  1666. if(!isset($args[1]))
  1667. $args[1]=0;
  1668. elseif ($args[1]===true)
  1669. $args[1]=1;
  1670. else
  1671. $args[1]=2;
  1672. if(!isset($args[2])) {
  1673. $args[2]=java_get_lifetime ();
  1674. }
  1675. return $client->getSession($args);
  1676. }
  1677. function java_session() {
  1678. return java_session_array(func_get_args());
  1679. }
  1680. function java_server_name() {
  1681. try {
  1682. $client=__javaproxy_Client_getClient();
  1683. return $client->getServerName();
  1684. } catch (java_ConnectException $ex) {
  1685. return null;
  1686. }
  1687. }
  1688. function java_context() {
  1689. $client=__javaproxy_Client_getClient();
  1690. return $client->getContext();
  1691. }
  1692. function java_closure_array($args) {
  1693. if(isset($args[2]) && ((!($args[2] instanceof java_JavaType))&&!is_array($args[2])))
  1694. throw new java_IllegalArgumentException($args[2]);
  1695. $client=__javaproxy_Client_getClient();
  1696. $args[0]=isset($args[0]) ? $client->globalRef->add($args[0]) : 0;
  1697. $client->protocol->invokeBegin(0,"makeClosure");
  1698. $n=count($args);
  1699. $client->protocol->writeULong($args[0]);
  1700. for($i=1; $i<$n; $i++) {
  1701. $client->writeArg($args[$i]);
  1702. }
  1703. $client->protocol->invokeEnd();
  1704. $val=$client->getResult();
  1705. return $val;
  1706. }
  1707. function java_closure() {
  1708. return java_closure_array(func_get_args());
  1709. }
  1710. function java_begin_document() {
  1711. }
  1712. function java_end_document() {
  1713. }
  1714. class java_JavaProxy implements java_JavaType {
  1715. public $__serialID,$__java;
  1716. public $__signature;
  1717. public $__client;
  1718. public $__tempGlobalRef;
  1719. function java_JavaProxy($java,$signature){
  1720. $this->__java=$java;
  1721. $this->__signature=$signature;
  1722. $this->__client=__javaproxy_Client_getClient();
  1723. }
  1724. function __cast($type) {
  1725. return $this->__client->cast($this,$type);
  1726. }
  1727. function __sleep() {
  1728. $args=array($this,java_get_lifetime());
  1729. $this->__serialID=$this->__client->invokeMethod(0,"serialize",$args);
  1730. $this->__tempGlobalRef=$this->__client->globalRef;
  1731. return array("__serialID","__tempGlobalRef");
  1732. }
  1733. function __wakeup() {
  1734. $args=array($this->__serialID,java_get_lifetime());
  1735. $this->__client=__javaproxy_Client_getClient();
  1736. if($this->__tempGlobalRef)
  1737. $this->__client->globalRef=$this->__tempGlobalRef;
  1738. $this->__tempGlobalRef=null;
  1739. $this->__java=$this->__client->invokeMethod(0,"deserialize",$args);
  1740. }
  1741. function __destruct() {
  1742. if(isset($this->__client))
  1743. $this->__client->unref($this->__java);
  1744. }
  1745. function __get($key) {
  1746. return $this->__client->getProperty($this->__java,$key);
  1747. }
  1748. function __set($key,$val) {
  1749. $this->__client->setProperty($this->__java,$key,$val);
  1750. }
  1751. function __call($method,$args) {
  1752. return $this->__client->invokeMethod($this->__java,$method,$args);
  1753. }
  1754. function __toString() {
  1755. try {
  1756. return $this->__client->invokeMethod(0,"ObjectToString",array($this));
  1757. } catch (JavaException $ex) {
  1758. trigger_error("Exception in Java::__toString(): ". java_truncate((string)$ex),E_USER_WARNING);
  1759. return "";
  1760. }
  1761. }
  1762. }
  1763. class java_objectIterator implements Iterator {
  1764. private $var;
  1765. function java_ObjectIterator($javaProxy) {
  1766. $this->var=java_cast ($javaProxy,"A");
  1767. }
  1768. function rewind() {
  1769. reset($this->var);
  1770. }
  1771. function valid() {
  1772. return $this->current() !==false;
  1773. }
  1774. function next() {
  1775. return next($this->var);
  1776. }
  1777. function key() {
  1778. return key($this->var);
  1779. }
  1780. function current() {
  1781. return current($this->var);
  1782. }
  1783. }
  1784. class java_IteratorProxy extends java_JavaProxy implements IteratorAggregate {
  1785. function getIterator() {
  1786. return new java_ObjectIterator($this);
  1787. }
  1788. }
  1789. class java_ArrayProxy extends java_IteratorProxy implements ArrayAccess {
  1790. function offsetExists($idx) {
  1791. $ar=array($this,$idx);
  1792. return $this->__client->invokeMethod(0,"offsetExists",$ar);
  1793. }
  1794. function offsetGet($idx) {
  1795. $ar=array($this,$idx);
  1796. return $this->__client->invokeMethod(0,"offsetGet",$ar);
  1797. }
  1798. function offsetSet($idx,$val) {
  1799. $ar=array($this,$idx,$val);
  1800. return $this->__client->invokeMethod(0,"offsetSet",$ar);
  1801. }
  1802. function offsetUnset($idx) {
  1803. $ar=array($this,$idx);
  1804. return $this->__client->invokeMethod(0,"offsetUnset",$ar);
  1805. }
  1806. }
  1807. class java_ExceptionProxy extends java_JavaProxy {
  1808. function __toExceptionString($trace) {
  1809. $args=array($this,$trace);
  1810. return $this->__client->invokeMethod(0,"ObjectToString",$args);
  1811. }
  1812. }
  1813. abstract class java_AbstractJava implements IteratorAggregate,ArrayAccess,java_JavaType {
  1814. public $__client;
  1815. public $__delegate;
  1816. public $__serialID;
  1817. public $__factory;
  1818. public $__java,$__signature;
  1819. public $__cancelProxyCreationTag;
  1820. function __createDelegate() {
  1821. $proxy=$this->__delegate=
  1822. $this->__factory->create($this->__java,$this->__signature);
  1823. $this->__java=$proxy->__java;
  1824. $this->__signature=$proxy->__signature;
  1825. }
  1826. function __cast($type) {
  1827. if(!isset($this->__delegate)) $this->__createDelegate();
  1828. return $this->__delegate->__cast($type);
  1829. }
  1830. function __sleep() {
  1831. if(!isset($this->__delegate)) $this->__createDelegate();
  1832. $this->__delegate->__sleep();
  1833. return array("__delegate");
  1834. }
  1835. function __wakeup() {
  1836. if(!isset($this->__delegate)) $this->__createDelegate();
  1837. $this->__delegate->__wakeup();
  1838. $this->__java=$this->__delegate->__java;
  1839. $this->__client=$this->__delegate->__client;
  1840. }
  1841. function __get($key) {
  1842. if(!isset($this->__delegate)) $this->__createDelegate();
  1843. return $this->__delegate->__get($key);
  1844. }
  1845. function __set($key,$val) {
  1846. if(!isset($this->__delegate)) $this->__createDelegate();
  1847. $this->__delegate->__set($key,$val);
  1848. }
  1849. function __call($method,$args) {
  1850. if(!isset($this->__delegate)) $this->__createDelegate();
  1851. return $this->__delegate->__call($method,$args);
  1852. }
  1853. function __toString() {
  1854. if(!isset($this->__delegate)) $this->__createDelegate();
  1855. return $this->__delegate->__toString();
  1856. }
  1857. function getIterator() {
  1858. if(!isset($this->__delegate)) $this->__createDelegate();
  1859. if(func_num_args()==0) return $this->__delegate->getIterator();
  1860. $args=func_get_args(); return $this->__call("getIterator",$args);
  1861. }
  1862. function offsetExists($idx) {
  1863. if(!isset($this->__delegate)) $this->__createDelegate();
  1864. if(func_num_args()==1) return $this->__delegate->offsetExists($idx);
  1865. $args=func_get_args(); return $this->__call("offsetExists",$args);
  1866. }
  1867. function offsetGet($idx) {
  1868. if(!isset($this->__delegate)) $this->__createDelegate();
  1869. if(func_num_args()==1) return $this->__delegate->offsetGet($idx);
  1870. $args=func_get_args(); return $this->__call("offsetGet",$args);
  1871. }
  1872. function offsetSet($idx,$val) {
  1873. if(!isset($this->__delegate)) $this->__createDelegate();
  1874. if(func_num_args()==2) return $this->__delegate->offsetSet($idx,$val);
  1875. $args=func_get_args(); return $this->__call("offsetSet",$args);
  1876. }
  1877. function offsetUnset($idx) {
  1878. if(!isset($this->__delegate)) $this->__createDelegate();
  1879. if(func_num_args()==1) return $this->__delegate->offsetUnset($idx);
  1880. $args=func_get_args(); return $this->__call("offsetUnset",$args);
  1881. }
  1882. }
  1883. class Java extends java_AbstractJava {
  1884. function Java() {
  1885. $client=$this->__client=__javaproxy_Client_getClient();
  1886. $args=func_get_args();
  1887. $name=array_shift($args);
  1888. if(is_array($name)) {$args=$name; $name=array_shift($args);}
  1889. $sig="&{$this->__signature}@{$name}";
  1890. $len=count($args);
  1891. $args2=array();
  1892. for($i=0; $i<$len; $i++) {
  1893. switch(gettype($val=$args[$i])) {
  1894. case 'boolean': array_push($args2,$val); $sig.='@b'; break;
  1895. case 'integer': array_push($args2,$val); $sig.='@i'; break;
  1896. case 'double': array_push($args2,$val); $sig.='@d'; break;
  1897. case 'string': array_push($args2,htmlspecialchars($val,ENT_COMPAT)); $sig.='@s'; break;
  1898. case 'array':$sig="~INVALID"; break;
  1899. case 'object':
  1900. if($val instanceof java_JavaType) {
  1901. array_push($args2,$val->__java);
  1902. $sig.="@o{$val->__signature}";
  1903. }
  1904. else {
  1905. $sig="~INVALID";
  1906. }
  1907. break;
  1908. case 'resource': array_push($args2,$val); $sig.='@r'; break;
  1909. case 'NULL': array_push($args2,$val); $sig.='@N'; break;
  1910. case 'unknown type': array_push($args2,$val); $sig.='@u'; break;
  1911. default: throw new java_IllegalArgumentException($val);
  1912. }
  1913. }
  1914. if(array_key_exists($sig,$client->methodCache)) {
  1915. $cacheEntry=&$client->methodCache[$sig];
  1916. $client->sendBuffer.=$client->preparedToSendBuffer;
  1917. if(strlen($client->sendBuffer)>=JAVA_SEND_SIZE) {
  1918. if($client->protocol->handler->write($client->sendBuffer)<=0)
  1919. throw new java_IllegalStateException("Connection out of sync,check backend log for details.");
  1920. $client->sendBuffer=null;
  1921. }
  1922. $client->preparedToSendBuffer=vsprintf($cacheEntry->fmt,$args2);
  1923. $this->__java=++$client->asyncCtx;
  1924. $this->__factory=$cacheEntry->factory;
  1925. $this->__signature=$cacheEntry->signature;
  1926. $this->__cancelProxyCreationTag=++$client->cancelProxyCreationTag;
  1927. } else {
  1928. $client->currentCacheKey=$sig;
  1929. $delegate=$this->__delegate=$client->createObject($name,$args);
  1930. $this->__java=$delegate->__java;
  1931. $this->__signature=$delegate->__signature;
  1932. }
  1933. }
  1934. function __destruct() {
  1935. if(!isset($this->__client)) return;
  1936. $client=$this->__client;
  1937. $preparedToSendBuffer=&$client->preparedToSendBuffer;
  1938. if($preparedToSendBuffer &&
  1939. $client->cancelProxyCreationTag==$this->__cancelProxyCreationTag) {
  1940. $preparedToSendBuffer[6]="3";
  1941. $client->sendBuffer.=$preparedToSendBuffer;
  1942. $preparedToSendBuffer=null;
  1943. $client->asyncCtx -=1;
  1944. } else {
  1945. if(!isset($this->__delegate)) {
  1946. $client->unref($this->__java);
  1947. }
  1948. }
  1949. }
  1950. function __call($method,$args) {
  1951. $client=$this->__client;
  1952. $sig="@{$this->__signature}@$method";
  1953. $len=count($args);
  1954. $args2=array($this->__java);
  1955. for($i=0; $i<$len; $i++) {
  1956. switch(gettype($val=$args[$i])) {
  1957. case 'boolean': array_push($args2,$val); $sig.='@b'; break;
  1958. case 'integer': array_push($args2,$val); $sig.='@i'; break;
  1959. case 'double': array_push($args2,$val); $sig.='@d'; break;
  1960. case 'string': array_push($args2,htmlspecialchars($val,ENT_COMPAT)); $sig.='@s'; break;
  1961. case 'array':$sig="~INVALID"; break;
  1962. case 'object':
  1963. if($val instanceof java_JavaType) {
  1964. array_push($args2,$val->__java);
  1965. $sig.="@o{$val->__signature}";
  1966. }
  1967. else {
  1968. $sig="~INVALID";
  1969. }
  1970. break;
  1971. case 'resource': array_push($args2,$val); $sig.='@r'; break;
  1972. case 'NULL': array_push($args2,$val); $sig.='@N'; break;
  1973. case 'unknown type': array_push($args2,$val); $sig.='@u'; break;
  1974. default: throw new java_IllegalArgumentException($val);
  1975. }
  1976. }
  1977. if(array_key_exists($sig,$client->methodCache)) {
  1978. $cacheEntry=&$client->methodCache[$sig];
  1979. $client->sendBuffer.=$client->preparedToSendBuffer;
  1980. if(strlen($client->sendBuffer)>=JAVA_SEND_SIZE) {
  1981. if($client->protocol->handler->write($client->sendBuffer)<=0)
  1982. throw new java_IllegalStateException("Out of sync. Check backend log for details.");
  1983. $client->sendBuffer=null;
  1984. }
  1985. $client->preparedToSendBuffer=vsprintf($cacheEntry->fmt,$args2);
  1986. if($cacheEntry->resultVoid) {
  1987. $client->cancelProxyCreationTag +=1;
  1988. return null;
  1989. } else {
  1990. $result=clone($client->cachedJavaPrototype);
  1991. $result->__factory=$cacheEntry->factory;
  1992. $result->__java=++$client->asyncCtx;
  1993. $result->__signature=$cacheEntry->signature;
  1994. $result->__cancelProxyCreationTag=++$client->cancelProxyCreationTag;
  1995. return $result;
  1996. }
  1997. } else {
  1998. $client->currentCacheKey=$sig;
  1999. $retval=parent::__call($method,$args);
  2000. return $retval;
  2001. }
  2002. }
  2003. }
  2004. class java_InternalJava extends Java {
  2005. function java_InternalJava($proxy) {
  2006. $this->__delegate=$proxy;
  2007. $this->__java=$proxy->__java;
  2008. $this->__signature=$proxy->__signature;
  2009. $this->__client=$proxy->__client;
  2010. }
  2011. }
  2012. class java_class extends Java {
  2013. function java_class() {
  2014. $this->__client=__javaproxy_Client_getClient();
  2015. $args=func_get_args();
  2016. $name=array_shift($args);
  2017. if(is_array($name)) { $args=$name; $name=array_shift($args); }
  2018. $delegate=$this->__delegate=$this->__client->referenceObject($name,$args);
  2019. $this->__java=$delegate->__java;
  2020. $this->__signature=$delegate->__signature;
  2021. }
  2022. }
  2023. class JavaClass extends java_class{}
  2024. class java_exception extends Exception implements java_JavaType {
  2025. public $__serialID,$__java,$__client;
  2026. public $__delegate;
  2027. public $__signature;
  2028. public $__hasDeclaredExceptions;
  2029. function java_exception() {
  2030. $this->__client=__javaproxy_Client_getClient();
  2031. $args=func_get_args();
  2032. $name=array_shift($args);
  2033. if(is_array($name)) { $args=$name; $name=array_shift($args); }
  2034. if (count($args)==0)
  2035. Exception::__construct($name);
  2036. else
  2037. Exception::__construct($args[0]);
  2038. $delegate=$this->__delegate=$this->__client->createObject($name,$args);
  2039. $this->__java=$delegate->__java;
  2040. $this->__signature=$delegate->__signature;
  2041. $this->__hasDeclaredExceptions='T';
  2042. }
  2043. function __cast($type) {
  2044. return $this->__delegate->__cast($type);
  2045. }
  2046. function __sleep() {
  2047. $this->__delegate->__sleep();
  2048. return array("__delegate");
  2049. }
  2050. function __wakeup() {
  2051. $this->__delegate->__wakeup();
  2052. $this->__java=$this->__delegate->__java;
  2053. $this->__client=$this->__delegate->__client;
  2054. }
  2055. function __get($key) {
  2056. return $this->__delegate->__get($key);
  2057. }
  2058. function __set($key,$val) {
  2059. $this->__delegate->__set($key,$val);
  2060. }
  2061. function __call($method,$args) {
  2062. return $this->__delegate->__call($method,$args);
  2063. }
  2064. function __toString() {
  2065. return $this->__delegate->__toExceptionString($this->getTraceAsString());
  2066. }
  2067. }
  2068. class JavaException extends java_exception {}
  2069. class java_InternalException extends JavaException {
  2070. function java_InternalException($proxy,$exception) {
  2071. $this->__delegate=$proxy;
  2072. $this->__java=$proxy->__java;
  2073. $this->__signature=$proxy->__signature;
  2074. $this->__client=$proxy->__client;
  2075. $this->__hasDeclaredExceptions=$exception;
  2076. }
  2077. }
  2078. class java_JavaProxyProxy extends Java {
  2079. function java_JavaProxyProxy($client) {
  2080. $this->__client=$client;
  2081. }
  2082. }
  2083. }
  2084. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement