php.java.script.servlet
Class PhpHttpScriptContext

java.lang.Object
  extended by php.java.script.PhpScriptContextDecorator
      extended by php.java.script.servlet.PhpHttpScriptContext
All Implemented Interfaces:
ScriptContext, IContext, IManaged, Invocable, IPhpScriptContext

public class PhpHttpScriptContext
extends PhpScriptContextDecorator

An example decorator for compiled script engines running in a servlet environment. Use

static final CompiledScript script = ((Compilable)(new ScriptEngineManager().getEngineByName("php-invocable"))).compile("");

script.eval(new php.java.script.servlet.PhpCompiledHttpScriptContext(script.getEngine().getContext(),this,application,request,response));

Author:
jostb

Field Summary
static int APPLICATION_SCOPE
          Integer value for the level of APPLICATION_SCOPE
static int REQUEST_SCOPE
          Integer value for the level of SCRIPT_SCOPE
static int SESSION_SCOPE
          Integer value for the level of SESSION_SCOPE
 
Fields inherited from interface php.java.bridge.http.IContext
ENGINE_SCOPE, GLOBAL_SCOPE, JAVA_BRIDGE, PHP_PROCEDURE, SERVLET, SERVLET_CONFIG, SERVLET_CONTEXT, SERVLET_REQUEST, SERVLET_RESPONSE
 
Fields inherited from interface javax.script.ScriptContext
ENGINE_SCOPE, GLOBAL_SCOPE
 
