jdk/src/java.scripting/share/classes/javax/script/ScriptEngineManager.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 27565 729f9700483a
child 34949 304424bbae03
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18233
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.script;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    29
import java.util.ServiceLoader;
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
    30
import java.util.ServiceConfigurationError;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>ScriptEngineManager</code> implements a discovery and instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * mechanism for <code>ScriptEngine</code> classes and also maintains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * collection of key/value pairs storing state shared by all engines created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * by the Manager. This class uses the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism to enumerate all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implementations of <code>ScriptEngineFactory</code>. <br><br>
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
    38
 * The <code>ScriptEngineManager</code> provides a method to return a list of all these factories
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * as well as utility methods which look up factories on the basis of language name, file extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope"  maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * by the manager is available to all instances of <code>ScriptEngine</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by the <code>ScriptEngineManager</code>.  The values in the <code>Bindings</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * generally exposed in all scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author A. Sundararajan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class ScriptEngineManager  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
18233
1b37223926e5 8006611: Improve scripting
sundar
parents: 16906
diff changeset
    54
     * The effect of calling this constructor is the same as calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @see java.lang.Thread#getContextClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public ScriptEngineManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
18233
1b37223926e5 8006611: Improve scripting
sundar
parents: 16906
diff changeset
    61
        init(ctxtLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * This constructor loads the implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <code>ScriptEngineFactory</code> visible to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>ClassLoader</code> using the <a href="../../../technotes/guides/jar/jar.html#Service%20Provider">service provider</a> mechanism.<br><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * If loader is <code>null</code>, the script engine factories that are
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    69
     * bundled with the platform are loaded. <br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param loader ClassLoader used to discover script engine factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public ScriptEngineManager(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        init(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private void init(final ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        globalScope = new SimpleBindings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        engineSpis = new HashSet<ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        nameAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        extensionAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
23922
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    83
        initEngines(loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    84
    }
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    85
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    86
    private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    87
        if (loader != null) {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    88
            return ServiceLoader.load(ScriptEngineFactory.class, loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    89
        } else {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    90
            return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    91
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void initEngines(final ClassLoader loader) {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
    95
        Iterator<ScriptEngineFactory> itr = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        try {
23922
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    97
            ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    98
                new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    99
                    @Override
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   100
                    public ServiceLoader<ScriptEngineFactory> run() {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   101
                        return getServiceLoader(loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   102
                    }
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   103
                });
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   104
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   105
            itr = sl.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.err.println("Can't find ScriptEngineFactory providers: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                          err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                try {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   121
                    ScriptEngineFactory fact = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    engineSpis.add(fact);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    System.err.println("ScriptEngineManager providers.next(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                 + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    // one factory failed, but check other factories...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            System.err.println("ScriptEngineManager providers.hasNext(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <code>setBindings</code> stores the specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * in the <code>globalScope</code> field. ScriptEngineManager sets this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param bindings The specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws IllegalArgumentException if bindings is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void setBindings(Bindings bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalArgumentException("Global scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>getBindings</code> returns the value of the <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * ScriptEngineManager sets this <code>Bindings</code> as global bindings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>ScriptEngine</code> objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return The globalScope field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Bindings getBindings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Sets the specified key/value pair in the Global Scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param key Key to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param value Value to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws NullPointerException if key is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws IllegalArgumentException if key is empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void put(String key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        globalScope.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Gets the value for the specified key in the Global Scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param key The key whose value is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return The value for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Object get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return globalScope.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Looks up and creates a <code>ScriptEngine</code> for a given  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * registered as a handler for the specified name using the <code>registerEngineName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * method.
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
   199
     * <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * stored by the constructor for one with the specified name.  If a <code>ScriptEngineFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * is found by either method, it is used to create instance of <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param shortName The short name of the <code>ScriptEngine</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return A <code>ScriptEngine</code> created by the factory located in the search.  Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * if no such factory was found.  The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * created <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @throws NullPointerException if shortName is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public ScriptEngine getEngineByName(String shortName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (shortName == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        //look for registered name first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (null != (obj = nameAssociations.get(shortName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            List<String> names = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                names = spi.getNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                for (String name : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    if (shortName.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Look up and create a <code>ScriptEngine</code> for a given extension.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * given extension using <code>registerEngineExtension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param extension The given extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return The engine to handle scripts with this extension.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws NullPointerException if extension is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public ScriptEngine getEngineByExtension(String extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (extension == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        //look for registered extension first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (null != (obj = extensionAssociations.get(extension))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            List<String> exts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                exts = spi.getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (exts == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            for (String ext : exts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (extension.equals(ext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Look up and create a <code>ScriptEngine</code> for a given mime type.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * given mime type using <code>registerEngineMimeType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param mimeType The given mime type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return The engine to handle scripts with this mime type.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @throws NullPointerException if mimeType is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public ScriptEngine getEngineByMimeType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (mimeType == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        //look for registered types first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (null != (obj = mimeTypeAssociations.get(mimeType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            List<String> types = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                types = spi.getMimeTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (types == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            for (String type : types) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (mimeType.equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
   348
     * Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * found by the discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return List of all discovered <code>ScriptEngineFactory</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public List<ScriptEngineFactory> getEngineFactories() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            res.add(spi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return Collections.unmodifiableList(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Registers a <code>ScriptEngineFactory</code> to handle a language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * name.  Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param factory The class to associate with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void registerEngineName(String name, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (name == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        nameAssociations.put(name, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Registers a <code>ScriptEngineFactory</code> to handle a mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param type The mime type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param factory The class to associate with the given mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (type == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        mimeTypeAssociations.put(type, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Registers a <code>ScriptEngineFactory</code> to handle an extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param extension The extension type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param factory The class to associate with the given extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (extension == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        extensionAssociations.put(extension, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /** Set of script engine factories discovered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private HashSet<ScriptEngineFactory> engineSpis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /** Map of engine name to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private HashMap<String, ScriptEngineFactory> nameAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /** Map of script file extension to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private HashMap<String, ScriptEngineFactory> extensionAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27565
diff changeset
   410
    /** Map of script MIME type to script engine factory. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /** Global bindings associated with script engines created by this manager. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private Bindings globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
}