### php.java.bridge

# Class JavaBridge

- `java.lang.Object`

- `php.java.bridge.JavaBridge`

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 -jar JavaBridge.jar INET_LOCAL:9676 5 bridge.log &
telnet localhost 9676
<CreateInstance value="java.lang.Long" predicate="Instance" id="0">
<Long value="6"/>
</CreateInstance>
<Invoke value="1" method="toString" predicate="Invoke" id="0"/>
`

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

## Field Summary

`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()`
  Only for internal use.

`JavaBridge(java.io.InputStream in, java.io.OutputStream out)`
  Only for internal use.

## Method Summary

`void`

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

`void`

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

`static boolean`

`InstanceOf(java.lang.Object ob, java.lang.Object claz)`
  Check if object is an instance of class.

`void`

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

`java.lang.String`

`ObjectToString(java.lang.Object ob)`
  Returns a string representation of the object

`static ISocketFactory`

`bind(java.lang.String sockname)`
  Create a new server socket and return it.

`java.lang.Object`

`cast(java.lang.Object ob, java.lang.Class type)`
  Cast a object to a type

`java.lang.Object`

`castToBoolean(java.lang.Object ob)`
  Cast an object to a boolean value

`java.lang.Object`

`castToExact(java.lang.Object ob)`
  Cast an object to an exact number

`java.lang.Object`

`castToInexact(java.lang.Object ob)`
  Cast an object to a inexact value

`java.lang.Object`

`castToString(java.lang.Object ob)`
  Cast an object to a string

`static java.lang.String`

`classDebugDescription(java.lang.Class cls)`
  Only for internal use

`int`

`deserialize(java.lang.String serialID, int timeout)`
  Load the object from the session store.

`JavaBridgeClassLoader`

`getClassLoader()`
  Return the current ClassLoader

`java.lang.Object`

`getContext()`
  Returns the JSR223 context.

`Options`

`getOptions()`
  Returns the connection options

`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.

`java.lang.Object`

`getValues(java.lang.Object ob)`
  Convert Map or Collection into a PHP array, sends the entire array, Map or Collection to the client.

`static void`

`init(s[] )`
  Global init

`java.lang.String`

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

`void`

`logDebug(java.lang.String msg)`
  Write a debug message

`void`

`logError(java.lang.String msg)`
  Write an error message.

`void`

`logFatal(java.lang.String msg)`
  Write a fatal message

`static void`

`logInvoke(java.lang.Object obj, java.lang.String method, args[] )`

`void`

`logMessage(java.lang.String msg)`
  Write a notice.

`static void`

`main(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.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, interfaces[] )`
  Create a dynamic proxy proxy for calling PHP code.
  Example:
  java_closure($this, "clickMe", $interfaces);

`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, interfaces[] )`
  Create a dynamic proxy proxy for calling PHP code.
  Example:
  java_closure($this, $map, $interfaces);

`static java.lang.String`

`objectDebugDescription(java.lang.Object obj)`
  Only for internal use

`boolean`

`offsetExists(java.lang.Object[] value, java.lang.Object pos)`
  Checks if a given position exists.

`boolean`

`offsetExists(java.util.Map value, java.lang.Object pos)`
  Checks if a given position exists.

`java.lang.Object`

`offsetGet(java.lang.Object[] value, java.lang.Object pos)`
  Returns the object at the posisition.

`java.lang.Object`

`offsetGet(java.util.Map value, java.lang.Object pos)`
  Returns the object at the posisition.

`void`

`offsetSet(java.lang.Object[] value, java.lang.Object pos, java.lang.Object val)`
  Set an object at position.

`void`

`offsetSet(java.util.Map value, java.lang.Object pos, java.lang.Object val)`
  Set an object at position.

`void`

`offsetUnset(java.lang.Object[] value, java.lang.Object pos)`
  Remove an object from the position.

`void`

`offsetUnset(java.util.Map value, java.lang.Object pos)`
  Remove an object from the position.

`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)`
  Store the object in the session store and return the serial id.

`void`

`setClassLoader(JavaBridgeClassLoader cl)`
  Set a new ClassLoader

`void`

`setFileEncoding(java.lang.String fileEncoding)`
  Set a new file encoding, used to code and decode strings.

`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 rawPath, java.lang.String extensionDir)`
  Set the library path for ECMA dll's

`void`

`setSessionFactory(SessionFactory sessionFactory)`
  This method sets a new session factory.

`void`

`warn(java.lang.String msg)`
  Write a warning.

### Methods inherited from class java.lang.Object

`equals`, `getClass`, `hashCode`, `notify`, `notifyAll`, `toString`, `wait`, `wait`, `wait`

## Field Details

