php.java.script.servlet
Class EngineFactory

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

public final 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()
          Create a new EngineFactory
 
Method Summary
static void addManaged(javax.servlet.http.HttpServletRequest req, InvocablePhpServletLocalHttpServerScriptEngine engine)
          Manage a script engine
static java.io.FileReader createPhpScriptFileReader(java.io.File phpScriptFile)
          Use createPhpScriptFileReader(ScriptFile) instead
static java.io.FileReader createPhpScriptFileReader(ScriptFile phpScriptFile)
          Create a Reader from a given PHP script file.
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 ScriptEngine getInvocablePhpScriptEngine(javax.servlet.Servlet servlet, javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String protocol, int port)
          Get a PHP JSR 223 ScriptEngine, which implements the Invocable interface, from a HTTP server running on the local host.
static ScriptEngine getInvocablePhpScriptEngine(javax.servlet.Servlet servlet, javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String protocol, int port, java.lang.String proxy)
          Get a PHP JSR 223 ScriptEngine, which implements the Invocable interface, from a HTTP server running on the local host.
static ScriptFile getPhpScript(java.lang.String path)
          Get a PHP script from the given Path.
static ScriptFile getPhpScript(java.lang.String path, java.io.Reader reader)
          Get a PHP script from the given Path.
static ScriptFile getPhpScript(java.lang.String webPath, java.lang.String path)
          Get a PHP script from the given Path.
static ScriptFile getPhpScript(java.lang.String webPath, 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 java.lang.String getRealPath(javax.servlet.ServletContext ctx, java.lang.String path)
          Wrapper for ServletContext.getRealPath(String), throws an IllegalArgumentException if the path could not be determined.
 void releaseScriptEngines(java.util.List list)
          Release all managed script engines.
 
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()
Create a new EngineFactory

Method Detail

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.lang.Exception
Get a PHP JSR 223 ScriptEngine from the servlet context. Example:
ScriptEngine scriptEngine = EngineFactory.getPhpScriptEngine(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.lang.Exception
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.lang.Exception
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.lang.Exception
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,
                                                       java.lang.String protocol,
                                                       int port)
                                                throws java.lang.Exception
Get a PHP JSR 223 ScriptEngine, which implements the Invocable interface, from a HTTP server running on the local host. Example:
ScriptEngine scriptEngine = EngineFactory.getInvocablePhpScriptEngine(this, application, request, response, "HTTP", 80);
...
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
protocol - either "HTTP" or "HTTPS"
port - the port number
Returns:
the invocable PHP JSR 223 ScriptEngine, an instance of the InvocablePhpServletScriptEngine
Throws:
java.lang.Exception
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,
                                                       java.lang.String protocol,
                                                       int port,
                                                       java.lang.String proxy)
                                                throws java.lang.Exception
Get a PHP JSR 223 ScriptEngine, which implements the Invocable interface, from a HTTP server running on the local host. Example:
ScriptEngine scriptEngine = EngineFactory.getInvocablePhpScriptEngine(this, application, request, response, "HTTP", 80);
...
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
protocol - either "HTTP" or "HTTPS"
port - the port number
proxy - the name of the PHP proxy, for example "/JavaProxy.php"
Returns:
the invocable PHP JSR 223 ScriptEngine, an instance of the InvocablePhpServletScriptEngine
Throws:
java.lang.Exception
java.net.MalformedURLException
java.lang.IllegalStateException

getPhpScript

public static ScriptFile 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 ScriptFile getPhpScript(java.lang.String webPath,
                                      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:
webPath - the web path of the script or the web path of a resource within the current context
path - the file path which should contain the cached script
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 ScriptFile 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)

getPhpScript

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

Parameters:
webPath - the web path of the script or the web path of a resource within the current context
path - the file path which should contain the cached script
Returns:
A pointer to the cached PHP script, usually named: path+"._cache_.php"
See Also:
createPhpScriptFileReader(File)

getRealPath

public static java.lang.String getRealPath(javax.servlet.ServletContext ctx,
                                           java.lang.String path)
Wrapper for ServletContext.getRealPath(String), throws an IllegalArgumentException if the path could not be determined.

Parameters:
ctx - ServletContext
path - the resource path
Returns:
The full path to the resource.

createPhpScriptFileReader

public static java.io.FileReader createPhpScriptFileReader(ScriptFile 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"

createPhpScriptFileReader

public static java.io.FileReader createPhpScriptFileReader(java.io.File phpScriptFile)
Use createPhpScriptFileReader(ScriptFile) instead

Parameters:
phpScriptFile -
Returns:
A new FileReader

releaseScriptEngines

public void releaseScriptEngines(java.util.List list)
Release all managed script engines. Will be called automatically at the end of each request, if a RequestListener has been declared.

Parameters:
list - the list from the request attribute RequestListener.ROOT_ENGINES_COLLECTION_ATTRIBUTE

addManaged

public static void addManaged(javax.servlet.http.HttpServletRequest req,
                              InvocablePhpServletLocalHttpServerScriptEngine engine)
                       throws ScriptException
Manage a script engine

Parameters:
req - the servlet request
engine - the engine to manage
Throws:
ScriptException
See Also:
releaseScriptEngines(List)