feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,311 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.io.Reader;
import java.util.Map;
import java.util.Iterator;
/**
* Provides a standard implementation for several of the variants of the <code>eval</code>
* method.
* <br><br>
* <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>
* <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>
* <br><br> are implemented using the abstract methods
* <br><br>
* <code><b>eval(Reader,ScriptContext)</b></code> or
* <code><b>eval(String, ScriptContext)</b></code>
* <br><br>
* with a <code>SimpleScriptContext</code>.
* <br><br>
* A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>
* of the <code>AbstractScriptEngine</code>..
*
* @author Mike Grogan
* @since 1.6
*/
public abstract class AbstractScriptEngine implements ScriptEngine {
/**
* The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.
*/
protected ScriptContext context;
/**
* Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>
* as its default <code>ScriptContext</code>.
*/
public AbstractScriptEngine() {
context = new SimpleScriptContext();
}
/**
* Creates a new instance using the specified <code>Bindings</code> as the
* <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>context</code> field.
*
* @param n The specified <code>Bindings</code>.
* @throws NullPointerException if n is null.
*/
public AbstractScriptEngine(Bindings n) {
this();
if (n == null) {
throw new NullPointerException("n is null");
}
context.setBindings(n, ScriptContext.ENGINE_SCOPE);
}
/**
* Sets the value of the protected <code>context</code> field to the specified
* <code>ScriptContext</code>.
*
* @param ctxt The specified <code>ScriptContext</code>.
* @throws NullPointerException if ctxt is null.
*/
public void setContext(ScriptContext ctxt) {
if (ctxt == null) {
throw new NullPointerException("null context");
}
context = ctxt;
}
/**
* Returns the value of the protected <code>context</code> field.
*
* @return The value of the protected <code>context</code> field.
*/
public ScriptContext getContext() {
return context;
}
/**
* Returns the <code>Bindings</code> with the specified scope value in
* the protected <code>context</code> field.
*
* @param scope The specified scope
*
* @return The corresponding <code>Bindings</code>.
*
* @throws IllegalArgumentException if the value of scope is
* invalid for the type the protected <code>context</code> field.
*/
public Bindings getBindings(int scope) {
if (scope == ScriptContext.GLOBAL_SCOPE) {
return context.getBindings(ScriptContext.GLOBAL_SCOPE);
} else if (scope == ScriptContext.ENGINE_SCOPE) {
return context.getBindings(ScriptContext.ENGINE_SCOPE);
} else {
throw new IllegalArgumentException("Invalid scope value.");
}
}
/**
* Sets the <code>Bindings</code> with the corresponding scope value in the
* <code>context</code> field.
*
* @param bindings The specified <code>Bindings</code>.
* @param scope The specified scope.
*
* @throws IllegalArgumentException if the value of scope is
* invalid for the type the <code>context</code> field.
* @throws NullPointerException if the bindings is null and the scope is
* <code>ScriptContext.ENGINE_SCOPE</code>
*/
public void setBindings(Bindings bindings, int scope) {
if (scope == ScriptContext.GLOBAL_SCOPE) {
context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);;
} else if (scope == ScriptContext.ENGINE_SCOPE) {
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);;
} else {
throw new IllegalArgumentException("Invalid scope value.");
}
}
/**
* Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>
* <code>Bindings</code> of the protected <code>context</code> field.
*
* @param key The specified key.
* @param value The specified value.
*
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key is empty.
*/
public void put(String key, Object value) {
Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
if (nn != null) {
nn.put(key, value);
}
}
/**
* Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the
* protected <code>context</code> field.
*
* @return The value for the specified key.
*
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key is empty.
*/
public Object get(String key) {
Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
if (nn != null) {
return nn.get(key);
}
return null;
}
/**
* <code>eval(Reader, Bindings)</code> calls the abstract
* <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>
* whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>
* are identical to those members of the protected <code>context</code> field. The specified
* <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>
*
* <code>Bindings</code> of the <code>context</code> field.
*
* @param reader A <code>Reader</code> containing the source of the script.
* @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>
* while the script executes.
*
* @return The return value from <code>eval(Reader, ScriptContext)</code>
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(Reader reader, Bindings bindings ) throws ScriptException {
ScriptContext ctxt = getScriptContext(bindings);
return eval(reader, ctxt);
}
/**
* Same as <code>eval(Reader, Bindings)</code> except that the abstract
* <code>eval(String, ScriptContext)</code> is used.
*
* @param script A <code>String</code> containing the source of the script.
*
* @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>
* while the script executes.
*
* @return The return value from <code>eval(String, ScriptContext)</code>
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(String script, Bindings bindings) throws ScriptException {
ScriptContext ctxt = getScriptContext(bindings);
return eval(script , ctxt);
}
/**
* <code>eval(Reader)</code> calls the abstract
* <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>
* field.
*
* @param reader A <code>Reader</code> containing the source of the script.
* @return The return value from <code>eval(Reader, ScriptContext)</code>
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(Reader reader) throws ScriptException {
return eval(reader, context);
}
/**
* Same as <code>eval(Reader)</code> except that the abstract
* <code>eval(String, ScriptContext)</code> is used.
*
* @param script A <code>String</code> containing the source of the script.
* @return The return value from <code>eval(String, ScriptContext)</code>
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if any of the parameters is null.
*/
public Object eval(String script) throws ScriptException {
return eval(script, context);
}
/**
* Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:
*<br><br>
* <ul>
* <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>
* </li>
* <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>
* method as its <code>GLOBAL_SCOPE</code>
* </li>
* <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this
* <code>ScriptEngine</code>
* </li>
* </ul>
* <br><br>
* A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods
* using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>
* versions.
*
* @param nn Bindings to use for the <code>ENGINE_SCOPE</code>
* @return The <code>SimpleScriptContext</code>
*/
protected ScriptContext getScriptContext(Bindings nn) {
SimpleScriptContext ctxt = new SimpleScriptContext();
Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
if (gs != null) {
ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
}
if (nn != null) {
ctxt.setBindings(nn,
ScriptContext.ENGINE_SCOPE);
} else {
throw new NullPointerException("Engine scope Bindings may not be null.");
}
ctxt.setReader(context.getReader());
ctxt.setWriter(context.getWriter());
ctxt.setErrorWriter(context.getErrorWriter());
return ctxt;
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.Map;
/**
* A mapping of key/value pairs, all of whose keys are
* <code>Strings</code>.
*
* @author Mike Grogan
* @since 1.6
*/
public interface Bindings extends Map<String, Object> {
/**
* Set a named value.
*
* @param name The name associated with the value.
* @param value The value associated with the name.
*
* @return The value previously associated with the given name.
* Returns null if no value was previously associated with the name.
*
* @throws NullPointerException if the name is null.
* @throws IllegalArgumentException if the name is empty String.
*/
public Object put(String name, Object value);
/**
* Adds all the mappings in a given <code>Map</code> to this <code>Bindings</code>.
* @param toMerge The <code>Map</code> to merge with this one.
*
* @throws NullPointerException
* if toMerge map is null or if some key in the map is null.
* @throws IllegalArgumentException
* if some key in the map is an empty String.
*/
public void putAll(Map<? extends String, ? extends Object> toMerge);
/**
* Returns <tt>true</tt> if this map contains a mapping for the specified
* key. More formally, returns <tt>true</tt> if and only if
* this map contains a mapping for a key <tt>k</tt> such that
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
* at most one such mapping.)
*
* @param key key whose presence in this map is to be tested.
* @return <tt>true</tt> if this map contains a mapping for the specified
* key.
*
* @throws NullPointerException if key is null
* @throws ClassCastException if key is not String
* @throws IllegalArgumentException if key is empty String
*/
public boolean containsKey(Object key);
/**
* Returns the value to which this map maps the specified key. Returns
* <tt>null</tt> if the map contains no mapping for this key. A return
* value of <tt>null</tt> does not <i>necessarily</i> indicate that the
* map contains no mapping for the key; it's also possible that the map
* explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>
* operation may be used to distinguish these two cases.
*
* <p>More formally, if this map contains a mapping from a key
* <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null :
* key.equals(k))</tt>, then this method returns <tt>v</tt>; otherwise
* it returns <tt>null</tt>. (There can be at most one such mapping.)
*
* @param key key whose associated value is to be returned.
* @return the value to which this map maps the specified key, or
* <tt>null</tt> if the map contains no mapping for this key.
*
* @throws NullPointerException if key is null
* @throws ClassCastException if key is not String
* @throws IllegalArgumentException if key is empty String
*/
public Object get(Object key);
/**
* Removes the mapping for this key from this map if it is present
* (optional operation). More formally, if this map contains a mapping
* from key <tt>k</tt> to value <tt>v</tt> such that
* <code>(key==null ? k==null : key.equals(k))</code>, that mapping
* is removed. (The map can contain at most one such mapping.)
*
* <p>Returns the value to which the map previously associated the key, or
* <tt>null</tt> if the map contained no mapping for this key. (A
* <tt>null</tt> return can also indicate that the map previously
* associated <tt>null</tt> with the specified key if the implementation
* supports <tt>null</tt> values.) The map will not contain a mapping for
* the specified key once the call returns.
*
* @param key key whose mapping is to be removed from the map.
* @return previous value associated with specified key, or <tt>null</tt>
* if there was no mapping for key.
*
* @throws NullPointerException if key is null
* @throws ClassCastException if key is not String
* @throws IllegalArgumentException if key is empty String
*/
public Object remove(Object key);
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.Map;
import java.io.Reader;
/**
* The optional interface implemented by ScriptEngines whose methods compile scripts
* to a form that can be executed repeatedly without recompilation.
*
* @author Mike Grogan
* @since 1.6
*/
public interface Compilable {
/**
* Compiles the script (source represented as a <code>String</code>) for
* later execution.
*
* @param script The source of the script, represented as a <code>String</code>.
*
* @return An instance of a subclass of <code>CompiledScript</code> to be executed later using one
* of the <code>eval</code> methods of <code>CompiledScript</code>.
*
* @throws ScriptException if compilation fails.
* @throws NullPointerException if the argument is null.
*
*/
public CompiledScript compile(String script) throws
ScriptException;
/**
* Compiles the script (source read from <code>Reader</code>) for
* later execution. Functionality is identical to
* <code>compile(String)</code> other than the way in which the source is
* passed.
*
* @param script The reader from which the script source is obtained.
*
* @return An instance of a subclass of <code>CompiledScript</code> to be executed
* later using one of its <code>eval</code> methods of <code>CompiledScript</code>.
*
* @throws ScriptException if compilation fails.
* @throws NullPointerException if argument is null.
*/
public CompiledScript compile(Reader script) throws
ScriptException;
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.Map;
/**
* Extended by classes that store results of compilations. State
* might be stored in the form of Java classes, Java class files or scripting
* language opcodes. The script may be executed repeatedly
* without reparsing.
* <br><br>
* Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an <code>eval</code>
* method of the <code>CompiledScript</code> causes the execution of the script by the
* <code>ScriptEngine</code>. Changes in the state of the <code>ScriptEngine</code> caused by execution
* of the <code>CompiledScript</code> may visible during subsequent executions of scripts by the engine.
*
* @author Mike Grogan
* @since 1.6
*/
public abstract class CompiledScript {
/**
* Executes the program stored in this <code>CompiledScript</code> object.
*
* @param context A <code>ScriptContext</code> that is used in the same way as
* the <code>ScriptContext</code> passed to the <code>eval</code> methods of
* <code>ScriptEngine</code>.
*
* @return The value returned by the script execution, if any. Should return <code>null</code>
* if no value is returned by the script execution.
*
* @throws ScriptException if an error occurs.
* @throws NullPointerException if context is null.
*/
public abstract Object eval(ScriptContext context) throws ScriptException;
/**
* Executes the program stored in the <code>CompiledScript</code> object using
* the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the
* associated <code>ScriptEngine</code> during script execution. If bindings is null,
* then the effect of calling this method is same as that of eval(getEngine().getContext()).
* <p>.
* The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>
* associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.
*
* @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
*
* @return The return value from the script execution
*
* @throws ScriptException if an error occurs.
*/
public Object eval(Bindings bindings) throws ScriptException {
ScriptContext ctxt = getEngine().getContext();
if (bindings != null) {
SimpleScriptContext tempctxt = new SimpleScriptContext();
tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),
ScriptContext.GLOBAL_SCOPE);
tempctxt.setWriter(ctxt.getWriter());
tempctxt.setReader(ctxt.getReader());
tempctxt.setErrorWriter(ctxt.getErrorWriter());
ctxt = tempctxt;
}
return eval(ctxt);
}
/**
* Executes the program stored in the <code>CompiledScript</code> object. The
* default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> is used.
* The effect of calling this method is same as that of eval(getEngine().getContext()).
*
* @return The return value from the script execution
*
* @throws ScriptException if an error occurs.
*/
public Object eval() throws ScriptException {
return eval(getEngine().getContext());
}
/**
* Returns the <code>ScriptEngine</code> whose <code>compile</code> method created this <code>CompiledScript</code>.
* The <code>CompiledScript</code> will execute in this engine.
*
* @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code>
*/
public abstract ScriptEngine getEngine();
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
/**
* The optional interface implemented by ScriptEngines whose methods allow the invocation of
* procedures in scripts that have previously been executed.
*
* @author Mike Grogan
* @author A. Sundararajan
* @since 1.6
*/
public interface Invocable {
/**
* Calls a method on a script object compiled during a previous script execution,
* which is retained in the state of the <code>ScriptEngine</code>.
*
* @param name The name of the procedure to be called.
*
* @param thiz If the procedure is a member of a class
* defined in the script and thiz is an instance of that class
* returned by a previous execution or invocation, the named method is
* called through that instance.
*
* @param args Arguments to pass to the procedure. The rules for converting
* the arguments to scripting variables are implementation-specific.
*
* @return The value returned by the procedure. The rules for converting the scripting
* variable returned by the script method to a Java Object are implementation-specific.
*
* @throws ScriptException if an error occurs during invocation of the method.
* @throws NoSuchMethodException if method with given name or matching argument types cannot be found.
* @throws NullPointerException if the method name is null.
* @throws IllegalArgumentException if the specified thiz is null or the specified Object is
* does not represent a scripting object.
*/
public Object invokeMethod(Object thiz, String name, Object... args)
throws ScriptException, NoSuchMethodException;
/**
* Used to call top-level procedures and functions defined in scripts.
*
* @param name of the procedure or function to call
* @param args Arguments to pass to the procedure or function
* @return The value returned by the procedure or function
*
* @throws ScriptException if an error occurs during invocation of the method.
* @throws NoSuchMethodException if method with given name or matching argument types cannot be found.
* @throws NullPointerException if method name is null.
*/
public Object invokeFunction(String name, Object... args)
throws ScriptException, NoSuchMethodException;
/**
* Returns an implementation of an interface using functions compiled in
* the interpreter. The methods of the interface
* may be implemented using the <code>invokeFunction</code> method.
*
* @param <T> the type of the interface to return
* @param clasz The <code>Class</code> object of the interface to return.
*
* @return An instance of requested interface - null if the requested interface is unavailable,
* i. e. if compiled functions in the <code>ScriptEngine</code> cannot be found matching
* the ones in the requested interface.
*
* @throws IllegalArgumentException if the specified <code>Class</code> object
* is null or is not an interface.
*/
public <T> T getInterface(Class<T> clasz);
/**
* Returns an implementation of an interface using member functions of
* a scripting object compiled in the interpreter. The methods of the
* interface may be implemented using the <code>invokeMethod</code> method.
*
* @param <T> the type of the interface to return
* @param thiz The scripting object whose member functions are used to implement the methods of the interface.
* @param clasz The <code>Class</code> object of the interface to return.
*
* @return An instance of requested interface - null if the requested interface is unavailable,
* i. e. if compiled methods in the <code>ScriptEngine</code> cannot be found matching
* the ones in the requested interface.
*
* @throws IllegalArgumentException if the specified <code>Class</code> object
* is null or is not an interface, or if the specified Object is
* null or does not represent a scripting object.
*/
public <T> T getInterface(Object thiz, Class<T> clasz);
}

View File

@@ -0,0 +1,207 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.List;
import java.io.Writer;
import java.io.Reader;
/**
* The interface whose implementing classes are used to connect Script Engines
* with objects, such as scoped Bindings, in hosting applications. Each scope is a set
* of named attributes whose values can be set and retrieved using the
* <code>ScriptContext</code> methods. ScriptContexts also expose Readers and Writers
* that can be used by the ScriptEngines for input and output.
*
* @author Mike Grogan
* @since 1.6
*/
public interface ScriptContext {
/**
* EngineScope attributes are visible during the lifetime of a single
* <code>ScriptEngine</code> and a set of attributes is maintained for each
* engine.
*/
public static final int ENGINE_SCOPE = 100;
/**
* GlobalScope attributes are visible to all engines created by same ScriptEngineFactory.
*/
public static final int GLOBAL_SCOPE = 200;
/**
* Associates a <code>Bindings</code> instance with a particular scope in this
* <code>ScriptContext</code>. Calls to the <code>getAttribute</code> and
* <code>setAttribute</code> methods must map to the <code>get</code> and
* <code>put</code> methods of the <code>Bindings</code> for the specified scope.
*
* @param bindings The <code>Bindings</code> to associate with the given scope
* @param scope The scope
*
* @throws IllegalArgumentException If no <code>Bindings</code> is defined for the
* specified scope value in ScriptContexts of this type.
* @throws NullPointerException if value of scope is <code>ENGINE_SCOPE</code> and
* the specified <code>Bindings</code> is null.
*
*/
public void setBindings(Bindings bindings, int scope);
/**
* Gets the <code>Bindings</code> associated with the given scope in this
* <code>ScriptContext</code>.
*
* @return The associated <code>Bindings</code>. Returns <code>null</code> if it has not
* been set.
*
* @param scope The scope
* @throws IllegalArgumentException If no <code>Bindings</code> is defined for the
* specified scope value in <code>ScriptContext</code> of this type.
*/
public Bindings getBindings(int scope);
/**
* Sets the value of an attribute in a given scope.
*
* @param name The name of the attribute to set
* @param value The value of the attribute
* @param scope The scope in which to set the attribute
*
* @throws IllegalArgumentException
* if the name is empty or if the scope is invalid.
* @throws NullPointerException if the name is null.
*/
public void setAttribute(String name, Object value, int scope);
/**
* Gets the value of an attribute in a given scope.
*
* @param name The name of the attribute to retrieve.
* @param scope The scope in which to retrieve the attribute.
* @return The value of the attribute. Returns <code>null</code> is the name
* does not exist in the given scope.
*
* @throws IllegalArgumentException
* if the name is empty or if the value of scope is invalid.
* @throws NullPointerException if the name is null.
*/
public Object getAttribute(String name, int scope);
/**
* Remove an attribute in a given scope.
*
* @param name The name of the attribute to remove
* @param scope The scope in which to remove the attribute
*
* @return The removed value.
* @throws IllegalArgumentException
* if the name is empty or if the scope is invalid.
* @throws NullPointerException if the name is null.
*/
public Object removeAttribute(String name, int scope);
/**
* Retrieves the value of the attribute with the given name in
* the scope occurring earliest in the search order. The order
* is determined by the numeric value of the scope parameter (lowest
* scope values first.)
*
* @param name The name of the the attribute to retrieve.
* @return The value of the attribute in the lowest scope for
* which an attribute with the given name is defined. Returns
* null if no attribute with the name exists in any scope.
* @throws NullPointerException if the name is null.
* @throws IllegalArgumentException if the name is empty.
*/
public Object getAttribute(String name);
/**
* Get the lowest scope in which an attribute is defined.
* @param name Name of the attribute
* .
* @return The lowest scope. Returns -1 if no attribute with the given
* name is defined in any scope.
* @throws NullPointerException if name is null.
* @throws IllegalArgumentException if name is empty.
*/
public int getAttributesScope(String name);
/**
* Returns the <code>Writer</code> for scripts to use when displaying output.
*
* @return The <code>Writer</code>.
*/
public Writer getWriter();
/**
* Returns the <code>Writer</code> used to display error output.
*
* @return The <code>Writer</code>
*/
public Writer getErrorWriter();
/**
* Sets the <code>Writer</code> for scripts to use when displaying output.
*
* @param writer The new <code>Writer</code>.
*/
public void setWriter(Writer writer);
/**
* Sets the <code>Writer</code> used to display error output.
*
* @param writer The <code>Writer</code>.
*/
public void setErrorWriter(Writer writer);
/**
* Returns a <code>Reader</code> to be used by the script to read
* input.
*
* @return The <code>Reader</code>.
*/
public Reader getReader();
/**
* Sets the <code>Reader</code> for scripts to read input
* .
* @param reader The new <code>Reader</code>.
*/
public void setReader(Reader reader);
/**
* Returns immutable <code>List</code> of all the valid values for
* scope in the ScriptContext.
*
* @return list of scope values
*/
public List<Integer> getScopes();
}

View File

@@ -0,0 +1,323 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.io.Reader;
import java.util.Map;
import java.util.Set;
/**
* <code>ScriptEngine</code> is the fundamental interface whose methods must be
* fully functional in every implementation of this specification.
* <br><br>
* These methods provide basic scripting functionality. Applications written to this
* simple interface are expected to work with minimal modifications in every implementation.
* It includes methods that execute scripts, and ones that set and get values.
* <br><br>
* The values are key/value pairs of two types. The first type of pairs consists of
* those whose keys are reserved and defined in this specification or by individual
* implementations. The values in the pairs with reserved keys have specified meanings.
* <br><br>
* The other type of pairs consists of those that create Java language Bindings, the values are
* usually represented in scripts by the corresponding keys or by decorated forms of them.
*
* @author Mike Grogan
* @since 1.6
*/
public interface ScriptEngine {
/**
* Reserved key for a named value that passes
* an array of positional arguments to a script.
*/
public static final String ARGV="javax.script.argv";
/**
* Reserved key for a named value that is
* the name of the file being executed.
*/
public static final String FILENAME = "javax.script.filename";
/**
* Reserved key for a named value that is
* the name of the <code>ScriptEngine</code> implementation.
*/
public static final String ENGINE = "javax.script.engine";
/**
* Reserved key for a named value that identifies
* the version of the <code>ScriptEngine</code> implementation.
*/
public static final String ENGINE_VERSION = "javax.script.engine_version";
/**
* Reserved key for a named value that identifies
* the short name of the scripting language. The name is used by the
* <code>ScriptEngineManager</code> to locate a <code>ScriptEngine</code>
* with a given name in the <code>getEngineByName</code> method.
*/
public static final String NAME = "javax.script.name";
/**
* Reserved key for a named value that is
* the full name of Scripting Language supported by the implementation.
*/
public static final String LANGUAGE = "javax.script.language";
/**
* Reserved key for the named value that identifies
* the version of the scripting language supported by the implementation.
*/
public static final String LANGUAGE_VERSION ="javax.script.language_version";
/**
* Causes the immediate execution of the script whose source is the String
* passed as the first argument. The script may be reparsed or recompiled before
* execution. State left in the engine from previous executions, including
* variable values and compiled procedures may be visible during this execution.
*
* @param script The script to be executed by the script engine.
*
* @param context A <code>ScriptContext</code> exposing sets of attributes in
* different scopes. The meanings of the scopes <code>ScriptContext.GLOBAL_SCOPE</code>,
* and <code>ScriptContext.ENGINE_SCOPE</code> are defined in the specification.
* <br><br>
* The <code>ENGINE_SCOPE</code> <code>Bindings</code> of the <code>ScriptContext</code> contains the
* bindings of scripting variables to application objects to be used during this
* script execution.
*
*
* @return The value returned from the execution of the script.
*
* @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
* <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
* implementations.
* @throws NullPointerException if either argument is null.
*/
public Object eval(String script, ScriptContext context) throws ScriptException;
/**
* Same as <code>eval(String, ScriptContext)</code> where the source of the script
* is read from a <code>Reader</code>.
*
* @param reader The source of the script to be executed by the script engine.
*
* @param context The <code>ScriptContext</code> passed to the script engine.
*
* @return The value returned from the execution of the script.
*
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if either argument is null.
*/
public Object eval(Reader reader , ScriptContext context) throws ScriptException;
/**
* Executes the specified script. The default <code>ScriptContext</code> for the <code>ScriptEngine</code>
* is used.
*
* @param script The script language source to be executed.
*
* @return The value returned from the execution of the script.
*
* @throws ScriptException if error occurs in script.
* @throws NullPointerException if the argument is null.
*/
public Object eval(String script) throws ScriptException;
/**
* Same as <code>eval(String)</code> except that the source of the script is
* provided as a <code>Reader</code>
*
* @param reader The source of the script.
*
* @return The value returned by the script.
*
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if the argument is null.
*/
public Object eval(Reader reader) throws ScriptException;
/**
* Executes the script using the <code>Bindings</code> argument as the <code>ENGINE_SCOPE</code>
* <code>Bindings</code> of the <code>ScriptEngine</code> during the script execution. The
* <code>Reader</code>, <code>Writer</code> and non-<code>ENGINE_SCOPE</code> <code>Bindings</code> of the
* default <code>ScriptContext</code> are used. The <code>ENGINE_SCOPE</code>
* <code>Bindings</code> of the <code>ScriptEngine</code> is not changed, and its
* mappings are unaltered by the script execution.
*
* @param script The source for the script.
*
* @param n The <code>Bindings</code> of attributes to be used for script execution.
*
* @return The value returned by the script.
*
* @throws ScriptException if an error occurs in script.
* @throws NullPointerException if either argument is null.
*/
public Object eval(String script, Bindings n) throws ScriptException;
/**
* Same as <code>eval(String, Bindings)</code> except that the source of the script
* is provided as a <code>Reader</code>.
*
* @param reader The source of the script.
* @param n The <code>Bindings</code> of attributes.
*
* @return The value returned by the script.
*
* @throws ScriptException if an error occurs.
* @throws NullPointerException if either argument is null.
*/
public Object eval(Reader reader , Bindings n) throws ScriptException;
/**
* Sets a key/value pair in the state of the ScriptEngine that may either create
* a Java Language Binding to be used in the execution of scripts or be used in some
* other way, depending on whether the key is reserved. Must have the same effect as
* <code>getBindings(ScriptContext.ENGINE_SCOPE).put</code>.
*
* @param key The name of named value to add
* @param value The value of named value to add.
*
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key is empty.
*/
public void put(String key, Object value);
/**
* Retrieves a value set in the state of this engine. The value might be one
* which was set using <code>setValue</code> or some other value in the state
* of the <code>ScriptEngine</code>, depending on the implementation. Must have the same effect
* as <code>getBindings(ScriptContext.ENGINE_SCOPE).get</code>
*
* @param key The key whose value is to be returned
* @return the value for the given key
*
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key is empty.
*/
public Object get(String key);
/**
* Returns a scope of named values. The possible scopes are:
* <br><br>
* <ul>
* <li><code>ScriptContext.GLOBAL_SCOPE</code> - The set of named values representing global
* scope. If this <code>ScriptEngine</code> is created by a <code>ScriptEngineManager</code>,
* then the manager sets global scope bindings. This may be <code>null</code> if no global
* scope is associated with this <code>ScriptEngine</code></li>
* <li><code>ScriptContext.ENGINE_SCOPE</code> - The set of named values representing the state of
* this <code>ScriptEngine</code>. The values are generally visible in scripts using
* the associated keys as variable names.</li>
* <li>Any other value of scope defined in the default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
* </li>
* </ul>
* <br><br>
* The <code>Bindings</code> instances that are returned must be identical to those returned by the
* <code>getBindings</code> method of <code>ScriptContext</code> called with corresponding arguments on
* the default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
*
* @param scope Either <code>ScriptContext.ENGINE_SCOPE</code> or <code>ScriptContext.GLOBAL_SCOPE</code>
* which specifies the <code>Bindings</code> to return. Implementations of <code>ScriptContext</code>
* may define additional scopes. If the default <code>ScriptContext</code> of the <code>ScriptEngine</code>
* defines additional scopes, any of them can be passed to get the corresponding <code>Bindings</code>.
*
* @return The <code>Bindings</code> with the specified scope.
*
* @throws IllegalArgumentException if specified scope is invalid
*
*/
public Bindings getBindings(int scope);
/**
* Sets a scope of named values to be used by scripts. The possible scopes are:
*<br><br>
* <ul>
* <li><code>ScriptContext.ENGINE_SCOPE</code> - The specified <code>Bindings</code> replaces the
* engine scope of the <code>ScriptEngine</code>.
* </li>
* <li><code>ScriptContext.GLOBAL_SCOPE</code> - The specified <code>Bindings</code> must be visible
* as the <code>GLOBAL_SCOPE</code>.
* </li>
* <li>Any other value of scope defined in the default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
*</li>
* </ul>
* <br><br>
* The method must have the same effect as calling the <code>setBindings</code> method of
* <code>ScriptContext</code> with the corresponding value of <code>scope</code> on the default
* <code>ScriptContext</code> of the <code>ScriptEngine</code>.
*
* @param bindings The <code>Bindings</code> for the specified scope.
* @param scope The specified scope. Either <code>ScriptContext.ENGINE_SCOPE</code>,
* <code>ScriptContext.GLOBAL_SCOPE</code>, or any other valid value of scope.
*
* @throws IllegalArgumentException if the scope is invalid
* @throws NullPointerException if the bindings is null and the scope is
* <code>ScriptContext.ENGINE_SCOPE</code>
*/
public void setBindings(Bindings bindings, int scope);
/**
* Returns an uninitialized <code>Bindings</code>.
*
* @return A <code>Bindings</code> that can be used to replace the state of this <code>ScriptEngine</code>.
**/
public Bindings createBindings();
/**
* Returns the default <code>ScriptContext</code> of the <code>ScriptEngine</code> whose Bindings, Reader
* and Writers are used for script executions when no <code>ScriptContext</code> is specified.
*
* @return The default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
*/
public ScriptContext getContext();
/**
* Sets the default <code>ScriptContext</code> of the <code>ScriptEngine</code> whose Bindings, Reader
* and Writers are used for script executions when no <code>ScriptContext</code> is specified.
*
* @param context A <code>ScriptContext</code> that will replace the default <code>ScriptContext</code> in
* the <code>ScriptEngine</code>.
* @throws NullPointerException if context is null.
*/
public void setContext(ScriptContext context);
/**
* Returns a <code>ScriptEngineFactory</code> for the class to which this <code>ScriptEngine</code> belongs.
*
* @return The <code>ScriptEngineFactory</code>
*/
public ScriptEngineFactory getFactory();
}

View File

@@ -0,0 +1,229 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.List;
/**
* <code>ScriptEngineFactory</code> is used to describe and instantiate
* <code>ScriptEngines</code>.
* <br><br>
* Each class implementing <code>ScriptEngine</code> has a corresponding factory
* that exposes metadata describing the engine class.
* <br><br>The <code>ScriptEngineManager</code>
* uses the service provider mechanism described in the <i>Jar File Specification</i> to obtain
* instances of all <code>ScriptEngineFactories</code> available in
* the current ClassLoader.
*
* @since 1.6
*/
public interface ScriptEngineFactory {
/**
* Returns the full name of the <code>ScriptEngine</code>. For
* instance an implementation based on the Mozilla Rhino Javascript engine
* might return <i>Rhino Mozilla Javascript Engine</i>.
* @return The name of the engine implementation.
*/
public String getEngineName();
/**
* Returns the version of the <code>ScriptEngine</code>.
* @return The <code>ScriptEngine</code> implementation version.
*/
public String getEngineVersion();
/**
* Returns an immutable list of filename extensions, which generally identify scripts
* written in the language supported by this <code>ScriptEngine</code>.
* The array is used by the <code>ScriptEngineManager</code> to implement its
* <code>getEngineByExtension</code> method.
* @return The list of extensions.
*/
public List<String> getExtensions();
/**
* Returns an immutable list of mimetypes, associated with scripts that
* can be executed by the engine. The list is used by the
* <code>ScriptEngineManager</code> class to implement its
* <code>getEngineByMimetype</code> method.
* @return The list of mime types.
*/
public List<String> getMimeTypes();
/**
* Returns an immutable list of short names for the <code>ScriptEngine</code>, which may be used to
* identify the <code>ScriptEngine</code> by the <code>ScriptEngineManager</code>.
* For instance, an implementation based on the Mozilla Rhino Javascript engine might
* return list containing {&quot;javascript&quot;, &quot;rhino&quot;}.
* @return an immutable list of short names
*/
public List<String> getNames();
/**
* Returns the name of the scripting language supported by this
* <code>ScriptEngine</code>.
* @return The name of the supported language.
*/
public String getLanguageName();
/**
* Returns the version of the scripting language supported by this
* <code>ScriptEngine</code>.
* @return The version of the supported language.
*/
public String getLanguageVersion();
/**
* Returns the value of an attribute whose meaning may be implementation-specific.
* Keys for which the value is defined in all implementations are:
* <ul>
* <li>ScriptEngine.ENGINE</li>
* <li>ScriptEngine.ENGINE_VERSION</li>
* <li>ScriptEngine.LANGUAGE</li>
* <li>ScriptEngine.LANGUAGE_VERSION</li>
* <li>ScriptEngine.NAME</li>
* </ul>
* <p>
* The values for these keys are the Strings returned by <code>getEngineName</code>,
* <code>getEngineVersion</code>, <code>getLanguageName</code>,
* <code>getLanguageVersion</code> for the first four keys respectively. For NAME, one of the Strings
* returned by <code>getNames</code> is returned.<br><br>
* A reserved key, <code><b>THREADING</b></code>, 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 the <code><b>THREADING</b></code> key are:<br><br>
* <ul>
* <li><code>null</code> - The engine implementation is not thread safe, and cannot
* be used to execute scripts concurrently on multiple threads.
* <li><code>&quot;MULTITHREADED&quot;</code> - 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.
* <li><code>&quot;THREAD-ISOLATED&quot;</code> - The implementation satisfies the requirements
* of &quot;MULTITHREADED&quot;, and also, the engine maintains independent values
* for symbols in scripts executing on different threads.
* <li><code>&quot;STATELESS&quot;</code> - The implementation satisfies the requirements of
* <li><code>&quot;THREAD-ISOLATED&quot;</code>. In addition, script executions do not alter the
* mappings in the <code>Bindings</code> which is the engine scope of the
* <code>ScriptEngine</code>. In particular, the keys in the <code>Bindings</code>
* and their associated values are the same before and after the execution of the script.
* </ul>
* <br><br>
* Implementations may define implementation-specific keys.
*
* @param key The name of the parameter
* @return The value for the given parameter. Returns <code>null</code> if no
* value is assigned to the key.
*
*/
public Object getParameter(String key);
/**
* Returns 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;
*
* <pre>{@code
* 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;
* }
* } </pre>
* <p>
*
* @param obj The name representing the object whose method is to be invoked. The
* name is the one used to create bindings using the <code>put</code> method of
* <code>ScriptEngine</code>, the <code>put</code> method of an <code>ENGINE_SCOPE</code>
* <code>Bindings</code>,or the <code>setAttribute</code> method
* of <code>ScriptContext</code>. The identifier used in scripts may be a decorated form of the
* specified one.
*
* @param m The name of the method to invoke.
* @param args names of the arguments in the method call.
*
* @return The String used to invoke the method in the syntax of the scripting language.
*/
public String getMethodCallSyntax(String obj, String m, String... args);
/**
* Returns 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;
*
* <pre><code>
* public String getOutputStatement(String toDisplay) {
* return "print(" + toDisplay + ")";
* }
* </code></pre>
*
* @param toDisplay The String to be displayed by the returned statement.
* @return The string used to display the String in the syntax of the scripting language.
*
*
*/
public String getOutputStatement(String toDisplay);
/**
* Returns a valid scripting language executable program with given statements.
* For instance an implementation for a PHP engine might be:
*
* <pre>{@code
* 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 += "?>";
* }
* }</pre>
*
* @param statements The statements to be executed. May be return values of
* calls to the <code>getMethodCallSyntax</code> and <code>getOutputStatement</code> methods.
* @return The Program
*/
public String getProgram(String... statements);
/**
* Returns an instance of the <code>ScriptEngine</code> associated with this
* <code>ScriptEngineFactory</code>. A new ScriptEngine is generally
* returned, but implementations may pool, share or reuse engines.
*
* @return A new <code>ScriptEngine</code> instance.
*/
public ScriptEngine getScriptEngine();
}

View File

@@ -0,0 +1,416 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.*;
import java.security.*;
import java.util.ServiceLoader;
import java.util.ServiceConfigurationError;
/**
* The <code>ScriptEngineManager</code> implements a discovery and instantiation
* mechanism for <code>ScriptEngine</code> classes and also maintains a
* collection of key/value pairs storing state shared by all engines created
* by the Manager. This class uses the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism to enumerate all the
* implementations of <code>ScriptEngineFactory</code>. <br><br>
* The <code>ScriptEngineManager</code> 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.
* <p>
* The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope" maintained
* by the manager is available to all instances of <code>ScriptEngine</code> created
* by the <code>ScriptEngineManager</code>. The values in the <code>Bindings</code> are
* generally exposed in all scripts.
*
* @author Mike Grogan
* @author A. Sundararajan
* @since 1.6
*/
public class ScriptEngineManager {
private static final boolean DEBUG = false;
/**
* The effect of calling this constructor is the same as calling
* <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
*
* @see java.lang.Thread#getContextClassLoader
*/
public ScriptEngineManager() {
ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
init(ctxtLoader);
}
/**
* This constructor loads the implementations of
* <code>ScriptEngineFactory</code> visible to the given
* <code>ClassLoader</code> using the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism.<br><br>
* If loader is <code>null</code>, the script engine factories that are
* bundled with the platform and that are in the usual extension
* directories (installed extensions) are loaded. <br><br>
*
* @param loader ClassLoader used to discover script engine factories.
*/
public ScriptEngineManager(ClassLoader loader) {
init(loader);
}
private void init(final ClassLoader loader) {
globalScope = new SimpleBindings();
engineSpis = new HashSet<ScriptEngineFactory>();
nameAssociations = new HashMap<String, ScriptEngineFactory>();
extensionAssociations = new HashMap<String, ScriptEngineFactory>();
mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
initEngines(loader);
}
private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
if (loader != null) {
return ServiceLoader.load(ScriptEngineFactory.class, loader);
} else {
return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
}
}
private void initEngines(final ClassLoader loader) {
Iterator<ScriptEngineFactory> itr = null;
try {
ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
@Override
public ServiceLoader<ScriptEngineFactory> run() {
return getServiceLoader(loader);
}
});
itr = sl.iterator();
} catch (ServiceConfigurationError err) {
System.err.println("Can't find ScriptEngineFactory providers: " +
err.getMessage());
if (DEBUG) {
err.printStackTrace();
}
// do not throw any exception here. user may want to
// manage his/her own factories using this manager
// by explicit registratation (by registerXXX) methods.
return;
}
try {
while (itr.hasNext()) {
try {
ScriptEngineFactory fact = itr.next();
engineSpis.add(fact);
} catch (ServiceConfigurationError err) {
System.err.println("ScriptEngineManager providers.next(): "
+ err.getMessage());
if (DEBUG) {
err.printStackTrace();
}
// one factory failed, but check other factories...
continue;
}
}
} catch (ServiceConfigurationError err) {
System.err.println("ScriptEngineManager providers.hasNext(): "
+ err.getMessage());
if (DEBUG) {
err.printStackTrace();
}
// do not throw any exception here. user may want to
// manage his/her own factories using this manager
// by explicit registratation (by registerXXX) methods.
return;
}
}
/**
* <code>setBindings</code> stores the specified <code>Bindings</code>
* in the <code>globalScope</code> field. ScriptEngineManager sets this
* <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
* objects created by it.
*
* @param bindings The specified <code>Bindings</code>
* @throws IllegalArgumentException if bindings is null.
*/
public void setBindings(Bindings bindings) {
if (bindings == null) {
throw new IllegalArgumentException("Global scope cannot be null.");
}
globalScope = bindings;
}
/**
* <code>getBindings</code> returns the value of the <code>globalScope</code> field.
* ScriptEngineManager sets this <code>Bindings</code> as global bindings for
* <code>ScriptEngine</code> objects created by it.
*
* @return The globalScope field.
*/
public Bindings getBindings() {
return globalScope;
}
/**
* Sets the specified key/value pair in the Global Scope.
* @param key Key to set
* @param value Value to set.
* @throws NullPointerException if key is null.
* @throws IllegalArgumentException if key is empty string.
*/
public void put(String key, Object value) {
globalScope.put(key, value);
}
/**
* Gets the value for the specified key in the Global Scope
* @param key The key whose value is to be returned.
* @return The value for the specified key.
*/
public Object get(String key) {
return globalScope.get(key);
}
/**
* Looks up and creates a <code>ScriptEngine</code> for a given name.
* The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
* registered as a handler for the specified name using the <code>registerEngineName</code>
* method.
* <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
* stored by the constructor for one with the specified name. If a <code>ScriptEngineFactory</code>
* is found by either method, it is used to create instance of <code>ScriptEngine</code>.
* @param shortName The short name of the <code>ScriptEngine</code> implementation.
* returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
* @return A <code>ScriptEngine</code> created by the factory located in the search. Returns null
* if no such factory was found. The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
* <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
* created <code>ScriptEngine</code>.
* @throws NullPointerException if shortName is null.
*/
public ScriptEngine getEngineByName(String shortName) {
if (shortName == null) throw new NullPointerException();
//look for registered name first
Object obj;
if (null != (obj = nameAssociations.get(shortName))) {
ScriptEngineFactory spi = (ScriptEngineFactory)obj;
try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
}
for (ScriptEngineFactory spi : engineSpis) {
List<String> names = null;
try {
names = spi.getNames();
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
if (names != null) {
for (String name : names) {
if (shortName.equals(name)) {
try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
}
}
}
}
return null;
}
/**
* Look up and create a <code>ScriptEngine</code> for a given extension. The algorithm
* used by <code>getEngineByName</code> is used except that the search starts
* by looking for a <code>ScriptEngineFactory</code> registered to handle the
* given extension using <code>registerEngineExtension</code>.
* @param extension The given extension
* @return The engine to handle scripts with this extension. Returns <code>null</code>
* if not found.
* @throws NullPointerException if extension is null.
*/
public ScriptEngine getEngineByExtension(String extension) {
if (extension == null) throw new NullPointerException();
//look for registered extension first
Object obj;
if (null != (obj = extensionAssociations.get(extension))) {
ScriptEngineFactory spi = (ScriptEngineFactory)obj;
try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
}
for (ScriptEngineFactory spi : engineSpis) {
List<String> exts = null;
try {
exts = spi.getExtensions();
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
if (exts == null) continue;
for (String ext : exts) {
if (extension.equals(ext)) {
try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
}
}
}
return null;
}
/**
* Look up and create a <code>ScriptEngine</code> for a given mime type. The algorithm
* used by <code>getEngineByName</code> is used except that the search starts
* by looking for a <code>ScriptEngineFactory</code> registered to handle the
* given mime type using <code>registerEngineMimeType</code>.
* @param mimeType The given mime type
* @return The engine to handle scripts with this mime type. Returns <code>null</code>
* if not found.
* @throws NullPointerException if mimeType is null.
*/
public ScriptEngine getEngineByMimeType(String mimeType) {
if (mimeType == null) throw new NullPointerException();
//look for registered types first
Object obj;
if (null != (obj = mimeTypeAssociations.get(mimeType))) {
ScriptEngineFactory spi = (ScriptEngineFactory)obj;
try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
}
for (ScriptEngineFactory spi : engineSpis) {
List<String> types = null;
try {
types = spi.getMimeTypes();
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
if (types == null) continue;
for (String type : types) {
if (mimeType.equals(type)) {
try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}
}
}
}
return null;
}
/**
* Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
* found by the discovery mechanism.
* @return List of all discovered <code>ScriptEngineFactory</code>s.
*/
public List<ScriptEngineFactory> getEngineFactories() {
List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
for (ScriptEngineFactory spi : engineSpis) {
res.add(spi);
}
return Collections.unmodifiableList(res);
}
/**
* Registers a <code>ScriptEngineFactory</code> to handle a language
* name. Overrides any such association found using the Discovery mechanism.
* @param name The name to be associated with the <code>ScriptEngineFactory</code>.
* @param factory The class to associate with the given name.
* @throws NullPointerException if any of the parameters is null.
*/
public void registerEngineName(String name, ScriptEngineFactory factory) {
if (name == null || factory == null) throw new NullPointerException();
nameAssociations.put(name, factory);
}
/**
* Registers a <code>ScriptEngineFactory</code> to handle a mime type.
* Overrides any such association found using the Discovery mechanism.
*
* @param type The mime type to be associated with the
* <code>ScriptEngineFactory</code>.
*
* @param factory The class to associate with the given mime type.
* @throws NullPointerException if any of the parameters is null.
*/
public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
if (type == null || factory == null) throw new NullPointerException();
mimeTypeAssociations.put(type, factory);
}
/**
* Registers a <code>ScriptEngineFactory</code> to handle an extension.
* Overrides any such association found using the Discovery mechanism.
*
* @param extension The extension type to be associated with the
* <code>ScriptEngineFactory</code>.
* @param factory The class to associate with the given extension.
* @throws NullPointerException if any of the parameters is null.
*/
public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
if (extension == null || factory == null) throw new NullPointerException();
extensionAssociations.put(extension, factory);
}
/** Set of script engine factories discovered. */
private HashSet<ScriptEngineFactory> engineSpis;
/** Map of engine name to script engine factory. */
private HashMap<String, ScriptEngineFactory> nameAssociations;
/** Map of script file extension to script engine factory. */
private HashMap<String, ScriptEngineFactory> extensionAssociations;
/** Map of script script MIME type to script engine factory. */
private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
/** Global bindings associated with script engines created by this manager. */
private Bindings globalScope;
}

