org.helma.javascript
Class RhinoEngine

java.lang.Object
  extended by org.helma.javascript.RhinoEngine

public class RhinoEngine
extends java.lang.Object

This class provides methods to create JavaScript objects from JavaScript files.

Author:
Hannes Wallnoefer

Field Summary
static java.lang.Object[] EMPTY_ARGS
           
 
Constructor Summary
RhinoEngine(java.util.List<Repository> repositories)
          Create a RhinoEngine which loads scripts from directory dir.
RhinoEngine(java.util.List<Repository> repositories, java.lang.Class[] classes)
          Create a RhinoEngine which loads scripts from directory dir and defines the given classes as native host objects.
 
Method Summary
 void addCallback(java.lang.String event, java.lang.String name, org.mozilla.javascript.Function func)
          Register a callback.
 org.mozilla.javascript.Scriptable createThreadScope(org.mozilla.javascript.Context cx)
          Create the per-thread top level javascript scope.
 void defineHostClass(java.lang.Class clazz)
          Define a Javascript host object implemented by the given class.
 java.lang.Object evaluate(java.util.Map<java.lang.String,java.lang.Object> globals, java.lang.String path)
          Evaluate a module and return the result.
 Repository findRepository(java.lang.String path, Repository localPath)
           
 Resource findResource(java.lang.String path, Repository localPath)
          Search for a resource in a local path, or the main repository.
 HelmaClassLoader getClassLoader()
           
 org.mozilla.javascript.ContextFactory getContextFactory()
           
 ExtendedJavaClass getExtendedClass(java.lang.Class type)
           
protected  Repository getRepository(org.mozilla.javascript.Scriptable scope)
          Get the repository associated with the scope or one of its prototypes
 Repository getRepository(java.lang.String path)
          Get a resource from our script repository
 Resource getResource(java.lang.String path)
          Get a resource from our script repository
 java.util.List<Resource> getResources(java.lang.String path)
          Get a list of all child resources for the given path relative to our script repository.
 ReloadableScript getScript(java.lang.String moduleName)
          Resolves a type name to a script file within our script directory and returns a Scriptable evaluated to the file.
 ReloadableScript getScript(java.lang.String moduleName, Repository localPath)
          Resolves a type name to a script file within our script directory and returns a Scriptable evaluated to the file.
 ModuleScope getShellScope()
          Return a shell scope for interactive evaluation
 org.mozilla.javascript.WrapFactory getWrapFactory()
           
protected  void initGlobalsAndArguments(org.mozilla.javascript.Scriptable scope, java.util.Map<java.lang.String,java.lang.Object> globals, java.lang.Object[] args)
          Initialize and normalize the global variables and arguments on a thread scope.
 java.lang.Object invoke(java.util.Map<java.lang.String,java.lang.Object> globals, org.mozilla.javascript.Scriptable thisObj, org.mozilla.javascript.Function function, java.lang.Object[] args)
          Invoke a javascript function.
 java.lang.Object invoke(java.util.Map<java.lang.String,java.lang.Object> globals, java.lang.String path, java.lang.String method, java.lang.Object... args)
          Invoke a javascript function.
 java.lang.Object invokeCallback(java.lang.String event, java.lang.Object thisObj, java.lang.Object... args)
          Invoke a callback.
 org.mozilla.javascript.Scriptable loadModule(org.mozilla.javascript.Context cx, java.lang.String moduleName, org.mozilla.javascript.Scriptable parentScope, org.mozilla.javascript.Scriptable loadingScope)
          Load a Javascript module into a module scope.
 void removeCallback(java.lang.String event, java.lang.String name)
          Unregister a previously registered callback.
protected  java.lang.Object wrapArgument(java.lang.Object value, org.mozilla.javascript.Scriptable scope)
          Prepare a single property or argument value for use within rhino.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARGS

public static final java.lang.Object[] EMPTY_ARGS
Constructor Detail

RhinoEngine

public RhinoEngine(java.util.List<Repository> repositories)
Create a RhinoEngine which loads scripts from directory dir.

Parameters:
repositories - a list of code repositories

RhinoEngine

public RhinoEngine(java.util.List<Repository> repositories,
                   java.lang.Class[] classes)
Create a RhinoEngine which loads scripts from directory dir and defines the given classes as native host objects.

Parameters:
repositories - a list of code repositories
classes - the host classes to define
Method Detail

defineHostClass

public void defineHostClass(java.lang.Class clazz)
                     throws java.lang.reflect.InvocationTargetException,
                            java.lang.InstantiationException,
                            java.lang.IllegalAccessException
Define a Javascript host object implemented by the given class.

Parameters:
clazz - The Java class implementing the host object.
Throws:
java.lang.IllegalAccessException - if access is not available to a reflected class member
java.lang.InstantiationException - if unable to instantiate the named class
java.lang.reflect.InvocationTargetException - if an exception is thrown during execution of methods of the named class

addCallback

public void addCallback(java.lang.String event,
                        java.lang.String name,
                        org.mozilla.javascript.Function func)
Register a callback. Callbacks are javascript functions that can be invoked on certain events.

Parameters:
name - the callback name
func - the callback function

removeCallback

public void removeCallback(java.lang.String event,
                           java.lang.String name)
Unregister a previously registered callback.

Parameters:
name - the callback name

invokeCallback

public java.lang.Object invokeCallback(java.lang.String event,
                                       java.lang.Object thisObj,
                                       java.lang.Object... args)
Invoke a callback. If no callback is registered under this name fail silently.

Parameters:
event - the callback event
args - the arguments
Returns:
the return value

evaluate

public java.lang.Object evaluate(java.util.Map<java.lang.String,java.lang.Object> globals,
                                 java.lang.String path)
                          throws java.io.IOException
