src/java.scripting/share/classes/javax/script/ScriptEngineManager.java
author michaelm
Wed, 29 Nov 2017 16:59:38 +0000
branchhttp-client-branch
changeset 55912 dfa9489d1cb1
parent 47216 71c04702a3d5
child 48720 290b480df13e
permissions -rw-r--r--
http-client-branch: fixed Http2TestServerConnection problem
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();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        engineSpis = new HashSet<ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        nameAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        extensionAssociations = new HashMap<String, ScriptEngineFactory>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
23922
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    84
        initEngines(loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    85
    }
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    86
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    87
    private ServiceLoader<ScriptEngineFactory> getServiceLoader(final ClassLoader loader) {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    88
        if (loader != null) {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    89
            return ServiceLoader.load(ScriptEngineFactory.class, loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    90
        } else {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    91
            return ServiceLoader.loadInstalled(ScriptEngineFactory.class);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    92
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private void initEngines(final ClassLoader loader) {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
    96
        Iterator<ScriptEngineFactory> itr = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
23922
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    98
            ServiceLoader<ScriptEngineFactory> sl = AccessController.doPrivileged(
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
    99
                new PrivilegedAction<ServiceLoader<ScriptEngineFactory>>() {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   100
                    @Override
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   101
                    public ServiceLoader<ScriptEngineFactory> run() {
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   102
                        return getServiceLoader(loader);
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   103
                    }
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   104
                });
f361e6f7d154 8036794: Collect more Collector Lambdas
sundar
parents: 23010
diff changeset
   105
11124
5fdabdc21259 7116957: javax.script.ScriptEngineManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 11119
diff changeset
   106
            itr = sl.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            System.err.println("Can't find ScriptEngineFactory providers: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                          err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            while (itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                try {
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 5506
diff changeset
   122
                    ScriptEngineFactory fact = itr.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    engineSpis.add(fact);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    System.err.println("ScriptEngineManager providers.next(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                 + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    // one factory failed, but check other factories...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (ServiceConfigurationError err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.err.println("ScriptEngineManager providers.hasNext(): "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            + err.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                err.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // do not throw any exception here. user may want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // manage his/her own factories using this manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // by explicit registratation (by registerXXX) methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <code>setBindings</code> stores the specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * in the <code>globalScope</code> field. ScriptEngineManager sets this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>Bindings</code> as global bindings for <code>ScriptEngine</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param bindings The specified <code>Bindings</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IllegalArgumentException if bindings is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void setBindings(Bindings bindings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (bindings == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException("Global scope cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        globalScope = bindings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <code>getBindings</code> returns the value of the <code>globalScope</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * ScriptEngineManager sets this <code>Bindings</code> as global bindings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <code>ScriptEngine</code> objects created by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @return The globalScope field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public Bindings getBindings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Sets the specified key/value pair in the Global Scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param key Key to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param value Value to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws NullPointerException if key is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws IllegalArgumentException if key is empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void put(String key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        globalScope.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Gets the value for the specified key in the Global Scope
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param key The key whose value is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return The value for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Object get(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return globalScope.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Looks up and creates a <code>ScriptEngine</code> for a given  name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The algorithm first searches for a <code>ScriptEngineFactory</code> that has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * registered as a handler for the specified name using the <code>registerEngineName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * method.
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
   200
     * <br><br> If one is not found, it searches the set of <code>ScriptEngineFactory</code> instances
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * stored by the constructor for one with the specified name.  If a <code>ScriptEngineFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * is found by either method, it is used to create instance of <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param shortName The short name of the <code>ScriptEngine</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * returned by the <code>getNames</code> method of its <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return A <code>ScriptEngine</code> created by the factory located in the search.  Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * if no such factory was found.  The <code>ScriptEngineManager</code> sets its own <code>globalScope</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <code>Bindings</code> as the <code>GLOBAL_SCOPE</code> <code>Bindings</code> of the newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * created <code>ScriptEngine</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws NullPointerException if shortName is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public ScriptEngine getEngineByName(String shortName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (shortName == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        //look for registered name first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (null != (obj = nameAssociations.get(shortName))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            List<String> names = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                names = spi.getNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (names != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                for (String name : names) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    if (shortName.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            if (DEBUG) exp.printStackTrace();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Look up and create a <code>ScriptEngine</code> for a given extension.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * given extension using <code>registerEngineExtension</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param extension The given extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return The engine to handle scripts with this extension.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws NullPointerException if extension is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public ScriptEngine getEngineByExtension(String extension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (extension == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        //look for registered extension first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (null != (obj = extensionAssociations.get(extension))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            List<String> exts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                exts = spi.getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (exts == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            for (String ext : exts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (extension.equals(ext)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        if (DEBUG) exp.printStackTrace();
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Look up and create a <code>ScriptEngine</code> for a given mime type.  The algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * used by <code>getEngineByName</code> is used except that the search starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * by looking for a <code>ScriptEngineFactory</code> registered to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * given mime type using <code>registerEngineMimeType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param mimeType The given mime type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return The engine to handle scripts with this mime type.  Returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws NullPointerException if mimeType is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public ScriptEngine getEngineByMimeType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (mimeType == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        //look for registered types first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (null != (obj = mimeTypeAssociations.get(mimeType))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ScriptEngineFactory spi = (ScriptEngineFactory)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            List<String> types = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                types = spi.getMimeTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (DEBUG) exp.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (types == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (String type : types) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (mimeType.equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        ScriptEngine engine = spi.getScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        return engine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    } catch (Exception exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        if (DEBUG) exp.printStackTrace();
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
12037
11e6e438f738 7149785: Minor corrections to ScriptEngineManager javadoc
ptisnovs
parents: 11124
diff changeset
   349
     * Returns a list whose elements are instances of all the <code>ScriptEngineFactory</code> classes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * found by the discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return List of all discovered <code>ScriptEngineFactory</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public List<ScriptEngineFactory> getEngineFactories() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        List<ScriptEngineFactory> res = new ArrayList<ScriptEngineFactory>(engineSpis.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (ScriptEngineFactory spi : engineSpis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            res.add(spi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return Collections.unmodifiableList(res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Registers a <code>ScriptEngineFactory</code> to handle a language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * name.  Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param name The name to be associated with the <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param factory The class to associate with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void registerEngineName(String name, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (name == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        nameAssociations.put(name, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Registers a <code>ScriptEngineFactory</code> to handle a mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param type The mime type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param factory The class to associate with the given mime type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public void registerEngineMimeType(String type, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (type == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        mimeTypeAssociations.put(type, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Registers a <code>ScriptEngineFactory</code> to handle an extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Overrides any such association found using the Discovery mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param extension The extension type  to be associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>ScriptEngineFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param factory The class to associate with the given extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @throws NullPointerException if any of the parameters is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public void registerEngineExtension(String extension, ScriptEngineFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (extension == null || factory == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        extensionAssociations.put(extension, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /** Set of script engine factories discovered. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private HashSet<ScriptEngineFactory> engineSpis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /** Map of engine name to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private HashMap<String, ScriptEngineFactory> nameAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /** Map of script file extension to script engine factory. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private HashMap<String, ScriptEngineFactory> extensionAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27565
diff changeset
   411
    /** Map of script MIME type to script engine factory. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private HashMap<String, ScriptEngineFactory> mimeTypeAssociations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /** Global bindings associated with script engines created by this manager. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private Bindings globalScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
}