src/java.scripting/share/classes/javax/script/ScriptEngineManager.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 48904 3ae9318001f8
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
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
34949
304424bbae03 8068938: javax.script package description should specify use of ServiceLoader
sundar
parents: 28059
diff changeset
    36
 * by the Manager. This class uses the service provider mechanism described in the
304424bbae03 8068938: javax.script package description should specify use of ServiceLoader
sundar
parents: 28059
diff changeset
    37
 * {@link java.util.ServiceLoader} class to enumerate all the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * implementations of <code>ScriptEngineFactory</code>. <br><br>
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
    39
 * The <code>ScriptEngineManager</code> provides a method to return a list of all these factories
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * as well as utility methods which look up factories on the basis of language name, file extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The <code>Bindings</code> of key/value pairs, referred to as the "Global Scope"  maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by the manager is available to all instances of <code>ScriptEngine</code> created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * by the <code>ScriptEngineManager</code>.  The values in the <code>Bindings</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * generally exposed in all scripts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author A. Sundararajan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class ScriptEngineManager  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
18233
1b37223926e5 8006611: Improve scripting
sundar
parents: 16906
diff changeset
    55
     * The effect of calling this constructor is the same as calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * <code>ScriptEngineManager(Thread.currentThread().getContextClassLoader())</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @see java.lang.Thread#getContextClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public ScriptEngineManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        ClassLoader ctxtLoader = Thread.currentThread().getContextClassLoader();
18233
1b37223926e5 8006611: Improve scripting
sundar
parents: 16906
diff changeset
    62
        init(ctxtLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * This constructor loads the implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>ScriptEngineFactory</code> visible to the given
34949
304424bbae03 8068938: javax.script package description should specify use of ServiceLoader
sundar
parents: 28059
diff changeset
    68
     * <code>ClassLoader</code> using the service provider mechanism.<br><br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * If loader is <code>null</code>, the script engine factories that are
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    70
     * bundled with the platform are loaded. <br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param loader ClassLoader used to discover script engine factories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public ScriptEngineManager(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        init(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private void init(final ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        globalScope = new SimpleBindings();
48904
3ae9318001f8 8196959: NullPointerException in discovery003.java
sdama
parents: 48720
diff changeset
    80
        engineSpis = new TreeSet<ScriptEngineFactory>(Comparator.comparing(
3ae9318001f8 8196959: NullPointerException in discovery003.java
sdama
parents: 48720
diff changeset
    81
            ScriptEngineFactory::getEngineName,
3ae9318001f8 8196959: NullPointerException in discovery003.java
sdama
parents: 48720
diff changeset
    82
            Comparator.nullsLast(Comparator.naturalOrder()))
3ae9318001f8 8196959: NullPointerException in discovery003.java
sdama
parents: 48720
diff changeset
    83
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        nameAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        extensionAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
23922
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    87
        initEngines(loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    88
    }
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    89
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    90
    private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    91
        if (loader != null) {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    92
            return ServiceLoader.load(ScriptEngineFactory.class, loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    93
        } else {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    94
            return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    95
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private void initEngines(final ClassLoader loader) {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
    99
        Iterator<ScriptEngineFactory> itr = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
23922
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   101
            ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   102
                new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   103
                    @Override
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   104
                    public ServiceLoader<ScriptEngineFactory> run() {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   105
                        return getServiceLoader(loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   106
                    }
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   107
                });
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   108
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   109
            itr = sl.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.err.println("Can't find ScriptEngineFactory providers: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                          err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                try {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   125
                    ScriptEngineFactory fact = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    engineSpis.add(fact);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    System.err.println("ScriptEngineManager providers.next(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                 + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    // one factory failed, but check other factories...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            System.err.println("ScriptEngineManager providers.hasNext(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                            + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <code>setBindings</code> stores the specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * in the <code>globalScope</code> field. ScriptEngineManager sets this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param bindings The specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws IllegalArgumentException if bindings is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void setBindings(Bindings bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new IllegalArgumentException("Global scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>getBindings</code> returns the value of the <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * ScriptEngineManager sets this <code>Bindings</code> as global bindings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <code>ScriptEngine</code> objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return The globalScope field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public Bindings getBindings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Sets the specified key/value pair in the Global Scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param key Key to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param value Value to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @throws NullPointerException if key is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws IllegalArgumentException if key is empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void put(String key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        globalScope.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Gets the value for the specified key in the Global Scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param key The key whose value is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @return The value for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public Object get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return globalScope.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Looks up and creates a <code>ScriptEngine</code> for a given  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * registered as a handler for the specified name using the <code>registerEngineName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * method.
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
   203
     * <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * stored by the constructor for one with the specified name.  If a <code>ScriptEngineFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * is found by either method, it is used to create instance of <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param shortName The short name of the <code>ScriptEngine</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return A <code>ScriptEngine</code> created by the factory located in the search.  Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * if no such factory was found.  The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * created <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws NullPointerException if shortName is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public ScriptEngine getEngineByName(String shortName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (shortName == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        //look for registered name first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (null != (obj = nameAssociations.get(shortName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            List<String> names = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                names = spi.getNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                for (String name : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    if (shortName.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
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
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Look up and create a <code>ScriptEngine</code> for a given extension.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * given extension using <code>registerEngineExtension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param extension The given extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return The engine to handle scripts with this extension.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws NullPointerException if extension is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public ScriptEngine getEngineByExtension(String extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (extension == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        //look for registered extension first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (null != (obj = extensionAssociations.get(extension))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            List<String> exts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                exts = spi.getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (exts == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            for (String ext : exts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if (extension.equals(ext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    }
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
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Look up and create a <code>ScriptEngine</code> for a given mime type.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * given mime type using <code>registerEngineMimeType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param mimeType The given mime type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return The engine to handle scripts with this mime type.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @throws NullPointerException if mimeType is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public ScriptEngine getEngineByMimeType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (mimeType == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        //look for registered types first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (null != (obj = mimeTypeAssociations.get(mimeType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            List<String> types = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                types = spi.getMimeTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (types == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            for (String type : types) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (mimeType.equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
   352
     * Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * found by the discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return List of all discovered <code>ScriptEngineFactory</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public List<ScriptEngineFactory> getEngineFactories() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            res.add(spi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return Collections.unmodifiableList(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Registers a <code>ScriptEngineFactory</code> to handle a language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * name.  Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param factory The class to associate with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public void registerEngineName(String name, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (name == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        nameAssociations.put(name, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Registers a <code>ScriptEngineFactory</code> to handle a mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param type The mime type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param factory The class to associate with the given mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (type == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        mimeTypeAssociations.put(type, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Registers a <code>ScriptEngineFactory</code> to handle an extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param extension The extension type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param factory The class to associate with the given extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (extension == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        extensionAssociations.put(extension, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /** Set of script engine factories discovered. */
48720
290b480df13e 8011697: ScriptEngine "js" randomly means either "rhino" or "nashorn", but should instead select one
sdama
parents: 47216
diff changeset
   406
    private TreeSet<ScriptEngineFactory> engineSpis;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /** Map of engine name to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private HashMap<String, ScriptEngineFactory> nameAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /** Map of script file extension to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private HashMap<String, ScriptEngineFactory> extensionAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27565
diff changeset
   414
    /** Map of script MIME type to script engine factory. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /** Global bindings associated with script engines created by this manager. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private Bindings globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
}