- java.lang.Object
-
- javax.script.ScriptEngineManager
-
public class ScriptEngineManager extends Object
TheScriptEngineManager
implements a discovery and instantiation mechanism forScriptEngine
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 to enumerate all the implementations ofScriptEngineFactory
.
TheScriptEngineManager
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 ofScriptEngine
created by theScriptEngineManager
. The values in theBindings
are generally exposed in all scripts.- Since:
- 1.6
-
-
Constructor Summary
Constructors Constructor and Description ScriptEngineManager()
If the thread context ClassLoader can be accessed by the caller, then the effect of calling this constructor is the same as callingScriptEngineManager(Thread.currentThread().getContextClassLoader())
.ScriptEngineManager(ClassLoader loader)
This constructor loads the implementations ofScriptEngineFactory
visible to the givenClassLoader
using the service provider mechanism.
If loader isnull
, the script engine factories that are bundled with the platform and that are in the usual extension directories (installed extensions) are loaded.
-
Method Summary
Methods Modifier and Type Method and Description Object
get(String key)
Gets the value for the specified key in the Global ScopeBindings
getBindings()
getBindings
returns the value of theglobalScope
field.ScriptEngine
getEngineByExtension(String extension)
Look up and create aScriptEngine
for a given extension.ScriptEngine
getEngineByMimeType(String mimeType)
Look up and create aScriptEngine
for a given mime type.ScriptEngine
getEngineByName(String shortName)
Looks up and creates aScriptEngine
for a given name.List<ScriptEngineFactory>
getEngineFactories()
Returns a list whose elements are instances of all theScriptEngineFactory
classes found by the discovery mechanism.void
put(String key, Object value)
Sets the specified key/value pair in the Global Scope.void
registerEngineExtension(String extension, ScriptEngineFactory factory)
Registers aScriptEngineFactory
to handle an extension.void
registerEngineMimeType(String type, ScriptEngineFactory factory)
Registers aScriptEngineFactory
to handle a mime type.void
registerEngineName(String name, ScriptEngineFactory factory)
Registers aScriptEngineFactory
to handle a language name.void
setBindings(Bindings bindings)
setBindings
stores the specifiedBindings
in theglobalScope
field.
-
-
-
Constructor Detail
-
ScriptEngineManager
public ScriptEngineManager()
If the thread context ClassLoader can be accessed by the caller, then the effect of calling this constructor is the same as callingScriptEngineManager(Thread.currentThread().getContextClassLoader())
. Otherwise, the effect is the same as callingScriptEngineManager(null)
.- See Also:
Thread.getContextClassLoader()
-
ScriptEngineManager
public ScriptEngineManager(ClassLoader loader)
This constructor loads the implementations ofScriptEngineFactory
visible to the givenClassLoader
using the service provider mechanism.
If loader isnull
, the script engine factories that are bundled with the platform and that are in the usual extension directories (installed extensions) are loaded.- Parameters:
loader
- ClassLoader used to discover script engine factories.
-
-
Method Detail
-
setBindings
public void setBindings(Bindings bindings)
setBindings
stores the specifiedBindings
in theglobalScope
field. ScriptEngineManager sets thisBindings
as global bindings forScriptEngine
objects created by it.- Parameters:
bindings
- The specifiedBindings
- Throws:
IllegalArgumentException
- if bindings is null.
-
getBindings
public Bindings getBindings()
getBindings
returns the value of theglobalScope
field. ScriptEngineManager sets thisBindings
as global bindings forScriptEngine
objects created by it.- Returns:
- The globalScope field.
-
put
public void put(String key, Object value)
Sets the specified key/value pair in the Global Scope.- Parameters:
key
- Key to setvalue
- Value to set.- Throws:
NullPointerException
- if key is null.IllegalArgumentException
- if key is empty string.
-
get
public Object get(String key)
Gets the value for the specified key in the Global Scope- Parameters:
key
- The key whose value is to be returned.- Returns:
- The value for the specified key.
-
getEngineByName
public ScriptEngine getEngineByName(String shortName)
Looks up and creates aScriptEngine
for a given name. The algorithm first searches for aScriptEngineFactory
that has been registered as a handler for the specified name using theregisterEngineName
method.
If one is not found, it searches the set ofScriptEngineFactory
instances stored by the constructor for one with the specified name. If aScriptEngineFactory
is found by either method, it is used to create instance ofScriptEngine
.- 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)
Look up and create aScriptEngine
for a given extension. The algorithm used bygetEngineByName
is used except that the search starts by looking for aScriptEngineFactory
registered to handle the given extension usingregisterEngineExtension
.- 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)
Look up and create aScriptEngine
for a given mime type. The algorithm used bygetEngineByName
is used except that the search starts by looking for aScriptEngineFactory
registered to handle the given mime type usingregisterEngineMimeType
.- 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()
Returns a list whose elements are instances of all theScriptEngineFactory
classes found by the discovery mechanism.- Returns:
- List of all discovered
ScriptEngineFactory
s.
-
registerEngineName
public void registerEngineName(String name, ScriptEngineFactory factory)
Registers aScriptEngineFactory
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)
Registers aScriptEngineFactory
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)
Registers aScriptEngineFactory
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.
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/script/ScriptEngineManager.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.