Source for file JavaBridge.inc

Documentation is available at JavaBridge.inc

  1. <?php /*-*- mode: php; tab-width:4 -*-*/
  2.  
  3.   /* java_JavaBridge.php -- provides the PHP/Java Bridge PHP API.
  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. if(!function_exists("java_get_base")) {
  42.   $version phpversion();
  43.   if ((version_compare("5.3.0"$version">"))) {
  44.    1E512// initialize errno
  45.    if ((version_compare("5.1.2"$version">"))) {
  46.     $msg "<br><strong>PHP $version too old.</strong><br>\nPlease set the path to
  47. a PHP 5.1.x executable, see php_exec in the WEB-INF/web.xml";
  48.     die($msg);
  49.    }
  50.   }
  51.   /**
  52.    * @access private
  53.    */
  54.   function java_get_base({
  55.     $ar get_required_files();
  56.     $arLen sizeof($ar);
  57.     if($arLen>0{
  58.       $thiz $ar[$arLen-1];
  59.       return dirname($thiz);
  60.     else {
  61.       return "java/";
  62.     }
  63.   }
  64.   $JAVA_BASE=java_get_base();
  65.   require_once("${JAVA_BASE}/JavaProxy.inc");
  66.  
  67.   /**
  68.    * @access private
  69.    */
  70.   class java_RuntimeException extends Exception {};
  71.   /**
  72.    * @access private
  73.    */
  74.   class java_IOException extends Exception {};
  75.   /**
  76.    * @access private
  77.    */
  78.   class java_ConnectException extends java_IOException {};
  79.   /**
  80.    * @access private
  81.    */
  82.   class java_IllegalStateException extends java_RuntimeException {};
  83.   /**
  84.    * @access private
  85.    */
  86.   class java_IllegalArgumentException extends java_RuntimeException {
  87.     function __construct($ob{
  88.       parent::__construct("illegal argument: ".gettype($ob));
  89.     }
  90.   };
  91.  
  92.  
  93.   /**
  94.    * @access private
  95.    */
  96.   function java_autoload_function5($x{
  97.     $str=str_replace("_""."$x);
  98.  
  99.     $client=__javaproxy_Client_getClient();
  100.     if(!($client->invokeMethod(0"typeExists"array($str)))) return false;
  101.  
  102.     $instance "class ${x} extends Java {".
  103.       "static function type(\$sub=null){if(\$sub) \$sub='\$'.\$sub; return java('${str}'.\"\$sub\");}".
  104.       'function __construct() {$args = func_get_args();'.
  105.       'array_unshift($args, '."'$str'".'); parent::__construct($args);}}';
  106.     eval ("$instance");
  107.     return true;
  108.   }
  109.  
  110.   /**
  111.    * @access private
  112.    */
  113.   function java_autoload_function($x{
  114.     $idx strrpos($x"::")if (!$idxreturn java_autoload_function5($x);
  115.  
  116.     $str=str_replace("::""."$x);
  117.  
  118.     $client=__javaproxy_Client_getClient();
  119.     if(!($client->invokeMethod(0"typeExists"array($str)))) return false;
  120.  
  121.     $package substr($x,0$idx);
  122.     $name substr($x2+$idx);
  123.     $instance "namespace $package; class ${name} extends ::Java {".
  124.       "static function type(\$sub=null){if(\$sub) \$sub='\$'.\$sub;return ::java('${str}'.\"\$sub\");}".
  125.       'function __construct() {$args = func_get_args();'.
  126.       'array_unshift($args, '."'$str'".'); parent::__construct($args);}}';
  127.     eval ("$instance");
  128.     return true;
  129.   }
  130.  
  131.   /**
  132.    * Load a set of java libraries and make them available in the current name space.
  133.    *
  134.    * Available since php 5.3. Example:
  135.    * <code>
  136.    * java_autoload("lucene.jar:log4j.jar");
  137.    *
  138.    * use org::apache::lucene;
  139.    * use org::apache::lucene::index;
  140.    * use org::apache::lucene::search;
  141.    * use org::apache::lucene::search::IndexSearcher as LuceneSearcher;
  142.    *
  143.    * $searcher = new LuceneSearcher(...);
  144.    * $term = new index::Term("name", "someTerm");
  145.    * $phrase = new search::PhraseQuery();
  146.    *
  147.    * $phrase->add($term);
  148.    * $hits = $searcher->search($phrase);
  149.    * ...
  150.    *</code>
  151.    * @param string The libraries separated by a semicolon
  152.    * @access public
  153.    * @since PHP 5.3
  154.    */
  155.   function java_autoload($libs=null{
  156.     static $once false;
  157.     if($once
  158.       throw new java_IllegalStateException("java_autoload called more than once");
  159.     $once true;
  160.     java_require($libs);
  161.     if(function_exists("spl_autoload_register")) {
  162.       spl_autoload_register("java_autoload_function");
  163.     else {
  164.       function __autoload($x{
  165.         return java_autoload_function($x);
  166.       }
  167.     }
  168.   }
  169.  
  170.   /**
  171.    * Access the java type with the given name.
  172.    *
  173.    * This procedure can be
  174.    * used to access constants or procedures within a class.  <br>
  175.    *
  176.    * To access class features, use the java constructor instead.<br>
  177.    *
  178.    * Example: <code> java("java.lang.Long")->MAX_VALUE </code>
  179.    *
  180.    * @access public
  181.    * @param string The type name
  182.    * @see Java
  183.    */
  184.   function Java($name
  185.     static $classMap array();
  186.     if(array_key_exists($name$classMap)) return $classMap[$name];
  187.     return $classMap[$name]=new JavaClass($name);
  188.   }
  189.   /**
  190.    * Alias for java_closure();
  191.    * @access private
  192.    * @see java_closure();
  193.    */
  194.   function java_get_closure({return java_closure_array(func_get_args());}
  195.  
  196.   /**
  197.    * Alias for java_closure();
  198.    * @access private
  199.    * @see java_closure();
  200.    */
  201.   function java_wrap({return java_closure_array(func_get_args());}
  202.   
  203.   /**
  204.    * Alias for java_values();
  205.    * @access private
  206.    * @see java_values();
  207.    */
  208.   function java_get_values($argreturn java_values($arg)}
  209.  
  210.   /**
  211.    * Alias for java_values();
  212.    * @access private
  213.    * @see java_values();
  214.    */
  215.   function java_unwrap($argreturn java_values($arg)}
  216.  
  217.   /**
  218.    * Alias for java_session();
  219.    * @access private
  220.    * @see java_session();
  221.    */
  222.   function java_get_session({return java_session_array(func_get_args());}
  223.  
  224.   /**
  225.    * Alias for java_context();
  226.    * @access private
  227.    * @see java_context();
  228.    */
  229.   function java_get_context({return java_context()}
  230.  
  231.   /**
  232.    * Alias for java_server_name();
  233.    * @access private
  234.    * @see java_server_name();
  235.    */
  236.   function java_get_server_name(return java_server_name()}
  237.  
  238.   /**
  239.    * Alias for java_is_null();
  240.    * @access private
  241.    * @see java_is_null();
  242.    */
  243.   function java_isnull($valuereturn is_null (java_values ($value))}
  244.  
  245.   /**
  246.    * Checks whether a value is null or not.
  247.    *
  248.    * Example: <code> java_is_null(java("java.lang.System")->getProperty("foo")) </code>
  249.    *
  250.    * @access public
  251.    * @param mixed A Java object or a PHP value
  252.    * @return true if $value is the PHP or Java null value.
  253.    */
  254.   function java_is_null($valuereturn is_null (java_values ($value))}
  255.  
  256.   /**
  257.    * Alias for java_is_true();
  258.    * @access private
  259.    * @see java_is_true();
  260.    */
  261.   function java_istrue($valuereturn  (boolean)(java_values ($value))}
  262.  
  263.   /**
  264.    * Checks whether a value is true or not.
  265.    *
  266.    * Example: <code> java_is_true(java("java.lang.System")->getProperty("foo")) </code>
  267.    *
  268.    * @access public
  269.    * @param mixed A Java object or a PHP value
  270.    * @return true if the PHP or Java value is true.
  271.    */
  272.   function java_is_true($valuereturn (boolean)(java_values ($value))}
  273.  
  274.   /**
  275.    * Alias for java_is_false();
  276.    * @access private
  277.    * @see java_is_false();
  278.    */
  279.   function java_isfalse($valuereturn !(java_values ($value))}
  280.  
  281.   /**
  282.    * Checks whether a value is false or not.
  283.    *
  284.    * Example: <code> java_is_false(java("java.lang.System")->getProperty("foo")) </code>
  285.    *
  286.    * @access public
  287.    * @param mixed A Java object or a PHP value
  288.    * @return true if the PHP or Java value is false.
  289.    */
  290.   function java_is_false($valuereturn !(java_values ($value))}
  291.  
  292.   /**
  293.    * Alias for java_set_file_encoding();
  294.    * @access private
  295.    * @see java_set_file_encoding();
  296.    */
  297.   function java_set_encoding($encreturn java_set_file_encoding ($enc)}
  298.  
  299.   /**
  300.    * @deprecated: Use java_require() instead.
  301.    * @access private
  302.    * @see java_require();
  303.    */
  304.   function java_set_library_path($argreturn java_require($arg)}
  305.  //!java_defined  DO NOT REMOVE THIS LINE
  306. ?>

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