org.norther.tammi.core.scripter
Class DefaultScripter

java.lang.Object
  extended by org.norther.tammi.core.base.Adaptee
      extended by org.norther.tammi.core.base.DefaultObjectFactory<org.apache.bsf.BSFEngine>
          extended by org.norther.tammi.core.scripter.DefaultScripter
All Implemented Interfaces:
Serializable, NotificationBroadcaster, NotificationEmitter, Manageable, MBeanDelegate, ObjectFactory<org.apache.bsf.BSFEngine>, Scripter

public class DefaultScripter
extends DefaultObjectFactory<org.apache.bsf.BSFEngine>
implements Scripter

A default implementation of Scripter.

Version:
$Id: DefaultScripter.java,v 1.12 2009/10/29 21:39:57 cvsimp Exp $
Author:
Ilkka Priha
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.norther.tammi.core.base.Adaptee
ADAPTEE_NOTIF_DESCRIPTION, ADAPTEE_NOTIFICATIONS
 
Constructor Summary
DefaultScripter()
          Constructs a new scripter.
 
Method Summary
 Object apply(File file, List<String> paramNames, List<Object> arguments)
          Applies a function.
 Object apply(String path, List<String> paramNames, List<Object> arguments)
          Applies a function.
 Object apply(String lang, String source, InputStream input, List<String> paramNames, List<Object> arguments)
          Applies a function.
 Object apply(String lang, String source, int lineNo, int columnNo, Object funcBody, List<String> paramNames, List<Object> arguments)
          Applies the given anonymous function of the given language to the given parameters and returns the resulting value.
 Object apply(String key, String path, List<String> paramNames, List<Object> arguments)
          Applies a function.
 Object apply(String lang, String source, Reader reader, List<String> paramNames, List<Object> arguments)
          Applies a function.
 Object apply(URL url, List<String> paramNames, List<Object> arguments)
          Applies a function.
 void compileApply(String lang, String source, int lineNo, int columnNo, Object funcBody, List<String> paramNames, List<Object> arguments, ScriptCodeBuffer cb)
          Compiles the application of the given anonymous function of the given language to the given parameters into the given code buffer.
 void compileExpr(String lang, String source, int lineNo, int columnNo, Object expr, ScriptCodeBuffer cb)
          Compiles the given expression of the given language into the given code buffer.
 void compileScript(String lang, String source, int lineNo, int columnNo, Object script, ScriptCodeBuffer cb)
          Compiles the given script of the given language into the given code buffer.
 void declareBean(String beanName, Object bean, Class<?> type)
          Declares a bean.
 Object evaluate(File file)
          Evaluates an expression.
 Object evaluate(String path)
          Evaluates an expression.
 Object evaluate(String key, String path)
          Evaluates an expression.
 Object evaluate(String lang, String source, InputStream input)
          Evaluates an expression.
 Object evaluate(String lang, String source, int lineNo, int columnNo, Object expr)
          Evaluates the given expression in the given language and returns the resulting value.
 Object evaluate(String lang, String source, Reader reader)
          Evaluates an expression.
 Object evaluate(URL url)
          Evaluates an expression.
 void execute(File file)
          Executes a script.
 void execute(String path)
          Executes a script.
 void execute(String key, String path)
          Executes a script.
 void execute(String lang, String source, InputStream input)
          Executes a script.
 void execute(String lang, String source, int lineNo, int columnNo, Object script)
          Executes the given script in the given language.
 void execute(String lang, String source, Reader reader)
          Executes a script.
 void execute(URL url)
          Executes a script.
 ClassLoader getClassLoader()
          Gets the class loader.
 String getClassPath()
          Gets the classpath.
 String getDefaultLanguage()
          Gets the default script language.
 org.apache.bsf.BSFEngine getInstance(String className, ObjectName loader, Object[] params, String[] signature)
          Gets an instance of a named class using a specified class loader.
 String getLangFromFilename(String fileName)
          Determines the language of a script file by looking at the file extension.
