classes and also maintains a
collection of key/value pairs storing state shared by all engines created
by the Manager. This class uses the
.
provides a method to return an array of all these factories
as well as utility methods which look up factories on the basis of language name, file extension
and mime type.
| Method from javax.script.ScriptEngineManager Detail: |
public Object get(String key) {
return globalScope.get(key);
}
Gets the value for the specified key in the Global Scope |
public Bindings getBindings() {
return globalScope;
}
getBindings returns the value of the globalScope field.
ScriptEngineManager sets this Bindings as global bindings for
ScriptEngine objects created by it.
|
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 ScriptEngine for a given extension. The algorithm
used by getEngineByName is used except that the search starts
by looking for a ScriptEngineFactory registered to handle the
given extension using registerEngineExtension. |
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;
}
Look up and create a ScriptEngine for a given mime type. The algorithm
used by getEngineByName is used except that the search starts
by looking for a ScriptEngineFactory registered to handle the
given mime type using registerEngineMimeType. |
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;
}
Looks up and creates a ScriptEngine for a given name.
The algorithm first searches for a ScriptEngineFactory that has been
registered as a handler for the specified name using the registerEngineName
method.
If one is not found, it searches the array of ScriptEngineFactory instances
stored by the constructor for one with the specified name. If a ScriptEngineFactory
is found by either method, it is used to create instance of ScriptEngine. |
public List getEngineFactories() {
List< ScriptEngineFactory > res = new ArrayList< ScriptEngineFactory >(engineSpis.size());
for (ScriptEngineFactory spi : engineSpis) {
res.add(spi);
}
return Collections.unmodifiableList(res);
}
Returns an array whose elements are instances of all the ScriptEngineFactory classes
found by the discovery mechanism. |
public void put(String key,
Object value) {
globalScope.put(key, value);
}
Sets the specified key/value pair in the Global Scope. |
public void registerEngineExtension(String extension,
ScriptEngineFactory factory) {
if (extension == null || factory == null) throw new NullPointerException();
extensionAssociations.put(extension, factory);
}
Registers a ScriptEngineFactory to handle an extension.
Overrides any such association found using the Discovery mechanism. |
public void registerEngineMimeType(String type,
ScriptEngineFactory factory) {
if (type == null || factory == null) throw new NullPointerException();
mimeTypeAssociations.put(type, factory);
}
Registers a ScriptEngineFactory to handle a mime type.
Overrides any such association found using the Discovery mechanism. |
public void registerEngineName(String name,
ScriptEngineFactory factory) {
if (name == null || factory == null) throw new NullPointerException();
nameAssociations.put(name, factory);
}
Registers a ScriptEngineFactory to handle a language
name. Overrides any such association found using the Discovery mechanism. |
public void setBindings(Bindings bindings) {
if (bindings == null) {
throw new IllegalArgumentException("Global scope cannot be null.");
}
globalScope = bindings;
}
setBindings stores the specified Bindings
in the globalScope field. ScriptEngineManager sets this
Bindings as global bindings for ScriptEngine
objects created by it.
|