php.java.bridge.http
Class Context

java.lang.Object
  extended by php.java.bridge.http.Context
All Implemented Interfaces:
IContext, IManaged, Invocable
Direct Known Subclasses:
HttpContext

public class Context
extends java.lang.Object
implements IManaged, Invocable, IContext

Emulates a JSR223 script context when the JSR223 classes are not available. The method call(kont) returns false, so that it can be used to check if a script was called from java:
function toString() {return "hello java, I am a php script, but in your eyes I am an ordinary java object...";}
java_context()->call(java_closure()) || die("This script must be called from java!");

Author:
jostb
See Also:
IPhpScriptContext, ScriptContext, PhpScriptContext

Field Summary
 
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
 
Method Summary
 boolean call(java.lang.Object kont)
          Call the java continuation with the current continuation kont as its argument.
 java.lang.Object get(java.lang.String key)
          Get a binding from the engine scope
 java.util.Map getAll()
          Get all bindings from the engine scope
 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 name, int scope)
          Retrieves the value associated with specified name in the specified level of scope.
 int getAttributesScope(java.lang.String name)
          Retrieves the lowest value of scopes for which the attribute is defined.
 java.lang.Object getHttpServletRequest()
          Throws IllegalStateException
 java.lang.Object getHttpServletResponse()
          Throws IllegalStateException
static java.lang.Object getManageable(java.lang.Object callable)
          Only for internal use
 java.lang.String getRealPath(java.lang.String path)
          Get the full file system path for the given resource.
static java.lang.String getRealPathInternal(java.lang.String path)
          Only for internal use
 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
 java.lang.Object getServlet()
          Throws IllegalStateException
 java.lang.Object getServletConfig()
          Throws IllegalStateException
 java.lang.Object getServletContext()
          Throws IllegalStateException
 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.
static void handleManaged(java.lang.Object closeable)
          Only for internal use.
 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 put(java.lang.String key, java.lang.Object val)
          Add a new binding to the engine scope
 void putAll(java.util.Map map)
          Put all bindings to the engine scope
 void remove(java.lang.String key)
          Remove a bindings from the engine scope
 java.lang.Object removeAttribute(java.lang.String name, int scope)
          Removes the specified attribute form the specified level of scope.
 void setAttribute(java.lang.String name, java.lang.Object value, int scope)
          Sets an attribute specified by the name in specified level of scope.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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 IContext
Parameters:
name - the name of the attribute
Returns:
the value of the attribute
Throws:
java.lang.IllegalArgumentException

getAttribute

public java.lang.Object getAttribute(java.lang.String name,
                                     int scope)
                              throws java.lang.IllegalArgumentException
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 IContext
Parameters:
name - the name of the attribute
scope - the level of scope
Returns:
the value value associated with the specified name in specified level of scope
Throws:
java.lang.IllegalArgumentException

getAttributesScope

public int getAttributesScope(java.lang.String name)
Retrieves the lowest value of scopes for which the attribute is defined. If there is no associate scope with the given attribute (-1) is returned.

Specified by:
getAttributesScope in interface IContext
Parameters:
name - the name of attribute
Returns:
the value of level of scope

getWriter

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

Specified by:
getWriter in interface IContext
Returns:
an instance of java.io.Writer
Throws:
java.io.IOException

removeAttribute

public java.lang.Object removeAttribute(java.lang.String name,
                                        int scope)
                                 throws java.lang.IllegalArgumentException
Removes the specified attribute form the specified level of scope.

Specified by:
removeAttribute in interface IContext
Parameters:
name - the name of the attribute
scope - the level of scope
Returns:
value which is removed
Throws:
java.lang.IllegalArgumentException

setAttribute

public void setAttribute(java.lang.String name,
                         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 IContext
Parameters:
name - 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

getHttpServletRequest

public java.lang.Object getHttpServletRequest()
Throws IllegalStateException

Specified by:
getHttpServletRequest in interface IContext
Returns:
none

getServletContext

public java.lang.Object getServletContext()
Throws IllegalStateException

Specified by:
getServletContext in interface IContext
Returns:
none

getHttpServletResponse

public java.lang.Object getHttpServletResponse()
Throws IllegalStateException

Specified by:
getHttpServletResponse in interface IContext
Returns:
none

getServlet

public java.lang.Object getServlet()
Throws IllegalStateException

Specified by:
getServlet in interface IContext
Returns:
none

getServletConfig

public java.lang.Object getServletConfig()
Throws IllegalStateException

Specified by:
getServletConfig in interface IContext
Returns:
none

call

public boolean call(java.lang.Object kont)
Description copied from interface: Invocable
Call the java continuation with the current continuation kont as its argument.

Specified by:
call in interface Invocable
Parameters:
kont - dummy
Returns:
false

handleManaged

public static void handleManaged(java.lang.Object closeable)
Only for internal use.

Used when scripts are running outside of a servlet environment: Either the Standalone or the JSR223 Standalone (see PhpScriptContext).
Within a servlet environment use the ContextLoaderListener instead: Either php.java.servlet.Context or the JSR223 Context (see PhpSimpleHttpScriptContext).

Parameters:
closeable - The procedure close(), will be called before the VM terminates

getManageable

public static java.lang.Object getManageable(java.lang.Object callable)
                                      throws java.lang.Exception
Only for internal use

Parameters:
callable - The callable
Returns:
The result of the Callable::call().
Throws:
java.lang.Exception

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
Parameters:
callable - Its call() method will be called synchronized.
Returns:
The result of the call() invocation.
Throws:
java.lang.Exception

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
Parameters:
closeable - Its close() method will be called before the context or the VM terminates.

getRealPathInternal

public static java.lang.String getRealPathInternal(java.lang.String path)
Only for internal use

Parameters:
path - the path
Returns:
the real path

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
Parameters:
path - the relative path to an existing resource
Returns:
the file system path

get

public java.lang.Object get(java.lang.String key)
Get a binding from the engine scope

Specified by:
get in interface IContext
Parameters:
key - the key
Returns:
the value

put

public void put(java.lang.String key,
                java.lang.Object val)
Add a new binding to the engine scope

Specified by:
put in interface IContext
Parameters:
key - the key
val - the value

remove

public void remove(java.lang.String key)
Remove a bindings from the engine scope

Specified by:
remove in interface IContext
Parameters:
key - the key

putAll

public void putAll(java.util.Map map)
Put all bindings to the engine scope

Specified by:
putAll in interface IContext
Parameters:
map - the map

getAll

public java.util.Map getAll()
Get all bindings from the engine scope

Specified by:
getAll in interface IContext
Returns:
the map

getSocketName

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

Specified by:
getSocketName in interface IContext
Returns:
the socket name

getRedirectString

public java.lang.String getRedirectString()
Deprecated. 

Specified by:
getRedirectString in interface IContext

getRedirectString

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

Specified by:
getRedirectString in interface IContext

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
Parameters:
webPath - Usually request.getContextPath()+request.getServletPath()
Returns:
the redirect string