Class ScriptEngineManager
public class ScriptEngineManager extends Object
ScriptEngineManager
implements a discovery and instantiation mechanism for ScriptEngine
classes and also maintains a collection of key/value pairs storing state shared by all engines created by the Manager. This class uses the service provider mechanism described in the ServiceLoader
class to enumerate all the implementations of ScriptEngineFactory
. The
ScriptEngineManager
provides a method to return a list of all these factories as well as utility methods which look up factories on the basis of language name, file extension and mime type. The Bindings
of key/value pairs, referred to as the "Global Scope" maintained by the manager is available to all instances of ScriptEngine
created by the ScriptEngineManager
. The values in the Bindings
are generally exposed in all scripts.
- Since:
- 1.6
Constructor Summary
Constructor | Description |
---|---|
ScriptEngineManager() |
The effect of calling this constructor is the same as calling ScriptEngineManager(Thread.currentThread().getContextClassLoader()) . |
ScriptEngineManager |
This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism.If loader is null , the script engine factories that are bundled with the platform are loaded. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
Object |
get |
Gets the value for the specified key in the Global Scope |
Bindings |
getBindings() |
getBindings returns the value of the globalScope field. |
ScriptEngine |
getEngineByExtension |
Look up and create a ScriptEngine for a given extension. |
ScriptEngine |
getEngineByMimeType |
Look up and create a ScriptEngine for a given mime type. |
ScriptEngine |
getEngineByName |
Looks up and creates a ScriptEngine for a given name. |
List<ScriptEngineFactory> |
getEngineFactories() |
Returns a list whose elements are instances of all the ScriptEngineFactory classes found by the discovery mechanism. |
void |
put |
Sets the specified key/value pair in the Global Scope. |
void |
registerEngineExtension |
Registers a ScriptEngineFactory to handle an extension. |
void |
registerEngineMimeType |
Registers a ScriptEngineFactory to handle a mime type. |
void |
registerEngineName |
Registers a ScriptEngineFactory to handle a language name. |
void |
setBindings |
setBindings stores the specified Bindings in the globalScope field. |
Constructor Details
ScriptEngineManager
public ScriptEngineManager()
ScriptEngineManager(Thread.currentThread().getContextClassLoader())
.- See Also:
ScriptEngineManager
public ScriptEngineManager(ClassLoader loader)
ScriptEngineFactory
visible to the given ClassLoader
using the service provider mechanism.If loader is
null
, the script engine factories that are bundled with the platform are loaded. - Parameters:
-
loader
- ClassLoader used to discover script engine factories.
Method Details
setBindings
public void setBindings(Bindings bindings)
setBindings
stores the specified Bindings
in the globalScope
field. ScriptEngineManager sets this Bindings
as global bindings for ScriptEngine
objects created by it.- Parameters:
-
bindings
- The specifiedBindings
- Throws:
-
IllegalArgumentException
- if bindings is null.
getBindings
public Bindings getBindings()
getBindings
returns the value of the globalScope
field. ScriptEngineManager sets this Bindings
as global bindings for ScriptEngine
objects created by it.- Returns:
- The globalScope field.
put
public void put(String key, Object value)
- Parameters:
-
key
- Key to set -
value
- Value to set. - Throws:
-
NullPointerException
- if key is null. -
IllegalArgumentException
- if key is empty string.
get
public Object get(String key)
- Parameters:
-
key
- The key whose value is to be returned. - Returns:
- The value for the specified key.
getEngineByName
public ScriptEngine getEngineByName(String shortName)
ScriptEngine
for a given name. The algorithm first searches for a ScriptEngineFactory
that has been registered as a handler for the specified name using the registerEngineName
method. If one is not found, it searches the set of
ScriptEngineFactory
instances stored by the constructor for one with the specified name. If a ScriptEngineFactory
is found by either method, it is used to create instance of ScriptEngine
.- Parameters:
-
shortName
- The short name of theScriptEngine
implementation. returned by thegetNames
method of itsScriptEngineFactory
. - Returns:
- A
ScriptEngine
created by the factory located in the search. Returns null if no such factory was found. TheScriptEngineManager
sets its ownglobalScope
Bindings
as theGLOBAL_SCOPE
Bindings
of the newly createdScriptEngine
. - Throws:
-
NullPointerException
- if shortName is null.
getEngineByExtension
public ScriptEngine getEngineByExtension(String extension)
ScriptEngine
for a given extension. The algorithm used by getEngineByName
is used except that the search starts by looking for a ScriptEngineFactory
registered to handle the given extension using registerEngineExtension
.- Parameters:
-
extension
- The given extension - Returns:
- The engine to handle scripts with this extension. Returns
null
if not found. - Throws:
-
NullPointerException
- if extension is null.
getEngineByMimeType
public ScriptEngine getEngineByMimeType(String mimeType)
ScriptEngine
for a given mime type. The algorithm used by getEngineByName
is used except that the search starts by looking for a ScriptEngineFactory
registered to handle the given mime type using registerEngineMimeType
.- Parameters:
-
mimeType
- The given mime type - Returns:
- The engine to handle scripts with this mime type. Returns
null
if not found. - Throws:
-
NullPointerException
- if mimeType is null.
getEngineFactories
public List<ScriptEngineFactory> getEngineFactories()
ScriptEngineFactory
classes found by the discovery mechanism.- Returns:
- List of all discovered
ScriptEngineFactory
s.
registerEngineName
public void registerEngineName(String name, ScriptEngineFactory factory)
ScriptEngineFactory
to handle a language name. Overrides any such association found using the Discovery mechanism.- Parameters:
-
name
- The name to be associated with theScriptEngineFactory
. -
factory
- The class to associate with the given name. - Throws:
-
NullPointerException
- if any of the parameters is null.
registerEngineMimeType
public void registerEngineMimeType(String type, ScriptEngineFactory factory)
ScriptEngineFactory
to handle a mime type. Overrides any such association found using the Discovery mechanism.- Parameters:
-
type
- The mime type to be associated with theScriptEngineFactory
. -
factory
- The class to associate with the given mime type. - Throws:
-
NullPointerException
- if any of the parameters is null.
registerEngineExtension
public void registerEngineExtension(String extension, ScriptEngineFactory factory)
ScriptEngineFactory
to handle an extension. Overrides any such association found using the Discovery mechanism.- Parameters:
-
extension
- The extension type to be associated with theScriptEngineFactory
. -
factory
- The class to associate with the given extension. - Throws:
-
NullPointerException
- if any of the parameters is null.
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.scripting/javax/script/ScriptEngineManager.html