php.java.bridge
Class Util

java.lang.Object
  extended by php.java.bridge.Util

public final class Util
extends java.lang.Object

Miscellaneous functions.

Author:
jostb

Nested Class Summary
static class Util.Logger
          Only for internal use.
static class Util.Process
          Starts a CGI process and returns the process handle.
static class Util.ProcessWithErrorHandler
          Starts a CGI process with an error handler attached and returns the process handle.
static class Util.Thread
          Convenience daemon thread class
 
Field Summary
static java.lang.String[] ALLOW_URL_INCLUDE
          The PHP argument allow_url_include=On, passed to all JSR223 script engines
static java.lang.String ASCII
          ASCII encoding
static int BACKLOG
          Backlog for TCP and unix domain connections.
static int BUF_SIZE
          The default buffer size
static java.util.HashMap COMMON_ENVIRONMENT
          A map containing environment values not in ENVIRONMENT_BLACKLIST.
static java.lang.String[] DEFAULT_CGI_LOCATIONS
          The default CGI locations: "/usr/bin/php-cgi", "c:/Program Files/PHP/php-cgi.exe
static java.lang.String DEFAULT_ENCODING
          DEFAULT currently UTF-8, will be changed when most OS support and use UTF-16.
static java.lang.String[] DEFAULT_EXT_DIRS
          The default extension directories.
static java.lang.String DEFAULT_LOG_FILE
          The default log file.
static int DEFAULT_LOG_LEVEL
          The default log level, java.log_level from php.ini overrides.
static java.util.List ENVIRONMENT_BLACKLIST
          Environment entries which should NOT be passed to PHP.
static java.lang.String EXTENSION_NAME
          The name of the extension, usually "JavaBridge" or "MonoBridge"
static FCGIConnectionPool fcgiConnectionPool
          Only for internal use.
static byte[] HEX_DIGITS
          Only for internal use
static java.io.File HOME_DIR
          Only for internal use
static boolean IS_GNU_JAVA
          Set to true if the VM is gcj, false otherwise
static java.lang.Class JAVA_INC
          The java/Java.inc code
static java.lang.Class JAVA_PROXY
          The java/JavaProxy.php code
static java.lang.String JAVABRIDGE_BASE
          The base directory of the PHP/Java Bridge.
static boolean JAVABRIDGE_PROMISCUOUS
          Set to true, if the Java VM has been started with -Dphp.java.bridge.promiscuous=true;
static java.lang.Class LAUNCHER_UNIX
          The launcher.sh code
static java.lang.Class LAUNCHER_WINDOWS
          The launcher.exe code
static java.lang.Class LAUNCHER_WINDOWS2
          The launcher.exe code
static java.lang.Class LAUNCHER_WINDOWS3
          The launcher.exe code
static java.lang.Class LAUNCHER_WINDOWS4
          The launcher.exe code
static int logLevel
          The loglevel:
0: log off
1: log fatal
2: log messages/exceptions
3: log verbose
4: log debug
5: log method invocations
static int MAX_WAIT
          Used by the watchdog.
static java.lang.String osArch
          Only for internal use
static java.lang.String osName
          Only for internal use
static java.lang.String PHP_EXEC
          Only for internal use
static ThreadPool PHP_SCRIPT_ENGINE_THREAD_POOL
          Script engines are started from this pool.
static java.lang.Class PHPDEBUGGER_PHP
          The java/Java.inc code
static byte[] RN
          Only for internal use
static java.lang.String THREAD_POOL_MAX_SIZE
          The max.
static java.io.File TMPDIR
           
static boolean USE_SH_WRAPPER
          True if /bin/sh exists, false otherwise
static java.lang.String UTF8
          UTF8 encoding
static java.lang.String VERSION
          Only for internal use
static java.lang.String VM_NAME
          The name of the VM, for example "1.4.2@http://java.sun.com/" or "1.4.2@http://gcc.gnu.org/java/".
static java.lang.String X_JAVABRIDGE_CONTEXT
          The standard Context ID used by the ContextFactory
static java.lang.String X_JAVABRIDGE_INCLUDE
           
static java.lang.String X_JAVABRIDGE_INCLUDE_ONLY
           