Evaluate a module and return the result.

Parameters:
globals - a map of global variables
path - the path of the module to evaluate
Returns:
the return value of the evaluation
Throws:
java.io.IOException - if an I/O related error occurred

invoke

public java.lang.Object invoke(java.util.Map<java.lang.String,java.lang.Object> globals,
                               java.lang.String path,
                               java.lang.String method,
                               java.lang.Object... args)
                        throws java.io.IOException,
                               java.lang.NoSuchMethodException
Invoke a javascript function. This enters a JavaScript context, creates a new per-thread scope, calls the function, exits the context and returns the return value of the invocation.

Parameters:
globals - map of global variables to set in the thread scope
path - the file path to the script
method - the method name to call in the script
args - the arguments to pass to the method
Returns:
the return value of the invocation
Throws:
java.lang.NoSuchMethodException - the method is not defined
java.io.IOException - an I/O related error occurred

getShellScope

public ModuleScope getShellScope()
Return a shell scope for interactive evaluation

Returns:
a shell scope

invoke

public java.lang.Object invoke(java.util.Map<java.lang.String,java.lang.Object> globals,
                               org.mozilla.javascript.Scriptable thisObj,
                               org.mozilla.javascript.Function function,
                               java.lang.Object[] args)
                        throws java.io.IOException,
                               java.lang.NoSuchMethodException
Invoke a javascript function. This enters a JavaScript context, creates a new per-thread scope, calls the function, exits the context and returns the return value of the invocation.

Parameters:
globals - map of global variables to set in the thread scope
thisObj - the object to invoke the function on
function - the function object to invoke
args - the arguments to pass to the method
Returns:
the return value of the invocation
Throws:
java.lang.NoSuchMethodException - the method is not defined
java.io.IOException - an I/O related error occurred

createThreadScope

public org.mozilla.javascript.Scriptable createThreadScope(org.mozilla.javascript.Context cx)
Create the per-thread top level javascript scope. This has the global shared scope as prototype, and serves as prototype for the module scopes loaded by this thread.

Parameters:
cx - the current context
Returns:
the scope object

initGlobalsAndArguments

protected void initGlobalsAndArguments(org.mozilla.javascript.Scriptable scope,
                                       java.util.Map<java.lang.String,java.lang.Object> globals,
                                       java.lang.Object[] args)
Initialize and normalize the global variables and arguments on a thread scope.

Parameters:
scope - the thread local scope
globals - the map of global objects
args - the arguments

wrapArgument

protected java.lang.Object wrapArgument(java.lang.Object value,
                                        org.mozilla.javascript.Scriptable scope)
Prepare a single property or argument value for use within rhino.

Parameters:
value - the property or argument value
scope - the scope
Returns:
the object wrapped and wired for rhino

getScript

public ReloadableScript getScript(java.lang.String moduleName)
                           throws org.mozilla.javascript.JavaScriptException,
                                  java.io.IOException
Resolves a type name to a script file within our script directory and returns a Scriptable evaluated to the file.

Parameters:
moduleName - the name of the module to load
Returns:
The raw compiled script for the module
Throws:
org.mozilla.javascript.JavaScriptException - if an error occurred evaluating the script file
java.io.IOException - if an error occurred reading the script file

getScript

public ReloadableScript getScript(java.lang.String moduleName,
                                  Repository localPath)
                           throws org.mozilla.javascript.JavaScriptException,
                                  java.io.IOException
Resolves a type name to a script file within our script directory and returns a Scriptable evaluated to the file.

Parameters:
moduleName - the name of the module to load
localPath - the path of the resource issuing this call
Returns:
The raw compiled script for the module
Throws:
org.mozilla.javascript.JavaScriptException - if an error occurred evaluating the script file
java.io.IOException - if an error occurred reading the script file

loadModule

public org.mozilla.javascript.Scriptable loadModule(org.mozilla.javascript.Context cx,
                                                    java.lang.String moduleName,
                                                    org.mozilla.javascript.Scriptable parentScope,
                                                    org.mozilla.javascript.Scriptable loadingScope)
                                             throws java.io.IOException
Load a Javascript module into a module scope. This checks if the module has already been loaded in the current context and if so returns the existing module scope.

Parameters:
cx - the current context
moduleName - the module name
parentScope - the parent scope to load the module
loadingScope - the scope requesting the module
Returns:
the loaded module scope
Throws:
java.io.IOException - indicates that in input/output related error occurred

getRepository

protected Repository getRepository(org.mozilla.javascript.Scriptable scope)
Get the repository associated with the scope or one of its prototypes

Parameters:
scope - the scope to get the repository from
Returns:
the repository, or null

getResource

public Resource getResource(java.lang.String path)
Get a resource from our script repository

Parameters:
path - the resource path
Returns:
the resource

getRepository

public Repository getRepository(java.lang.String path)
Get a resource from our script repository

Parameters:
path - the resource path
Returns:
the resource

getResources

public java.util.List<Resource> getResources(java.lang.String path)
Get a list of all child resources for the given path relative to our script repository.

Parameters:
path - the repository path
Returns:
a list of all nested child resources

findResource

public Resource findResource(java.lang.String path,
                             Repository localPath)
Search for a resource in a local path, or the main repository.

Parameters:
path - the resource name
localPath - a repository to look first
Returns:
the resource

findRepository

public Repository findRepository(java.lang.String path,
                                 Repository localPath)

getContextFactory

public org.mozilla.javascript.ContextFactory getContextFactory()

getClassLoader

public HelmaClassLoader getClassLoader()

getWrapFactory

public org.mozilla.javascript.WrapFactory getWrapFactory()

getExtendedClass

public ExtendedJavaClass getExtendedClass(java.lang.Class type)