Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/script/ScriptEngineFactory.java
38829 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;2627import java.util.List;2829/**30* <code>ScriptEngineFactory</code> is used to describe and instantiate31* <code>ScriptEngines</code>.32* <br><br>33* Each class implementing <code>ScriptEngine</code> has a corresponding factory34* that exposes metadata describing the engine class.35* <br><br>The <code>ScriptEngineManager</code>36* uses the service provider mechanism described in the <i>Jar File Specification</i> to obtain37* instances of all <code>ScriptEngineFactories</code> available in38* the current ClassLoader.39*40* @since 1.641*/42public interface ScriptEngineFactory {43/**44* Returns the full name of the <code>ScriptEngine</code>. For45* instance an implementation based on the Mozilla Rhino Javascript engine46* might return <i>Rhino Mozilla Javascript Engine</i>.47* @return The name of the engine implementation.48*/49public String getEngineName();5051/**52* Returns the version of the <code>ScriptEngine</code>.53* @return The <code>ScriptEngine</code> implementation version.54*/55public String getEngineVersion();565758/**59* Returns an immutable list of filename extensions, which generally identify scripts60* written in the language supported by this <code>ScriptEngine</code>.61* The array is used by the <code>ScriptEngineManager</code> to implement its62* <code>getEngineByExtension</code> method.63* @return The list of extensions.64*/65public List<String> getExtensions();666768/**69* Returns an immutable list of mimetypes, associated with scripts that70* can be executed by the engine. The list is used by the71* <code>ScriptEngineManager</code> class to implement its72* <code>getEngineByMimetype</code> method.73* @return The list of mime types.74*/75public List<String> getMimeTypes();7677/**78* Returns an immutable list of short names for the <code>ScriptEngine</code>, which may be used to79* identify the <code>ScriptEngine</code> by the <code>ScriptEngineManager</code>.80* For instance, an implementation based on the Mozilla Rhino Javascript engine might81* return list containing {"javascript", "rhino"}.82* @return an immutable list of short names83*/84public List<String> getNames();8586/**87* Returns the name of the scripting language supported by this88* <code>ScriptEngine</code>.89* @return The name of the supported language.90*/91public String getLanguageName();9293/**94* Returns the version of the scripting language supported by this95* <code>ScriptEngine</code>.96* @return The version of the supported language.97*/98public String getLanguageVersion();99100/**101* Returns the value of an attribute whose meaning may be implementation-specific.102* Keys for which the value is defined in all implementations are:103* <ul>104* <li>ScriptEngine.ENGINE</li>105* <li>ScriptEngine.ENGINE_VERSION</li>106* <li>ScriptEngine.LANGUAGE</li>107* <li>ScriptEngine.LANGUAGE_VERSION</li>108* <li>ScriptEngine.NAME</li>109* </ul>110* <p>111* The values for these keys are the Strings returned by <code>getEngineName</code>,112* <code>getEngineVersion</code>, <code>getLanguageName</code>,113* <code>getLanguageVersion</code> for the first four keys respectively. For NAME, one of the Strings114* returned by <code>getNames</code> is returned.<br><br>115* A reserved key, <code><b>THREADING</b></code>, whose value describes the behavior of the engine116* with respect to concurrent execution of scripts and maintenance of state is also defined.117* These values for the <code><b>THREADING</b></code> key are:<br><br>118* <ul>119* <li><code>null</code> - The engine implementation is not thread safe, and cannot120* be used to execute scripts concurrently on multiple threads.121* <li><code>"MULTITHREADED"</code> - The engine implementation is internally122* thread-safe and scripts may execute concurrently although effects of script execution123* on one thread may be visible to scripts on other threads.124* <li><code>"THREAD-ISOLATED"</code> - The implementation satisfies the requirements125* of "MULTITHREADED", and also, the engine maintains independent values126* for symbols in scripts executing on different threads.127* <li><code>"STATELESS"</code> - The implementation satisfies the requirements of128* <li><code>"THREAD-ISOLATED"</code>. In addition, script executions do not alter the129* mappings in the <code>Bindings</code> which is the engine scope of the130* <code>ScriptEngine</code>. In particular, the keys in the <code>Bindings</code>131* and their associated values are the same before and after the execution of the script.132* </ul>133* <br><br>134* Implementations may define implementation-specific keys.135*136* @param key The name of the parameter137* @return The value for the given parameter. Returns <code>null</code> if no138* value is assigned to the key.139*140*/141public Object getParameter(String key);142143/**144* Returns a String which can be used to invoke a method of a Java object using the syntax145* of the supported scripting language. For instance, an implementation for a Javascript146* engine might be;147*148* <pre>{@code149* public String getMethodCallSyntax(String obj,150* String m, String... args) {151* String ret = obj;152* ret += "." + m + "(";153* for (int i = 0; i < args.length; i++) {154* ret += args[i];155* if (i < args.length - 1) {156* ret += ",";157* }158* }159* ret += ")";160* return ret;161* }162* } </pre>163* <p>164*165* @param obj The name representing the object whose method is to be invoked. The166* name is the one used to create bindings using the <code>put</code> method of167* <code>ScriptEngine</code>, the <code>put</code> method of an <code>ENGINE_SCOPE</code>168* <code>Bindings</code>,or the <code>setAttribute</code> method169* of <code>ScriptContext</code>. The identifier used in scripts may be a decorated form of the170* specified one.171*172* @param m The name of the method to invoke.173* @param args names of the arguments in the method call.174*175* @return The String used to invoke the method in the syntax of the scripting language.176*/177public String getMethodCallSyntax(String obj, String m, String... args);178179/**180* Returns a String that can be used as a statement to display the specified String using181* the syntax of the supported scripting language. For instance, the implementation for a Perl182* engine might be;183*184* <pre><code>185* public String getOutputStatement(String toDisplay) {186* return "print(" + toDisplay + ")";187* }188* </code></pre>189*190* @param toDisplay The String to be displayed by the returned statement.191* @return The string used to display the String in the syntax of the scripting language.192*193*194*/195public String getOutputStatement(String toDisplay);196197198/**199* Returns a valid scripting language executable program with given statements.200* For instance an implementation for a PHP engine might be:201*202* <pre>{@code203* public String getProgram(String... statements) {204* String retval = "<?\n";205* int len = statements.length;206* for (int i = 0; i < len; i++) {207* retval += statements[i] + ";\n";208* }209* return retval += "?>";210* }211* }</pre>212*213* @param statements The statements to be executed. May be return values of214* calls to the <code>getMethodCallSyntax</code> and <code>getOutputStatement</code> methods.215* @return The Program216*/217218public String getProgram(String... statements);219220/**221* Returns an instance of the <code>ScriptEngine</code> associated with this222* <code>ScriptEngineFactory</code>. A new ScriptEngine is generally223* returned, but implementations may pool, share or reuse engines.224*225* @return A new <code>ScriptEngine</code> instance.226*/227public ScriptEngine getScriptEngine();228}229230231