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