php.java.script.servlet
Class PhpServletScriptEngine

java.lang.Object
  extended by javax.script.AbstractScriptEngine
      extended by php.java.script.PhpScriptEngine
          extended by php.java.script.servlet.PhpServletScriptEngine
All Implemented Interfaces:
java.io.Closeable, ScriptEngine

public final class PhpServletScriptEngine
extends PhpScriptEngine

A PHP script engine for Servlets. See ContextLoaderListener for details. In order to evaluate PHP scripts follow these steps:

  1. Create a factory which creates a PHP script file from a reader using the methods from EngineFactory:
    private static File script;
    private static final File getHelloScript() {
       if (script!=null) return script;

       String webCacheDir = ctx.getRealPath(req.getServletPath());
       Reader reader = new StringReader ("<?php echo 'hello from PHP'; ?>");
       return EngineFactory.getPhpScript(webCacheDir, reader);
    }
  2. Acquire a PHP script engine from the EngineFactory:
    ScriptEngine scriptEngine = EngineFactory.getPhpScriptEngine(this, ctx, req, res);
  3. Create a FileReader for the created script file:
    Reader readerHello = EngineFactory.createPhpScriptFileReader(getHelloScript());
  4. Connect its output:
    scriptEngine.getContext().setWriter (out);
  5. Evaluate the engine:
    scriptEngine.eval(readerHello);
  6. Close the reader:
    readerHello.close();

Alternatively one may use the following "quick and dirty" code which creates a new PHP script for each eval:
ScriptEngine e = EngineFactory.getPhpScriptEngine(this, ctx, req, res);
e.getContext().setWriter (out);
e.eval("<?php echo "hello java world"; ?>");


Field Summary
 
Fields inherited from interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
 
Method Summary
 void close()
          Release the script engine.
 Bindings createBindings()
          Retrieves an uninitailized namespace which can be used as the scope of the ScriptEngine.
 java.lang.Object eval(java.io.Reader reader, ScriptContext context)
          Evaluates a script obtained using the specified reader as the script source and using the namespaces in the specifed ScriptContext.
 java.lang.Object eval(java.lang.String script, ScriptContext context)
          Evaluates a script using the namespaces in the specifed ScriptContext.
 ScriptEngineFactory getFactory()
          Retrieves a ScriptEngineFactory for the class to which describes the underlying ScriptEngine.
 void release()
          Release the continuation
 
Methods inherited from class javax.script.AbstractScriptEngine
eval, eval, eval, eval, get, getBindings, getContext, put, setBindings, setContext
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

eval

public java.lang.Object eval(java.io.Reader reader,
                             ScriptContext context)
                      throws ScriptException
Description copied from interface: ScriptEngine
Evaluates a script obtained using the specified reader as the script source and using the namespaces in the specifed ScriptContext. Returns null for non-returning scripts

Specified by:
eval in interface ScriptEngine
Parameters:
reader - the script source
context - the context contianing different namespace for script evaluation
Returns:
the value of the evaluated script
Throws:
ScriptException - if an error occurs

eval

public java.lang.Object eval(java.lang.String script,
                             ScriptContext context)
                      throws ScriptException
Description copied from interface: ScriptEngine
Evaluates a script using the namespaces in the specifed ScriptContext. Return null for non-returning scripts.

Specified by:
eval in interface ScriptEngine
Parameters:
script - the String representation of the script
context - tbe ScriptContext containing namespaces for the script evaluation
Returns:
the value of the evaluated script
Throws:
ScriptException - if an error occurs

getFactory

public ScriptEngineFactory getFactory()
Description copied from interface: ScriptEngine
Retrieves a ScriptEngineFactory for the class to which describes the underlying ScriptEngine.

Specified by:
getFactory in interface ScriptEngine
Returns:
an instance of ScriptEngineFactory which describes the underlying ScriptEngine

release

public void release()
Release the continuation


createBindings

public Bindings createBindings()
Retrieves an uninitailized namespace which can be used as the scope of the ScriptEngine.

Specified by:
createBindings in interface ScriptEngine
Returns:
an initialzed namespace which can be used to repalce the state of the ScriptEngine

close

public void close()
           throws java.io.IOException
Release the script engine.

Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException