Asterisk-Java

org.asteriskjava.fastagi
Class ScriptEngineMappingStrategy

java.lang.Object
  extended by org.asteriskjava.fastagi.ScriptEngineMappingStrategy
All Implemented Interfaces:
MappingStrategy

public class ScriptEngineMappingStrategy
extends java.lang.Object
implements MappingStrategy

A MappingStrategy that uses ScriptEngine to run AgiScripts. This MappingStrategy can be used to run JavaScript, Groovy, JRuby, etc. scripts.

Since:
1.0.0

Nested Class Summary
protected  class ScriptEngineMappingStrategy.ScriptEngineAgiScript
           
 
Field Summary
static java.lang.String CHANNEL
          The binding under which the AGI channel is made available to scripts.
protected  java.lang.String[] libPath
           
protected  Log logger
           
static java.lang.String REQUEST
          The binding under which the AGI request is made available to scripts.
protected  javax.script.ScriptEngineManager scriptEngineManager
           
protected  java.lang.String[] scriptPath
           
 
Constructor Summary
ScriptEngineMappingStrategy()
          Creates a new ScriptEngineMappingStrategy that searches for scripts in the current directory.
ScriptEngineMappingStrategy(java.lang.String[] scriptPath, java.lang.String[] libPath)
          Creates a new ScriptEngineMappingStrategy that searches for scripts on the given path.
 
Method Summary
 AgiScript determineScript(AgiRequest request)
          Returns the AgiScript instance that is responsible to handle the given request.
protected  java.lang.ClassLoader getClassLoader()
          Returns the ClassLoader to use for the ScriptEngineManager.
protected static java.lang.String getExtension(java.lang.String scriptName)
          Returns the extension (the part after the last ".") of the given script.
protected static java.io.Reader getReader(java.io.File file)
           
protected  javax.script.ScriptEngine getScriptEngine(java.io.File file)
          Searches for a ScriptEngine that can handle the given file.
protected  javax.script.ScriptEngineManager getScriptEngineManager()
          Returns the ScriptEngineManager to use for loading the ScriptEngine.
protected  boolean isInside(java.io.File file, java.io.File dir)
          Checks whether a file is contained within a given directory (or a sub directory) or not.
protected  void populateBindings(java.io.File file, AgiRequest request, AgiChannel channel, javax.script.Bindings bindings)
          Override this method if you want to add additional bindings before the script is run.
protected  java.io.File searchFile(java.lang.String scriptName, java.lang.String[] path)
          Searches for the file with the given name on the path.
 void setLibPath(java.lang.String[] libPath)
          Sets the path to search for additional libraries (jar files).
 void setScriptPath(java.lang.String[] scriptPath)
          Sets the path to search for script files.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger

REQUEST

public static final java.lang.String REQUEST
The binding under which the AGI request is made available to scripts.

See Also:
Constant Field Values

CHANNEL

public static final java.lang.String CHANNEL
The binding under which the AGI channel is made available to scripts.

See Also:
Constant Field Values

scriptPath

protected java.lang.String[] scriptPath

libPath

protected java.lang.String[] libPath

scriptEngineManager

protected javax.script.ScriptEngineManager scriptEngineManager
Constructor Detail

ScriptEngineMappingStrategy

public ScriptEngineMappingStrategy()
Creates a new ScriptEngineMappingStrategy that searches for scripts in the current directory.


ScriptEngineMappingStrategy

public ScriptEngineMappingStrategy(java.lang.String[] scriptPath,
                                   java.lang.String[] libPath)
Creates a new ScriptEngineMappingStrategy that searches for scripts on the given path.

Parameters:
scriptPath - array of directory names to search for script files.
libPath - array of directory names to search for additional libraries (jar files).
Method Detail

setScriptPath

public void setScriptPath(java.lang.String[] scriptPath)
Sets the path to search for script files.

Default is "agi".

Parameters:
scriptPath - array of directory names to search for script files.

setLibPath

public void setLibPath(java.lang.String[] libPath)
Sets the path to search for additional libraries (jar files).

Default is "lib".

Parameters:
libPath - array of directory names to search for additional libraries (jar files).

determineScript

public AgiScript determineScript(AgiRequest request)
Description copied from interface: MappingStrategy
Returns the AgiScript instance that is responsible to handle the given request.

Specified by:
determineScript in interface MappingStrategy
Parameters:
request - the request to lookup.
Returns:
the AgiScript instance to handle this request or null if none could be determined by this strategy.

getScriptEngine

protected javax.script.ScriptEngine getScriptEngine(java.io.File file)
Searches for a ScriptEngine that can handle the given file.

Parameters:
file - the file to search a ScriptEngine for.
Returns:
the ScriptEngine or null if none is found.

getScriptEngineManager

protected javax.script.ScriptEngineManager getScriptEngineManager()
Returns the ScriptEngineManager to use for loading the ScriptEngine. The ScriptEngineManager is only created once and reused for subsequent requests. Override this method to provide your own implementation.

Returns:
the ScriptEngineManager to use for loading the ScriptEngine.
See Also:
ScriptEngineManager.ScriptEngineManager()

getClassLoader

protected java.lang.ClassLoader getClassLoader()
Returns the ClassLoader to use for the ScriptEngineManager. Adds all jar files in the "lib" subdirectory of the current directory to the class path. Override this method to provide your own ClassLoader.

Returns:
the ClassLoader to use for the ScriptEngineManager.
See Also:
getScriptEngineManager()

searchFile

protected java.io.File searchFile(java.lang.String scriptName,
                                  java.lang.String[] path)
Searches for the file with the given name on the path.

Parameters:
scriptName - the name of the file to search for.
path - an array of directories to search for the file in order of preference.
Returns:
the canonical file if found on the path or null if not found.

isInside

protected final boolean isInside(java.io.File file,
                                 java.io.File dir)
                          throws java.io.IOException
Checks whether a file is contained within a given directory (or a sub directory) or not.

Parameters:
file - the file to check.
dir - the directory to check.
Returns:
true if file is below directory, false otherwise.
Throws:
java.io.IOException - if the canonical path of file or dir cannot be determined.

getExtension

protected static java.lang.String getExtension(java.lang.String scriptName)
Returns the extension (the part after the last ".") of the given script.

Parameters:
scriptName - the name of the script to return the extension of.
Returns:
the extension of the script or null if there is no extension.

getReader

protected static java.io.Reader getReader(java.io.File file)
                                   throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

populateBindings

protected void populateBindings(java.io.File file,
                                AgiRequest request,
                                AgiChannel channel,
                                javax.script.Bindings bindings)
Override this method if you want to add additional bindings before the script is run. By default the AGI request, AGI channel and the filename are available to scripts under the bindings "request", "channel" and "javax.script.filename".

Parameters:
file - the script file.
request - the AGI request.
channel - the AGI channel.
bindings - the bindings to populate.

Asterisk-Java

Copyright © 2004-2009 Stefan Reuter. All Rights Reserved.