public class BSFManager
extends java.lang.Object
BSFManager serves as the registry of available scripting engines as well. Loading and unloading of scripting engines is supported as well. Each BSFManager loads one engine per language. Several BSFManagers can be created per JVM.
Modifier and Type | Field and Description |
---|---|
protected java.lang.ClassLoader |
classLoader |
protected java.lang.String |
classPath |
protected java.util.Vector |
declaredBeans |
protected static java.util.Hashtable |
extn2Lang |
protected java.util.Hashtable |
loadedEngines |
protected ObjectRegistry |
objectRegistry |
protected java.beans.PropertyChangeSupport |
pcs |
protected static java.util.Hashtable |
registeredEngines |
protected java.lang.String |
tempDir |
protected static java.lang.String |
version |
Constructor and Description |
---|
BSFManager() |
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
apply(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object funcBody,
java.util.Vector paramNames,
java.util.Vector arguments)
Apply the given anonymous function of the given language to the given
parameters and return the resulting value.
|
void |
compileApply(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object funcBody,
java.util.Vector paramNames,
java.util.Vector arguments,
CodeBuffer cb)
Compile the application of the given anonymous function of the given
language to the given parameters into the given CodeBuffer.
|
void |
compileExpr(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object expr,
CodeBuffer cb)
Compile the given expression of the given language into the given
CodeBuffer.
|
void |
compileScript(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object script,
CodeBuffer cb)
Compile the given script of the given language into the given
CodeBuffer.
|
void |
declareBean(java.lang.String beanName,
java.lang.Object bean,
java.lang.Class type)
Declare a bean.
|
java.lang.Object |
eval(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object expr)
Evaluate the given expression of the given language and return the
resulting value.
|
void |
exec(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object script)
Execute the given script of the given language.
|
java.lang.ClassLoader |
getClassLoader()
Get classLoader
|
java.lang.String |
getClassPath()
Get classPath
|
static java.lang.String |
getLangFromFilename(java.lang.String fileName)
Determine the language of a script file by looking at the file
extension.
|
ObjectRegistry |
getObjectRegistry()
Return the current object registry of the manager.
|
java.lang.String |
getTempDir()
Get tempDir
|
static java.lang.String |
getVersion()
Returns the version string of BSF.
|
void |
iexec(java.lang.String lang,
java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object script)
Execute the given script of the given language, attempting to
emulate an interactive session w/ the language.
|
static boolean |
isLanguageRegistered(java.lang.String lang)
Determine whether a language is registered.
|
BSFEngine |
loadScriptingEngine(java.lang.String lang)
Load a scripting engine based on the lang string identifying it.
|
java.lang.Object |
lookupBean(java.lang.String beanName)
return a handle to a bean registered in the bean registry by the
application or a scripting engine.
|
void |
registerBean(java.lang.String beanName,
java.lang.Object bean)
Registering a bean allows a scripting engine or the application to
access that bean by name and to manipulate it.
|
static void |
registerScriptingEngine(java.lang.String lang,
java.lang.String engineClassName,
java.lang.String[] extensions)
Register a scripting engine in the static registry of the
BSFManager.
|
void |
setClassLoader(java.lang.ClassLoader classLoader)
Set the class loader for those that need to use it.
|
void |
setClassPath(java.lang.String classPath)
Set the classpath for those that need to use it.
|
void |
setObjectRegistry(ObjectRegistry objectRegistry)
Set the object registry used by this manager.
|
void |
setTempDir(java.lang.String tempDir)
Temporary directory to put stuff into (for those who need to).
|
void |
terminate()
Gracefully terminate all engines
|
void |
undeclareBean(java.lang.String beanName)
Undeclare a previously declared bean.
|
void |
unregisterBean(java.lang.String beanName)
Unregister a previously registered bean.
|
protected static java.lang.String version
protected static java.util.Hashtable registeredEngines
protected static java.util.Hashtable extn2Lang
protected java.util.Hashtable loadedEngines
protected ObjectRegistry objectRegistry
protected java.beans.PropertyChangeSupport pcs
protected java.lang.ClassLoader classLoader
protected java.lang.String tempDir
protected java.lang.String classPath
protected java.util.Vector declaredBeans
public static java.lang.String getVersion()
240.20061006
"
stands for: BSF version 2.4.0
as of 2006-10-06
.public java.lang.Object apply(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object funcBody, java.util.Vector paramNames, java.util.Vector arguments) throws BSFException
lang
- language identifiersource
- (context info) the source of this expression
(e.g., filename)lineNo
- (context info) the line number in source for exprcolumnNo
- (context info) the column number in source for exprfuncBody
- the multi-line, value returning script to evaluateparamNames
- the names of the parameters above assumesarguments
- values of the above parametersBSFException
- if anything goes wrong while running the scriptpublic void compileApply(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object funcBody, java.util.Vector paramNames, java.util.Vector arguments, CodeBuffer cb) throws BSFException
lang
- language identifiersource
- (context info) the source of this expression
(e.g., filename)lineNo
- (context info) the line number in source for exprcolumnNo
- (context info) the column number in source for exprfuncBody
- the multi-line, value returning script to evaluateparamNames
- the names of the parameters above assumesarguments
- values of the above parameterscb
- code buffer to compile intoBSFException
- if anything goes wrong while running the scriptpublic void compileExpr(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object expr, CodeBuffer cb) throws BSFException
lang
- language identifiersource
- (context info) the source of this expression
(e.g., filename)lineNo
- (context info) the line number in source for exprcolumnNo
- (context info) the column number in source for exprexpr
- the expression to compilecb
- code buffer to compile intoBSFException
- if any error while compiling the expressionpublic void compileScript(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object script, CodeBuffer cb) throws BSFException
lang
- language identifiersource
- (context info) the source of this script
(e.g., filename)lineNo
- (context info) the line number in source for scriptcolumnNo
- (context info) the column number in source for scriptscript
- the script to compilecb
- code buffer to compile intoBSFException
- if any error while compiling the scriptpublic void declareBean(java.lang.String beanName, java.lang.Object bean, java.lang.Class type) throws BSFException
When a bean is declared it is automatically registered as well so that any declared bean can be gotton to by looking it up as well.
If any of the languages that are already running in this manager says they don't like this (by throwing an exception) then this method will simply quit with that exception. That is, any engines that come after than in the engine enumeration will not even be told about this new bean.
So, in general its best to declare beans before the manager has been asked to load any engines because then the user can be informed when an engine rejects it. Also, its much more likely that an engine can declare a bean at start time than it can at any time.
beanName
- name to declare bean asbean
- the bean that's being declaredtype
- the type to represent the bean asBSFException
- if any of the languages that are already
running decides to throw an exception when asked to
declare this bean.public java.lang.Object eval(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object expr) throws BSFException
lang
- language identifiersource
- (context info) the source of this expression
(e.g., filename)lineNo
- (context info) the line number in source for exprcolumnNo
- (context info) the column number in source for exprexpr
- the expression to evaluateBSFException
- if anything goes wrong while running the scriptpublic void exec(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object script) throws BSFException
lang
- language identifiersource
- (context info) the source of this expression
(e.g., filename)lineNo
- (context info) the line number in source for exprcolumnNo
- (context info) the column number in source for exprscript
- the script to executeBSFException
- if anything goes wrong while running the scriptpublic void iexec(java.lang.String lang, java.lang.String source, int lineNo, int columnNo, java.lang.Object script) throws BSFException
lang
- language identifiersource
- (context info) the source of this expression
(e.g., filename)lineNo
- (context info) the line number in source for exprcolumnNo
- (context info) the column number in source for exprscript
- the script to executeBSFException
- if anything goes wrong while running the scriptpublic java.lang.ClassLoader getClassLoader()
public java.lang.String getClassPath()
public static java.lang.String getLangFromFilename(java.lang.String fileName) throws BSFException
fileName
- the name of the fileBSFException
- if file's extension is unknown.public ObjectRegistry getObjectRegistry()
public java.lang.String getTempDir()
public static boolean isLanguageRegistered(java.lang.String lang)
lang
- string identifying a languagepublic BSFEngine loadScriptingEngine(java.lang.String lang) throws BSFException
lang
- string identifying languageBSFException
- if the language is unknown (i.e., if it
has not been registered) with a reason of
REASON_UNKNOWN_LANGUAGE. If the language is known but
if the interface can't be created for some reason, then
the reason is set to REASON_OTHER_ERROR and the actual
exception is passed on as well.public java.lang.Object lookupBean(java.lang.String beanName)
beanName
- name of bean to look uppublic void registerBean(java.lang.String beanName, java.lang.Object bean)
beanName
- name to register underbean
- the bean to registerpublic static void registerScriptingEngine(java.lang.String lang, java.lang.String engineClassName, java.lang.String[] extensions)
lang
- string identifying languageengineClassName
- fully qualified name of the class interfacing
the language to BSF.extensions
- array of file extensions that should be mapped to
this language type. may be null.public void setClassLoader(java.lang.ClassLoader classLoader)
classLoader
- the class loader to use.public void setClassPath(java.lang.String classPath)
classPath
- the classpath to usepublic void setObjectRegistry(ObjectRegistry objectRegistry)
objectRegistry
- the registry to usepublic void setTempDir(java.lang.String tempDir)
tempDir
- the temporary directorypublic void terminate()
public void undeclareBean(java.lang.String beanName) throws BSFException
beanName
- name of bean to undeclareBSFException
- if any of the languages that are already
running decides to throw an exception when asked to
undeclare this bean.public void unregisterBean(java.lang.String beanName)
beanName
- name of bean to unregister