APIdia Launch Partner
Top Description Fields Constructors Methods
javax.script

public Class ScriptEngineManager

extends Object
Class Inheritance
Imports
java.util.*, .ServiceLoader, .ServiceConfigurationError, java.util.function.Function, java.util.stream.Stream

The 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 java.util.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.

Authors
Mike Grogan, A. Sundararajan
Since
1.6

Field Summary

Modifier and TypeField and Description
private static final Comparator<ScriptEngineFactory>
private static final boolean
private final TreeSet<ScriptEngineFactory>
engineSpis

Set of script engine factories discovered.

private final HashMap<String, ScriptEngineFactory>
extensionAssociations

Map of script file extension to script engine factory.

private Bindings
globalScope

Global bindings associated with script engines created by this manager.

private final HashMap<String, ScriptEngineFactory>
mimeTypeAssociations

Map of script MIME type to script engine factory.

private final HashMap<String, ScriptEngineFactory>
nameAssociations

Map of engine name to script engine factory.

Constructor Summary

AccessConstructor and Description
public
ScriptEngineManager()

The effect of calling this constructor is the same as calling ScriptEngineManager(Thread.currentThread().getContextClassLoader()).

public
ScriptEngineManager(ClassLoader
ClassLoader used to discover script engine factories.
loader
)

This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism.

Method Summary

Modifier and TypeMethod and Description
private static void
private static void
public Object

Returns:

The value for the specified key.
get
(String
The key whose value is to be returned.
key
)

Gets the value for the specified key in the Global Scope

public Bindings

Returns:

The globalScope field.
getBindings
()

getBindings returns the value of the globalScope field.

private ScriptEngine
public ScriptEngine

Returns:

The engine to handle scripts with this extension. Returns null if not found.
getEngineByExtension
(String
The given extension
extension
)

Look up and create a ScriptEngine for a given extension.

public ScriptEngine

Returns:

The engine to handle scripts with this mime type. Returns null if not found.
getEngineByMimeType
(String
The given mime type
mimeType
)

Look up and create a ScriptEngine for a given mime type.

public ScriptEngine

Returns:

A ScriptEngine created by the factory located in the search. Returns null if no such factory was found. The ScriptEngineManager sets its own globalScope Bindings as the GLOBAL_SCOPE Bindings of the newly created ScriptEngine.
getEngineByName
(String
The short name of the ScriptEngine implementation. returned by the getNames method of its ScriptEngineFactory.
shortName
)

Looks up and creates a ScriptEngine for a given name.

public List<ScriptEngineFactory>

Returns:

List of all discovered ScriptEngineFactorys.
getEngineFactories
()

Returns a list whose elements are instances of all the ScriptEngineFactory classes found by the discovery mechanism.

private ServiceLoader<ScriptEngineFactory>
private void
initEngines(final ClassLoader loader)

public void
put(String
Key to set
key
,
Object
Value to set.
value
)

Sets the specified key/value pair in the Global Scope.

public void
registerEngineExtension(String
The extension type to be associated with the ScriptEngineFactory.
extension
,
ScriptEngineFactory
The class to associate with the given extension.
factory
)

Registers a ScriptEngineFactory to handle an extension.

public void
registerEngineMimeType(String
The mime type to be associated with the ScriptEngineFactory.
type
,
ScriptEngineFactory
The class to associate with the given mime type.
factory
)

Registers a ScriptEngineFactory to handle a mime type.

public void
registerEngineName(String
The name to be associated with the ScriptEngineFactory.
name
,
ScriptEngineFactory
The class to associate with the given name.
factory
)

Registers a ScriptEngineFactory to handle a language name.

private static void
public void
setBindings(Bindings
The specified Bindings
bindings
)

setBindings stores the specified Bindings in the globalScope field.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COMPARATORback to summary
private static final Comparator<ScriptEngineFactory> COMPARATOR
DEBUGback to summary
private static final boolean DEBUG
engineSpisback to summary
private final TreeSet<ScriptEngineFactory> engineSpis

Set of script engine factories discovered.

extensionAssociationsback to summary
private final HashMap<String, ScriptEngineFactory> extensionAssociations

Map of script file extension to script engine factory.

globalScopeback to summary
private Bindings globalScope

Global bindings associated with script engines created by this manager.

mimeTypeAssociationsback to summary
private final HashMap<String, ScriptEngineFactory> mimeTypeAssociations

Map of script MIME type to script engine factory.

