Path: blob/master/src/java.scripting/share/classes/javax/script/ScriptEngineFactory.java
41149 views
/*1* Copyright (c) 2005, 2016, 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 corresponding34* factory that exposes metadata describing the engine class.35* <br><br>The <code>ScriptEngineManager</code>36* uses the service-provider loader mechanism described in the37* {@link java.util.ServiceLoader} class to obtain38* instances of {@code ScriptEngineFactory} instances.39* See {@link ScriptEngineManager#ScriptEngineManager()} and40* {@link ScriptEngineManager#ScriptEngineManager(java.lang.ClassLoader)}.41*42* @since 1.643*/44public interface ScriptEngineFactory {45/**46* Returns the full name of the <code>ScriptEngine</code>. For47* instance an implementation based on the Mozilla Rhino Javascript engine48* might return <i>Rhino Mozilla Javascript Engine</i>.49* @return The name of the engine implementation.50*/51public String getEngineName();5253/**54* Returns the version of the <code>ScriptEngine</code>.55* @return The <code>ScriptEngine</code> implementation version.56*/57public String getEngineVersion();585960/**61* Returns an immutable list of filename extensions, which generally identify scripts62* written in the language supported by this <code>ScriptEngine</code>.63* The array is used by the <code>ScriptEngineManager</code> to implement its64* <code>getEngineByExtension</code> method.65* @return The list of extensions.66*/67public List<String> getExtensions();686970/**71* Returns an immutable list of mimetypes, associated with scripts that72* can be executed by the engine. The list is used by the73* <code>ScriptEngineManager</code> class to implement its74* <code>getEngineByMimetype</code> method.75* @return The list of mime types.76*/77public List<String> getMimeTypes();7879/**80* Returns an immutable list of short names for the <code>ScriptEngine</code>, which may be used to81* identify the <code>ScriptEngine</code> by the <code>ScriptEngineManager</code>.82* For instance, an implementation based on the Mozilla Rhino Javascript engine might83* return list containing {"javascript", "rhino"}.84* @return an immutable list of short names85*/86public List<String> getNames();8788/**89* Returns the name of the scripting language supported by this90* <code>ScriptEngine</code>.91* @return The name of the supported language.92*/93public String getLanguageName();9495/**96* Returns the version of the scripting language supported by this97* <code>ScriptEngine</code>.98* @return The version of the supported language.99*/100public String getLanguageVersion();101102/**103* Returns the value of an attribute whose meaning may be implementation-specific.104* Keys for which the value is defined in all implementations are:105* <ul>106* <li>ScriptEngine.ENGINE</li>107* <li>ScriptEngine.ENGINE_VERSION</li>108* <li>ScriptEngine.LANGUAGE</li>109* <li>ScriptEngine.LANGUAGE_VERSION</li>110* <li>ScriptEngine.NAME</li>111* </ul>112* <p>113* The values for these keys are the Strings returned by <code>getEngineName</code>,114* <code>getEngineVersion</code>, <code>getLanguageName</code>,115* <code>getLanguageVersion</code> for the first four keys respectively. For NAME, one of the Strings116* returned by <code>getNames</code> is returned.<br><br>117* A reserved key, <code><b>THREADING</b></code>, whose value describes the behavior of the engine118* with respect to concurrent execution of scripts and maintenance of state is also defined.119* These values for the <code><b>THREADING</b></code> key are:<br><br>120* <ul>121* <li><code>null</code> - The engine implementation is not thread safe, and cannot122* be used to execute scripts concurrently on multiple threads.123* <li><code>"MULTITHREADED"</code> - The engine implementation is internally124* thread-safe and scripts may execute concurrently although effects of script execution125* on one thread may be visible to scripts on other threads.126* <li><code>"THREAD-ISOLATED"</code> - The implementation satisfies the requirements127* of "MULTITHREADED", and also, the engine maintains independent values128* for symbols in scripts executing on different threads.129* <li><code>"STATELESS"</code> - The implementation satisfies the requirements of130* <code>"THREAD-ISOLATED"</code>. In addition, script executions do not alter the131* mappings in the <code>Bindings</code> which is the engine scope of the132* <code>ScriptEngine</code>. In particular, the keys in the <code>Bindings</code>133* and their associated values are the same before and after the execution of the script.134* </ul>135* <br><br>136* Implementations may define implementation-specific keys.137*138* @param key The name of the parameter139* @return The value for the given parameter. Returns <code>null</code> if no140* value is assigned to the key.141*142* @throws NullPointerException if the key is null.143*/144public Object getParameter(String key);145146/**147* Returns a String which can be used to invoke a method of a Java object using the syntax148* of the supported scripting language. For instance, an implementation for a Javascript149* engine might be;150*151* <pre>{@code152* public String getMethodCallSyntax(String obj,153* String m, String... args) {154* String ret = obj;155* ret += "." + m + "(";156* for (int i = 0; i < args.length; i++) {157* ret += args[i];158* if (i < args.length - 1) {159* ret += ",";160* }161* }162* ret += ")";163* return ret;164* }165* } </pre>166*167* @param obj The name representing the object whose method is to be invoked. The168* name is the one used to create bindings using the <code>put</code> method of169* <code>ScriptEngine</code>, the <code>put</code> method of an <code>ENGINE_SCOPE</code>170* <code>Bindings</code>,or the <code>setAttribute</code> method171* of <code>ScriptContext</code>. The identifier used in scripts may be a decorated form of the172* specified one.173*174* @param m The name of the method to invoke.175* @param args names of the arguments in the method call.176*177* @return The String used to invoke the method in the syntax of the scripting language.178*179* @throws NullPointerException if obj or m or args or any of the elements of args is null.180*/181public String getMethodCallSyntax(String obj, String m, String... args);182183/**184* Returns a String that can be used as a statement to display the specified String using185* the syntax of the supported scripting language. For instance, the implementation for a Perl186* engine might be;187*188* <pre><code>189* public String getOutputStatement(String toDisplay) {190* return "print(" + toDisplay + ")";191* }192* </code></pre>193*194* @param toDisplay The String to be displayed by the returned statement.195* @return The string used to display the String in the syntax of the scripting language.196*197*198*/199public String getOutputStatement(String toDisplay);200201202/**203* Returns a valid scripting language executable program with given statements.204* For instance an implementation for a PHP engine might be:205*206* <pre>{@code207* public String getProgram(String... statements) {208* String retval = "<?\n";209* int len = statements.length;210* for (int i = 0; i < len; i++) {211* retval += statements[i] + ";\n";212* }213* return retval += "?>";214* }215* }</pre>216*217* @param statements The statements to be executed. May be return values of218* calls to the <code>getMethodCallSyntax</code> and <code>getOutputStatement</code> methods.219* @return The Program220*221* @throws NullPointerException if the <code>statements</code> array or any of its elements is null222*/223public String getProgram(String... statements);224225/**226* Returns an instance of the <code>ScriptEngine</code> associated with this227* <code>ScriptEngineFactory</code>. A new ScriptEngine is generally228* returned, but implementations may pool, share or reuse engines.229*230* @return A new <code>ScriptEngine</code> instance.231*/232public ScriptEngine getScriptEngine();233}234235236