protected  ScriptManager getManager()
          Gets the implementation of the manager.
 ScriptObjectRegistry getObjectRegistry()
          Returns the current object registry of the manager.
 String getScriptKey()
          Gets the path finder key for resolving script paths.
 String getTempDir()
          Gets the temporary directory.
 boolean isLanguageLoadable(String lang)
          Checks whether a scripting engine for the specified language is registered and loadable.
 boolean isLanguageRegistered(String lang)
          Determines whether a language is registered.
 org.apache.bsf.BSFEngine loadScriptingEngine(String lang)
          Loads a scripting engine based on the lang string identifying it.
 Object lookupBean(String beanName)
          Returns a handle to a declared MBean.
 void postmanaged()
          This method is called when the implementing adaptee has been managed either during post-registration of the corresponding adapter MBean or just after it is explicitly added to the adapter MBean during run-time.
 void registerBean(String beanName, Object bean)
          Registers a bean allowing a scripting engine or the application to access that bean by name and to manipulate it.
 void registerScriptingEngine(String lang, String engineClassName, String[] extensions)
          Registers a scripting engine in the static registry of the BSFManager.
 void reinitialize()
          Reinitializes all engines ignoring exceptions.
 void reinitialize(String lang)
          Reinitializes an engine of the specified language.
 void setClassLoader(ClassLoader classLoader)
          Sets the class loader for those that need to use it.
 void setClassPath(String classPath)
          Sets the classpath for those that need to use it.
 void setDefaultLanguage(String lang)
          Sets the default script language.
 void setObjectRegistry(ScriptObjectRegistry objectRegistry)
          Sets the object registry used by this manager.
 void setScriptKey(String key)
          Sets the path finder key for resolving script paths.
 void setTempDir(String tempDir)
          Sets the temporary directory to put stuff into (for those who need to).
 void terminate()
          Gracefully terminates all engines
 void undeclareBean(String beanName)
          Undeclares a previously declared bean.
 void unmanaged()
          This method is called when the implementing adaptee is unmanaged either during pre-deregistration of the corresponding adapter MBean or just before it is explicitly removed from the adapter MBean during run-time.
 void unregisterBean(String beanName)
          Unregisters a previously registered bean.
 
Methods inherited from class org.norther.tammi.core.base.DefaultObjectFactory
getInstance, getInstance, getInstance, isLoaderSupported
 
Methods inherited from class org.norther.tammi.core.base.Adaptee
addAdaptee, addNotificationListener, getAttributeSupport, getBroker, getCanonicalName, getDomain, getFactory, getLoader, getLog, getLog, getMBean, getMBeanServer, getNotificationInfo, getObjectName, getRegistrationTime, getSequenceNumber, hasListeners, isRegistered, premanaged, removeNotificationListener, removeNotificationListener, sendNotification, sendNotification, sendNotification, sendNotification, unregister
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.norther.tammi.core.base.ObjectFactory
getInstance, getInstance, getInstance, isLoaderSupported
 

Constructor Detail

DefaultScripter

public DefaultScripter()
Constructs a new scripter.

Method Detail

postmanaged

public void postmanaged()
Description copied from interface: Manageable
This method is called when the implementing adaptee has been managed either during post-registration of the corresponding adapter MBean or just after it is explicitly added to the adapter MBean during run-time.

Specified by:
postmanaged in interface Manageable
Overrides:
postmanaged in class Adaptee

unmanaged

public void unmanaged()
               throws Exception
Description copied from interface: Manageable
This method is called when the implementing adaptee is unmanaged either during pre-deregistration of the corresponding adapter MBean or just before it is explicitly removed from the adapter MBean during run-time.

Specified by:
unmanaged in interface Manageable
Overrides:
unmanaged in class Adaptee
Throws:
Exception - if the adaptee refused to be unmanaged.

getScriptKey

public String getScriptKey()
Description copied from interface: Scripter
Gets the path finder key for resolving script paths.

Specified by:
getScriptKey in interface Scripter
Returns:
the path finder key.

setScriptKey

public void setScriptKey(String key)
Description copied from interface: Scripter
Sets the path finder key for resolving script paths. The default key is PathFinderMBean.SCRIPT.

Specified by:
setScriptKey in interface Scripter
Parameters:
key - the path finder key.

getDefaultLanguage

public String getDefaultLanguage()
Description copied from interface: Scripter
Gets the default script language.

Specified by:
getDefaultLanguage in interface Scripter
Returns:
the default language.

setDefaultLanguage

public void setDefaultLanguage(String lang)
Description copied from interface: Scripter
Sets the default script language.

Specified by:
setDefaultLanguage in interface Scripter
Parameters:
lang - the default language.

compileApply

public void compileApply(String lang,
                         String source,
                         int lineNo,
                         int columnNo,
                         Object funcBody,
                         List<String> paramNames,
                         List<Object> arguments,
                         ScriptCodeBuffer cb)
                  throws LogException
Description copied from interface: Scripter
Compiles the application of the given anonymous function of the given language to the given parameters into the given code buffer.

