php.java.bridge
Class JavaBridge

java.lang.Object
  extended by php.java.bridge.JavaBridge
All Implemented Interfaces:
java.lang.Runnable

public class JavaBridge
extends java.lang.Object
implements java.lang.Runnable

This is the main class for the PHP/Java Bridge. It starts the standalone backend, listenes for protocol requests and handles CreateInstance, GetSetProp and Invoke requests. Supported protocol modes are INET (listens on all interfaces), INET_LOCAL (loopback only) and UNIX (uses a local, invisible communication channel, requires natcJavaBridge.so). Furthermore it contains utility methods which can be used by clients.

Example:
java -Djava.awt.headless=true JavaBridge.jar INET_LOCAL:9676 5 bridge.log &
telnet localhost 9676
<Invoke value="" method=setJarLibraryPath" predicate="Instance" id="0">
<String value="j2ee.jar"/>
</Invoke>
<CreateInstance value="java.lang.Long" predicate="Instance" id="0">
<Long value="6"/>
</CreateInstance>
<Invoke value="1" method="toString" predicate="Invoke" id="0">
</Invoke>

Author:
Sam Ruby (methods coerce and select), Kai Londenberg, Jost Boekemeier

Nested Class Summary
static class JavaBridge.SessionFactory
           
 
Field Summary
 JavaBridgeClassLoader cl
          For internal use only.
static JavaBridge.SessionFactory defaultSessionFactory
          For internal use only.
 java.io.InputStream in
          For internal use only.
 java.lang.Throwable lastException
          For PHP4's last_exception_get.
static short load
          For internal use only.
 int logLevel
          For internal use only.
 java.io.OutputStream out
          For internal use only.
 Request request
          For internal use only.
 
Constructor Summary
JavaBridge(java.io.InputStream in, java.io.OutputStream out)
           
 
Method Summary
static int accept(int socket)
           
static ISocketFactory bind(java.lang.String sockname)
          Create a new server socket and return it.
static java.lang.String classDebugDescription(java.lang.Class cls)
           
 void CreateObject(java.lang.String name, boolean createInstance, java.lang.Object[] args, Response response)
          Create an new instance of a given class
 int deserialize(java.lang.String serialID, int timeout)
          Deserialize serialID
 PhpMap getPhpMap(java.lang.Object value)
          Return map for the value (PHP 5 only)
 ISession getSession(java.lang.String name, boolean clientIsNew, int timeout)
          Return a session handle shared among all JavaBridge instances.
 void GetSetProp(java.lang.Object object, java.lang.String prop, java.lang.Object[] args, Response response)
          Get or Set a property
 java.lang.Object getValues(java.lang.Object ob)
          for PHP5: convert Map or Collection into a PHP array, sends the entire Map or Collection to the client.
static void init(java.lang.String[] s)
          Global init
 java.lang.String inspect(java.lang.Object object)
          For internal use only.
static boolean InstanceOf(java.lang.Object ob, java.lang.Object c)
           
 void Invoke(java.lang.Object object, java.lang.String method, java.lang.Object[] args, Response response)
          Invoke a method on a given object
 void logDebug(java.lang.String msg)
           
 void logError(java.lang.String msg)
           
 void logFatal(java.lang.String msg)
           
static void logInvoke(java.lang.Object obj, java.lang.String method, java.lang.Object[] args)
           
 void logMessage(java.lang.String msg)
           
static void main(java.lang.String[] s)
          Start the PHP/Java Bridge.
 java.lang.Object makeClosure(long object)
          Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure();
java_closure($this);
 java.lang.Object makeClosure(long object, java.util.Map names)
          Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, $map);
 java.lang.Object makeClosure(long object, java.util.Map names, java.lang.Class[] interfaces)
          Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, $map, $interfaces);
 java.lang.Object makeClosure(long object, java.lang.String name)
          Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, "clickMe");
 java.lang.Object makeClosure(long object, java.lang.String name, java.lang.Class[] interfaces)
          Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, "clickMe", $interfaces);
static java.lang.String objectDebugDescription(java.lang.Object obj)
           
static java.lang.String ObjectToString(java.lang.Object ob)
           