nameAssociationsback to summary
private final HashMap<String, ScriptEngineFactory> nameAssociations

Map of engine name to script engine factory.

Constructor Detail

ScriptEngineManagerback to summary
public ScriptEngineManager()

The effect of calling this constructor is the same as calling ScriptEngineManager(Thread.currentThread().getContextClassLoader()).

See Also
java.lang.Thread#getContextClassLoader
ScriptEngineManagerback to summary
public ScriptEngineManager(ClassLoader loader)

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.

Parameters
loader:ClassLoader

ClassLoader used to discover script engine factories.

Method Detail

associateFactoryback to summary
private static void associateFactory(Map<String, ScriptEngineFactory> associations, String association, ScriptEngineFactory factory)
debugPrintback to summary
private static void debugPrint(Throwable exp)
getback to summary
public Object get(String key)

Gets the value for the specified key in the Global Scope

Parameters
key:String

The key whose value is to be returned.

Returns:Object

The value for the specified key.

getBindingsback to summary
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:Bindings

The globalScope field.

getEngineByback to summary
private ScriptEngine getEngineBy(String selector, Map<String, ScriptEngineFactory> associations, Function<ScriptEngineFactory, List<String>> valuesFn)
getEngineByExtensionback to summary
public ScriptEngine getEngineByExtension(String extension)

Look up and create a 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:String

The given extension

Returns:ScriptEngine

The engine to handle scripts with this extension. Returns null if not found.

Exceptions
NullPointerException:
if extension is null.
getEngineByMimeTypeback to summary
public ScriptEngine getEngineByMimeType(String mimeType)

Look up and create a 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:String

The given mime type

Returns:ScriptEngine

The engine to handle scripts with this mime type. Returns null if not found.

Exceptions
NullPointerException:
if mimeType is null.
getEngineByNameback to summary
public ScriptEngine getEngineByName(String shortName)

Looks up and creates a 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:String

The short name of the ScriptEngine implementation. returned by the getNames method of its ScriptEngineFactory.

Returns:ScriptEngine

A ScriptEngine created by the factory located in the search. Returns null if no such factory was found. The ScriptEngineManager sets its own globalScope Bindings as the GLOBAL_SCOPE Bindings of the newly created ScriptEngine.

Exceptions
NullPointerException:
if shortName is null.
getEngineFactoriesback to summary
public List<ScriptEngineFactory> getEngineFactories()

Returns a list whose elements are instances of all the ScriptEngineFactory classes found by the discovery mechanism.

Returns:List<ScriptEngineFactory>

List of all discovered ScriptEngineFactorys.

getServiceLoaderback to summary
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader)
initEnginesback to summary
private void initEngines(final ClassLoader loader)
putback to summary
public void put(String key, Object value)

Sets the specified key/value pair in the Global Scope.

Parameters
key:String

Key to set

value:Object

Value to set.

Exceptions
NullPointerException:
if key is null.
IllegalArgumentException:
if key is empty string.
registerEngineExtensionback to summary
public void registerEngineExtension(String extension, ScriptEngineFactory factory)

Registers a ScriptEngineFactory to handle an extension. Overrides any such association found using the Discovery mechanism.

Parameters
extension:String

The extension type to be associated with the ScriptEngineFactory.

factory:ScriptEngineFactory

The class to associate with the given extension.

Exceptions
NullPointerException:
if any of the parameters is null.
registerEngineMimeTypeback to summary
public void registerEngineMimeType(String type, ScriptEngineFactory factory)

Registers a ScriptEngineFactory to handle a mime type. Overrides any such association found using the Discovery mechanism.

Parameters
type:String

The mime type to be associated with the ScriptEngineFactory.

factory:ScriptEngineFactory

The class to associate with the given mime type.

Exceptions
NullPointerException:
if any of the parameters is null.
registerEngineNameback to summary
public void registerEngineName(String name, ScriptEngineFactory factory)

Registers a ScriptEngineFactory to handle a language name. Overrides any such association found using the Discovery mechanism.

Parameters
name:String

The name to be associated with the ScriptEngineFactory.

factory:ScriptEngineFactory

The class to associate with the given name.

Exceptions
NullPointerException:
if any of the parameters is null.
reportExceptionback to summary
private static void reportException(String msg, Throwable exp)
setBindingsback to summary
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:Bindings

The specified Bindings

Exceptions
IllegalArgumentException:
if bindings is null.