View File

@@ -0,0 +1,154 @@
/*
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
/**
* The generic <code>Exception</code> class for the Scripting APIs. Checked
* exception types thrown by underlying scripting implementations must be wrapped in instances of
* <code>ScriptException</code>. The class has members to store line and column numbers and
* filenames if this information is available.
*
* @author Mike Grogan
* @since 1.6
*/
public class ScriptException extends Exception {
private static final long serialVersionUID = 8265071037049225001L;
private String fileName;
private int lineNumber;
private int columnNumber;
/**
* Creates a <code>ScriptException</code> with a String to be used in its message.
* Filename, and line and column numbers are unspecified.
*
* @param s The String to use in the message.
*/
public ScriptException(String s) {
super(s);
fileName = null;
lineNumber = -1;
columnNumber = -1;
}
/**
* Creates a <code>ScriptException</code> wrapping an <code>Exception</code> thrown by an underlying
* interpreter. Line and column numbers and filename are unspecified.
*
* @param e The wrapped <code>Exception</code>.
*/
public ScriptException(Exception e) {
super(e);
fileName = null;
lineNumber = -1;
columnNumber = -1;
}
/**
* Creates a <code>ScriptException</code> with message, filename and linenumber to
* be used in error messages.
*
* @param message The string to use in the message
*
* @param fileName The file or resource name describing the location of a script error
* causing the <code>ScriptException</code> to be thrown.
*
* @param lineNumber A line number describing the location of a script error causing
* the <code>ScriptException</code> to be thrown.
*/
public ScriptException(String message, String fileName, int lineNumber) {
super(message);
this.fileName = fileName;
this.lineNumber = lineNumber;
this.columnNumber = -1;
}
/**
* <code>ScriptException</code> constructor specifying message, filename, line number
* and column number.
* @param message The message.
* @param fileName The filename
* @param lineNumber the line number.
* @param columnNumber the column number.
*/
public ScriptException(String message,
String fileName,
int lineNumber,
int columnNumber) {
super(message);
this.fileName = fileName;
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
}
/**
* Returns a message containing the String passed to a constructor as well as
* line and column numbers and filename if any of these are known.
* @return The error message.
*/
public String getMessage() {
String ret = super.getMessage();
if (fileName != null) {
ret += (" in " + fileName);
if (lineNumber != -1) {
ret += " at line number " + lineNumber;
}
if (columnNumber != -1) {
ret += " at column number " + columnNumber;
}
}
return ret;
}
/**
* Get the line number on which an error occurred.
* @return The line number. Returns -1 if a line number is unavailable.
*/
public int getLineNumber() {
return lineNumber;
}
/**
* Get the column number on which an error occurred.
* @return The column number. Returns -1 if a column number is unavailable.
*/
public int getColumnNumber() {
return columnNumber;
}
/**
* Get the source of the script causing the error.
* @return The file name of the script or some other string describing the script
* source. May return some implementation-defined string such as <i>&lt;unknown&gt;</i>
* if a description of the source is unavailable.
*/
public String getFileName() {
return fileName;
}
}