Specified by:
compileApply in interface Scripter
Parameters:
lang - the language.
source - the source.
lineNo - the line number.
columnNo - the column number.
funcBody - the body of the function.
paramNames - the names of parameters.
arguments - the values of parameters.
cb - a code buffer.
Throws:
LogException - on errors.

compileExpr

public void compileExpr(String lang,
                        String source,
                        int lineNo,
                        int columnNo,
                        Object expr,
                        ScriptCodeBuffer cb)
                 throws LogException
Description copied from interface: Scripter
Compiles the given expression of the given language into the given code buffer.

Specified by:
compileExpr in interface Scripter
Parameters:
lang - the language.
source - the source.
lineNo - the line number.
columnNo - the column number.
expr - the expression.
cb - a code buffer.
Throws:
LogException - on errors.

compileScript

public void compileScript(String lang,
                          String source,
                          int lineNo,
                          int columnNo,
                          Object script,
                          ScriptCodeBuffer cb)
                   throws LogException
Description copied from interface: Scripter
Compiles the given script of the given language into the given code buffer.

Specified by:
compileScript in interface Scripter
Parameters:
lang - the language.
source - the source.
lineNo - the line number.
columnNo - the column number.
script - the script.
cb - a code buffer.
Throws:
LogException - on errors.

apply