static java.lang.String X_JAVABRIDGE_OVERRIDE_HOSTS
          Used to re-direct back to the current VM
static java.lang.String X_JAVABRIDGE_OVERRIDE_HOSTS_REDIRECT
           
static java.lang.String X_JAVABRIDGE_REDIRECT
           
static java.lang.Object[] ZERO_ARG
          Only for internal use
static java.lang.Class[] ZERO_PARAM
          Only for internal use
 
Method Summary
static void appendArgs(java.lang.Object[] args, java.lang.Class[] params, java.lang.StringBuffer buf)
          Append function arguments and their types to a StringBuffer
static void appendObject(java.lang.Object obj, java.lang.StringBuffer buf)
          Append an object to a StringBuffer
static void appendParam(java.lang.Class c, java.lang.StringBuffer buf)
          Append a function parameter to a StringBuffer
static void appendParam(java.lang.Object obj, java.lang.StringBuffer buf)
          Append a function parameter to a StringBuffer
static void appendShortObject(java.lang.Object obj, java.lang.StringBuffer buf)
          Append a parameter object to a StringBuffer
static void appendTrace(java.lang.Throwable throwable, java.lang.String trace, java.lang.StringBuffer buf)
          Append a stack trace to buf.
static java.lang.String argsToString(java.lang.Object[] args, java.lang.Class[] params)
          Return function arguments and their types as a String
static java.lang.String[] checkCgiBinary(java.lang.String php)
          Checks if the cgi binary buf-<os.arch>-<os.name>.sh or buf-<os.arch>-<os.name>.exe or buf-<os.arch>-<os.name> exists.
static java.lang.String checkError(java.lang.String s)
          Returns s if s contains "PHP Fatal error:";
static java.lang.Class classForName(java.lang.String name)
           
static AppThreadPool createThreadPool(java.lang.String name)
          Create a new AppThreadPool.
static void destroy()
          Destroy the thread associated with util.
static java.lang.String formatDateTime(long ms)
          Return the time in GMT
static java.lang.Class getClass(java.lang.Object obj)
          Return the class or the object, if obj is already a class.
static java.lang.String getClassName(java.lang.Object obj)
          Return the class name
static java.lang.ClassLoader getContextClassLoader()
          Return the thread context class loader
static java.lang.String getHostAddress(boolean promiscuous)
          Returns the string "127.0.0.1".
static ILogger getLogger()
           
static java.lang.String getShortClassName(java.lang.Object obj)
          Return the short class name
static java.lang.String getShortName(java.lang.Class clazz)
          Return the short class name
static java.lang.String getSimpleRedirectString(java.lang.String webPath, java.lang.String socketName, boolean isSecure)
           
static java.lang.String[] hashToStringArray(java.util.Map h)
          Create a string array from a hashtable.
static void logDebug(java.lang.String msg)
          Display a debug message
static void logError(java.lang.String msg)
          Display an error or an exception
static void logFatal(java.lang.String msg)
          Display a fatal error
static void logMessage(java.lang.String msg)
          Display a message
static void println(int level, java.lang.String msg)
          print a message on a given log level
static void printStackTrace(java.lang.Throwable t)
          Display a stack trace if logLevel >= 1
static void setDefaultLogger(ILogger logger)
          Sets the fall back logger, used when no thread-local logger exists.
static java.lang.String stringValueOf(java.lang.Object object)
          This procedure should be used whenever object may be a dynamic proxy: String.valueOf(object) returns null, if object is a proxy and returns null.
static byte[] toBytes(java.lang.String s)
          Locale-independent getBytes(), uses ASCII encoding
static void warn(java.lang.String msg)
          Display a warning if logLevel >= 1
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PHP_SCRIPT_ENGINE_THREAD_POOL

public static final ThreadPool PHP_SCRIPT_ENGINE_THREAD_POOL
Script engines are started from this pool. Use pool.destroy() to destroy the thread pool upon JVM or servlet shutdown


fcgiConnectionPool

public static FCGIConnectionPool fcgiConnectionPool
Only for internal use. The library standalone ScriptEngine FastCGI connection pool, if any


MAX_WAIT

public static int MAX_WAIT
Used by the watchdog. After MAX_WAIT (default 1500ms) the ContextRunner times out. Raise this value if you want to debug the bridge. See also system property php.java.bridge.max_wait