View File

@@ -0,0 +1,223 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.util.Set;
/**
* A simple implementation of Bindings backed by
* a <code>HashMap</code> or some other specified <code>Map</code>.
*
* @author Mike Grogan
* @since 1.6
*/
public class SimpleBindings implements Bindings {
/**
* The <code>Map</code> field stores the attributes.
*/
private Map<String,Object> map;
/**
* Constructor uses an existing <code>Map</code> to store the values.
* @param m The <code>Map</code> backing this <code>SimpleBindings</code>.
* @throws NullPointerException if m is null
*/
public SimpleBindings(Map<String,Object> m) {
if (m == null) {
throw new NullPointerException();
}
this.map = m;
}
/**
* Default constructor uses a <code>HashMap</code>.
*/
public SimpleBindings() {
this(new HashMap<String,Object>());
}
/**
* Sets the specified key/value in the underlying <code>map</code> field.
*
* @param name Name of value
* @param value Value to set.
*
* @return Previous value for the specified key. Returns null if key was previously
* unset.
*
* @throws NullPointerException if the name is null.
* @throws IllegalArgumentException if the name is empty.
*/
public Object put(String name, Object value) {
checkKey(name);
return map.put(name,value);
}
/**
* <code>putAll</code> is implemented using <code>Map.putAll</code>.
*
* @param toMerge The <code>Map</code> of values to add.
*
* @throws NullPointerException
* if toMerge map is null or if some key in the map is null.
* @throws IllegalArgumentException
* if some key in the map is an empty String.
*/
public void putAll(Map<? extends String, ? extends Object> toMerge) {
if (toMerge == null) {
throw new NullPointerException("toMerge map is null");
}
for (Map.Entry<? extends String, ? extends Object> entry : toMerge.entrySet()) {
String key = entry.getKey();
checkKey(key);
put(key, entry.getValue());
}
}
/** {@inheritDoc} */
public void clear() {
map.clear();
}
/**
* Returns <tt>true</tt> if this map contains a mapping for the specified
* key. More formally, returns <tt>true</tt> if and only if
* this map contains a mapping for a key <tt>k</tt> such that
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
* at most one such mapping.)
*
* @param key key whose presence in this map is to be tested.
* @return <tt>true</tt> if this map contains a mapping for the specified
* key.
*
* @throws NullPointerException if key is null
* @throws ClassCastException if key is not String
* @throws IllegalArgumentException if key is empty String
*/
public boolean containsKey(Object key) {
checkKey(key);
return map.containsKey(key);
}
/** {@inheritDoc} */
public boolean containsValue(Object value) {
return map.containsValue(value);
}
/** {@inheritDoc} */
public Set<Map.Entry<String, Object>> entrySet() {
return map.entrySet();
}
/**
* Returns the value to which this map maps the specified key. Returns
* <tt>null</tt> if the map contains no mapping for this key. A return
* value of <tt>null</tt> does not <i>necessarily</i> indicate that the
* map contains no mapping for the key; it's also possible that the map
* explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>
* operation may be used to distinguish these two cases.
*
* <p>More formally, if this map contains a mapping from a key
* <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null :
* key.equals(k))</tt>, then this method returns <tt>v</tt>; otherwise
* it returns <tt>null</tt>. (There can be at most one such mapping.)
*
* @param key key whose associated value is to be returned.
* @return the value to which this map maps the specified key, or
* <tt>null</tt> if the map contains no mapping for this key.
*
* @throws NullPointerException if key is null
* @throws ClassCastException if key is not String
* @throws IllegalArgumentException if key is empty String
*/
public Object get(Object key) {
checkKey(key);
return map.get(key);
}
/** {@inheritDoc} */
public boolean isEmpty() {
return map.isEmpty();
}
/** {@inheritDoc} */
public Set<String> keySet() {
return map.keySet();
}
/**
* Removes the mapping for this key from this map if it is present
* (optional operation). More formally, if this map contains a mapping
* from key <tt>k</tt> to value <tt>v</tt> such that
* <code>(key==null ? k==null : key.equals(k))</code>, that mapping
* is removed. (The map can contain at most one such mapping.)
*
* <p>Returns the value to which the map previously associated the key, or
* <tt>null</tt> if the map contained no mapping for this key. (A
* <tt>null</tt> return can also indicate that the map previously
* associated <tt>null</tt> with the specified key if the implementation
* supports <tt>null</tt> values.) The map will not contain a mapping for
* the specified key once the call returns.
*
* @param key key whose mapping is to be removed from the map.
* @return previous value associated with specified key, or <tt>null</tt>
* if there was no mapping for key.
*
* @throws NullPointerException if key is null
* @throws ClassCastException if key is not String
* @throws IllegalArgumentException if key is empty String
*/
public Object remove(Object key) {
checkKey(key);
return map.remove(key);
}
/** {@inheritDoc} */
public int size() {
return map.size();
}
/** {@inheritDoc} */
public Collection<Object> values() {
return map.values();
}
private void checkKey(Object key) {
if (key == null) {
throw new NullPointerException("key can not be null");
}
if (!(key instanceof String)) {
throw new ClassCastException("key should be a String");
}
if (key.equals("")) {
throw new IllegalArgumentException("key can not be empty");
}
}
}

