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

Documentation generated on Sun, 30 Nov 2008 10:51:49 +0100 by phpDocumentor 1.4.0a2