public Object apply(String path,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies a function.

Specified by:
apply in interface Scripter
Parameters:
path - a path to a file or to folder of files to apply.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result of the last applied statement.
Throws:
LogException - on errors.

apply

public Object apply(String key,
                    String path,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies a function.

Specified by:
apply in interface Scripter
Parameters:
key - a path finder key.
path - a path to a file or to folder of files to apply.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result of the last applied statement.
Throws:
LogException - on errors.

apply

public Object apply(File file,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies a function.

Specified by:
apply in interface Scripter
Parameters:
file - a file or folder of files to evaluate.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

apply

public Object apply(URL url,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies a function.

Specified by:
apply in interface Scripter
Parameters:
url - a URL to evaluate.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

apply

public Object apply(String lang,
                    String source,
                    InputStream input,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies a function.

Specified by:
apply in interface Scripter
Parameters:
lang - the language of the stream.
source - the source of the stream.
input - a stream to apply.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result of the last applied statement.
Throws:
LogException - on errors.

apply

public Object apply(String lang,
                    String source,
                    Reader reader,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies a function.

Specified by:
apply in interface Scripter
Parameters:
lang - the language of the reader.
source - the source of the reader.
reader - a reader to apply.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result of the last applied statement.
Throws:
LogException - on errors.

apply

public Object apply(String lang,
                    String source,
                    int lineNo,
                    int columnNo,
                    Object funcBody,
                    List<String> paramNames,
                    List<Object> arguments)
             throws LogException
Description copied from interface: Scripter
Applies the given anonymous function of the given language to the given parameters and returns the resulting value.

Specified by:
apply in interface Scripter
Parameters:
lang - the language.
source - the source.
lineNo - the line number.
columnNo - the column number.
funcBody - the body of the function.
paramNames - the names of parameters.
arguments - the values of parameters.
Returns:
the result.
Throws:
LogException - on errors.

evaluate

public Object evaluate(String path)
                throws LogException
Description copied from interface: Scripter
Evaluates an expression.

Specified by:
evaluate in interface Scripter
Parameters:
path - a path to a file or to folder of files to evaluate.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

evaluate

public Object evaluate(String key,
                       String path)
                throws LogException
Description copied from interface: Scripter
Evaluates an expression.

Specified by:
evaluate in interface Scripter
Parameters:
key - a path finder key.
path - a path to a file or to folder of files to evaluate.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

evaluate

public Object evaluate(File file)
                throws LogException
Description copied from interface: Scripter
Evaluates an expression.

Specified by:
evaluate in interface Scripter
Parameters:
file - a file or folder of files to evaluate.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

evaluate

public Object evaluate(URL url)
                throws LogException
Description copied from interface: Scripter
Evaluates an expression.

Specified by:
evaluate in interface Scripter
Parameters:
url - a URL to evaluate.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

evaluate

public Object evaluate(String lang,
                       String source,
                       InputStream input)
                throws LogException
Description copied from interface: Scripter
Evaluates an expression.

Specified by:
evaluate in interface Scripter
Parameters:
lang - the language of the stream.
source - the source of the stream.
input - a stream to evaluate.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

evaluate

public Object evaluate(String lang,
                       String source,
                       Reader reader)
                throws LogException
Description copied from interface: Scripter
Evaluates an expression.

Specified by:
evaluate in interface Scripter
Parameters:
lang - the language of the reader.
source - the source of the reader.
reader - a reader to evaluate.
Returns:
the result of the last evaluated statement.
Throws:
LogException - on errors.

evaluate

public Object evaluate(String lang,
                       String source,
                       int lineNo,
                       int columnNo,
                       Object expr)
                throws LogException
Description copied from interface: Scripter
Evaluates the given expression in the given language and returns the resulting value.

Specified by:
evaluate in interface Scripter
Parameters:
lang - the language.
source - the source.
lineNo - the line number.
columnNo - the column number.
expr - the expression.
Returns:
the result.
Throws:
LogException - on errors.

execute

public void execute(String path)
             throws LogException
Description copied from interface: Scripter
Executes a script.

Specified by:
execute in interface Scripter
Parameters:
path - a path to a file or to folder of files to execute.
Throws:
LogException - on errors.

execute

public void execute(String key,
                    String path)
             throws LogException
Description copied from interface: Scripter
Executes a script.

Specified by:
execute in interface Scripter
Parameters:
key - a path finder key.
path - a path to a file or to folder of files to execute.
Throws:
LogException - on errors.

execute

public void execute(File file)
             throws LogException
Description copied from interface: Scripter
Executes a script.

Specified by:
execute in interface Scripter
Parameters:
file - a file or folder of files to execute.
Throws:
LogException - on errors.

execute

public void execute(URL url)
             throws LogException
Description copied from interface: Scripter
Executes a script.

Specified by:
execute in interface Scripter
Parameters:
url - a URL to execute.
Throws:
LogException - on errors.

execute

public void execute(String lang,
                    String source,
                    InputStream input)
             throws LogException
Description copied from interface: Scripter
Executes a script.

Specified by:
execute in interface Scripter
Parameters:
lang - the language of the stream.
source - the source of the stream.
input - a stream to execute.
Throws:
LogException - on errors.

execute

public void execute(String lang,
                    String source,
                    Reader reader)
             throws LogException
Description copied from interface: Scripter
Executes a script.

Specified by:
execute in interface Scripter
Parameters:
lang - the language of the reader.
source - the source of the reader.
reader - a reader to execute.
Throws:
LogException - on errors.

execute

public void execute(String lang,
                    String source,
                    int lineNo,
                    int columnNo,
                    Object script)
             throws LogException
Description copied from interface: Scripter
Executes the given script in the given language.

Specified by:
execute in interface Scripter
Parameters:
lang - the language.
source - the source.
lineNo - the line number.
columnNo - the column number.
script - the script.
Throws:
LogException - on errors.

getClassLoader

public ClassLoader getClassLoader()
Description copied from interface: Scripter
Gets the class loader.

Specified by:
getClassLoader in interface Scripter
Returns:
the class loader.

setClassLoader

public void setClassLoader(ClassLoader classLoader)
Description copied from interface: Scripter
Sets the class loader for those that need to use it.

Specified by:
setClassLoader in interface Scripter
Parameters:
classLoader - the class loader.

getClassPath

public String getClassPath()
Description copied from interface: Scripter
Gets the classpath.

Specified by:
getClassPath in interface Scripter
Returns:
the class path.

setClassPath

public void setClassPath(String classPath)
Description copied from interface: Scripter
Sets the classpath for those that need to use it.

Specified by:
setClassPath in interface Scripter
Parameters:
classPath - the class path.

getObjectRegistry

public ScriptObjectRegistry getObjectRegistry()
Description copied from interface: Scripter
Returns the current object registry of the manager.

Specified by:
getObjectRegistry in interface Scripter
Returns:
the object registry.

setObjectRegistry

public void setObjectRegistry(ScriptObjectRegistry objectRegistry)
Description copied from interface: Scripter
Sets the object registry used by this manager.

Specified by:
setObjectRegistry in interface Scripter
Parameters:
objectRegistry - the object registry.

getTempDir

public String getTempDir()
Description copied from interface: Scripter
Gets the temporary directory.

Specified by:
getTempDir in interface Scripter
Returns:
the temp directory.

setTempDir

public void setTempDir(String tempDir)
Description copied from interface: Scripter
Sets the temporary directory to put stuff into (for those who need to).

Specified by:
setTempDir in interface Scripter
Parameters:
tempDir - the temp directory.

getLangFromFilename

public String getLangFromFilename(String fileName)
Description copied from interface: Scripter
Determines the language of a script file by looking at the file extension.

Specified by:
getLangFromFilename in interface Scripter
Parameters:
fileName - the name of the file.
Returns:
the corresponding language.

isLanguageRegistered

public boolean isLanguageRegistered(String lang)
Description copied from interface: Scripter
Determines whether a language is registered.

Specified by:
isLanguageRegistered in interface Scripter
Parameters:
lang - the language.
Returns:
true or false.

registerScriptingEngine

public void registerScriptingEngine(String lang,
                                    String engineClassName,
                                    String[] extensions)
Description copied from interface: Scripter
Registers a scripting engine in the static registry of the BSFManager.

Specified by:
registerScriptingEngine in interface Scripter
Parameters:
lang - the language.
engineClassName - the engine class name.
extensions - an array of extensions.

isLanguageLoadable

public boolean isLanguageLoadable(String lang)
Description copied from interface: Scripter
Checks whether a scripting engine for the specified language is registered and loadable.

Specified by:
isLanguageLoadable in interface Scripter
Parameters:
lang - the language.
Returns:
true if available, false otherwise.

loadScriptingEngine

public org.apache.bsf.BSFEngine loadScriptingEngine(String lang)
                                             throws LogException
Description copied from interface: Scripter
Loads a scripting engine based on the lang string identifying it.

Specified by:
loadScriptingEngine in interface Scripter
Parameters:
lang - the language.
Returns:
the scripting engine.
Throws:
LogException - on errors.

lookupBean

public Object lookupBean(String beanName)
Description copied from interface: Scripter
Returns a handle to a declared MBean.

Specified by:
lookupBean in interface Scripter
Parameters:
beanName - the name of the MBean.
Returns:
the MBean object.

registerBean

public void registerBean(String beanName,
                         Object bean)
Description copied from interface: Scripter
Registers a bean allowing a scripting engine or the application to access that bean by name and to manipulate it.

Specified by:
registerBean in interface Scripter
Parameters:
beanName - the name of the bean.
bean - the bean object.

unregisterBean

public void unregisterBean(String beanName)
Description copied from interface: Scripter
Unregisters a previously registered bean.

Specified by:
unregisterBean in interface Scripter
Parameters:
beanName - the name of the bean.

declareBean

public void declareBean(String beanName,
                        Object bean,
                        Class<?> type)
                 throws LogException
Description copied from interface: Scripter
Declares a bean. The difference between declaring and registering is that engines are supposed to make declared beans "pre-available" in the scripts as far as possible. That is, if a script author needs a registered bean, he needs to look it up in some way. However if he needs a declared bean, the language has the responsibility to make those beans available "automatically."

Declared beans are also registered.

Specified by:
declareBean in interface Scripter
Parameters:
beanName - the name of the bean.
bean - the bean object.
type - the class.
Throws:
LogException - on errors.

undeclareBean

public void undeclareBean(String beanName)
                   throws LogException
Description copied from interface: Scripter
Undeclares a previously declared bean.

Specified by:
undeclareBean in interface Scripter
Parameters:
beanName - the name of the bean.
Throws:
LogException - on errors.

reinitialize

public void reinitialize(String lang)
Description copied from interface: Scripter
Reinitializes an engine of the specified language.

Specified by:
reinitialize in interface Scripter
Parameters:
lang - the language.

reinitialize

public void reinitialize()
Description copied from interface: Scripter
Reinitializes all engines ignoring exceptions.

Specified by:
reinitialize in interface Scripter

terminate

public void terminate()
Description copied from interface: Scripter
Gracefully terminates all engines

Specified by:
terminate in interface Scripter

getInstance

public org.apache.bsf.BSFEngine getInstance(String className,
                                            ObjectName loader,
                                            Object[] params,
                                            String[] signature)
                                     throws ConstructionException
Description copied from interface: ObjectFactory
Gets an instance of a named class using a specified class loader. Parameters for its constructor are given as an array of objects, primitive types must be wrapped with a corresponding class.

Class loaders are supported only if the isLoaderSupported method returns true. Otherwise the loader parameter is ignored.

Specified by:
getInstance in interface ObjectFactory<org.apache.bsf.BSFEngine>
Overrides:
getInstance in class DefaultObjectFactory<org.apache.bsf.BSFEngine>
Parameters:
className - the name of the class.
loader - the class loader.
params - an array containing the parameters of the constructor.
signature - an array containing the signature of the constructor.
Returns:
the instance.
Throws:
ConstructionException - if construction fails.

getManager

protected ScriptManager getManager()
Gets the implementation of the manager.

Returns:
the manager.


Copyright © 2004 The Norther Organization. All rights reserved.