static boolean openLog(java.lang.String logFile)
          Open a system log file with the correct (Unix) permissions.
 void printStackTrace(java.lang.Throwable t)
          Print a stack trace to the log file.
 void reset()
          Reset the global caches of the bridge.
 void run()
          Communication with client in a new thread
static void sclose(int peer)
          Close a local socket.
 java.lang.String serialize(java.lang.Object obj, int timeout)
          Serialize the object obj and return the serial id.
 void setFileEncoding(java.lang.String fileEncoding)
           
 void setJarLibraryPath(java.lang.String path, java.lang.String extensionDir)
          Append the path to the current library path
Examples:
setJarLibPath(";file:///tmp/test.jar;file:///tmp/my.jar");
setJarLibPath("|file:c:/t.jar|http://.../a.jar|jar:file:///tmp/x.jar!/");
 void setLibraryPath(java.lang.String _path, java.lang.String extensionDir)
          Set the library path for ECMA dll's
 void setSessionFactory(JavaBridge.SessionFactory sessionFactory)
          This method sets a new session factory.
static int startNative(int logLevel, int backlog, java.lang.String sockname)
          Create a local ("Unix domain") socket for sockname and return the handle.
 void warn(java.lang.String msg)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lastException

public java.lang.Throwable lastException
For PHP4's last_exception_get.


cl

public JavaBridgeClassLoader cl
For internal use only. The classloader.


in

public java.io.InputStream in
For internal use only. The input stream for the current channel.


out

public java.io.OutputStream out
For internal use only. The output stream for the current channel.


logLevel

public int logLevel
For internal use only. The request log level.


request

public Request request
For internal use only. The current request (if any)


load

public static short load
For internal use only.


defaultSessionFactory

public static final JavaBridge.SessionFactory defaultSessionFactory
For internal use only.

Constructor Detail

JavaBridge

public JavaBridge(java.io.InputStream in,
                  java.io.OutputStream out)
Method Detail

openLog

public static boolean openLog(java.lang.String logFile)
Open a system log file with the correct (Unix) permissions.

Parameters:
logFile - The log file or "" for standard out
Returns:
true if it was possible to re-direct stdout and stderr. If yes, we can simply print to standard out.

startNative

public static int startNative(int logLevel,
                              int backlog,
                              java.lang.String sockname)
Create a local ("Unix domain") socket for sockname and return the handle. If it was possible to obtain the user credentials, setGlobals will be called with the uid and gid.

Parameters:
bridge - The bridge instance.
logLevel - The current log level.
backlog - The current backlog.
sockname - The sockename.
Returns:
local socket handle ("Unix domain socket")

accept

public static int accept(int socket)

sclose

public static void sclose(int peer)
Close a local socket.

Parameters:
peer - The socket handle

run

public void run()
Communication with client in a new thread

Specified by:
run in interface java.lang.Runnable

bind

public static ISocketFactory bind(java.lang.String sockname)
                           throws java.lang.Exception
Create a new server socket and return it.

Parameters:
sockname - the socket name
Returns:
the server socket
Throws:
java.lang.Exception

init

public static void init(java.lang.String[] s)
Global init

Parameters:
s - an array of [socketname, level, logFile]

main

public static void main(java.lang.String[] s)
Start the PHP/Java Bridge.
Example:
java -Djava.awt.headless=true -jar JavaBridge.jar INET:9656 5 /var/log/php-java-bridge.log

Parameters:
s - an array of [socketname, level, logFile]

printStackTrace

public void printStackTrace(java.lang.Throwable t)
Print a stack trace to the log file.

Parameters:
t - the throwable

logDebug

public void logDebug(java.lang.String msg)

logFatal

public void logFatal(java.lang.String msg)

logError

public void logError(java.lang.String msg)

logMessage

public void logMessage(java.lang.String msg)

warn

public void warn(java.lang.String msg)

CreateObject

public void CreateObject(java.lang.String name,
                         boolean createInstance,
                         java.lang.Object[] args,
                         Response response)
Create an new instance of a given class


logInvoke

public static void logInvoke(java.lang.Object obj,
                             java.lang.String method,
                             java.lang.Object[] args)

Invoke

