/JavaBridge.inc

Description

java_JavaBridge.php -- provides the PHP/Java Bridge PHP API.

Copyright (C) 2003-2007 Jost Boekemeier

This file is part of the PHP/Java Bridge.

The PHP/Java Bridge ("the library") is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

The library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the PHP/Java Bridge; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

Linking this file statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination.

As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.

Includes
require_once (java_get_base()."/Options.inc") (line 71)
require_once (java_get_base()."/JavaProxy.inc") (line 72)
Functions
Java (line 207)

Access the java type with the given name.

This procedure can be used to access constants or procedures within a class.

To access class features, use the java constructor instead.

Example:

  1.  java("java.lang.Long")->MAX_VALUE


Any declared exception can be caught by PHP code.
Exceptions derived from java.lang.RuntimeException or Error must not be caught unless declared in the methods throws clause -- OutOfMemoryErrors cannot be caught at all, even if declared.

  • see: Java
  • access: public
void Java (string $name)
  • string $name: The type name
java_call_with_continuation (line 413)

Call the current Java continuation with the closed-over PHP environment $kontinuation as its argument.

This procedure can be used to transfer control back to a Java continuation so that Java can call PHP procedures defined within the passed environment. If no current Java continuation exists, the procedure does nothing.

Example PHP script:

  1.  <?php require_once("java/Java.inc");
  2.  
  3.  // ... calculate sales for a given month ...
  4.  function calculateSales($month{return ((int)(string)$month)+1;}
  5.  
  6.  write_response($response);
  7.  
  8.  // prepare for out-of-band data: make our top-level environment
  9.  // available to Java. But only if it has been requested by Java:
  10.  ?>

If the above PHP script is named "calculateSales.php", the JSR 223 API can be used to invoke its functions or methods, to debug or test certain functionality from Java while keeping the script running in a production environment. The Java code follows:

  1.  import javax.script.*;
  2.  import java.net.*;
  3.  import java.io.*;
  4.  class SalesTest {
  5.      public static void test(Integer monththrows Exception {
  6.          ScriptEngine e new ScriptEngineManager().getEngineByName("php-invocable");
  7.          ByteArrayOutputStream out;
  8.          OutputStreamWriter writer;
  9.          e.getContext().setWriter(writer=new OutputStreamWriter(out=new ByteArrayOutputStream()));
  10.          Object res=e.eval(new php.java.script.URLReader(new URL("http://localhost/calculateSales.php")));
  11.          System.err.println(((Invocable)e).invokeFunction("calculateSales"new Object[]{month}));
  12.          ((Closeable)e).close();
  13.          System.err.println("PHP exit() code:" String.valueOf(res));
  14.          System.err.println("PHP output:" String.valueOf(out));
  15.       }
  16.       public static void main(String s[]throws Exception {
  17.          test(new Integer(12));
  18.       }
  19.  }

The above Java program opens a URL connection to the PHP script, invokes the calculateSales() function from the passed PHP environment and then outputs the exit code and the HTML response of the PHP script.

  • access: public
void java_call_with_continuation ([mixed $kontinuation = null])
  • mixed $kontinuation: May either be null, a PHP function name, a PHP object or a java_closure. If a PHP object is provided, the object is passed to Java. If a function name is provided, the function must return a java_closure. Otherwise the current environment is used.
java_is_false (line 344)

Checks whether a value is NULL, 0, "" or false.

Note that if you want to check if a value is boolean false, use the following test:

  1. java_values($value)===false

Example:

  1.  java_is_false(new java.lang.String("12")) => false

Example:

  1.  java_is_false(new java.lang.String("")) => true

Example:

  1.  java_is_false(1==1=> false

Example:

  1.  java_is_false(java("java.lang.System")->getProperty("foo"))

  • return: if the PHP or Java value is false.
  • access: public
true java_is_false (mixed $value)
  • mixed $value: A Java object or a PHP value
java_is_null (line 271)

Checks whether a value is null or not.

Example:

  1.  java_is_null(java("java.lang.System")->getProperty("foo"))

  • return: if $value is the PHP or Java null value.
  • access: public
true java_is_null (mixed $value)
  • mixed $value: A Java object or a PHP value
java_is_true (line 308)

Checks whether a value is not NULL, 0, "" or false.

Note that if you want to check if a value is boolean true, use the following test:

  1. java_values($value)===true

Example:

  1.  java_is_true(new java.lang.String("12")) => true

Example:

  1.  java_is_true(new java.lang.String("")) => false

Example:

  1.  java_is_true(1==1=> true

Example:

  1.  java_is_true(java("java.lang.System")->getProperty("foo"))

  • return: if the PHP or Java value is true.
  • access: public
true java_is_true (mixed $value)
  • mixed $value: A Java object or a PHP value
java_virtual (line 174)

Include the output of some Java resource.

java_virtual() uses the PHP/Java Bridge protocol to render .jsp, .jsf or other Java resources through PHP scripts running within Apache, IIS or as a JEE sub component.

Example:

  1.  <?php require_once("java/Java.inc");
  2.  echo java_virtual("/test.jsp"true);
  3.  ?>

mixed java_virtual (string $path, [bool $return = false])
  • string $path: The local resource
  • bool $return: return the response

Documentation generated on Sun, 02 May 2010 11:11:05 +0200 by phpDocumentor 1.4.3