### in

    public java.io.InputStream in

> For internal use only. The input stream for the current channel.

### lastException

    public java.lang.Throwable lastException

> For PHP4's last_exception_get.

### load

    public static short load

> For internal use only.

### logLevel

    public int logLevel

> For internal use only. The request log level.

### out

    public java.io.OutputStream out

> For internal use only. The output stream for the current channel.

### request

    public Request request

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

## Constructor Details

### JavaBridge

    public JavaBridge()

> Only for internal use.

> See Also:
>   `php.java.bridge.JavaBridge.main(String[])`, `JavaBridgeRunner`

### JavaBridge

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

> Only for internal use.

> Parameters:
>   `in` -
>   `out` -

> See Also:
>   `JavaBridgeRunner`, `php.java.bridge.JavaBridge.main(String[])`

## Method Details

### CreateObject

    public void CreateObject(java.lang.String name,
                             boolean createInstance,
                             args[] ,
                             Response response)

> Create an new instance of a given class

### GetSetProp

    public void GetSetProp(java.lang.Object object,
                           java.lang.String prop,
                           args[] ,
                           Response response)

> Get or Set a property

### InstanceOf

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

> Check if object is an instance of class.

> Parameters:
>   `ob` - The object
>   `claz` - The class or an instance of a class

> Returns:
>   true if ob is an instance of class, false otherwise.

### Invoke

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

> Invoke a method on a given object

### ObjectToString

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

> Returns a string representation of the object

> Parameters:
>   `ob` - The object

> Returns:
>   A string representation.

### 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

### cast

    public java.lang.Object cast(java.lang.Object ob,
                                 java.lang.Class type)

> Cast a object to a type

> Parameters:
>   `ob` - - The object to cast
>   `type` - - The target type

> Returns:
>   The passed `ob`, will be coerced by the appropriate writer.

### castToBoolean

    public java.lang.Object castToBoolean(java.lang.Object ob)

> Cast an object to a boolean value

> Parameters:
>   `ob` - - The object to cast

> Returns:
>   The passed `ob`, will be coerced by the appropriate writer.

### castToExact

    public java.lang.Object castToExact(java.lang.Object ob)

> Cast an object to an exact number

> Parameters:
>   `ob` - - The object to cast

> Returns:
>   The passed `ob`, will be coerced by the appropriate writer.

### castToInexact

    public java.lang.Object castToInexact(java.lang.Object ob)

> Cast an object to a inexact value

> Parameters:
>   `ob` - - The object to cast

> Returns:
>   The passed `ob`, will be coerced by the appropriate writer.

### castToString

    public java.lang.Object castToString(java.lang.Object ob)

> Cast an object to a string

> Parameters:
>   `ob` - - The object to cast

> Returns:
>   The passed `ob`, will be coerced by the appropriate writer.

### classDebugDescription

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

> Only for internal use

> Parameters:
>   `cls` - The class

> Returns:
>   A debug description.

### deserialize

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

> Load the object from the session store. The C code requires that this method is called "deserialize" even though it doesn't deserialize anything.

> Parameters:
>   `serialID` - The key
>   `timeout` - The timeout, usually 1400 seconds.

> Returns:
>   the new object identity.

> Throws:
>   `java.lang.IllegalArgumentException` - if serialID does not exist anymore.

> See Also:
>   `JSessionAdapter in the php_java_lib folder. For real serialization/deserialization see the JPersistenceAdapter in the php_java_lib folder.`

### getClassLoader

    public JavaBridgeClassLoader getClassLoader()

> Return the current ClassLoader

> Returns:
>   The ClassLoader.

### getContext

    public java.lang.Object getContext()

> Returns the JSR223 context.

> Returns:
>   The JSR223 context.

### getOptions

    public Options getOptions()

> Returns the connection options

> Returns:
>   The Options.

### getPhpMap

    public PhpMap getPhpMap(java.lang.Object value)

> Return map for the value (PHP 5 only)

> Parameters:
>   `value` - - The value which must be an array or implement Map or Collection.

> Returns:
>   The PHP map.

> See Also:
>   `PhpMap`

### 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`

### getValues

    public java.lang.Object getValues(java.lang.Object ob)

> Convert Map or Collection into a PHP array, sends the entire array, Map or Collection to the client. This is much more efficient than generating round-trips while iterating over the values of an array, Map or Collection.

> Parameters:
>   `ob` - - The object to expand

> Returns:
>   The passed `ob`, will be expanded by the appropriate writer.

### init

    public static void init(s[] )

> Global init

> Parameters:

### 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.

### logDebug

    public void logDebug(java.lang.String msg)

> Write a debug message

> Parameters:
>   `msg` - The message