public void Invoke(java.lang.Object object,
                   java.lang.String method,
                   java.lang.Object[] args,
                   Response response)
Invoke a method on a given object


objectDebugDescription

public static java.lang.String objectDebugDescription(java.lang.Object obj)

classDebugDescription

public static java.lang.String classDebugDescription(java.lang.Class cls)

GetSetProp

public void GetSetProp(java.lang.Object object,
                       java.lang.String prop,
                       java.lang.Object[] args,
                       Response response)
Get or Set a property


getValues

public java.lang.Object getValues(java.lang.Object ob)
for PHP5: convert Map or Collection into a PHP array, sends the entire Map or Collection to the client. This is much more efficient than generating round-trips when iterating through a Map or Collection.


getPhpMap

public PhpMap getPhpMap(java.lang.Object value)
Return map for the value (PHP 5 only)


setJarLibraryPath

public void setJarLibraryPath(java.lang.String path,
                              java.lang.String extensionDir)
Append the path to the current library path
Examples:
setJarLibPath(";file:///tmp/test.jar;file:///tmp/my.jar");
setJarLibPath("|file:c:/t.jar|http://.../a.jar|jar:file:///tmp/x.jar!/");

Parameters:
path - A file or url list, usually separated by ';'
extensionDir - The php extension directory.

setLibraryPath

public void setLibraryPath(java.lang.String _path,
                           java.lang.String extensionDir)
Set the library path for ECMA dll's


inspect

public java.lang.String inspect(java.lang.Object object)
For internal use only.

Parameters:
object - The java object
Returns:
A list of all visible constructors, methods, fields and inner classes.

setFileEncoding

public void setFileEncoding(java.lang.String fileEncoding)

InstanceOf

public static boolean InstanceOf(java.lang.Object ob,
                                 java.lang.Object c)

ObjectToString

public static java.lang.String ObjectToString(java.lang.Object ob)

getSession

public ISession getSession(java.lang.String name,
                           boolean clientIsNew,
                           int timeout)
Return a session handle shared among all JavaBridge instances. If it is a HTTP session, the session is shared with the servlet or jsp.

See Also:
ISession

makeClosure

public java.lang.Object makeClosure(long object,
                                    java.util.Map names)
Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, $map);

Parameters:
object - the PHP environment (the php "class")
names - maps java to php names
Returns:
the proxy

makeClosure

public java.lang.Object makeClosure(long object,
                                    java.util.Map names,
                                    java.lang.Class[] interfaces)
Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, $map, $interfaces);

Parameters:
object - the PHP environment (the php "class")
names - maps java to php names
interfaces - list of interfaces which the PHP environment must implement
Returns:
the proxy

makeClosure

public java.lang.Object makeClosure(long object,
                                    java.lang.String name)
Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, "clickMe");

Parameters:
object - the PHP environment (the php "class")
name - maps all java names to this php name

makeClosure

public java.lang.Object makeClosure(long object,
                                    java.lang.String name,
                                    java.lang.Class[] interfaces)
Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure($this, "clickMe", $interfaces);

Parameters:
object - the PHP environment (the php "class")
name - maps all java names to this php name
interfaces - list of interfaces which the PHP environment must implement
Returns:
the proxy

makeClosure

public java.lang.Object makeClosure(long object)
Create a dynamic proxy proxy for calling PHP code.
Example:
java_closure();
java_closure($this);

Parameters:
object - the PHP environment (the php "class")
Returns:
the proxy

reset

public void reset()
Reset the global caches of the bridge. Currently this is the classloader and the session.


setSessionFactory

public void setSessionFactory(JavaBridge.SessionFactory sessionFactory)
This method sets a new session factory. Used by the servlet to implement session sharing.

Parameters:
sessionFactory - The sessionFactory to set.

deserialize

public int deserialize(java.lang.String serialID,
                       int timeout)
Deserialize serialID

Parameters:
serialID -
timeout -
Returns:
the new object identity.
Throws:
java.lang.IllegalArgumentException - if serialID does not exist anymore.

serialize

public java.lang.String serialize(java.lang.Object obj,
                                  int timeout)
Serialize the object obj and return the serial id.

Parameters:
obj -
timeout -
Returns:
the serialID