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

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