### logError

    public void logError(java.lang.String msg)

> Write an error message.

> Parameters:
>   `msg` - The message

### logFatal

    public void logFatal(java.lang.String msg)

> Write a fatal message

> Parameters:
>   `msg` - The message

### logInvoke

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

### logMessage

    public void logMessage(java.lang.String msg)

> Write a notice.

> Parameters:
>   `msg` - The message

### main

    public static void main(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:

### 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

### 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

> Returns:
>   the proxy

### makeClosure

    public java.lang.Object makeClosure(long object,
                                        java.lang.String name,
                                        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

> Returns:
>   the proxy

### 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,
                                        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

> Returns:
>   the proxy

### objectDebugDescription

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

> Only for internal use

> Parameters:
>   `obj` - The object

> Returns:
>   A debug description.

### offsetExists

    public boolean offsetExists(java.lang.Object[] value,
                                java.lang.Object pos)

> Checks if a given position exists.

> Parameters:
>   `value` - The array.
>   `pos` - The position

> Returns:
>   true if an element exists at this position, false otherwise.

### offsetExists

    public boolean offsetExists(java.util.Map value,
                                java.lang.Object pos)

> Checks if a given position exists.

> Parameters:
>   `value` - The map.
>   `pos` - The position

> Returns:
>   true if an element exists at this position, false otherwise.

### offsetGet

    public java.lang.Object offsetGet(java.lang.Object[] value,
                                      java.lang.Object pos)

> Returns the object at the posisition.

> Parameters:
>   `value` - The array.
>   `pos` - The position.

> Returns:
>   The object at the given position.

### offsetGet

    public java.lang.Object offsetGet(java.util.Map value,
                                      java.lang.Object pos)

> Returns the object at the posisition.

> Parameters:
>   `value` - The map.
>   `pos` - The position.

> Returns:
>   The object at the given position.

### offsetSet

    public void offsetSet(java.lang.Object[] value,
                          java.lang.Object pos,
                          java.lang.Object val)

> Set an object at position.

> Parameters:
>   `value` - The array.
>   `pos` - The position.
>   `val` - The object.

### offsetSet

    public void offsetSet(java.util.Map value,
                          java.lang.Object pos,
                          java.lang.Object val)

> Set an object at position.

> Parameters:
>   `value` - The map.
>   `pos` - The position.
>   `val` - The object.

### offsetUnset

    public void offsetUnset(java.lang.Object[] value,
                            java.lang.Object pos)

> Remove an object from the position.

> Parameters:
>   `value` - The array.
>   `pos` - The position.

### offsetUnset

    public void offsetUnset(java.util.Map value,
                            java.lang.Object pos)

> Remove an object from the position.

> Parameters:
>   `value` - The map.
>   `pos` - The position.

### printStackTrace

    public void printStackTrace(java.lang.Throwable t)

> Print a stack trace to the log file.

> Parameters:
>   `t` - the throwable

### reset

    public void reset()

> Reset the global caches of the bridge. Currently this is the classloader and the session. This is a no-op when the backend is running in a servlet engine or application server.

### run

    public void run()

> Communication with client in a new thread
> Specified by:
>   run in interface java.lang.Runnable

### sclose

    public static void sclose(int peer)

> Close a local socket.

> Parameters:
>   `peer` - The socket handle

### serialize

    public java.lang.String serialize(java.lang.Object obj,
                                      int timeout)

> Store the object in the session store and return the serial id. The C code requires that this method is called "serialize" even though it doesn't serialize anything.

> Parameters:
>   `obj` - The object
>   `timeout` - The timeout, usually 1400 seconds

> Returns:
>   the serialID

> See Also:
>   `JSessionAdapter in the php_java_lib folder. For real serialization/deserialization see the JPersistenceAdapter in the php_java_lib folder.`

### setClassLoader

    public void setClassLoader(JavaBridgeClassLoader cl)

> Set a new ClassLoader

> Parameters:
>   `cl` - The ClassLoader

### setFileEncoding

    public void setFileEncoding(java.lang.String fileEncoding)

> Set a new file encoding, used to code and decode strings. Example: setFileEncoding("UTF-8")

> Parameters:
>   `fileEncoding` - The file encoding.

### 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 rawPath,
                               java.lang.String extensionDir)

> Set the library path for ECMA dll's

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

### setSessionFactory

    public void setSessionFactory(SessionFactory sessionFactory)

> This method sets a new session factory. Used by the servlet to implement session sharing.

> Parameters:
>   `sessionFactory` - The sessionFactory to set.

### warn

    public void warn(java.lang.String msg)

> Write a warning.

> Parameters:
>   `msg` - The warning.