/JavaProxy.inc

Description
Classes
Class Description
java_JavaType Implemented by JavaException and Java.
Java The Java proxy class.
JavaException The java exception proxy.
Includes
require_once (java_get_base()."/Client.inc") (line 41)
Functions
java_cast (line 343)

Converts the java object obj into a PHP value.

Converts the java object obj into a PHP value. This procedure converts the Java argument and then calls java_values() to fetch its content. Use java_values() if the conversion is not necessary. The second argument must be [s]tring, [b]oolean, [i]nteger, [f]loat or [d]ouble, [a]rray, [n]ull or [o]bject (which does nothing).
Example:

  1.  $str new java("java.lang.String""12");
  2.  echo is_string ($str"#t":"#f";
  3.  => #f
  4.  $phpString = (string)$str;
  5.  echo is_string ($phpString"#t":"#f";
  6.  => #t
  7.  $phpNumber = (integer)(string)$str;
  8.  echo $phpNumber;
  9.  => 12
  10.  $phpNumber2 java_cast($str"integer");
  11.  echo $phpNumber2;
  12.  => 12

void java_cast (object A $object, string $type)
  • object A $object: java object
  • string $type: A PHP type description, either [Ss]tring, [Bb]oolean, [Ll]ong or [Ii]nteger, [Dd]ouble or [Ff]loat, [Nn]ull, [Aa]rray, [Oo]bject.
java_closure (line 565)

Wraps a PHP environment.

Wraps a PHP environment. Closes over the php environment and packages it up as a java class. Use java_closure() to convert a PHP object into an equivalent Java object. Example:

  1.  function toString({return "helloWorld";};
  2.  $object java_closure();
  3.  echo "Java says that PHP says: $object\n";
When a php instance is supplied as an argument, the instance will be used instead. When a string or key/value map is supplied as a second argument, the java procedure names are mapped to the php procedure names. Example:
  1.  function hello({return "hello";};
  2.  echo (string)java_closure(null"hello");
When an array of java interfaces is supplied as a third argument, the environment must implement these interfaces. Example:
  1.  class Listener {
  2.    function actionPerformed($actionEvent{
  3.        ...
  4.      }
  5.  }
  6.  function getListener({
  7.      return java_closure(new Listener()nullarray(new Java("java.awt.event.ActionListener")));
  8.  }

void java_closure ()
java_context (line 500)

Returns the jsr223 script context handle.

Returns the jsr223 script context handle. Example which closes over the current environment and passes it back to java:

  1.  define ("ENGINE_SCOPE"100);
  2.  $ctx java_context();
  3.  if(java_is_false($ctx->call(java_closure()))) die "Script should be called from java";
A second example which shows how to invoke PHP methods without the JSR 223 getInterface() and invokeMethod() helper procedures. The Java code can fetch the current PHP continuation from the context using the key "php.java.bridge.PhpProcedure":
  1.  String s = "<?php class Runnable function run({...} };
  2.             // example which captures an environment and
  3.             // passes it as a continuation back to Java
  4.             $Runnable java('java.lang.Runnable');
  5.             java_context()->call(java_closure(new Runnable()null$Runnable));
  6.             ?>";
  7.  ScriptEngine e = new ScriptEngineManager().getEngineByName("php-invocable");
  8.  e.eval (s);
  9.  Thread t = new Thread((Runnable)e.get("php.java.bridge.PhpProcedure"));
  10.  t.join ();
  11.  ((Closeable)e).close ();
A synchronized init() procedure can be called from the context to initialize a library once, and a shutdown hook can be registered to destroy the library before the (web-) context is destroyed. The init hook can be written in PHP, but the shutdown hook must be written in Java. Example:
  1.  function getShutdownHook(return java("myJavaHelper")->getShutdownHook()}
  2.  function call(// called by init()
  3.    ...
  4.    // register shutdown hook
  5.    java_context()->onShutdown(getShutdownHook());
  6.  }
  7.  java_context()->init(java_closure(nullnulljava("java.util.concurrent.Callable")));
It is possible to access implicit web objects (the session, the application store etc.) from the context. Please see the JSR223 documentation for details. Example:
  1.  $req $ctx->getHttpServletRequest();
  2.  $res $ctx->getHttpServletResponse();
Example which fetches the servlet-, config and context:
  1.  $config $ctx->getAttribute "php.java.servlet.ServletConfig",  ENGINE_SCOPE);
  2.  $context $ctx->getAttribute"php.java.servlet.ServletContext"ENGINE_SCOPE);
  3.  $servlet $ctx->getAttribute"php.java.servlet.Servlet"ENGINE_SCOPE);

void java_context ()
java_inspect (line 241)

Returns the contents (public fields, public methods, public classes) of object as a string.

Returns the contents (public fields, public methods, public classes) of object as a string. Example:

  • access: public
void java_inspect (object A $object)
  • object A $object: java object or type.
java_instanceof (line 284)

Tests if object is an instance of clazz.

Tests if object is an instance of clazz. Example:

  1.  return($o instanceof Java && $c instanceof Java && java_instanceof($o$c));

  • access: public
void java_instanceof (object A $ob, object A $clazz)
  • object A $ob: java object
  • object A $clazz: java object or type.
java_invoke (line 154)

Invoke a method dynamically.

Invoke a method dynamically. Example:

  1.  java_invoke(new java("java.lang.String","hello")"toString"array())

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

void java_invoke (object A $object, string $method, array $args)
  • object A $object: java object or type
  • string $method: A method string
  • array $args: A argument array
java_last_exception_clear (line 124)

Clear a stored Java exception.

Clear a stored Java exception.

void java_last_exception_clear ()
java_last_exception_get (line 114)

Return the last stored Java exception.

Return the last stored Java exception. The last stored Java exception is the first undeclared java.lang.RuntimeException or java.lang.Error after the last java_last_exception_clear() invocation or, if no RuntimeException/Error occured, the last declared exception reported via the script engine's try/catch mechanism. Useful for script engines which do not support try/catch or if you want to handle java.lang.RuntimeException or java.lang.Error in your PHP code.
Note the distinction between java.lang.Exception, which can be caught on PHP level, and java.lang.RuntimeException/java.lang.Error, which raise a fatal PHP error at the end of the php script (unless the error condition is cleared using java_last_exception_clear()).
java.lang.Exception is a typical exception in your business logic, java.lang.RuntimeException is an exception caused by a bug in your program, for example java.lang.NullPointerException. java.lang.Error marks a serious problem, for example java.lang.OutOfMemoryError. Example:

  1.  define ("JAVA_PREFER_VALUES"false);
  2.  require_once("http://localhost:8080/JavaBridge/java/Java.inc");
  3.  try {
  4.  new java("java.lang.String"null)// raises java.lang.RuntimeException
  5.  catch (JavaException $e{
  6.     echo $e;
  7.  }

mixed java_last_exception_get ()
java_require (line 352)

Set the library path.

Set the library path. This function should not be used in new programs. Please use <a href="http://php-java-bridge.sourceforge.net/pjb/webapp.php>tomcat or jee hot deployment</a> instead.

  • access: public
void java_require ( $arg)
  • $arg
java_server_name (line 436)

Returns the name of the back-end or null, if the back-end is not running.

Returns the name of the back-end or null, if the back-end is not running. Example:

  1.  $backend java_server_name();
  2.  if(!$backendwakeup_administrator("back-end not running");
  3.  echo "Connected to the back-end: $backend\n";

  • access: public
void java_server_name ()
java_session (line 421)

Return a session handle.

Return a session handle. When java_session() is called without arguments, the session is shared with java. Example:

  1.  java_session()->put("key"new Java("java.lang.Object"));
  2.  [...]
The java components (jsp, servlets) can retrieve the value, for example with:
  1.  getSession().getAttribute("key");
When java_session() is called with a session name, the session is not shared with java and no cookies are set. Example:
  1.  java_session("myPublicApplicationStore")->put("key""value");
When java_session() is called with a second argument set to true, a new session is allocated, the old session is destroyed if necessary. Example:
  1.  java_session(nulltrue)->put("key""val");
The optional third argument specifies the default lifetime of the session, it defaults to
  1.  session.gc_maxlifetime
. The value 0 means that the session never times out. The synchronized init() and onShutdown() callbacks from java_context() and the JPersistenceAdapter (see JPersistenceAdapter.php from the php_java_lib directory) may also be useful to load a Java singleton object after the JavaBridge library has been initialized, and to store it right before the web context or the entire JVM will be terminated.

void java_session ()
java_set_file_encoding (line 259)

Set the java file encoding, for example UTF-8 or ASCII.

Set the java file encoding, for example UTF-8 or ASCII. Needed because php does not support unicode. All string to byte array conversions use this encoding. Example:

  1.  java_set_file_encoding("ISO-8859-1");

  • access: public
void java_set_file_encoding (string $enc)
  • string $enc: A valid file.encoding string. Please see your Java
    1. file.encoding
    documentation for a list of valid encodings.
java_unwrap (line 174)

Unwrap a Java object.

Unwrap a Java object. Fetches the PHP object which has been wrapped by java_closure(). Example:

  1.  class foo function __toString({return "php"function toString({return "java";} }
  2.  $foo java_closure(new foo());
  3.  echo $foo;
  4.  => java;
  5.  $foo java_unwrap($foo);
  6.  echo $foo;
  7.  => php

void java_unwrap ( $object)
  • $object
java_values (line 210)

Evaluate a Java object.

Evaluate a Java object. Evaluate a object and fetch its content, if possible. Use java_values() to convert a Java object into an equivalent PHP value. A java array, Map or Collection object is returned as a php array. An array, Map or Collection proxy is returned as a java array, Map or Collection object, and a null proxy is returned as null. All values of java types for which a primitive php type exists are returned as php values. Everything else is returned unevaluated. Please make sure that the values do not not exceed php's memory limit. Example:

  1.  $str new java("java.lang.String""hello");
  2.  echo java_values($str);
  3.  => hello
  4.  $chr $str->toCharArray();
  5.  echo $chr;
  6.  => [o(array_of-C):"[C@1b10d42"]
  7.  $ar java_values($chr);
  8.  print $ar;
  9.  => Array
  10.  print $ar[0];
  11.  => [o(Character):"h"]
  12.  print java_values($ar[0]);
  13.  => h

void java_values (object A $object)
  • object A $object: java object or type.

Documentation generated on Sun, 06 Dec 2009 17:38:18 +0100 by phpDocumentor 1.4.2