Constructor Summary
PhpHttpScriptContext(ScriptContext ctx, javax.servlet.Servlet servlet, javax.servlet.ServletContext context, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Create a new PhpCompiledScriptContext using an existing PhpScriptContext
 
Method Summary
 Continuation createContinuation(java.io.Reader reader, java.util.Map env, java.io.OutputStream out, java.io.OutputStream err, HeaderParser headerParser, ResultProxy result, ILogger logger, boolean isCompiled)
          Create a continuation
 java.lang.Object getAttribute(java.lang.String name)
          Retrieves the value for getAttribute(String, int) for the lowest scope in which it returns a non-null value.
 java.lang.Object getAttribute(java.lang.String key, int scope)
          Retrieves the value associated with specified name in the specified level of scope.
 javax.servlet.ServletContext getContext()
          Get the ServletContext
 ContextServer getContextServer()
          Get the context server associated with this context, usually a HttpServer (JavaBridgeRunner) or a ContextServer from a ContextLoaderListener
 java.io.Writer getErrorWriter()
          Returns the Writer used to display error output.
 java.lang.Object getHttpServletRequest()
          Return the http servlet request
 java.lang.Object getHttpServletResponse()
          Return the http servlet response
 java.io.Reader getReader()
          Returns a Reader to be used by the script to read input.
 java.lang.String getRealPath(java.lang.String path)
          Get the full file system path for the given resource.
 java.lang.String getRedirectString()
          Deprecated.  
 java.lang.String getRedirectString(java.lang.String webPath)
          Deprecated.  
 java.lang.String getRedirectURL(java.lang.String webPath)
          Return the redirect string, for example http://localhost:8080/webPath Used by the non-compiling script engines to hard-code the back-end URL in the Java.inc
 javax.servlet.http.HttpServletRequest getRequest()
          Get the HttpServletRequest
 javax.servlet.http.HttpServletResponse getResponse()
          Get the servlet response
 java.lang.Object getServlet()
          Return the http servlet
 java.lang.Object getServletConfig()
          Return the servlet config
 java.lang.Object getServletContext()
          Return the servlet context
 java.lang.String getSocketName()
          Return the socket name, for example 8080
 java.io.Writer getWriter()
          Retrieves an instance of java.io.Writer which can be used by scripts to display their output.
 java.lang.Object init(java.lang.Object callable)
          Initialize a library.
 void onShutdown(java.lang.Object closeable)
          Register a shutdown hook for the library.
 void setAttribute(java.lang.String key, java.lang.Object value, int scope)
          Sets an attribute specified by the name in specified level of scope.
 void setErrorWriter(java.io.Writer errorWriter)
          Sets the Writer used to display error output.
 void setReader(java.io.Reader reader)
          Sets the Reader for scripts to read input .
 void setWriter(java.io.Writer writer)
          Sets the Writer for scripts to use when displaying output.
 void startContinuation()
          Start the current continuation using a context-specific thread pool
 
Methods inherited from class php.java.script.PhpScriptContextDecorator
call, get, getAll, getAttributesScope, getBindings, getContinuation, getScopes, put, putAll, remove, removeAttribute, setBindings, setContinuation
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REQUEST_SCOPE

public static final int REQUEST_SCOPE
Integer value for the level of SCRIPT_SCOPE

See Also:
Constant Field Values

SESSION_SCOPE

public static final int SESSION_SCOPE
Integer value for the level of SESSION_SCOPE

See Also:
Constant Field Values

APPLICATION_SCOPE

public static final int APPLICATION_SCOPE
Integer value for the level of APPLICATION_SCOPE

See Also:
Constant Field Values
Constructor Detail

PhpHttpScriptContext

public PhpHttpScriptContext(ScriptContext ctx,
                            javax.servlet.Servlet servlet,
                            javax.servlet.ServletContext context,
                            javax.servlet.http.HttpServletRequest request,
                            javax.servlet.http.HttpServletResponse response)
Create a new PhpCompiledScriptContext using an existing PhpScriptContext

Parameters:
ctx - the script context to be decorated
Method Detail

createContinuation

public Continuation createContinuation(java.io.Reader reader,
                                       java.util.Map env,
                                       java.io.OutputStream out,
                                       java.io.OutputStream err,
                                       HeaderParser headerParser,
                                       ResultProxy result,
                                       ILogger logger,
                                       boolean isCompiled)
Create a continuation

Specified by:
createContinuation in interface IPhpScriptContext
Overrides:
createContinuation in class PhpScriptContextDecorator
Parameters:
reader - the script reader
env - the environment passed to php
out - the fcgi output stream
err - the fcgi error stream
headerParser - fcgi header parser
result - the result proxy
logger - the logger
isCompiled - create a continuation for a compiled or non-compiled script engine
Returns:
the Continuation

startContinuation

public void startContinuation()
Description copied from class: PhpScriptContextDecorator
Start the current continuation using a context-specific thread pool

Specified by:
startContinuation in interface IPhpScriptContext
Overrides:
startContinuation in class PhpScriptContextDecorator

getAttribute

public java.lang.Object getAttribute(java.lang.String key,
                                     int scope)
Retrieves the value associated with specified name in the specified level of scope. Returns null if no value is associated with specified key in specified level of scope.

Specified by:
getAttribute in interface ScriptContext
Specified by:
getAttribute in interface IContext
Overrides:
getAttribute in class PhpScriptContextDecorator
Parameters:
key - the name of the attribute
scope - the level of scope
Returns:
the value value associated with the specified name in specified level of scope

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
                              throws java.lang.IllegalArgumentException
Retrieves the value for getAttribute(String, int) for the lowest scope in which it returns a non-null value.

Specified by:
getAttribute in interface ScriptContext
Specified by:
getAttribute in interface IContext
Overrides:
getAttribute in class PhpScriptContextDecorator
Parameters:
name - the name of the attribute
Returns:
the value of the attribute
Throws:
java.lang.IllegalArgumentException - if the name is null

setAttribute

public void setAttribute(java.lang.String key,
                         java.lang.Object value,
                         int scope)
                  throws java.lang.IllegalArgumentException
Sets an attribute specified by the name in specified level of scope.

Specified by:
setAttribute in interface ScriptContext
Specified by:
setAttribute in interface IContext
Overrides:
setAttribute in class PhpScriptContextDecorator
Parameters:
key - the name of the attribute
value - the value of the attribute
scope - the level of the scope
Throws:
java.lang.IllegalArgumentException - if the name is null scope is invlaid

getResponse

public javax.servlet.http.HttpServletResponse getResponse()
Get the servlet response

Returns:
The HttpServletResponse

getRequest

public javax.servlet.http.HttpServletRequest getRequest()
Get the HttpServletRequest

Returns:
The HttpServletRequest

getContext

public javax.servlet.ServletContext getContext()
Get the ServletContext

Returns:
The current ServletContext

getWriter

public java.io.Writer getWriter()
Retrieves an instance of java.io.Writer which can be used by scripts to display their output.

Specified by:
getWriter in interface ScriptContext
Specified by:
getWriter in interface IContext
Overrides:
getWriter in class PhpScriptContextDecorator
Returns:
an instance of java.io.Writer

setWriter

public void setWriter(java.io.Writer writer)
Sets the Writer for scripts to use when displaying output.

Specified by:
setWriter in interface ScriptContext
Overrides:
setWriter in class PhpScriptContextDecorator
Parameters:
writer - The new Writer.

getErrorWriter

public java.io.Writer getErrorWriter()
Returns the Writer used to display error output.

Specified by:
getErrorWriter in interface ScriptContext
Overrides:
getErrorWriter in class PhpScriptContextDecorator
Returns:
The Writer

setErrorWriter

public void setErrorWriter(java.io.Writer errorWriter)
Sets the Writer used to display error output.

Specified by:
setErrorWriter in interface ScriptContext
Overrides:
setErrorWriter in class PhpScriptContextDecorator
Parameters:
errorWriter - The Writer.

getReader

public java.io.Reader getReader()
Returns a Reader to be used by the script to read input.

Specified by:
getReader in interface ScriptContext
Overrides:
getReader in class PhpScriptContextDecorator
Returns:
The Reader.

setReader

public void setReader(java.io.Reader reader)
Description copied from class: PhpScriptContextDecorator
Sets the Reader for scripts to read input .

Specified by:
setReader in interface ScriptContext
Overrides:
setReader in class PhpScriptContextDecorator
Parameters:
reader - The new Reader.

init

public java.lang.Object init(java.lang.Object callable)
                      throws java.lang.Exception
Initialize a library. This method may be called via java_context()->init(...) to initialize a library. Within init() onShutdown() may be called to register a shutdown hook for the library.

Specified by:
init in interface IManaged
Overrides:
init in class PhpScriptContextDecorator
Parameters:
callable - Its call() method will be called synchronized.
Returns:
The result of the call() invocation.
Throws:
java.lang.Exception - The result of the call() invocation.

onShutdown

public void onShutdown(java.lang.Object closeable)
Register a shutdown hook for the library. This method may be called via java_context()->onShutdown(...) to register a shutdown hook during init().

Specified by:
onShutdown in interface IManaged
Overrides:
onShutdown in class PhpScriptContextDecorator
Parameters:
closeable - Its close() method will be called before the context or the VM terminates.

getHttpServletResponse

public java.lang.Object getHttpServletResponse()
Return the http servlet response

Specified by:
getHttpServletResponse in interface IContext
Overrides:
getHttpServletResponse in class PhpScriptContextDecorator
Returns:
The http servlet reponse

getHttpServletRequest

public java.lang.Object getHttpServletRequest()
Return the http servlet request

Specified by:
getHttpServletRequest in interface IContext
Overrides:
getHttpServletRequest in class PhpScriptContextDecorator
Returns:
The http servlet request

getServlet

public java.lang.Object getServlet()
Return the http servlet

Specified by:
getServlet in interface IContext
Overrides:
getServlet in class PhpScriptContextDecorator
Returns:
The http servlet

getServletConfig

public java.lang.Object getServletConfig()
Return the servlet config

Specified by:
getServletConfig in interface IContext
Overrides:
getServletConfig in class PhpScriptContextDecorator
Returns:
The servlet config

getServletContext

public java.lang.Object getServletContext()
Return the servlet context

Specified by:
getServletContext in interface IContext
Overrides:
getServletContext in class PhpScriptContextDecorator
Returns:
The servlet context

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Get the full file system path for the given resource.

Specified by:
getRealPath in interface IContext
Overrides:
getRealPath in class PhpScriptContextDecorator
Parameters:
path - the relative path to an existing resource
Returns:
the file system path

getRedirectString

public java.lang.String getRedirectString(java.lang.String webPath)
Deprecated. 

Specified by:
getRedirectString in interface IContext
Overrides:
getRedirectString in class PhpScriptContextDecorator

getRedirectString

public java.lang.String getRedirectString()
Deprecated. 

Specified by:
getRedirectString in interface IContext
Overrides:
getRedirectString in class PhpScriptContextDecorator

getRedirectURL

public java.lang.String getRedirectURL(java.lang.String webPath)
Return the redirect string, for example http://localhost:8080/webPath Used by the non-compiling script engines to hard-code the back-end URL in the Java.inc

Specified by:
getRedirectURL in interface IContext
Overrides:
getRedirectURL in class PhpScriptContextDecorator
Parameters:
webPath - Usually request.getContextPath()+request.getServletPath()
Returns:
the redirect string

getSocketName

public java.lang.String getSocketName()
Return the socket name, for example 8080

Specified by:
getSocketName in interface IContext
Overrides:
getSocketName in class PhpScriptContextDecorator
Returns:
the socket name

getContextServer

public ContextServer getContextServer()
Get the context server associated with this context, usually a HttpServer (JavaBridgeRunner) or a ContextServer from a ContextLoaderListener

Specified by:
getContextServer in interface IPhpScriptContext
Overrides:
getContextServer in class PhpScriptContextDecorator
Returns:
the ContextServer