Class DefaultGremlinScriptEngineManager
- java.lang.Object
-
- org.apache.tinkerpop.gremlin.jsr223.DefaultGremlinScriptEngineManager
-
- All Implemented Interfaces:
GremlinScriptEngineManager
- Direct Known Subclasses:
CachedGremlinScriptEngineManager
public class DefaultGremlinScriptEngineManager extends Object implements GremlinScriptEngineManager
TheScriptEngineManager
implements a discovery, instantiation and configuration mechanism forGremlinScriptEngine
classes and also maintains a collection of key/value pairs storing state shared by all engines created by it. This class uses theServiceProvider
mechanism to enumerate all the implementations ofGremlinScriptEngineFactory
. 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. TheBindings
of key/value pairs, referred to as the "Global Scope" maintained by the manager is available to all instances of @code ScriptEngine} created by theGremlinScriptEngineManager
. The values in theBindings
are generally exposed in all scripts. This class is based quite heavily on the workings of theScriptEngineManager
supplied in thejavax.script
packages, but adds some additional features that are specific to Gremlin and TinkerPop. Unfortunately, it's not easily possible to extendScriptEngineManager
directly as there certain behaviors don't appear to be be straightforward to implement and member variables are all private. It is important to note that this class is designed to provide support for "Gremlin-enabled"ScriptEngine
instances (i.e. those that extend fromGremlinScriptEngine
) and is not meant to manage just anyScriptEngine
instance that may be on the path. As this is a "Gremlin"ScriptEngine
, certain common imports are automatically applied when aGremlinScriptEngine
is instantiated via theGremlinScriptEngineFactory
.. Initial imports from gremlin-core come from theCoreImports
.- Author:
- Stephen Mallette (http://stephen.genoprime.com)
-
-
Constructor Summary
Constructors Constructor Description DefaultGremlinScriptEngineManager()
The effect of calling this constructor is the same as callingDefaultGremlinScriptEngineManager(Thread.currentThread().getContextClassLoader())
.DefaultGremlinScriptEngineManager(ClassLoader loader)
This constructor loads the implementations ofGremlinScriptEngineFactory
visible to the givenClassLoader
using theServiceLoader
mechanism.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addPlugin(GremlinPlugin plugin)
AddGremlinPlugin
instances to customize newly createdGremlinScriptEngine
instances.Object
get(String key)
Gets the value for the specified key in the global scope.Bindings
getBindings()
Gets the bindings of theBindings
in global scope.List<Customizer>
getCustomizers(String scriptEngineName)
Get the list ofCustomizer
instances filtered by thescriptEngineName
.GremlinScriptEngine
getEngineByExtension(String extension)
Look up and create aGremlinScriptEngine
for a given extension.GremlinScriptEngine
getEngineByMimeType(String mimeType)
Look up and create aGremlinScriptEngine
for a given mime type.GremlinScriptEngine
getEngineByName(String shortName)
Looks up and creates aGremlinScriptEngine
for a given name.List<GremlinScriptEngineFactory>
getEngineFactories()
Returns a list whose elements are instances of all theGremlinScriptEngineFactory
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, GremlinScriptEngineFactory factory)
Registers aGremlinScriptEngineFactory
to handle an extension.void
registerEngineMimeType(String type, GremlinScriptEngineFactory factory)
Registers aGremlinScriptEngineFactory
to handle a mime type.void
registerEngineName(String name, GremlinScriptEngineFactory factory)
Registers aGremlinScriptEngineFactory
to handle a language name.void
setBindings(Bindings bindings)
Stores the specifiedBindings
as a global for allGremlinScriptEngine
objects created by it.
-
-
-
Constructor Detail
-
DefaultGremlinScriptEngineManager
public DefaultGremlinScriptEngineManager()
The effect of calling this constructor is the same as callingDefaultGremlinScriptEngineManager(Thread.currentThread().getContextClassLoader())
.
-
DefaultGremlinScriptEngineManager
public DefaultGremlinScriptEngineManager(ClassLoader loader)
This constructor loads the implementations ofGremlinScriptEngineFactory
visible to the givenClassLoader
using theServiceLoader
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 Detail
-
getCustomizers
public List<Customizer> getCustomizers(String scriptEngineName)
Description copied from interface:GremlinScriptEngineManager
Get the list ofCustomizer
instances filtered by thescriptEngineName
.- Specified by:
getCustomizers
in interfaceGremlinScriptEngineManager
-
addPlugin
public void addPlugin(GremlinPlugin plugin)
Description copied from interface:GremlinScriptEngineManager
AddGremlinPlugin
instances to customize newly createdGremlinScriptEngine
instances.- Specified by:
addPlugin
in interfaceGremlinScriptEngineManager
-
setBindings
public void setBindings(Bindings bindings)
Stores the specifiedBindings
as a global for allGremlinScriptEngine
objects created by it. If the bindings are to be updated by multiple threads it is recommended that aConcurrentBindings
instance is supplied.- Specified by:
setBindings
in interfaceGremlinScriptEngineManager
- Throws:
IllegalArgumentException
- if bindings is null.
-
getBindings
public Bindings getBindings()
Gets the bindings of theBindings
in global scope.- Specified by:
getBindings
in interfaceGremlinScriptEngineManager
-
put
public void put(String key, Object value)
Sets the specified key/value pair in the global scope. The key may not be null or empty.- Specified by:
put
in interfaceGremlinScriptEngineManager
- Throws:
IllegalArgumentException
- if key is null or empty.
-
get
public Object get(String key)
Gets the value for the specified key in the global scope.- Specified by:
get
in interfaceGremlinScriptEngineManager
-
getEngineByName
public GremlinScriptEngine getEngineByName(String shortName)
Looks up and creates aGremlinScriptEngine
for a given name. The algorithm first searches for aGremlinScriptEngineFactory
that has been registered as a handler for the specified name using theregisterEngineExtension(String, GremlinScriptEngineFactory)
method. If one is not found, it searches the set ofGremlinScriptEngineFactory
instances stored by the constructor for one with the specified name. If aScriptEngineFactory
is found by either method, it is used to create instance ofGremlinScriptEngine
.- Specified by:
getEngineByName
in interfaceGremlinScriptEngineManager
- Parameters:
shortName
- The short name of theGremlinScriptEngine
implementation returned by theScriptEngineFactory.getNames()
method.- Returns:
- A
GremlinScriptEngine
created by the factory located in the search. Returnsnull
if no such factory was found. The global scope of this manager is applied to the newly createdGremlinScriptEngine
- Throws:
NullPointerException
- if shortName isnull
.
-
getEngineByExtension
public GremlinScriptEngine getEngineByExtension(String extension)
Look up and create aGremlinScriptEngine
for a given extension. The algorithm used bygetEngineByName(String)
is used except that the search starts by looking for aGremlinScriptEngineFactory
registered to handle the given extension usingregisterEngineExtension(String, GremlinScriptEngineFactory)
.- Specified by:
getEngineByExtension
in interfaceGremlinScriptEngineManager
- Returns:
- The engine to handle scripts with this extension. Returns
null
if not found. - Throws:
NullPointerException
- if extension isnull
.
-
getEngineByMimeType
public GremlinScriptEngine getEngineByMimeType(String mimeType)
Look up and create aGremlinScriptEngine
for a given mime type. The algorithm used bygetEngineByName(String)
is used except that the search starts by looking for aGremlinScriptEngineFactory
registered to handle the given mime type usingregisterEngineMimeType(String, GremlinScriptEngineFactory)
.- Specified by:
getEngineByMimeType
in interfaceGremlinScriptEngineManager
- Parameters:
mimeType
- The given mime type- Returns:
- The engine to handle scripts with this mime type. Returns
null
if not found. - Throws:
NullPointerException
- if mime-type isnull
.
-
getEngineFactories
public List<GremlinScriptEngineFactory> getEngineFactories()
Returns a list whose elements are instances of all theGremlinScriptEngineFactory
classes found by the discovery mechanism.- Specified by:
getEngineFactories
in interfaceGremlinScriptEngineManager
- Returns:
- List of all discovered
GremlinScriptEngineFactory
objects.
-
registerEngineName
public void registerEngineName(String name, GremlinScriptEngineFactory factory)
Registers aGremlinScriptEngineFactory
to handle a language name. Overrides any such association found using the discovery mechanism.- Specified by:
registerEngineName
in interfaceGremlinScriptEngineManager
- Parameters:
name
- The name to be associated with theGremlinScriptEngineFactory
factory
- The class to associate with the given name.- Throws:
NullPointerException
- if any of the parameters is null.
-
registerEngineMimeType
public void registerEngineMimeType(String type, GremlinScriptEngineFactory factory)
Registers aGremlinScriptEngineFactory
to handle a mime type. Overrides any such association found using the discovery mechanism.- Specified by:
registerEngineMimeType
in interfaceGremlinScriptEngineManager
- Parameters:
type
- The mime type to be associated with theGremlinScriptEngineFactory
.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, GremlinScriptEngineFactory factory)
Registers aGremlinScriptEngineFactory
to handle an extension. Overrides any such association found using the discovery mechanism.- Specified by:
registerEngineExtension
in interfaceGremlinScriptEngineManager
- Parameters:
extension
- The extension type to be associated with theGremlinScriptEngineFactory
factory
- The class to associate with the given extension.- Throws:
NullPointerException
- if any of the parameters is null.
-
-