# Function java_context

Returns the jsr223 script context handle.

Exposes the bindings from the ENGINE_SCOPE to PHP scripts. Values set with engine.set("key", val) can be fetched from PHP with java_context()->get("key"). Values set with java_context()->put("key", java_closure($val)) can be fetched from Java with engine.get("key"). The get/put methods are convenience shortcuts for getAttribute/setAttribute. Example:

    engine.put("key1", 2);
    engine.eval("<?php java_context()->put("key2", 1+(int)(string)java_context()->get('key1'));?>");
    System.out.println(engine.get("key2"));

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:

    function getShutdownHook() { return java("myJavaHelper")->getShutdownHook(); }
    function call() { // called by init()
      ...
      // register shutdown hook
      java_context()->onShutdown(getShutdownHook());
    }
    java_context()->init(java_closure(null, null, java("java.util.concurrent.Callable")));

It is possible to access implicit web objects (the session, the application store etc.) from the context. Example:

    $req = $ctx->getHttpServletRequest();
    $res = $ctx->getHttpServletResponse();
    $servlet = $ctx->getServlet();
    $config = $ctx->getServletConfig();
    $context = $ctx->getServletContext();

The global bindings (shared with all available script engines) are available from the GLOBAL_SCOPE, the script engine bindings are available from the ENGINE_SCOPE. Example

    define ("ENGINE_SCOPE", 100);
    define ("GLOBAL_SCOPE", 200);
    echo java_context()->getBindings(ENGINE_SCOPE)->keySet();
    echo java_context()->getBindings(GLOBAL_SCOPE)->keySet();

Furthermore the context exposes the java continuation to PHP scripts. Example which closes over the current environment and passes it back to java:

    define ("ENGINE_SCOPE", 100);
    $ctx = java_context();
    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":

    String s = "<?php class Runnable { function run() {...} };
               // example which captures an environment and
               // passes it as a continuation back to Java
               $Runnable = java('java.lang.Runnable');
               java_context()->call(java_closure(new Runnable(), null, $Runnable));
               ?>";
    ScriptEngine e = new ScriptEngineManager().getEngineByName("php-invocable");
    e.eval (s);
    Thread t = new Thread((Runnable)e.get("php.java.bridge.PhpProcedure"));
    t.join ();
    ((Closeable)e).close ();

License: GPL
Author: Jost Boekemeier
See: `java_session()`
Located at JavaProxy.inc