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

Documentation generated on Sun, 16 Mar 2008 19:11:45 +0100 by phpDocumentor 1.4.0a2