nashorn/src/jdk/nashorn/api/scripting/NashornScriptEngineFactory.java
author sundar
Tue, 12 Mar 2013 18:12:42 +0530
changeset 16522 d643e3ee819c
parent 16199 3722d034c582
child 16525 1409942e618e
permissions -rw-r--r--
8009757: Package access clean up and refactoring Reviewed-by: jlaskey, lagergren, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.api.scripting;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import java.util.Arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import java.util.Collections;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.util.List;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import javax.script.ScriptEngine;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import javax.script.ScriptEngineFactory;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import jdk.nashorn.internal.runtime.Version;
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
    34
import sun.reflect.Reflection;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
 * JSR-223 compliant script engine factory for Nashorn. The engine answers for:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 * <ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 * <li>names {@code "nashorn"}, {@code "Nashorn"}, {@code "js"}, {@code "JS"}, {@code "JavaScript"},
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
 * {@code "javascript"}, {@code "ECMAScript"}, and {@code "ecmascript"};</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
 * <li>MIME types {@code "application/javascript"}, {@code "application/ecmascript"}, {@code "text/javascript"}, and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
 * {@code "text/ecmascript"};</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
 * <li>as well as for the extension {@code "js"}.</li>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
 * </ul>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
 * Programs executing in engines created using {@link #getScriptEngine(String[])} will have the passed arguments
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
 * accessible as a global variable named {@code "arguments"}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
public final class NashornScriptEngineFactory implements ScriptEngineFactory {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
    public String getEngineName() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
        return (String) getParameter(ScriptEngine.ENGINE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
    public String getEngineVersion() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
        return (String) getParameter(ScriptEngine.ENGINE_VERSION);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
    public List<String> getExtensions() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
        return Collections.unmodifiableList(extensions);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
    public String getLanguageName() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
        return (String) getParameter(ScriptEngine.LANGUAGE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
    public String getLanguageVersion() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
        return (String) getParameter(ScriptEngine.LANGUAGE_VERSION);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
    public String getMethodCallSyntax(final String obj, final String method, final String... args) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
        final StringBuilder sb = new StringBuilder().append(obj).append('.').append(method).append('(');
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
        final int len = args.length;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
        if (len > 0) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
            sb.append(args[0]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
        for (int i = 1; i < len; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
            sb.append(',').append(args[i]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
        sb.append(')');
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
    public List<String> getMimeTypes() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
        return Collections.unmodifiableList(mimeTypes);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
    public List<String> getNames() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
        return Collections.unmodifiableList(names);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
    public String getOutputStatement(final String toDisplay) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
        return "print(" + toDisplay + ")";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    public Object getParameter(final String key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
        switch (key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
        case ScriptEngine.NAME:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
            return "javascript";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
        case ScriptEngine.ENGINE:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
            return "Oracle Nashorn";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
        case ScriptEngine.ENGINE_VERSION:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
            return Version.version();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
        case ScriptEngine.LANGUAGE:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
            return "ECMAScript";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
        case ScriptEngine.LANGUAGE_VERSION:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
            return "ECMA - 262 Edition 5.1";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
        case "THREADING":
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
            // The engine implementation is not thread-safe. Can't be
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
            // used to execute scripts concurrently on multiple threads.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
            throw new IllegalArgumentException("Invalid key");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
    public String getProgram(final String... statements) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
        final StringBuilder sb = new StringBuilder();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
        for (final String statement : statements) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
            sb.append(statement).append(';');
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
        return sb.toString();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    public ScriptEngine getScriptEngine() {
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   140
        return new NashornScriptEngine(this, getAppClassLoader());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
    /**
16199
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   144
     * Create a new Script engine initialized by given class loader.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   145
     *
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   146
     * @param appLoader class loader to be used as script "app" class loader.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   147
     * @return newly created script engine.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   148
     */
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   149
    public ScriptEngine getScriptEngine(final ClassLoader appLoader) {
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   150
        checkConfigPermission();
16199
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   151
        return new NashornScriptEngine(this, appLoader);
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   152
    }
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   153
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   154
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
     * Create a new Script engine initialized by given arguments.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
     * @param args arguments array passed to script engine.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
     * @return newly created script engine.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
    public ScriptEngine getScriptEngine(final String[] args) {
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   161
        checkConfigPermission();
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   162
        return new NashornScriptEngine(this, args, getAppClassLoader());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
16199
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   165
    /**
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   166
     * Create a new Script engine initialized by given arguments.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   167
     *
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   168
     * @param args arguments array passed to script engine.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   169
     * @param appLoader class loader to be used as script "app" class loader.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   170
     * @return newly created script engine.
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   171
     */
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   172
    public ScriptEngine getScriptEngine(final String[] args, final ClassLoader appLoader) {
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   173
        checkConfigPermission();
16199
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   174
        return new NashornScriptEngine(this, args, appLoader);
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   175
    }
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   176
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
    // -- Internals only below this point
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
16522
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   179
    private void checkConfigPermission() {
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   180
        final SecurityManager sm = System.getSecurityManager();
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   181
        if (sm != null) {
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   182
            sm.checkPermission(new RuntimePermission("nashorn.setConfig"));
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   183
        }
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   184
    }
d643e3ee819c 8009757: Package access clean up and refactoring
sundar
parents: 16199
diff changeset
   185
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
    private static final List<String> names;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
    private static final List<String> mimeTypes;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
    private static final List<String> extensions;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
    static {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
        names = immutableList(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
                    "nashorn", "Nashorn",
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
                    "js", "JS",
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
                    "JavaScript", "javascript",
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
                    "ECMAScript", "ecmascript"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
                );
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
        mimeTypes = immutableList(
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
                        "application/javascript",
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
                        "application/ecmascript",
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
                        "text/javascript",
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
                        "text/ecmascript"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
                    );
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        extensions = immutableList("js");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
    private static List<String> immutableList(final String... elements) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
        return Collections.unmodifiableList(Arrays.asList(elements));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
    }
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   211
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   212
    private static ClassLoader getAppClassLoader() {
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   213
        if (System.getSecurityManager() == null) {
16199
3722d034c582 8007091: Provide private API to pass application class loader for nashorn script engine
sundar
parents: 16197
diff changeset
   214
            return Thread.currentThread().getContextClassLoader();
16197
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   215
        }
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   216
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   217
        // Try to determine the caller class loader. Use that if it can be
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   218
        // found. If not, use the class loader of nashorn itself as the
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   219
        // "application" class loader for scripts.
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   220
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   221
        // User could have called ScriptEngineFactory.getScriptEngine()
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   222
        //
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   223
        // <caller>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   224
        //  <factory.getScriptEngine()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   225
        //   <factory.getAppClassLoader()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   226
        //    <Reflection.getCallerClass()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   227
        //
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   228
        // or used one of the getEngineByABC methods of ScriptEngineManager.
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   229
        //
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   230
        // <caller>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   231
        //  <ScriptEngineManager.getEngineByName()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   232
        //   <factory.getScriptEngine()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   233
        //    <factory.getAppClassLoader()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   234
        //     <Reflection.getCallerClass()>
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   235
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   236
        // So, stack depth is 3 or 4 (recall it is zero based). We try
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   237
        // stack depths 3, 4 and look for non-bootstrap caller.
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   238
        Class<?> caller = null;
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   239
        for (int depth = 3; depth < 5; depth++) {
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   240
            caller = Reflection.getCallerClass(depth);
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   241
            if (caller != null && caller.getClassLoader() != null) {
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   242
                // found a non-bootstrap caller
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   243
                break;
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   244
            }
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   245
        }
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   246
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   247
        final ClassLoader ccl = (caller == null)? null : caller.getClassLoader();
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   248
        // if caller loader is null, then use nashorn's own loader
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   249
        return (ccl == null)? NashornScriptEngineFactory.class.getClassLoader() : ccl;
1a5414cce91a 8007004: nashorn script engine should not use thread context class loader as script 'application loader'
sundar
parents: 16151
diff changeset
   250
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
}