- java.lang.Object
-
- jdk.nashorn.api.scripting.NashornScriptEngineFactory
-
- All Implemented Interfaces:
ScriptEngineFactory
public final class NashornScriptEngineFactory extends Object implements ScriptEngineFactory
JSR-223 compliant script engine factory for Nashorn. The engine answers for:- names
"nashorn","Nashorn","js","JS","JavaScript","javascript","ECMAScript", and"ecmascript"; - MIME types
"application/javascript","application/ecmascript","text/javascript", and"text/ecmascript"; - as well as for the extension
"js".
getScriptEngine(String[])will have the passed arguments accessible as a global variable named"arguments".- Since:
- 1.8u40
-
-
Constructor Summary
Constructors Constructor Description NashornScriptEngineFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetEngineName()Returns the full name of theScriptEngine.StringgetEngineVersion()Returns the version of theScriptEngine.List<String>getExtensions()Returns an immutable list of filename extensions, which generally identify scripts written in the language supported by thisScriptEngine.StringgetLanguageName()Returns the name of the scripting language supported by thisScriptEngine.StringgetLanguageVersion()Returns the version of the scripting language supported by thisScriptEngine.StringgetMethodCallSyntax(String obj, String method, String... args)Returns a String which can be used to invoke a method of a Java object using the syntax of the supported scripting language.List<String>getMimeTypes()Returns an immutable list of mimetypes, associated with scripts that can be executed by the engine.List<String>getNames()Returns an immutable list of short names for theScriptEngine, which may be used to identify theScriptEngineby theScriptEngineManager.StringgetOutputStatement(String toDisplay)Returns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language.ObjectgetParameter(String key)Returns the value of an attribute whose meaning may be implementation-specific.StringgetProgram(String... statements)Returns a valid scripting language executable program with given statements.ScriptEnginegetScriptEngine()Returns an instance of theScriptEngineassociated with thisScriptEngineFactory.ScriptEnginegetScriptEngine(ClassLoader appLoader)Create a new Script engine initialized with the given class loader.ScriptEnginegetScriptEngine(String... args)Create a new Script engine initialized with the given arguments.ScriptEnginegetScriptEngine(String[] args, ClassLoader appLoader)Create a new Script engine initialized with the given arguments and the given class loader.ScriptEnginegetScriptEngine(String[] args, ClassLoader appLoader, ClassFilter classFilter)Create a new Script engine initialized with the given arguments, class loader and class filter.ScriptEnginegetScriptEngine(ClassFilter classFilter)Create a new Script engine initialized with the given class filter.
-
-
-
Method Detail
-
getEngineName
public String getEngineName()
Description copied from interface:ScriptEngineFactoryReturns the full name of theScriptEngine. For instance an implementation based on the Mozilla Rhino Javascript engine might return Rhino Mozilla Javascript Engine.- Specified by:
getEngineNamein interfaceScriptEngineFactory- Returns:
- The name of the engine implementation.
-
getEngineVersion
public String getEngineVersion()
Description copied from interface:ScriptEngineFactoryReturns the version of theScriptEngine.- Specified by:
getEngineVersionin interfaceScriptEngineFactory- Returns:
- The
ScriptEngineimplementation version.
-
getExtensions
public List<String> getExtensions()
Description copied from interface:ScriptEngineFactoryReturns an immutable list of filename extensions, which generally identify scripts written in the language supported by thisScriptEngine. The array is used by theScriptEngineManagerto implement itsgetEngineByExtensionmethod.- Specified by:
getExtensionsin interfaceScriptEngineFactory- Returns:
- The list of extensions.
-
getLanguageName
public String getLanguageName()
Description copied from interface:ScriptEngineFactoryReturns the name of the scripting language supported by thisScriptEngine.- Specified by:
getLanguageNamein interfaceScriptEngineFactory- Returns:
- The name of the supported language.
-
getLanguageVersion
public String getLanguageVersion()
Description copied from interface:ScriptEngineFactoryReturns the version of the scripting language supported by thisScriptEngine.- Specified by:
getLanguageVersionin interfaceScriptEngineFactory- Returns:
- The version of the supported language.
-
getMethodCallSyntax
public String getMethodCallSyntax(String obj, String method, String... args)
Description copied from interface:ScriptEngineFactoryReturns a String which can be used to invoke a method of a Java object using the syntax of the supported scripting language. For instance, an implementation for a Javascript engine might be;public String getMethodCallSyntax(String obj, String m, String... args) { String ret = obj; ret += "." + m + "("; for (int i = 0; i < args.length; i++) { ret += args[i]; if (i < args.length - 1) { ret += ","; } } ret += ")"; return ret; }- Specified by:
getMethodCallSyntaxin interfaceScriptEngineFactory- Parameters:
obj- The name representing the object whose method is to be invoked. The name is the one used to create bindings using theputmethod ofScriptEngine, theputmethod of anENGINE_SCOPEBindings,or thesetAttributemethod ofScriptContext. The identifier used in scripts may be a decorated form of the specified one.method- The name of the method to invoke.args- names of the arguments in the method call.- Returns:
- The String used to invoke the method in the syntax of the scripting language.
-
getMimeTypes
public List<String> getMimeTypes()
Description copied from interface:ScriptEngineFactoryReturns an immutable list of mimetypes, associated with scripts that can be executed by the engine. The list is used by theScriptEngineManagerclass to implement itsgetEngineByMimetypemethod.- Specified by:
getMimeTypesin interfaceScriptEngineFactory- Returns:
- The list of mime types.
-
getNames
public List<String> getNames()
Description copied from interface:ScriptEngineFactoryReturns an immutable list of short names for theScriptEngine, which may be used to identify theScriptEngineby theScriptEngineManager. For instance, an implementation based on the Mozilla Rhino Javascript engine might return list containing {"javascript", "rhino"}.- Specified by:
getNamesin interfaceScriptEngineFactory- Returns:
- an immutable list of short names
-
getOutputStatement
public String getOutputStatement(String toDisplay)
Description copied from interface:ScriptEngineFactoryReturns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language. For instance, the implementation for a Perl engine might be;public String getOutputStatement(String toDisplay) { return "print(" + toDisplay + ")"; }- Specified by:
getOutputStatementin interfaceScriptEngineFactory- Parameters:
toDisplay- The String to be displayed by the returned statement.- Returns:
- The string used to display the String in the syntax of the scripting language.
-
getParameter
public Object getParameter(String key)
Description copied from interface:ScriptEngineFactoryReturns the value of an attribute whose meaning may be implementation-specific. Keys for which the value is defined in all implementations are:- ScriptEngine.ENGINE
- ScriptEngine.ENGINE_VERSION
- ScriptEngine.LANGUAGE
- ScriptEngine.LANGUAGE_VERSION
- ScriptEngine.NAME
The values for these keys are the Strings returned by
getEngineName,getEngineVersion,getLanguageName,getLanguageVersionfor the first four keys respectively. For NAME, one of the Strings returned bygetNamesis returned.
A reserved key,THREADING, whose value describes the behavior of the engine with respect to concurrent execution of scripts and maintenance of state is also defined. These values for theTHREADINGkey are:
null- The engine implementation is not thread safe, and cannot be used to execute scripts concurrently on multiple threads."MULTITHREADED"- The engine implementation is internally thread-safe and scripts may execute concurrently although effects of script execution on one thread may be visible to scripts on other threads."THREAD-ISOLATED"- The implementation satisfies the requirements of "MULTITHREADED", and also, the engine maintains independent values for symbols in scripts executing on different threads."STATELESS"- The implementation satisfies the requirements of"THREAD-ISOLATED". In addition, script executions do not alter the mappings in theBindingswhich is the engine scope of theScriptEngine. In particular, the keys in theBindingsand their associated values are the same before and after the execution of the script.
Implementations may define implementation-specific keys.- Specified by:
getParameterin interfaceScriptEngineFactory- Parameters:
key- The name of the parameter- Returns:
- The value for the given parameter. Returns
nullif no value is assigned to the key.
-
getProgram
public String getProgram(String... statements)
Description copied from interface:ScriptEngineFactoryReturns a valid scripting language executable program with given statements. For instance an implementation for a PHP engine might be:public String getProgram(String... statements) { String retval = "<?\n"; int len = statements.length; for (int i = 0; i < len; i++) { retval += statements[i] + ";\n"; } return retval += "?>"; }- Specified by:
getProgramin interfaceScriptEngineFactory- Parameters:
statements- The statements to be executed. May be return values of calls to thegetMethodCallSyntaxandgetOutputStatementmethods.- Returns:
- The Program
-
getScriptEngine
public ScriptEngine getScriptEngine()
Description copied from interface:ScriptEngineFactoryReturns an instance of theScriptEngineassociated with thisScriptEngineFactory. A new ScriptEngine is generally returned, but implementations may pool, share or reuse engines.- Specified by:
getScriptEnginein interfaceScriptEngineFactory- Returns:
- A new
ScriptEngineinstance.
-
getScriptEngine
public ScriptEngine getScriptEngine(ClassLoader appLoader)
Create a new Script engine initialized with the given class loader.- Parameters:
appLoader- class loader to be used as script "app" class loader.- Returns:
- newly created script engine.
- Throws:
SecurityException- if the security manager'scheckPermissiondeniesRuntimePermission("nashorn.setConfig")
-
getScriptEngine
public ScriptEngine getScriptEngine(ClassFilter classFilter)
Create a new Script engine initialized with the given class filter.- Parameters:
classFilter- class filter to use.- Returns:
- newly created script engine.
- Throws:
NullPointerException- ifclassFilterisnullSecurityException- if the security manager'scheckPermissiondeniesRuntimePermission("nashorn.setConfig")
-
getScriptEngine
public ScriptEngine getScriptEngine(String... args)
Create a new Script engine initialized with the given arguments.- Parameters:
args- arguments array passed to script engine.- Returns:
- newly created script engine.
- Throws:
NullPointerException- ifargsisnullSecurityException- if the security manager'scheckPermissiondeniesRuntimePermission("nashorn.setConfig")
-
getScriptEngine
public ScriptEngine getScriptEngine(String[] args, ClassLoader appLoader)
Create a new Script engine initialized with the given arguments and the given class loader.- Parameters:
args- arguments array passed to script engine.appLoader- class loader to be used as script "app" class loader.- Returns:
- newly created script engine.
- Throws:
NullPointerException- ifargsisnullSecurityException- if the security manager'scheckPermissiondeniesRuntimePermission("nashorn.setConfig")
-
getScriptEngine
public ScriptEngine getScriptEngine(String[] args, ClassLoader appLoader, ClassFilter classFilter)
Create a new Script engine initialized with the given arguments, class loader and class filter.- Parameters:
args- arguments array passed to script engine.appLoader- class loader to be used as script "app" class loader.classFilter- class filter to use.- Returns:
- newly created script engine.
- Throws:
NullPointerException- ifargsorclassFilterisnullSecurityException- if the security manager'scheckPermissiondeniesRuntimePermission("nashorn.setConfig")
-
-