What is the PHP/Java Bridge?

The PHP/Java Bridge is an optimized XML protocol which can be used to connect a native script engine with a Java or ECMA 335 virtual machine. The php java extension uses this protocol to connect running PHP instances with already running Java or .NET backends. The communication works in both directions, the JSR 223 interface can be used to connect to a running PHP server (Apache/IIS, FastCGI, ...) so that Java components can call PHP instances and PHP scripts can invoke CLR (e.g. VB.NET, C#, COM) or Java (e.g. Java, KAWA, JRuby) based applications or transfer control back to the environment where the request came from. The bridge can be set up to automatically start the PHP frontend or the Java/.NET backend, if needed.

Each request-handling PHP process of a multi-process HTTP server communicates with a corresponding thread spawned by the VM. Requests from more than one HTTP server may either be routed to an application server running the PHP/Java Bridge or each HTTP server may own a PHP/Java Bridge and communicate with a J2EE java application server by exchanging java value objects; the necessary client-stub classes (ejb client .jar) can be loaded at run-time.

ECMA 335 based classes can be accessed if at least one backend is running inside a ECMA complient VM, for example Novell's MONO or Microsoft's .NET. Special features such as varargs, reflection or assembly loading are also supported.

When the backend is running in a J2EE environment, session sharing between PHP and JSP is always possible. Clustering and load balancing is available if the J2EE environment supports these features.

Unlike previous attempts (the ext/java or the JSR 223 sample implementation) the PHP/Java Bridge does not use the java native interface ("JNI"). Php instances are allocated from the HTTP (Apache/IIS) pool, instances of java/j2ee components are allocated from the backend. The allocated instances communicate using a "continuation passing style", see java_closure() and the invocable interface. In case a php instance crashes, it will not take down the java application server or servlet engine.

Motivation

PHP components are essentially transient. For complex applications is usually necessary to introduce "middleware" components such as (enterprise-) "java beans" or enterprise applications which provide caching, connection pooling or the "business logic" for the pages generated by the PHP components. Parsing XML files for example is an expensive task and it might be necessary to cache the generated graph. Establishing connections to databases is an expensive operation so that it might be necessary to re-use used connections. The standard PHP XML or DB abstractions are useless in this area because they cannot rely on a middle tier to do caching or db connection pooling.

Even for trivial tasks it might be necessary to use a java class or java library. For example it might be necessary to generate Word, Excel or PDF documents without tying the application to a specific system platform.

Apache/Tomcat
with PHP scripts and the Java Server Faces framework

PHP, the PHP/Java Bridge and the php files can be packaged within a standard J2EE archive, customers can easily deploy it into a J2EE application server or servlet engine. They don't have to install PHP and they usually cannot tell the difference whether the pages are generated by PHP, JSP or servlets. Since the bridge allows session sharing between PHP and the J2EE components, developers can migrate their JSP based solutions to PHP step by step.

PHP and the PHP/Java Bridge might also be interesting for java developers. There are a number of technologies based on the JSP template system such as jakarta Struts and its successor Java Server Faces. Since PHP/Java Bridge version 3.0 it is possible to embed php scripts into the JSF framework, so that UI developers can concentrate on developing HTML templates while web developers can create a prototype using php code and use the existing framework from their code.

Description

The bridge adds the following primitives to PHP. The type mappings are shown in table 1.


Table 1. Type Mappings

PHPJavaDescriptionExample
objectjava.lang.ObjectAn opaque object handle. However, we guarantee that the first handle always starts with 1 and that the next handle is n+1 (useful if you work with the raw XML protocol, see the python and scheme examples).$buf=new java("java.io.ByteArrayOutputStream");
$outbuf=new java("java.io.PrintStream", $buf);
nullnullNULL value$outbuf->println(null);
exact numberinteger (default) or long, depending on the flag java.ext_java_compatibility.64 bit data on protocol level, coerced to 32bit int/Integer or 64bit long/Long$outbuf->println(100);
booleanbooleanboolean value $outbuf->println(true);
inexact numberdoubleIEEE floating point$outbuf->println(3.14);
stringbyte[]binary data, unconverted$bytes=$buf->toByteArray();
stringjava.lang.StringAn UTF-8 encoded string. Since PHP does not support unicode, all java.lang.String values are auto-converted into a byte[] (see above) using UTF-8 encoding. The encoding can be changed with the java_set_file_encoding() primitive.$string=$buf->toString();
array (as array)java.util.Collection or T[]PHP4 sends and receives arrays as values. PHP5 sends arrays as values and receives object handles which implement the new iterator and array interface.// pass a Collection to Vector
$ar=array(1, 2, 3);
$v=new java("java.util.Vector", $ar);
echo $v->capacity();

// pass T[] to asList()
$A=new JavaClass("java.util.Arrays");
$lst=$A->asList($ar);
echo $lst->size();
array (as hash)java.util.MapPHP4 sends and receives hashtables as values. PHP5 sends hashtables as values and receives object handles which implement the new iterator interface.$h=array("k"=>"v", "k2"=>"v2");
$m=new java("java.util.HashMap",$h);
echo $m->size();
JavaExceptionjava.lang.ExceptionA wrapped exception class. The original exception can be retrieved with $exception->getCause();...
catch(JavaException $ex) {
echo $ex->getCause();
}


There is one example provided: test.php. You can either invoke the test.php by typing ./test.php or copy the example into the document root of you web-server and evaluate the file using the browser.

Custom java libraries (.jar files) can be stored in the following locations:

  1. Somewhere on a HTTP or FTP server, see PHP function java_require. On Security Enhanced Linux .jar files can only be loaded from locations which are tagged with the lib_t security context.
  2. In the sub-directory "lib" of the PHP extension directory, if it exists and is accessible when the JVM starts the bridge. This is usually "`php-config --extension-dir`/lib". On Security Enhanced Linux this directory is tagged with the lib_t security context.
  3. In the /usr/share/java/ directory, if it exists and is accessible when the JVM starts the bridge. On Security Enhanced Linux this directory is tagged with the lib_t security context.

The PHP/Java Bridge can operate in 4 different modes:

  1. Invoked from the dl() function. In this mode the bridge starts when the HTTP server receives a client request and terminates after the response is written. This is very slow because it starts a new backend (Java or .NET VM) for each request. But it does not require any administrative privileges.
  2. Permanently activated in the global php.ini file with an internal java process running in the HTTP server domain. In this mode the bridge backend starts when the HTTP server starts and terminates when the HTTP server terminates. Recommended during development. Please see web server installation below.
  3. Permanently activated in the global php.ini file with an external java process. Recommended for production systems. This mode is used by the RPM package available for RedHat Enterprise Linux. In this mode the bridge starts and stops as a system service.
  4. Permanently activated in the global php.ini file with an external application server or servlet engine. PHP be can run as a CGI sub-component within a pure java application server or be installed as a Apache/IIS module.

Furthermore the PHP/Java Bridge can be:



Installation instructions

If you have a RedHat Linux system (RedHat Enterprise or Fedora), you can download the 32bit RPM and type rpm -i php-java-bridge-v.x.y-1-i386.rpm php-java-bridge-standalone-v.x.y-1-i386.rpm to install it (if you run a 64bit system and a 64bit JVM you need to install the 64bit RPM instead). For other operating systems please follow the instructions below (on Security Enhanced Linux please use the RPM or please read the README before installation).

Table 2. .ini options

NameDefaultDescriptionExample
java.java_homecompile time option.The java installation directory.java.java_home="/opt/jdk1.5"
java.javacompile time option.The java executable.java.java="/opt/jdk1.5/jre/bin/java"
java.socketname/var/run/.php-java-bridge_socketThe name of the communication channel for the local backend. Must be an integer, if a secure "unix domain" channel is not available (Windows, Mac OSX).java.socketname="9167"
java.log_level1The log level from 0 (log off) to 4 (log debug).java.log_level="3"
java.log_file/var/log/php-java-bridge.logThe log file for the local PHP/Java Bridge backend.java.log_file="/tmp/php-java-bridge.log"
java.hosts<none>Additional bridge hosts which are used when the local backend is not available.java.hosts="127.0.0.1:9168;127.0.0.1:9169"
java.servletOffThe communication protocol. If set to On or to User, the bridge uses HTTP to communicate with the java.hosts backends. The option User preserves the context, On is for backward compatibility and rewrites the context to: /JavaBridge/JavaBridge.php.;; Make sure that this option is only set
;; if your backend is deployed in a
;; servlet engine or application server.

java.servlet=User
java.classpath compile time option.The java classpath. Please do not change the default valuejava.classpath="/tmp/myJavaBridge.jar:/tmp/myCasses/"
java.libpathcompile time option.The directory which contains the natcJavaBridge.so used for local ("unix domain") sockets. Please do not change the default value. java.libpath="/tmp/"
java.ext_java_compatibilityOffSince version 3 the bridge is no longer backward compatible to its predecessor "ext/java": Exact php numbers are now represented as java integers, java return values are no longer auto-converted into php values. Leave it off, if possible. java.ext_java_compatibility=On


For further information please read the README, INSTALL, INSTALL.LINUX, INSTALL.J2EE and INSTALL.WINDOWS documents contained in the download files.

The NEWS file lists the latest user visible changes, development documentation can be found in the documentation and server/documentation folder.



FAQ

  • The bridge doesn't compile on my system!?!

    Please follow the instructions from the INSTALL document as close as possible. In particular the backend needs the specified versions of autoconf, libtool and automake in the path. Backend compilation can be switched off with the --disable-backend configure option. A compiled, platform-independent backend ("JavaBridge.war") can be found in the download folder. If there are problems compiling the bridge code against any PHP version >= 4.3.4 on Windows, Linux/Unix or MacOS, please do not report this problem to the mailing list. Please open a problem report instead (you don't need an account to submit a problem report).

    On RPM based Security Enhanced Linux installations (RHEL, Fedora) please install one of the binary RPM's or compile a binary RPM from the source RPM using the command: rpmbuild --rebuild php-java-bridge*src.rpm

    On recent Debian X86 installations, Ubuntu for example, it is also possible to install the RPM binaries: Open the appropriate binary RPM with a file manager and drag and drop the contents of the lib/php directory to /usr/lib/php and restart apache.

  • Can I use java libraries without installing java?

    Yes. Simply ommit the --with-java= configure option. The bridge will use the libgcj library, which is part of the GNU gcc compiler, to interpret java libraries. This library also uses much less system resources (memory, files) than a "real" java VM.

  • Does the bridge run native code within my servlet engine or application server?

    No. The bridge backend is written in pure java, it doesn't use any native code. Native PHP runs within Apache, IIS, a FCGI server or via CGI. If the PHP instance crashes, an error page is returned to the client and a new PHP instance is started for the next request.

  • How do I make my script state (objects or variables) persistent?

    If you must code it yourself: with e.g. java_session()->put("buf", $stringBuffer) or via $_SESSION["buf"]=$stringBuffer. The $_SESSION is syntactic shugar provided by the php session module, it uses java_session() internaly. If you don't want depend on the php session module, for example if you have compiled php without the session.so, use java_session() instead.

    If you use the Java Server Faces framework, you declare the scope of the script in the php managed bean descriptor. For example if the managed-bean-scope is changed from request to session, the framework automatically saves the state of the php script instance and restores it when the next request belonging to the same session comes in.

  • How many threads does the bridge start?

    Request-handling threads are started from a thread pool, which limits the number of user requests to 20 (default), see system property php.java.bridge.threads. All further requests have to wait until one of the worker threads returns to the pool.

    When running in a servlet engine, a ContextServer is started which handles the pipe or local socket communication channel.

    When java invokes local scripts outside of a HTTP environment, the bridge starts a HttpServer, a ContextServer and a HttpProxy. The HttpProxy represents the php continuation and the HttpServer the request-handling java continuation associated with the JSR223 script.

  • How do I lock down the VM so that users cannot start threads or call System.exit?

    Usually with a java policy file. An example file will ship with version 3.0.9.

  • Where is my output?

    System.out and System.err are redirected to the server log file(s). When php scripts are invoked from a java framework (Java Server Faces for example), even the php output is redirected. For the standalone backend the output appears in the /var/log/php-java-bridge.log or in JavaBridge.log, see .ini option java.log_file. For the j2ee backend the location of the log file(s) depends on the j2ee server configuration.

  • How do I access enums or inner classes?

    With the classname$inner syntax. For example

    public interface php {
      public class java {
        public enum bridge {JavaBridge, JavaBridgeRunner};
      }
    }

    can be accessed with:

    <?php
    java_require(getcwd()); // load php.class
    $bridge = new java('php$java$bridge');
    echo $bridge->JavaBridgeRunner;
    ?>

    The above code is not a good programming example but it demonstrates why a different syntax is used to access inner classes.

  • How do I create a primitive array?

    Primitive types are wrapped by associated java classes. The following example uses reflect.Array to create a new byte array:

    $Byte = new JavaClass("java.lang.Byte");
    $byte = $Byte->TYPE;
    $Array = new JavaClass("java.lang.reflect.Array");
    $byteArray = $Array->newInstance($byte, 255);
    $System = new JavaClass("java.lang.System");
    $length = $System->in->read($byteArray);
    $str = new Java("java.lang.String, $byteArray, 0, $length);
    echo "You have typed: $str\n";

  • How fast is it?

    The following scripts were executed on one 1.688 GHZ x86 cpu running RedHat Fedora Core 4 Linux and Sun jdk1.6.0:

    The PHP 5.1.2 code

    <?php
    $String = new JavaClass("java.lang.String");
    $buf=new java("java.lang.StringBuffer");

    $i=0;
    java_begin_document();
    while($i<400000) {
        $i=$i+1;
        $buf->append(new java("java.lang.String", $String->valueOf($i)));
    }
    java_end_document();

    print $buf->length() . "\n";
    ?>

    The jython 2.1 code

    from java import lang

    buf = lang.StringBuffer();
    i=0
    while i<400000:
        i=i+1
        buf.append(lang.String(lang.String.valueOf(i)));

    print buf.length();

    The ECMAScript ("Mozilla Rhino") code

    buf = new java.lang.StringBuffer();
    for(i=0; i<400000; i++) buf.append(new String(i));
    print (buf.toString().length());

    CommandScript EngineCommunication ChannelExecution time (real, user, sys)
    time jrunscript -l php t11.phpPHP5 + PHP/Java Bridge 3.0.8named pipes0m20.112s,
    0m18.999s,
    0m0.651s
    java -jar JavaBridge.jar INET:9967 & time php t11.phpPHP5 + PHP/Java Bridge 3.0.8TCP Sockets, client and server running on the same CPU0m22.407s,
    0m9.848s,
    0m0.145s
    java -jar JavaBridge.jar INET:9967 & time php t11.phpPHP5 + PHP/Java Bridge 3.0.8TCP Sockets, client and server running on different machines0m31.507s,
    0m26.460s,
    0m3.567s
    time jrunscript -J-Xmx512M -J-Xms512M -l js t11.jsECMA scriptnone (native code)1m36.877s,
    0m55.398s,
    0m0.323s
    time java -classpath jython.jar org.python.util.jython t11.pyJython 2.1none (native code)0m6.725s,
    0m6.551s,
    0m0.107s

  • How do I start a persistent VM?

    If you want to start one persistent VM per HTTP server running on a computer, see the web server installation description. If you want to start one persistent VM per computer, please see Linux RPM package or the application server or servlet engine description.

    Furthermore it is possible to start standalone backend, for example with the command:

      java -jar JavaBridge.jar INET_LOCAL:9676 3

    The php.ini entry might look like:

      [java]
      java.hosts = 127.0.0.1:9676
      java.servlet = Off

    Please see the JavaBridge documentation for details.

  • I want to start the backend automatically as a sub-component of my HTTP Server. How do I pass my own java options and how do I change the security context and the UID of the java process?

    The bridge uses a wrapper binary which can carry the SUID bit and can be tagged with the SEL security context. This wrapper also allows you to change the standard options, which are: java -Djava.library.path=... -Djava.class.path=... -Djava.awt.headless=true php.java.bridge.JavaBridge SOCKET_NAME LOG_LEVEL LOG_FILE. A custom wrapper can be set with:

    java.wrapper=/path/to/wrapper/binary

    On Unix the bridge terminates the sub-process hierarchy with SIGTERM, sleep 5 seconds and SIGTERM, if necessary, sleep 5 seconds and SIGKILL, if necessary. On Windows the bridge emulates the Unix kill behaviour, the bridge kills the entire sub-process hierarchy so that you can use a cmd /c wrapper.

    Please see the JavaWrapper from the RedHat php-java-bridge-standalone RPM for an example.

  • I want to use the bridge as a replacement for the PHP 4 servlet API, how do I install it into tomcat?

    Download the J2EE binary and copy the JavaBridge.war into the tomcat webapps folder. After that visit http://localhost:8080/JavaBridge and run the supplied php examples. Please see webapps/JavaBridge/WEB-INF/cgi/README for details.

  • How does the bridge handle OutOfMemoryErrors?

    OutOfMemoryErrors may happen because a cached variable cannot be released, either because

    1. the variable is permanently referenced by a request-handling thread or
    2. the variable has been entered into the session or application store or
    3. the variable is referenced by a thread outside of the scope of the PHP/Java Bridge.

    When a java.lang.OutOfMemoryError reaches the request-handling thread, the PHP/Java Bridge thread pool removes the thread from its pool and writes a message FATAL: OutOfMemoryError to the PHP/Java Bridge log file. The session store is cleaned and all client connections are terminated without confirmation.

    If the OutOfMemoryError persists, this means that a thread outside of the PHP/Java Bridge has caused this error condition.

    The following code example could cause an OutOfMemoryError:

    <?php
    session_start();
    if(!$_SESSION["buffer"]) $_SESSION["buffer"]=new java("java.lang.StringBuffer");
    $_SESSION["buffer"]->append(...);
    ?>

    OutOfMemory conditions can be debugged by running the backend with e.g.:

    java -agentlib:hprof=heap=sites -jar JavaBridge.jar

  • How do I retrieve the values from a java object?

    With java_values() or with settype. Examples:

    $str = new java("java.lang.String", "hello"); echo $str;
    => [o(String):"hello"]

    echo (java_values($str));
    => hello


    print_r (java_values($str->toCharArray()));
    => array('h', 'e', 'l', 'l', 'o')

    settype($str, "string"); echo $str;
    => hello

    $vector = new java("java.util.Vector", array("foo"=>1,"bar"=>2));
    settype($vector, "array"); print_r ($vector);
    => array(foo=>1, bar=>2)

    settype($vector, "object"); echo $vector->bar;
    => 2

Related