php.java.script.servlet
Class EngineFactory

java.lang.Object
  extended by php.java.script.servlet.EngineFactory

public class EngineFactory
extends java.lang.Object

Create JSR 223 script engines from a servlet context.

See Also:
ContextLoaderListener, InvocablePhpServletScriptEngine, PhpServletScriptEngine

Field Summary
static java.lang.String ROOT_ENGINE_FACTORY_ATTRIBUTE
          The key used to store the factory in the servlet context
 
Constructor Summary
EngineFactory()
          Only for internal use
 
Method Summary
static java.io.FileReader createPhpScriptFileReader(java.io.File phpScriptFile)
          Create a Reader from a given PHP script file.
static EngineFactory getEngineFactory(javax.servlet.ServletContext ctx)
          Get an engine factory from the servlet context
static ScriptEngine getInvocablePhpScriptEngine(javax.servlet.Servlet servlet, javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Get a PHP JSR 223 ScriptEngine which implements the Invocable interface from the servlet context.
static java.io.File getPhpScript(java.lang.String path)
          Get a PHP script from the given Path.
static java.io.File getPhpScript(java.lang.String path, java.io.Reader reader)
          Get a PHP script from the given Path.
static ScriptEngine getPhpScriptEngine(javax.servlet.Servlet servlet, javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Get a PHP JSR 223 ScriptEngine from the servlet context.
static EngineFactory getRequiredEngineFactory(javax.servlet.ServletContext ctx)
          Get an engine factory from the servlet context
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ROOT_ENGINE_FACTORY_ATTRIBUTE

public static final java.lang.String ROOT_ENGINE_FACTORY_ATTRIBUTE
The key used to store the factory in the servlet context

Constructor Detail

EngineFactory

public EngineFactory()
Only for internal use

Method Detail

getEngineFactory

public static EngineFactory getEngineFactory(javax.servlet.ServletContext ctx)
Get an engine factory from the servlet context

Parameters:
ctx - The servlet context
Returns:
the factory or null

getRequiredEngineFactory

public static EngineFactory getRequiredEngineFactory(javax.servlet.ServletContext ctx)
                                              throws java.lang.IllegalStateException
Get an engine factory from the servlet context

Parameters:
ctx - The servlet context
Returns:
the factory
Throws:
java.lang.IllegalStateException

getPhpScriptEngine

public static ScriptEngine getPhpScriptEngine(javax.servlet.Servlet servlet,
                                              javax.servlet.ServletContext ctx,
                                              javax.servlet.http.HttpServletRequest req,
                                              javax.servlet.http.HttpServletResponse res)
                                       throws java.net.MalformedURLException,
                                              java.lang.IllegalStateException
Get a PHP JSR 223 ScriptEngine from the servlet context. Example:
ScriptEngine scriptEngine = EngineFactory.PhpScriptengine(this, application, request, response);
scriptEngine.eval(reader);
reader.close();

Parameters:
servlet - the servlet
ctx - the servlet context
req - the request
res - the response
Returns:
the PHP JSR 223 ScriptEngine, an instance of the PhpServletScriptEngine
Throws:
java.net.MalformedURLException
java.lang.IllegalStateException

getInvocablePhpScriptEngine

public static ScriptEngine getInvocablePhpScriptEngine(javax.servlet.Servlet servlet,
                                                       javax.servlet.ServletContext ctx,
                                                       javax.servlet.http.HttpServletRequest req,
                                                       javax.servlet.http.HttpServletResponse res)
                                                throws java.net.MalformedURLException,
                                                       java.lang.IllegalStateException,
                                                       java.net.URISyntaxException
Get a PHP JSR 223 ScriptEngine which implements the Invocable interface from the servlet context. Example:
ScriptEngine scriptEngine = EngineFactory.getInvocablePhpScriptEngine(this, application, request, response);
...
scriptEngine.eval(reader);
reader.close ();
Invocable invocableEngine = (Invocable)scriptEngine;
invocableEngine.invoceFunction("phpinfo", new Object[]{});
...
scriptEngine.eval ((Reader)null);

Parameters:
servlet - the servlet
ctx - the servlet context
req - the request
res - the response
Returns:
the invocable PHP JSR 223 ScriptEngine, an instance of the InvocablePhpServletScriptEngine
Throws:
java.net.MalformedURLException
java.lang.IllegalStateException
java.net.URISyntaxException

getPhpScript

public static java.io.File getPhpScript(java.lang.String path,
                                        java.io.Reader reader)
Get a PHP script from the given Path. This procedure can be used to cache dynamically-generated scripts

Parameters:
path - the file path which should contain the cached script, must be within the web app directory
reader - the JSR 223 script reader
Returns:
A pointer to the cached PHP script, named: path+"._cache_.php"
See Also:
createPhpScriptFileReader(File)

getPhpScript

public static java.io.File getPhpScript(java.lang.String path)
Get a PHP script from the given Path. This procedure can be used to cache dynamically-generated scripts

Parameters:
path - the file path which should contain the cached script, must be within the web app directory
Returns:
A pointer to the cached PHP script, usually named: path+"._cache_.php"
See Also:
createPhpScriptFileReader(File)

createPhpScriptFileReader

public static java.io.FileReader createPhpScriptFileReader(java.io.File phpScriptFile)
Create a Reader from a given PHP script file. This procedure can be used to create a reader from a cached script Example:
private static File script;
private static final File getScript() {
   if (script!=null) return script;
   return EngineFactory.getPhpScript(ctx.getRealPath(req.getServletPath(),new StringReader("<?php phpinfo();?>"));
}
...
FileReader reader = EngineFactory.createPhpScriptFileReader(getScript());
scriptEngine.eval (reader);
reader.close();
...

Parameters:
phpScriptFile - the file containing the cached script, obtained from getPhpScript(String, Reader) or getPhpScript(String)
Returns:
A pointer to the cached PHP script, usually named: path+"._cache_.php"