This class is the entry point to the bean scripting framework. An
application wishing to integrate scripting to a Java app would
place an instance of a BSFManager in their code and use its services
to register the beans they want to make available for scripting,
load scripting engines, and run scripts.
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.
Each BSFManager and engine participate in the debugger framework.
Per JVM, a debug manager is created and manages the different
instances of BSFManagers, which in turn manage the engines.
The debug manager allows remote debugger to register themselves
for a particular language. The debugger will be notified of the
creation and termination of engines for that language of interest.
Each debugger can then ask the engines for its language-specific
debugging interface. Usually, this interface allows the debugger
to register its language-specific callbacks for debugging events.
classLoader
protected ClassLoader classLoader
classPath
protected String classPath
declaredBeans
protected Vector declaredBeans
extn2Lang
protected static Hashtable extn2Lang
loadedEngines
protected Hashtable loadedEngines
pcs
protected PropertyChangeSupport pcs
registeredEngines
protected static Hashtable registeredEngines
tempDir
protected String tempDir
apply
public Object apply(String lang,
String source,
int lineNo,
int columnNo,
Object funcBody,
Vector paramNames,
Vector arguments)
throws BSFException
Apply the given anonymous function of the given language to the given
parameters and return the resulting value.
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 parameters
BSFException
- if anything goes wrong while running the script
compileApply
public void compileApply(String lang,
String source,
int lineNo,
int columnNo,
Object funcBody,
Vector paramNames,
Vector arguments,
CodeBuffer cb)
throws BSFException
Compile the application of the given anonymous function of the given
language to the given parameters into the given CodeBuffer.
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 into
BSFException
- if anything goes wrong while running the script
compileExpr
public void compileExpr(String lang,
String source,
int lineNo,
int columnNo,
Object expr,
CodeBuffer cb)
throws BSFException
Compile the given expression of the given language into the given
CodeBuffer.
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 into
compileScript
public void compileScript(String lang,
String source,
int lineNo,
int columnNo,
Object script,
CodeBuffer cb)
throws BSFException
Compile the given script of the given language into the given
CodeBuffer.
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 into
declareBean
public void declareBean(String beanName,
Object bean,
Class type)
throws BSFException
Declare a bean. The difference between declaring and registering
is that engines are spsed 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 avaialable "automatically."
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 as
BSFException
- if any of the languages that are already
running decides to throw an exception when asked to
declare this bean.
eval
public Object eval(String lang,
String source,
int lineNo,
int columnNo,
Object expr)
throws BSFException
Evaluate the given expression of the given language and return the
resulting value.
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 evaluate
BSFException
- if anything goes wrong while running the script
exec
public void exec(String lang,
String source,
int lineNo,
int columnNo,
Object script)
throws BSFException
Execute the given script of the given language.
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 execute
BSFException
- if anything goes wrong while running the script
getClassLoader
public ClassLoader getClassLoader()
Get classLoader
getClassPath
public String getClassPath()
Get classPath
getDebugManager
public static BSFDebugManager getDebugManager()
Get debug manager
getLangFromFilename
public static String getLangFromFilename(String fileName)
throws BSFException
Determine the language of a script file by looking at the file
extension.
- the scripting language the file is in if the file extension
is known to me (must have been registered via
registerScriptingEngine).
getObjectRegistry
public ObjectRegistry getObjectRegistry()
Return the current object registry of the manager.
getTempDir
public String getTempDir()
Get tempDir
initBSFDebugManager
public static void initBSFDebugManager()
This is the init for the debugging framework.
The framework relies on a BSFDebugManager,
a unique object per JVM. The DebugManager acts
as a remote server for debuggers to register.
Once a debugger is registered for a given language, such as
javascript, the DebugManager will forward creation/deletion
events to the debugger about BSFManager and BSFEngine
instances.
Debugger can then ask the engine for its language-specific
debug interface and use it for registering its language-specific
callback interface.
From there on, the debugger is connected to the engine.
When the BSFEngine terminates, the debugger will be notified.
isLanguageRegistered
public static boolean isLanguageRegistered(String lang)
Determine whether a language is registered.
lang
- string identifying a language
loadScriptingEngine
public BSFEngine loadScriptingEngine(String lang)
throws BSFException
Load a scripting engine based on the lang string identifying it.
lang
- string identifying language
BSFException
- 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.
lookupBean
public Object lookupBean(String beanName)
return a handle to a bean registered in the bean registry by the
application or a scripting engine. Returns null if bean is not found.
beanName
- name of bean to look up
- the bean if its found or null
registerBean
public void registerBean(String beanName,
Object bean)
Registering a bean allows a scripting engine or the application to
access that bean by name and to manipulate it.
beanName
- name to register underbean
- the bean to register
registerScriptingEngine
public static void registerScriptingEngine(String lang,
String engineClassName,
String[] extensions)
Register a scripting engine in the static registry of the
BSFManager.
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.
setClassLoader
public void setClassLoader(ClassLoader classLoader)
Set the class loader for those that need to use it. Default is he
who loaded me or null (i.e., its Class.forName).
classLoader
- the class loader to use.
setClassPath
public void setClassPath(String classPath)
Set the classpath for those that need to use it. Default is the value
of the java.class.path property.
classPath
- the classpath to use
setObjectRegistry
public void setObjectRegistry(ObjectRegistry objectRegistry)
Set the object registry used by this manager. By default a new
one is created when the manager is new'ed and this overwrites
that one.
objectRegistry
- the registry to use
setTempDir
public void setTempDir(String tempDir)
Temporary directory to put stuff into (for those who need to). Note
that unless this directory is in the classpath or unless the
classloader knows to look in here, any classes here will not
be found! BSFManager provides a service method to load a class
which uses either the classLoader provided by the class loader
property or, if that fails, a class loader which knows to load from
the tempdir to try to load the class. Default value of tempDir
is "." (current working dir).
tempDir
- the temporary directory
terminate
public void terminate()
Gracefully terminate all engines
undeclareBean
public void undeclareBean(String beanName)
throws BSFException
Undeclare a previously declared bean. This removes the bean from
the list of declared beans in the manager as well as asks every
running engine to undeclared the bean. As with above, if any
of the engines except when asked to undeclare, this method does
not catch that exception. Quietly returns if the bean is unknown.
beanName
- name of bean to undeclare
BSFException
- if any of the languages that are already
running decides to throw an exception when asked to
undeclare this bean.
unregisterBean
public void unregisterBean(String beanName)
Unregister a previously registered bean. Silent if name is not found.
beanName
- name of bean to unregister