JAVA_INC

public static java.lang.Class JAVA_INC
The java/Java.inc code


PHPDEBUGGER_PHP

public static java.lang.Class PHPDEBUGGER_PHP
The java/Java.inc code


JAVA_PROXY

public static java.lang.Class JAVA_PROXY
The java/JavaProxy.php code


LAUNCHER_UNIX

public static java.lang.Class LAUNCHER_UNIX
The launcher.sh code


LAUNCHER_WINDOWS

public static java.lang.Class LAUNCHER_WINDOWS
The launcher.exe code


LAUNCHER_WINDOWS2

public static java.lang.Class LAUNCHER_WINDOWS2
The launcher.exe code


LAUNCHER_WINDOWS3

public static java.lang.Class LAUNCHER_WINDOWS3
The launcher.exe code


LAUNCHER_WINDOWS4

public static java.lang.Class LAUNCHER_WINDOWS4
The launcher.exe code


HEX_DIGITS

public static final byte[] HEX_DIGITS
Only for internal use


USE_SH_WRAPPER

public static final boolean USE_SH_WRAPPER
True if /bin/sh exists, false otherwise


ALLOW_URL_INCLUDE

public static final java.lang.String[] ALLOW_URL_INCLUDE
The PHP argument allow_url_include=On, passed to all JSR223 script engines


X_JAVABRIDGE_OVERRIDE_HOSTS

public static final java.lang.String X_JAVABRIDGE_OVERRIDE_HOSTS
Used to re-direct back to the current VM

See Also:
Constant Field Values

X_JAVABRIDGE_CONTEXT

public static final java.lang.String X_JAVABRIDGE_CONTEXT
The standard Context ID used by the ContextFactory

See Also:
Constant Field Values

X_JAVABRIDGE_OVERRIDE_HOSTS_REDIRECT

public static final java.lang.String X_JAVABRIDGE_OVERRIDE_HOSTS_REDIRECT
See Also:
Constant Field Values

X_JAVABRIDGE_REDIRECT

public static final java.lang.String X_JAVABRIDGE_REDIRECT
See Also:
Constant Field Values

X_JAVABRIDGE_INCLUDE

public static final java.lang.String X_JAVABRIDGE_INCLUDE
See Also:
Constant Field Values

X_JAVABRIDGE_INCLUDE_ONLY

public static final java.lang.String X_JAVABRIDGE_INCLUDE_ONLY
See Also:
Constant Field Values

DEFAULT_CGI_LOCATIONS

public static java.lang.String[] DEFAULT_CGI_LOCATIONS
The default CGI locations: "/usr/bin/php-cgi", "c:/Program Files/PHP/php-cgi.exe


ASCII

public static final java.lang.String ASCII
ASCII encoding

See Also:
Constant Field Values

UTF8

public static final java.lang.String UTF8
UTF8 encoding

See Also:
Constant Field Values

DEFAULT_ENCODING

public static final java.lang.String DEFAULT_ENCODING
DEFAULT currently UTF-8, will be changed when most OS support and use UTF-16.

See Also:
Constant Field Values

BUF_SIZE

public static final int BUF_SIZE
The default buffer size

See Also:
Constant Field Values

ENVIRONMENT_BLACKLIST

public static java.util.List ENVIRONMENT_BLACKLIST
Environment entries which should NOT be passed to PHP. For example PHPRC, which is set by some broken PHP installers


COMMON_ENVIRONMENT

public static java.util.HashMap COMMON_ENVIRONMENT
A map containing environment values not in ENVIRONMENT_BLACKLIST. At least: "PATH", "LD_LIBRARY_PATH", "LD_ASSUME_KERNEL", "USER", "TMP", "TEMP", "HOME", "HOMEPATH", "LANG", "TZ", "OS" They can be set with e.g.: java -DPATH="$PATH" -DHOME="$HOME" -jar JavaBridge.jar or java -DPATH="%PATH%" -jar JavaBridge.jar.


DEFAULT_EXT_DIRS