View File

@@ -0,0 +1,333 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.script;
import java.util.*;
import java.io.*;
/**
* Simple implementation of ScriptContext.
*
* @author Mike Grogan
* @since 1.6
*/
public class SimpleScriptContext implements ScriptContext {
/**
* This is the writer to be used to output from scripts.
* By default, a <code>PrintWriter</code> based on <code>System.out</code>
* is used. Accessor methods getWriter, setWriter are used to manage
* this field.
* @see java.lang.System#out
* @see java.io.PrintWriter
*/
protected Writer writer;
/**
* This is the writer to be used to output errors from scripts.
* By default, a <code>PrintWriter</code> based on <code>System.err</code> is
* used. Accessor methods getErrorWriter, setErrorWriter are used to manage
* this field.
* @see java.lang.System#err
* @see java.io.PrintWriter
*/
protected Writer errorWriter;
/**
* This is the reader to be used for input from scripts.
* By default, a <code>InputStreamReader</code> based on <code>System.in</code>
* is used and default charset is used by this reader. Accessor methods
* getReader, setReader are used to manage this field.
* @see java.lang.System#in
* @see java.io.InputStreamReader
*/
protected Reader reader;
/**
* This is the engine scope bindings.
* By default, a <code>SimpleBindings</code> is used. Accessor
* methods setBindings, getBindings are used to manage this field.
* @see SimpleBindings
*/
protected Bindings engineScope;
/**
* This is the global scope bindings.
* By default, a null value (which means no global scope) is used. Accessor
* methods setBindings, getBindings are used to manage this field.
*/
protected Bindings globalScope;
/**
* Create a {@code SimpleScriptContext}.
*/
public SimpleScriptContext() {
engineScope = new SimpleBindings();
globalScope = null;
reader = new InputStreamReader(System.in);
writer = new PrintWriter(System.out , true);
errorWriter = new PrintWriter(System.err, true);
}
/**
* Sets a <code>Bindings</code> of attributes for the given scope. If the value
* of scope is <code>ENGINE_SCOPE</code> the given <code>Bindings</code> replaces the
* <code>engineScope</code> field. If the value
* of scope is <code>GLOBAL_SCOPE</code> the given <code>Bindings</code> replaces the
* <code>globalScope</code> field.
*
* @param bindings The <code>Bindings</code> of attributes to set.
* @param scope The value of the scope in which the attributes are set.
*
* @throws IllegalArgumentException if scope is invalid.
* @throws NullPointerException if the value of scope is <code>ENGINE_SCOPE</code> and
* the specified <code>Bindings</code> is null.
*/
public void setBindings(Bindings bindings, int scope) {
switch (scope) {
case ENGINE_SCOPE:
if (bindings == null) {
throw new NullPointerException("Engine scope cannot be null.");
}
engineScope = bindings;
break;
case GLOBAL_SCOPE:
globalScope = bindings;
break;
default:
throw new IllegalArgumentException("Invalid scope value.");
}
}
/**
* Retrieves the value of the attribute with the given name in
* the scope occurring earliest in the search order. The order
* is determined by the numeric value of the scope parameter (lowest
* scope values first.)
*
* @param name The name of the the attribute to retrieve.
* @return The value of the attribute in the lowest scope for
* which an attribute with the given name is defined. Returns
* null if no attribute with the name exists in any scope.
* @throws NullPointerException if the name is null.
* @throws IllegalArgumentException if the name is empty.
*/
public Object getAttribute(String name) {
checkName(name);
if (engineScope.containsKey(name)) {
return getAttribute(name, ENGINE_SCOPE);
} else if (globalScope != null && globalScope.containsKey(name)) {
return getAttribute(name, GLOBAL_SCOPE);
}
return null;
}
/**
* Gets the value of an attribute in a given scope.
*
* @param name The name of the attribute to retrieve.
* @param scope The scope in which to retrieve the attribute.
* @return The value of the attribute. Returns <code>null</code> is the name
* does not exist in the given scope.
*
* @throws IllegalArgumentException
* if the name is empty or if the value of scope is invalid.
* @throws NullPointerException if the name is null.
*/
public Object getAttribute(String name, int scope) {
checkName(name);
switch (scope) {
case ENGINE_SCOPE:
return engineScope.get(name);
case GLOBAL_SCOPE:
if (globalScope != null) {
return globalScope.get(name);
}
return null;
default:
throw new IllegalArgumentException("Illegal scope value.");
}
}
/**
* Remove an attribute in a given scope.
*
* @param name The name of the attribute to remove
* @param scope The scope in which to remove the attribute
*
* @return The removed value.
* @throws IllegalArgumentException
* if the name is empty or if the scope is invalid.
* @throws NullPointerException if the name is null.
*/
public Object removeAttribute(String name, int scope) {
checkName(name);
switch (scope) {
case ENGINE_SCOPE:
if (getBindings(ENGINE_SCOPE) != null) {
return getBindings(ENGINE_SCOPE).remove(name);
}
return null;
case GLOBAL_SCOPE:
if (getBindings(GLOBAL_SCOPE) != null) {
return getBindings(GLOBAL_SCOPE).remove(name);
}
return null;
default:
throw new IllegalArgumentException("Illegal scope value.");
}
}
/**
* Sets the value of an attribute in a given scope.
*
* @param name The name of the attribute to set
* @param value The value of the attribute
* @param scope The scope in which to set the attribute
*
* @throws IllegalArgumentException
* if the name is empty or if the scope is invalid.
* @throws NullPointerException if the name is null.
*/
public void setAttribute(String name, Object value, int scope) {
checkName(name);
switch (scope) {
case ENGINE_SCOPE:
engineScope.put(name, value);
return;
case GLOBAL_SCOPE:
if (globalScope != null) {
globalScope.put(name, value);
}
return;
default:
throw new IllegalArgumentException("Illegal scope value.");
}
}
/** {@inheritDoc} */
public Writer getWriter() {
return writer;
}
/** {@inheritDoc} */
public Reader getReader() {
return reader;
}
/** {@inheritDoc} */
public void setReader(Reader reader) {
this.reader = reader;
}
/** {@inheritDoc} */
public void setWriter(Writer writer) {
this.writer = writer;
}
/** {@inheritDoc} */
public Writer getErrorWriter() {
return errorWriter;
}
/** {@inheritDoc} */
public void setErrorWriter(Writer writer) {
this.errorWriter = writer;
}
/**
* Get the lowest scope in which an attribute is defined.
* @param name Name of the attribute
* .
* @return The lowest scope. Returns -1 if no attribute with the given
* name is defined in any scope.
* @throws NullPointerException if name is null.
* @throws IllegalArgumentException if name is empty.
*/
public int getAttributesScope(String name) {
checkName(name);
if (engineScope.containsKey(name)) {
return ENGINE_SCOPE;
} else if (globalScope != null && globalScope.containsKey(name)) {
return GLOBAL_SCOPE;
} else {
return -1;
}
}
/**
* Returns the value of the <code>engineScope</code> field if specified scope is
* <code>ENGINE_SCOPE</code>. Returns the value of the <code>globalScope</code> field if the specified scope is
* <code>GLOBAL_SCOPE</code>.
*
* @param scope The specified scope
* @return The value of either the <code>engineScope</code> or <code>globalScope</code> field.
* @throws IllegalArgumentException if the value of scope is invalid.
*/
public Bindings getBindings(int scope) {
if (scope == ENGINE_SCOPE) {
return engineScope;
} else if (scope == GLOBAL_SCOPE) {
return globalScope;
} else {
throw new IllegalArgumentException("Illegal scope value.");
}
}
/** {@inheritDoc} */
public List<Integer> getScopes() {
return scopes;
}
private void checkName(String name) {
Objects.requireNonNull(name);
if (name.isEmpty()) {
throw new IllegalArgumentException("name cannot be empty");
}
}
private static List<Integer> scopes;
static {
scopes = new ArrayList<Integer>(2);
scopes.add(ENGINE_SCOPE);
scopes.add(GLOBAL_SCOPE);
scopes = Collections.unmodifiableList(scopes);
}
}