Source for file JavaProxy.inc

Documentation is available at JavaProxy.inc

  1. <?php /*-*- mode: php; tab-width:4 -*-*/
  2.  
  3.   /* java_Proxy.php -- contains the main interface
  4.  
  5.   Copyright (C) 2003-2007 Jost Boekemeier
  6.  
  7.   This file is part of the PHP/Java Bridge.
  8.  
  9.   The PHP/Java Bridge ("the library") is free software; you can
  10.   redistribute it and/or modify it under the terms of the GNU General
  11.   Public License as published by the Free Software Foundation; either
  12.   version 2, or (at your option) any later version.
  13.  
  14.   The library is distributed in the hope that it will be useful, but
  15.   WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.   General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU General Public License
  20.   along with the PHP/Java Bridge; see the file COPYING.  If not, write to the
  21.   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22.   02111-1307 USA.
  23.  
  24.   Linking this file statically or dynamically with other modules is
  25.   making a combined work based on this library.  Thus, the terms and
  26.   conditions of the GNU General Public License cover the whole
  27.   combination.
  28.  
  29.   As a special exception, the copyright holders of this library give you
  30.   permission to link this library with independent modules to produce an
  31.   executable, regardless of the license terms of these independent
  32.   modules, and to copy and distribute the resulting executable under
  33.   terms of your choice, provided that you also meet, for each linked
  34.   independent module, the terms and conditions of the license of that
  35.   module.  An independent module is a module which is not derived from
  36.   or based on this library.  If you modify this library, you may extend
  37.   this exception to your version of the library, but you are not
  38.   obligated to do so.  If you do not wish to do so, delete this
  39.   exception statement from your version. */
  40.  
  41. require_once("${JAVA_BASE}/Client.inc");
  42.  
  43. /**
  44.  * Implemented by JavaException and Java.
  45.  * @see Java
  46.  * @see JavaException
  47.  * @access public
  48.  */
  49. interface java_JavaType {};
  50.  
  51. /**
  52.  * @access private
  53.  */
  54. function __javaproxy_Client_getClient({
  55.   static $client;
  56.   if(isset($client)) return $client;
  57.   
  58.   if (function_exists("java_create_client")) $client java_create_client();
  59.   else $client=new java_Client();
  60.   
  61.   return $client;
  62. }
  63.  
  64. /**
  65.  * @deprecated: Use PHP5 try/catch instead
  66.  * @access private
  67.  */
  68. function java_last_exception_get({
  69.   $client=__javaproxy_Client_getClient();
  70.   return $client->getProperty(0"lastException");
  71. }
  72. /**
  73.  * @deprecated: Use PHP5 try/catch instead.
  74.  * @access private
  75.  */
  76. function java_last_exception_clear({
  77.   $client=__javaproxy_Client_getClient();
  78.   $client->setProperty(0"lastException"null);
  79. }
  80. /**
  81.  * Only for internal use
  82.  * @access private
  83.  */
  84. function java_values_internal($object{
  85.   if(!$object instanceof java_JavaTypereturn $object;
  86.   $client=__javaproxy_Client_getClient();
  87.   return $client->invokeMethod(0"getValues"array($object));
  88. }
  89. /**
  90.  * Unwraps a Java object.
  91.  * 
  92.  * Evaluates the object and fetches its content, if possible. Use java_values() to convert a Java object into an equivalent PHP value.
  93.  *
  94.  * A java array, Map or Collection object is returned
  95.  * as a php array. An array, Map or Collection proxy is returned as a java array, Map or Collection object, and a null proxy is returned as null. All values of java types for which a primitive php type exists are returned as php values. Everything else is returned unevaluated. Please make sure that the values do not not exceed
  96.  * php's memory limit. Example:
  97.  *
  98.  * 
  99.  * <code>
  100.  * $str = new java("java.lang.String", "hello");
  101.  * echo java_values($str);
  102.  * => hello
  103.  * $chr = $str->toCharArray();
  104.  * echo $chr;
  105.  * => [o(array_of-C):"[C@1b10d42"]
  106.  * $ar = java_values($chr);
  107.  * print $ar;
  108.  * => Array
  109.  * print $ar[0];
  110.  * => [o(Character):"h"]
  111.  * print java_values($ar[0]);
  112.  * => h
  113.  * </code>
  114.  * 
  115.  * @see java_closure()
  116.  * @param object  java object or type.
  117.  * @access public
  118.  */
  119. function java_values($object{
  120.   return java_values_internal($object);
  121. }
  122. /**
  123.  * Only for internal use.
  124.  * @access public
  125.  */
  126. function java_reset({
  127.   $client=__javaproxy_Client_getClient();
  128.   user_error("Your script has called the privileged procedure \"java_reset()\" which resets the java back-end to its initial state. Therefore all java caches are gone.");
  129.   return $client->invokeMethod(0"reset"array());
  130. }
  131. /**
  132.  * Only for internal use
  133.  * @access private
  134.  */
  135. function java_inspect_internal($object{
  136.   if(!$object instanceof java_JavaTypethrow new java_IllegalArgumentException($object);
  137.   $client=__javaproxy_Client_getClient();
  138.   return $client->invokeMethod(0"inspect"array($object));
  139. }
  140. /**
  141.  * Returns the contents (public fields, public methods, public
  142.  * classes) of object as a string.
  143.  *
  144.  * Example:
  145.  * <code>
  146.  * echo java_inspect(java_context());
  147.  * </code>
  148.  * @param object java object or type.
  149.  * @access public
  150.  */
  151. function java_inspect($object{
  152.   return java_inspect_internal($object);
  153. }
  154. /**
  155.  * Set the java file encoding, for example UTF-8 or ASCII.
  156.  *
  157.  * Needed
  158.  * because php does not support unicode. All string to byte array
  159.  * conversions use this encoding. Example:
  160.  * <code>
  161.  * java_set_file_encoding("ISO-8859-1");
  162.  * </code>
  163.  *
  164.  * @param string A valid file.encoding string. Please see your Java
  165.  *  <code>file.encoding</code> documentation for a list of valid
  166.  *  encodings.
  167.  * @access public
  168.  */
  169. function java_set_file_encoding($enc{
  170.   $client=__javaproxy_Client_getClient();
  171.   return $client->invokeMethod(0"setFileEncoding"array($enc));
  172. }
  173. /**
  174.  * Only for internal use
  175.  * @access private
  176.  */
  177. function java_instanceof_internal($ob$clazz{
  178.   if(!$ob instanceof java_JavaTypethrow new java_IllegalArgumentException($ob);
  179.   if(!$clazz instanceof java_JavaTypethrow new java_IllegalArgumentException($clazz);
  180.   $client=__javaproxy_Client_getClient();
  181.   return $client->invokeMethod(0"instanceOf"array($ob$clazz));
  182. }
  183. /**
  184.  * Tests if object is an instance of clazz.
  185.  *
  186.  * Example:
  187.  * <code>
  188.  * return($o instanceof Java && $c instanceof Java && java_instanceof($o, $c));
  189.  * </code>
  190.  * @param object java object
  191.  * @param object java object or type.
  192.  * @access public
  193.  */
  194. function java_instanceof($ob$clazz{
  195.   return java_instanceof_internal($ob$clazz);
  196. }
  197. /**
  198.  * Only for internal use
  199.  * @access private
  200.  */
  201. function java_cast_internal($object$type
  202.     if(!$object instanceof java_JavaType{
  203.       switch($type[0]{
  204.     case 'S'case 's':
  205.       return (string)$object;
  206.     case 'B'case 'b':
  207.       return (boolean)$object;
  208.     case 'L'case 'I'case 'l'case 'i':
  209.       return (integer)$object;
  210.     case 'D'case 'd'case 'F'case 'f':
  211.       return (float) $object;
  212.     case 'N'case 'n':
  213.       return null;
  214.     case 'A'case 'a':
  215.       return (array)$object;
  216.     case 'O'case 'o':
  217.       return (object)$object;
  218.       }
  219.     }    
  220.     return $object->__cast($type)
  221. }
  222. /**
  223.  * Converts the java object obj into a PHP value.
  224.  *
  225.  * This procedure converts the Java argument and then calls java_values() to fetch
  226.  * its content. Use java_values() if the conversion is not necessary.
  227.  * 
  228.  * The second argument
  229.  * must be [s]tring, [b]oolean, [i]nteger, [f]loat or [d]ouble,
  230.  * [a]rray, [n]ull or [o]bject (which does nothing).
  231.  *
  232.  * <br>
  233.  * Example:
  234.  * <code>
  235.  * $str = new java("java.lang.String", "12");
  236.  * echo is_string ($str) ? "#t":"#f";
  237.  * => #f
  238.  * $phpString = (string)$str;
  239.  * echo is_string ($phpString) ? "#t":"#f";
  240.  * => #t
  241.  * $phpNumber = (integer)(string)$str;
  242.  * echo $phpNumber;
  243.  * => 12
  244.  * $phpNumber2 = java_cast($str, "integer");
  245.  * echo $phpNumber2;
  246.  * => 12
  247.  * </code>
  248.  * @see java_values()
  249.  * @param object java object
  250.  * @param string A PHP type description, either [Ss]tring, [Bb]oolean, [Ll]ong or [Ii]nteger, [Dd]ouble or [Ff]loat, [Nn]ull, [Aa]rray, [Oo]bject.
  251.  * @access public
  252.  */
  253. function java_cast($object$type
  254.   return java_cast_internal($object$type);
  255. }
  256.  
  257. /**
  258.  * Set the library path.
  259.  *
  260.  * Example:
  261.  * <code>
  262.  * java_require("foo.jar;bar.jar");
  263.  * </code>
  264.  *
  265.  * The .jar files should be stored in /usr/share/java or
  266.  * extension_dir/lib one of its sub-directories or under the PHP
  267.  * include_path/LIBNAME/LIBNAME.jar. However, it is also possible to
  268.  * fetch .jar files from a remote server, for example:
  269.  * <code>
  270.  * java_require("http://php-java-bridge.sf.net/kawa.jar;...");
  271.  * </code>
  272.  *
  273.  * Note that Java doesn't have a module system.
  274.  * Therefore libraries and their dependencies must be loaded by one and only one class loader;
  275.  * do not report a ClassCastException saying that a class is incompatible to itself or
  276.  * a NoClassDefFoundError to the PHP/Java Bridge mailing list.
  277.  * @param string The list of Java libraries.
  278.  * @see java_autoload()
  279.  * @access public
  280.  */
  281. function java_require($arg{
  282.   $client=__javaproxy_Client_getClient();
  283.   return $client->invokeMethod(0"updateJarLibraryPath"
  284.                                array($argini_get("extension_dir")getcwd()ini_get("include_path")));
  285. }
  286. /**
  287.  * @access private
  288.  */
  289. function java_get_lifetime ()
  290. {
  291.   $session_max_lifetime=ini_get("session.gc_maxlifetime");
  292.   return $session_max_lifetime ? (int)$session_max_lifetime 1440;
  293. }
  294.   
  295. /**
  296.  * Only for internal use
  297.  * @access private
  298.  */
  299. function java_session_array($args{
  300.   $client=__javaproxy_Client_getClient();
  301.   if(!isset($args[0])) $args[0]=null;
  302.   if(!isset($args[1])) $args[1]=false;
  303.   if(!isset($args[2])) {
  304.     $args[2java_get_lifetime ();
  305.   }
  306.   return $client->getSession($args);
  307. }
  308. /**
  309.  * Return a session handle.
  310.  *
  311.  * When java_session() is called without
  312.  * arguments, the session is shared with java.
  313.  * Example:
  314.  * <code>
  315.  * java_session()->put("key", new Java("java.lang.Object"));
  316.  * [...]
  317.  * </code>
  318.  * The java components (jsp, servlets) can retrieve the value, for
  319.  * example with:
  320.  * <code>
  321.  * getSession().getAttribute("key");
  322.  * </code>
  323.  *
  324.  * When java_session() is called with a session name, the session
  325.  * is not shared with java and no cookies are set. Example:
  326.  * <code>
  327.  * java_session("myPublicApplicationStore")->put("key", "value");
  328.  * </code>
  329.  *
  330.  * When java_session() is called with a second argument set to true,
  331.  * a new session is allocated, the old session is destroyed if necessary.
  332.  * Example:
  333.  * <code>
  334.  * java_session(null, true)->put("key", "val");
  335.  * </code>
  336.  *
  337.  * The optional third argument specifies the default lifetime of the session, it defaults to <code> session.gc_maxlifetime </code>. The value 0 means that the session never times out.
  338.  *
  339.  * @access public
  340.  * @see java_context()
  341.  */
  342. function java_session({
  343.   return java_session_array(func_get_args());
  344. }
  345.  
  346. /**
  347.  * Returns the name of the back-end or null, if the back-end is not running.O
  348.  *
  349.  * Example:
  350.  * <code>
  351.  * $backend = java_server_name();
  352.  * if(!$backend) wakeup_administrator("back-end not running");
  353.  * echo "Connected to the back-end: $backend\n";
  354.  * </code>
  355.  * @access public
  356. */
  357. function java_server_name({
  358.   try {
  359.     $client=__javaproxy_Client_getClient();
  360.     return $client->getServerName();
  361.   catch (java_ConnectException $ex{
  362.     return null;
  363.   }
  364. }
  365.  
  366. /**
  367.  * Returns the jsr223 script context handle.
  368.  *
  369.  * Example which closes over the current environment and passes it back to java:
  370.  * <code>
  371.  * define ("ENGINE_SCOPE", 100);
  372.  * $ctx = java_context();
  373.  * if(java_is_false($ctx->call(java_closure()))) die "Script should be called from java";
  374.  * </code>
  375.  * 
  376.  * A second example which shows how to invoke PHP methods without the JSR 223 getInterface() and invokeMethod()
  377.  * helper procedures:
  378.  * <code>
  379.  * String s = "<?php class Runnable { function run() {...} };
  380.  *            // example which captures an environment and
  381.  *            // passes it as a continuation back to Java
  382.  *            $Runnable = java('java.lang.Runnable');
  383.  *            $closure = java_closure(new Runnable(), null, $Runnable);
  384.  *            java_context()->setAttribute("kont", $closure, ENGINE_SCOPE);
  385.  *            java_context()->call($closure);
  386.  *            ?>";
  387.  * ScriptEngine e = new ScriptEngineManager().getEngineByName("php-invocable");
  388.  * e.eval (s);
  389.  * Runnable r = e.get("kont");
  390.  * new Thread(r).start();
  391.  * </code>
  392.  * 
  393.  * It is possible to access implicit web objects (the session, the
  394.  * application store etc.) from the context. Please see the JSR223
  395.  * documentation for details. Example:
  396.  * <code>
  397.  * $req = $ctx->getHttpServletRequest();
  398.  * $res = $ctx->getHttpServletResponse();
  399.  * </code>
  400.  *
  401.  * Example which fetches the servlet-, config and context:
  402.  * <code>
  403.  * $config = $ctx->getAttribute ( "php.java.servlet.ServletConfig",  ENGINE_SCOPE);
  404.  * $context = $ctx->getAttribute( "php.java.servlet.ServletContext", ENGINE_SCOPE);
  405.  * $servlet = $ctx->getAttribute( "php.java.servlet.Servlet", ENGINE_SCOPE);
  406.  * </code>
  407.  *
  408.  * @access public
  409.  * @see java_session()
  410.  */
  411. function java_context({
  412.   $client=__javaproxy_Client_getClient();
  413.   return $client->getContext();
  414. }
  415. /**
  416.  * Only for internal use
  417.  * @access private
  418.  */
  419. function java_closure_array($args{
  420.   if(isset($args[2]&& ((!($args[2instanceof java_JavaType))&&!is_array($args[2])))
  421.     throw new java_IllegalArgumentException($args[2]);
  422.  
  423.   $client=__javaproxy_Client_getClient();
  424.   $args[0= isset($args[0]$client->globalRef->add($args[0]0;
  425.  
  426.   /* The following is identical to 
  427.    return $client->invokeMethod(0, "makeClosure", $args); 
  428.    except that the ref (args[0]) must be an unsigned value */
  429.   $client->protocol->invokeBegin(0"makeClosure""5""6");
  430.   $n count($args);
  431.   $client->protocol->writeULong($args[0])// proper PHP "long" -> Java 64 bit value conversion
  432.   for($i=1$i<$n$i++{
  433.     $client->writeArg($args[$i]);
  434.   }
  435.   $client->protocol->invokeEnd();
  436.   $val $client->getResult();
  437.   return $val;
  438. }
  439. /**
  440.  * Wraps a PHP environment.
  441.  * 
  442.  * Closes over the php environment and packages it up as a java
  443.  * class. Use java_closure() to convert a PHP object into an equivalent Java object.
  444.  *
  445.  * Example:
  446.  * <code>
  447.  * function toString() {return "helloWorld";};
  448.  * $object = java_closure();
  449.  * echo "Java says that PHP says: $object\n";
  450.  * </code>
  451.  *
  452.  * When a php instance is supplied as an argument, the instance will be used
  453.  * instead. When a string or key/value map is supplied as a second argument,
  454.  * the java procedure names are mapped to the php procedure names. Example:
  455.  * <code>
  456.  * function hello() {return "hello";};
  457.  * echo (string)java_closure(null, "hello");
  458.  * </code>
  459.  * 
  460.  * When an array of java interfaces is supplied as a third argument,
  461.  * the environment must implement these interfaces.
  462.  * Example:
  463.  * <code>
  464.  * class Listener {
  465.  *   function actionPerformed($actionEvent) {
  466.  *       ...
  467.  *     }
  468.  * }
  469.  * function getListener() {
  470.  *     return java_closure(new Listener(), null, array(new Java("java.awt.event.ActionListener")));
  471.  * }
  472.  * </code>
  473.  * @see java_values()
  474.  * @access public
  475.  */
  476. function java_closure({
  477.   return java_closure_array(func_get_args());
  478. }
  479.  
  480. /**
  481.  * Enters stream mode (asynchronuous protocol).
  482.  *
  483.  * The statements are
  484.  * sent to the back-end in one XML stream.
  485.  *
  486.  * Use this protocol
  487.  * mode when you have a large number of set operations and you don't
  488.  * expect an exception. Any exception raised during stream mode is
  489.  * reported when java_end_document() is called.
  490.  * @access public
  491.  */
  492. function java_begin_document({
  493.   $client __javaproxy_Client_getClient();
  494.   if (!$client->isAsync{
  495.     $client->invokeMethod(0"beginDocument"array());
  496.     $client->setAsyncHandler();
  497.   }
  498.   $client->isAsync+=1;
  499. }
  500. /**
  501.  * Ends stream mode.
  502.  * 
  503.  * Fires a JavaException if any statement executed during
  504.  * stream mode raised an exception.
  505.  * @access public
  506.  */
  507. function java_end_document({
  508.   $client __javaproxy_Client_getClient();
  509.   if ($client->isAsync==1{
  510.     $client->setDefaultHandler();
  511.     $client->invokeMethod(0"endDocument"array());
  512.   }
  513.   if ($client->isAsync 0$client->isAsync-=1;
  514. }
  515.  
  516. /**
  517.  * @access private
  518.  */
  519. class java_JavaProxy implements java_JavaType {
  520.   public $__serialID$__java;
  521.   public $__signature;
  522.   public $__client;
  523.   public $__tempGlobalRef;
  524.  
  525.   function java_JavaProxy($java$signature)
  526.     $this->__java=$java;
  527.     $this->__signature=$signature;
  528.     $this->__client __javaproxy_Client_getClient();
  529.   }
  530.   function __cast($type{
  531.     return $this->__client->cast($this$type);
  532.   }
  533.   function __sleep({
  534.     $args array($thisjava_get_lifetime());
  535.     $this->__serialID $this->__client->invokeMethod(0"serialize"$args);
  536.     $this->__tempGlobalRef $this->__client->globalRef;
  537.     if(JAVA_DEBUGecho "proxy sleep called for $this->__java$this->__signature\n";
  538.     return array("__serialID""__tempGlobalRef");
  539.   }
  540.   function __wakeup({
  541.     $args array($this->__serialIDjava_get_lifetime());
  542.     if(JAVA_DEBUGecho "proxy wakeup called for $this->__java$this->__signature\n";
  543.     $this->__client __javaproxy_Client_getClient();
  544.     if($this->__tempGlobalRef)
  545.         $this->__client->globalRef $this->__tempGlobalRef;
  546.     $this->__tempGlobalRef null;
  547.     $this->__java $this->__client->invokeMethod(0"deserialize"$args);
  548.   }
  549.   function __destruct(
  550.     if(isset($this->__client)) 
  551.       $this->__client->unref($this->__java);
  552.   }
  553.   function __get($key
  554.     return $this->__client->getProperty($this->__java$key);
  555.   }
  556.   function __set($key$val{
  557.     $this->__client->setProperty($this->__java$key$val);
  558.   }
  559.   function __call($method$args
  560.     return $this->__client->invokeMethod($this->__java$method$args);
  561.   }
  562.   function __toString({
  563.     try {
  564.       return $this->__client->invokeMethod(0,"ObjectToString",array($this));
  565.     catch (JavaException $ex{
  566.       trigger_error("Exception in Java::__toString(): ". (string)$exE_USER_WARNING);
  567.       return "";
  568.     }
  569.   }
  570. }
  571.  
  572. /**
  573.  * @access private
  574.  */
  575. class java_objectIterator implements Iterator {
  576.   private $var;
  577.  
  578.   function java_ObjectIterator($javaProxy{
  579.     $this->var java_cast ($javaProxy"A");
  580.   }
  581.   function rewind({
  582.     reset($this->var);
  583.   }
  584.   function valid({
  585.     return $this->current(!== false;
  586.   }
  587.   function next({
  588.     return next($this->var);
  589.   }
  590.   function key({
  591.     return key($this->var);
  592.   }
  593.   function current({
  594.     return current($this->var);
  595.   }
  596. }
  597. /**
  598.  * @access private
  599.  */
  600. class java_IteratorProxy extends java_JavaProxy implements IteratorAggregate {
  601.   function java_IteratorProxy($java$signature{
  602.     parent::java_JavaProxy($java$signature);
  603.   }
  604.   function getIterator({
  605.     return new java_ObjectIterator($this);
  606.   }
  607. }
  608. /**
  609.  * @access private
  610.  */
  611. class java_ArrayProxy extends java_IteratorProxy implements ArrayAccess {
  612.   
  613.   function java_ArrayProxy($java$signature{
  614.     parent::java_JavaProxy($java$signature);
  615.   }
  616.   function offsetExists($idx{
  617.     $ar array($this$idx);
  618.     return $this->__client->invokeMethod(0,"offsetExists"$ar);
  619.   }  
  620.   function offsetGet($idx{
  621.     $ar array($this$idx);
  622.     return $this->__client->invokeMethod(0,"offsetGet"$ar);
  623.   }
  624.   function offsetSet($idx$val{
  625.     $ar array($this$idx$val);
  626.     return $this->__client->invokeMethod(0,"offsetSet"$ar);
  627.   }
  628.   function offsetUnset($idx{
  629.     $ar array($this$idx);
  630.     return $this->__client->invokeMethod(0,"offsetUnset"$ar);
  631.   }
  632. }
  633. /**
  634.  * @access private
  635.  */
  636. class java_ExceptionProxy extends java_JavaProxy {
  637.   function java_ExceptionProxy($java$signature)
  638.     parent::java_JavaProxy($java$signature);
  639.   }
  640.   function __toExceptionString($trace{
  641.     $args array($this$trace);
  642.     return $this->__client->invokeMethod(0,"ObjectToString",$args);
  643.   }
  644. }
  645. /**
  646.  * This decorator/bridge overrides all magic methods and delegates to
  647.  * the proxy so that it may handle them or pass them on to the
  648.  * back-end.  The actual implementation of this bridge depends on the
  649.  * back-end response, see PROTOCOL.TXT: "p: char ([A]rray,
  650.  * [C]ollection, [O]bject, [E]xception)". See the getProxy() and
  651.  * create() methods in Client.php and writeObject() and getType() in
  652.  * Response.java.<p>
  653.  *
  654.  * The constructor is an exception. If it is called, the user has
  655.  * already allocated Java, so that $wrap is false and the proxy is
  656.  * returned and set into $__delegate.
  657.  * @access private
  658.  * @see java_InternalJava
  659. */
  660. abstract class java_AbstractJava implements IteratorAggregate,ArrayAccess,java_JavaType {
  661.   public $__client;
  662.   
  663.   public $__delegate;
  664.  
  665.   public $__serialID;
  666.  
  667.   public $__factory;
  668.   public $__java$__signature;
  669.  
  670.   public $__cancelProxyCreationTag;
  671.  
  672.   function __createDelegate({
  673.     $proxy $this->__delegate 
  674.       $this->__factory->create($this->__java$this->__signature);
  675.     $this->__java $proxy->__java;
  676.     $this->__signature $proxy->__signature;
  677.   }
  678.   function __cast($type{
  679.     if(!isset($this->__delegate)) $this->__createDelegate();
  680.     return $this->__delegate->__cast($type);
  681.   }
  682.   function __sleep({
  683.     if(!isset($this->__delegate)) $this->__createDelegate();
  684.     $this->__delegate->__sleep();
  685.     return array("__delegate");
  686.   }
  687.   function __wakeup({
  688.     if(!isset($this->__delegate)) $this->__createDelegate();
  689.     $this->__delegate->__wakeup();
  690.     $this->__java $this->__delegate->__java;
  691.     $this->__client $this->__delegate->__client;
  692.   }
  693.   function __get($key
  694.      if(!isset($this->__delegate)) $this->__createDelegate();
  695.     return $this->__delegate->__get($key);
  696.   }
  697.   function __set($key$val{
  698.      if(!isset($this->__delegate)) $this->__createDelegate();
  699.     $this->__delegate->__set($key$val);
  700.   }
  701.   function __call($method$args
  702.     if(!isset($this->__delegate)) $this->__createDelegate();
  703.     return $this->__delegate->__call($method$args);
  704.   }
  705.   function __toString({
  706.     if(!isset($this->__delegate)) $this->__createDelegate();
  707.     return $this->__delegate->__toString();
  708.   }
  709.  
  710.   // The following functions are for backward compatibility
  711.   function getIterator({
  712.     if(!isset($this->__delegate)) $this->__createDelegate();
  713.     if(func_num_args()==0return $this->__delegate->getIterator();
  714.     $args func_get_args()return $this->__call("getIterator"$args);
  715.   }
  716.   function offsetExists($idx{
  717.     if(!isset($this->__delegate)) $this->__createDelegate();
  718.     if(func_num_args()==1return $this->__delegate->offsetExists($idx);
  719.     $args func_get_args()return $this->__call("offsetExists"$args);
  720.   }
  721.   function offsetGet($idx{
  722.     if(!isset($this->__delegate)) $this->__createDelegate();
  723.     if(func_num_args()==1return $this->__delegate->offsetGet($idx);
  724.     $args func_get_args()return $this->__call("offsetGet"$args);
  725.   }
  726.   function offsetSet($idx$val{
  727.     if(!isset($this->__delegate)) $this->__createDelegate();
  728.     if(func_num_args()==2return $this->__delegate->offsetSet($idx$val);
  729.     $args func_get_args()return $this->__call("offsetSet"$args);
  730.   }
  731.   function offsetUnset($idx{
  732.     if(!isset($this->__delegate)) $this->__createDelegate();
  733.     if(func_num_args()==1return $this->__delegate->offsetUnset($idx);
  734.     $args func_get_args()return $this->__call("offsetUnset"$args);
  735.   }
  736. }
  737.  
  738. /**
  739.  * The Java proxy class.
  740.  * 
  741.  * Use this class to create a Java instance. Use the Java function to access a Java type.
  742.  *
  743.  * Example which creates an instance:
  744.  * <code>
  745.  * $s = new Java("java.lang.String", "hello");
  746.  * </code>
  747.  * 
  748.  * @access public
  749.  * @see JavaException
  750.  * @see function Java
  751.  */
  752. class Java extends java_AbstractJava {
  753.   /**
  754.    * Create a new instance.
  755.    *
  756.    * This constructor can be
  757.    * used to create an instance of a Java class to access its
  758.    * features.<br>
  759.    *
  760.    * To access constants or procedures within a class, use the java function instead.<br>
  761.    *
  762.    * To convert a Java object into a PHP value, use the java_values() function.<br>
  763.    *
  764.    * Example which creates an instance:
  765.    * <code>
  766.    * $s = new Java("java.lang.String", "hello");
  767.    * </code>
  768.    * 
  769.    * Example which accesses the System class:
  770.    * <code>
  771.    * $s = Java("java.lang.System");
  772.    * </code>
  773.    *
  774.    * If the option JAVA_PREFER_VALUES is set, Java values are automatically coerced to PHP values.
  775.    * Example which sets the option JAVA_PREFER_VALUES:
  776.    * <code>
  777.    * define("JAVA_PREFER_VALUES", true);
  778.    * require_once("java/Java.inc");
  779.    * ...
  780.    * if (java("java.lang.System")->getProperty("foo", false)) ...
  781.    * </code>
  782.    * Otherwise java_values() must be used to fetch a PHP value from a Java object or Java value.
  783.    * The same example which usually executes 5 times faster:
  784.    * <code>
  785.    * require_once("java/Java.inc");
  786.    * ...
  787.    * if (java_values(java("java.lang.System")->getProperty("foo", false))) ...
  788.    * </code>
  789.    *
  790.    * @see JAVA_PREFER_VALUES
  791.    * @see function Java
  792.    * @see function java_values
  793.    */
  794.   function Java({
  795.     $client $this->__client __javaproxy_Client_getClient();
  796.     
  797.     $args func_get_args();
  798.     $name array_shift($args);
  799.  
  800.     // compatibility with the C implementation
  801.     if(is_array($name)) {$args $name$name array_shift($args);}
  802.  
  803.     /* do not delete this line, it is used when generating Mono.inc from Java.inc */
  804.  
  805.     $sig="&{$this->__signature}@{$name}";
  806.     $len = count($args);
  807.     $args2 = array();
  808.     for($i=0; $i<$len; $i++) {
  809.       switch(gettype($val = $args[$i])) {
  810.       case 'boolean'array_push($args2, $val); $sig.='@b'; break; 
  811.       case 'integer'array_push($args2, $val); $sig.='@i'; break; 
  812.       case 'double'array_push($args2, $val); $sig.='@d'; break; 
  813.       case 'string'array_push($args2, htmlspecialchars($val, ENT_COMPAT)); $sig.='@s'; break; 
  814.       case 'array':$sig="~INVALID"; break; 
  815.       case 'object':
  816.         if($val instanceof java_JavaType) {
  817.           array_push($args2, $val->__java);
  818.           $sig.="@o{$val->__signature}"; 
  819.         }
  820.         else {
  821.           $sig="~INVALID";
  822.         }
  823.         break;
  824.       case 'resource'array_push($args2, $val); $sig.='@r'; break; 
  825.       case 'NULL'array_push($args2, $val); $sig.='@N'; break; 
  826.       case 'unknown type'array_push($args2, $val); $sig.='@u'; break;
  827.       default: throw new java_IllegalArgumentException($val);
  828.       }
  829.     }
  830.  
  831.     if(array_key_exists($sig, $client->methodCache)) {
  832.       if(JAVA_DEBUG) { echo "cache hit for new Java: $sig\n"; }
  833.       $cacheEntry = &$client->methodCache[$sig];
  834.       $client->sendBuffer.= $client->preparedToSendBuffer;
  835.       if(strlen($client->sendBuffer)>=JAVA_SEND_SIZE{
  836.           if($client->protocol->handler->write($client->sendBuffer)<=0
  837.              throw new java_IllegalStateException("Connection out of sync, check backend log for details.");
  838.           $client->sendBuffer=null;
  839.       }
  840.       
  841.       $client->preparedToSendBuffer=vsprintf($cacheEntry->fmt$args2);
  842.  
  843.       if(JAVA_DEBUG{
  844.         print_r($args2);
  845.         echo "set prepared to send buffer: $client->preparedToSendBuffer$cacheEntry->fmt, for key: $sig\n";
  846.       }
  847.       $this->__java = ++$client->asyncCtx;
  848.  
  849.       if(JAVA_DEBUG{echo "setresult from new Java cache: object:"; echo sprintf("%x", $client->asyncCtx)echo "\n";}
  850.       $this->__factory $cacheEntry->factory;
  851.         $this->__signature $cacheEntry->signature;
  852.  
  853.       $this->__cancelProxyCreationTag = ++$client->cancelProxyCreationTag;
  854.     } else {
  855.       if(JAVA_DEBUG) { echo "cache miss for new Java: $sig\n"; }
  856.           $client->currentCacheKey $sig;
  857.       $delegate $this->__delegate $client->createObject($name$args);
  858.       $this->__java $delegate->__java;
  859.       $this->__signature $delegate->__signature;
  860.     }
  861.   }
  862.   /** @access private */
  863.   function __destruct() {
  864.     if(!isset($this->__client)) return;
  865.     $client $this->__client;
  866.  
  867.     $preparedToSendBuffer &$client->preparedToSendBuffer;
  868.  
  869.     // Cancel proxy creation: If the created instance is collected
  870.     // before the next java statement is executed, we set the result
  871.     // type to void
  872.     if($preparedToSendBuffer &&
  873.        $client->cancelProxyCreationTag==$this->__cancelProxyCreationTag{
  874.  
  875.       $preparedToSendBuffer[6]="3";
  876.       if(JAVA_DEBUG) {
  877.         echo "cancel result proxy creation:"; echo $this->__javaecho " {$client->preparedToSendBuffer}"; echo "\n";
  878.       }
  879.       $client->sendBuffer.=$preparedToSendBuffer;
  880.       $preparedToSendBuffer null;
  881.       $client->asyncCtx -= 1;
  882.     } else {
  883.       if(!$this->__delegate{ // write unref ourselfs if we don't have a delegate yet (see cachedJavaPrototype and Java::__factory in __call below)
  884.         if(JAVA_DEBUG) {
  885.           echo "unref java:"; echo $this->__javaecho "\n";
  886.         }
  887.         $client->unref($this->__java);
  888.       }
  889.     }    
  890.   }
  891.   /**
  892.    * Call a method on a Java object
  893.    *
  894.    * Example:
  895.    *<code>
  896.    * $s->substring(1, 10);
  897.    * </code>
  898.    * @param string The method name
  899.    * @param array The argument array
  900.    */
  901.   function __call($method, $args) { 
  902.     $client = $this->__client;
  903.  
  904.     $sig="@{$this->__signature}@$method";
  905.     $len = count($args);
  906.     $args2=array($this->__java);
  907.     for($i=0$i<$len$i++{
  908.       switch(gettype($val = $args[$i])) {
  909.       case 'boolean'array_push($args2, $val); $sig.='@b'; break; 
  910.       case 'integer'array_push($args2, $val); $sig.='@i'; break; 
  911.       case 'double'array_push($args2, $val); $sig.='@d'; break; 
  912.       case 'string'array_push($args2, htmlspecialchars($val, ENT_COMPAT)); $sig.='@s'; break; 
  913.       case 'array':$sig="~INVALID"; break; 
  914.       case 'object':
  915.         if($val instanceof java_JavaType) {
  916.           array_push($args2, $val->__java);
  917.           $sig.="@o{$val->__signature}"; 
  918.         }
  919.         else {
  920.           $sig="~INVALID";
  921.         }
  922.         break;
  923.       case 'resource'array_push($args2, $val); $sig.='@r'; break; 
  924.       case 'NULL'array_push($args2, $val); $sig.='@N'; break; 
  925.       case 'unknown type'array_push($args2, $val); $sig.='@u'; break; 
  926.       default: throw new java_IllegalArgumentException($val);
  927.       }
  928.     }
  929.  
  930.     if(array_key_exists($sig, $client->methodCache)) {
  931.       if(JAVA_DEBUG) { echo "cache hit for __call: $sig\n"; }
  932.       $cacheEntry = &$client->methodCache[$sig];
  933.       $client->sendBuffer.=$client->preparedToSendBuffer;
  934.       if(strlen($client->sendBuffer)>=JAVA_SEND_SIZE{
  935.           if($client->protocol->handler->write($client->sendBuffer)<=0
  936.              throw new java_IllegalStateException("Out of sync. Check backend log for details.");
  937.           $client->sendBuffer=null;
  938.       }
  939.       $client->preparedToSendBuffer=vsprintf($cacheEntry->fmt$args2);
  940.       if(JAVA_DEBUG{
  941.         print_r($args2);
  942.         echo "set prepared to send buffer: {$client->preparedToSendBuffer}, {$cacheEntry->fmt}\n";
  943.       }
  944.       if($cacheEntry->resultVoid{
  945.         $client->cancelProxyCreationTag += 1// expire tag
  946.         return null;
  947.       } else {
  948.         $result = clone($client->cachedJavaPrototype);
  949.         $result->__factory $cacheEntry->factory;
  950.         $result->__java = ++$client->asyncCtx;
  951.         if(JAVA_DEBUG{echo "setresult from __call cache: object:"; echo sprintf("%x", $client->asyncCtx)echo "\n";}
  952.         $result->__signature $cacheEntry->signature;
  953.         $result->__cancelProxyCreationTag = ++$client->cancelProxyCreationTag;
  954.         return $result;
  955.       }
  956.     } else {
  957.       if(JAVA_DEBUG) { echo "cache miss for __call: $sig\n"; }
  958.       $client->currentCacheKey $sig;
  959.       $retval parent::__call($method$args);
  960.       return $retval;
  961.     }
  962.   }
  963. }
  964.  
  965. /**
  966.  * @access private
  967.  */
  968. class java_InternalJava extends Java {
  969.   function java_InternalJava($proxy) {
  970.     $this->__delegate $proxy;
  971.     $this->__java $proxy->__java;
  972.     $this->__signature $proxy->__signature;
  973.     $this->__client $proxy->__client;
  974.   }
  975. }
  976.  
  977. /**
  978.  * @access private
  979.  */
  980. class java_class extends Java {
  981.   function java_class() {
  982.     $this->__client __javaproxy_Client_getClient();
  983.  
  984.     $args func_get_args();
  985.     $name array_shift($args);
  986.  
  987.     // compatibility with the C implementation
  988.     if(is_array($name)) { $args = $name; $name = array_shift($args); }
  989.  
  990.     /* do not delete this line, it is used when generating Mono.inc from Java.inc */
  991.  
  992.     $delegate = $this->__delegate $this->__client->referenceObject($name$args);
  993.  
  994.     $this->__java $delegate->__java;
  995.     $this->__signature $delegate->__signature;
  996.   }
  997. }
  998. /**
  999.  * @access private
  1000.  */
  1001. class JavaClass extends java_class{}
  1002. /**
  1003.  * A decorator pattern which overrides all magic methods.
  1004.  * 
  1005.  * @access private
  1006.  */
  1007. class java_exception extends Exception implements java_JavaType {
  1008.   /** @access private */
  1009.   public $__serialID, $__java, $__client;
  1010.   /** @access private */
  1011.   public $__delegate;
  1012.   /** @access private */
  1013.   public $__signature;
  1014.   
  1015.   /**
  1016.    * Create a new Exception.
  1017.    * 
  1018.    * Example:
  1019.    * <code>
  1020.    * $ex = new java_exception("java.lang.NullPointerException");
  1021.    * throw $ex;
  1022.    * </code>
  1023.    */
  1024.   function java_exception() {
  1025.     $this->__client __javaproxy_Client_getClient();
  1026.  
  1027.     $args func_get_args();
  1028.     $name array_shift($args);
  1029.  
  1030.     // compatibility with the C implementation
  1031.     if(is_array($name)) { $args = $name; $name = array_shift($args); }
  1032.  
  1033.     if (count($args) >= 1) Exception::__construct($args[0]);
  1034.  
  1035.     /* do not delete this line, it is used when generating Mono.inc from Java.inc */
  1036.  
  1037.     $delegate = $this->__delegate $this->__client->createObject($name$args);
  1038.  
  1039.     $this->__java $delegate->__java;
  1040.     $this->__signature $delegate->__signature;
  1041.   }
  1042.   /**
  1043.    * @access private
  1044.    */
  1045.   function __cast($type) {
  1046.     return $this->__delegate->__cast($type);
  1047.   }
  1048.   /**
  1049.    * @access private
  1050.    */
  1051.   function __sleep() {
  1052.     $this->__delegate->__sleep();
  1053.     return array("__delegate");
  1054.   }
  1055.   /**
  1056.    * @access private
  1057.    */
  1058.   function __wakeup() {
  1059.     $this->__delegate->__wakeup();
  1060.     $this->__java $this->__delegate->__java;
  1061.     $this->__client $this->__delegate->__client;
  1062.   }
  1063.   /**
  1064.    * @access private
  1065.    */
  1066.   function __get($key) { 
  1067.     return $this->__delegate->__get($key);
  1068.   }
  1069.   /**
  1070.    * @access private
  1071.    */
  1072.   function __set($key, $val) {
  1073.     $this->__delegate->__set($key$val);
  1074.   }
  1075.   /**
  1076.    * @access private
  1077.    */
  1078.   function __call($method, $args) { 
  1079.     return $this->__delegate->__call($method$args);
  1080.   }
  1081.   /**
  1082.    * @access private
  1083.    */
  1084.   function __toString() {
  1085.     return $this->__delegate->__toExceptionString($this->getTraceAsString());
  1086.   }
  1087. }
  1088. /**
  1089.  * The java exception proxy.
  1090.  * 
  1091.  * Example:
  1092.  * <code>
  1093.  * $ex = new JavaException("java.lang.NullPointerException");
  1094.  * throw $ex;
  1095.  * </code>
  1096.  * 
  1097.  * @access public
  1098.  */
  1099. class JavaException extends java_exception {}
  1100. /**
  1101.  * @access private
  1102.  */
  1103. class java_InternalException extends JavaException {
  1104.   function java_InternalException($proxy, $exception) {
  1105.     Exception::__construct($exception);
  1106.  
  1107.     $this->__delegate $proxy;
  1108.     $this->__java $proxy->__java;
  1109.     $this->__signature $proxy->__signature;
  1110.     $this->__client $proxy->__client;
  1111.   }
  1112. }
  1113.  
  1114. /**
  1115.  * @access private
  1116.  */
  1117. class java_JavaProxyProxy extends Java {
  1118.   function java_JavaProxyProxy($client) {
  1119.     $this->__client $client;
  1120.   }
  1121. }
  1122.  

Documentation generated on Mon, 05 Jan 2009 21:10:59 +0100 by phpDocumentor 1.4.2