public static final java.lang.String[] DEFAULT_EXT_DIRS
The default extension directories. If one of the directories "/usr/share/java/ext", "/usr/java/packages/lib/ext" contains java libraries, the bridge loads these libraries automatically. Useful if you have non-pure java libraries (=libraries which use the Java Native Interface to load native dll's or shared libraries).


IS_GNU_JAVA

public static final boolean IS_GNU_JAVA
Set to true if the VM is gcj, false otherwise


EXTENSION_NAME

public static java.lang.String EXTENSION_NAME
The name of the extension, usually "JavaBridge" or "MonoBridge"


THREAD_POOL_MAX_SIZE

public static java.lang.String THREAD_POOL_MAX_SIZE
The max. number of threads in the thread pool. Default is 20.

See Also:
property php.java.bridge.threads

DEFAULT_LOG_LEVEL

public static int DEFAULT_LOG_LEVEL
The default log level, java.log_level from php.ini overrides. Default is 3, if started via java -jar JavaBridge.jar or 2, if started as a sub-process of Apache/IIS.

See Also:
property php.java.bridge.default_log_level

BACKLOG

public static final int BACKLOG
Backlog for TCP and unix domain connections.

See Also:
Constant Field Values

ZERO_ARG

public static final java.lang.Object[] ZERO_ARG
Only for internal use


ZERO_PARAM

public static final java.lang.Class[] ZERO_PARAM
Only for internal use


RN

public static final byte[] RN
Only for internal use


TMPDIR

public static java.io.File TMPDIR

VM_NAME

public static java.lang.String VM_NAME
The name of the VM, for example "1.4.2@http://java.sun.com/" or "1.4.2@http://gcc.gnu.org/java/".


JAVABRIDGE_PROMISCUOUS

public static boolean JAVABRIDGE_PROMISCUOUS
Set to true, if the Java VM has been started with -Dphp.java.bridge.promiscuous=true;


DEFAULT_LOG_FILE

public static java.lang.String DEFAULT_LOG_FILE
The default log file. Default is stderr, if started as a sub-process of Apache/IIS or EXTENSION_NAME.log, if started via java -jar JavaBridge.jar.

See Also:
property php.java.bridge.default_log_file

JAVABRIDGE_BASE

public static java.lang.String JAVABRIDGE_BASE
The base directory of the PHP/Java Bridge. Usually /usr/php/modules/ or $HOME


VERSION

public static java.lang.String VERSION
Only for internal use


osArch

public static java.lang.String osArch
Only for internal use


osName

public static java.lang.String osName
Only for internal use


PHP_EXEC

public static java.lang.String PHP_EXEC
Only for internal use


HOME_DIR

public static java.io.File HOME_DIR
Only for internal use


logLevel

public static int logLevel
The loglevel:
0: log off
1: log fatal
2: log messages/exceptions
3: log verbose
4: log debug
5: log method invocations

Method Detail

println

public static void println(int level,
                           java.lang.String msg)
print a message on a given log level

Parameters:
level - The log level
msg - The message

warn

public static void warn(java.lang.String msg)
Display a warning if logLevel >= 1

Parameters:
msg - The warn message

printStackTrace

public static void printStackTrace(java.lang.Throwable t)
Display a stack trace if logLevel >= 1

Parameters:
t - The Throwable

logDebug

public static void logDebug(java.lang.String msg)
Display a debug message

Parameters:
msg - The message

logFatal

public static void logFatal(java.lang.String msg)
Display a fatal error

Parameters:
msg - The error

logError

public static void logError(java.lang.String msg)
Display an error or an exception

Parameters:
msg - The error or the exception

logMessage

public static void logMessage(java.lang.String msg)
Display a message

Parameters:
msg - The message

getClassName

public static java.lang.String getClassName(java.lang.Object obj)
Return the class name

Parameters:
obj - The object
Returns:
The class name

getShortClassName

public static java.lang.String getShortClassName(java.lang.Object obj)
Return the short class name

Parameters:
obj - The object
Returns:
The class name

getShortName

public static java.lang.String getShortName(java.lang.Class clazz)
Return the short class name

Parameters:
clazz - The class
Returns:
The class name

getClass

public static java.lang.Class getClass(java.lang.Object obj)
Return the class or the object, if obj is already a class.

Parameters:
obj - The object
Returns:
Either obj or the class of obj.

appendObject

public static void appendObject(java.lang.Object obj,
                                java.lang.StringBuffer buf)
Append an object to a StringBuffer

Parameters:
obj - The object
buf - The StringBuffer

appendTrace

public static void appendTrace(java.lang.Throwable throwable,
                               java.lang.String trace,
                               java.lang.StringBuffer buf)
Append a stack trace to buf.

Parameters:
throwable - The throwable object
trace - The trace from PHP
buf - The current buffer.

appendShortObject

public static void appendShortObject(java.lang.Object obj,
                                     java.lang.StringBuffer buf)
Append a parameter object to a StringBuffer

Parameters:
obj - The object
buf - The StringBuffer

appendParam

public static void appendParam(java.lang.Class c,
                               java.lang.StringBuffer buf)
Append a function parameter to a StringBuffer

Parameters:
c - The parameter
buf - The StringBuffer

appendParam

public static void appendParam(java.lang.Object obj,
                               java.lang.StringBuffer buf)
Append a function parameter to a StringBuffer

Parameters:
obj - The parameter object
buf - The StringBuffer

argsToString

public static java.lang.String argsToString(java.lang.Object[] args,
                                            java.lang.Class[] params)
Return function arguments and their types as a String

Parameters:
args - The args
params - The associated types
Returns:
A new string

appendArgs

public static void appendArgs(java.lang.Object[] args,
                              java.lang.Class[] params,
                              java.lang.StringBuffer buf)
Append function arguments and their types to a StringBuffer

Parameters:
args - The args
params - The associated types
buf - The StringBuffer

toBytes

public static byte[] toBytes(java.lang.String s)
Locale-independent getBytes(), uses ASCII encoding

Parameters:
s - The String
Returns:
The ASCII encoded bytes

hashToStringArray

public static java.lang.String[] hashToStringArray(java.util.Map h)
Create a string array from a hashtable.

Parameters:
h - The hashtable
Returns:
The String
Throws:
java.lang.NullPointerException

setDefaultLogger

public static void setDefaultLogger(ILogger logger)
Sets the fall back logger, used when no thread-local logger exists. The default logger is initialized with: new Logger(new FileLogger()).

Parameters:
logger - the logger
See Also:
logDebug(java.lang.String)

getLogger

public static ILogger getLogger()
Returns:
Returns the logger.

getHostAddress

public static java.lang.String getHostAddress(boolean promiscuous)
Returns the string "127.0.0.1". If the system property "php.java.bridge.promiscuous" is "true", the real host address is returned.

Returns:
The host address as a string.

checkCgiBinary

public static java.lang.String[] checkCgiBinary(java.lang.String php)
Checks if the cgi binary buf-<os.arch>-<os.name>.sh or buf-<os.arch>-<os.name>.exe or buf-<os.arch>-<os.name> exists.

Parameters:
php - the php binary or null
Returns:
The full name or null.

checkError

public static java.lang.String checkError(java.lang.String s)
Returns s if s contains "PHP Fatal error:";

Parameters:
s - The error string
Returns:
The fatal error or null

stringValueOf

public static java.lang.String stringValueOf(java.lang.Object object)
This procedure should be used whenever object may be a dynamic proxy: String.valueOf(object) returns null, if object is a proxy and returns null.

Parameters:
object - The object or dynamic proxy
Returns:
The string representation of object

createThreadPool

public static AppThreadPool createThreadPool(java.lang.String name)
Create a new AppThreadPool.

Parameters:
name - The pool name
Returns:
A new AppThreadPool for up to THREAD_POOL_MAX_SIZE runnables

formatDateTime

public static java.lang.String formatDateTime(long ms)
Return the time in GMT

Parameters:
ms - the time in milliseconds
Returns:
The formatted date string

getContextClassLoader

public static final java.lang.ClassLoader getContextClassLoader()
Return the thread context class loader

Returns:
The context class loader

classForName

public static final java.lang.Class classForName(java.lang.String name)
                                          throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

getSimpleRedirectString

public static java.lang.String getSimpleRedirectString(java.lang.String webPath,
                                                       java.lang.String socketName,
                                                       boolean isSecure)

destroy

public static void destroy()
Destroy the thread associated with util.