src/java.scripting/share/classes/com/sun/tools/script/shell/Main.java
author sspitsyn
Fri, 25 May 2018 00:18:47 -0700
changeset 50263 42165815ee40
parent 48543 7067fe4e054e
child 52902 e3398b2e1ab0
permissions -rw-r--r--
8203816: add tests failed because of 8203174 to the ProblemList-graal Summary: add tests to the ProblemList-graal.txt Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     4
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    10
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    15
 * accompanied this code).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    16
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    20
 *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    23
 * questions.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    24
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    25
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    26
package com.sun.tools.script.shell;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    27
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    28
import java.io.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    29
import java.net.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    30
import java.text.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    31
import java.util.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    32
import javax.script.*;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    33
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    34
/**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    35
 * This is the main class for Java script shell.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    36
 */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    37
public class Main {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    38
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    39
     * main entry point to the command line tool
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    40
     * @param args command line argument array
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    41
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    42
    public static void main(String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    43
        // parse command line options
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    44
        String[] scriptArgs = processOptions(args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    45
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    46
        // process each script command
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    47
        for (Command cmd : scripts) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    48
            cmd.run(scriptArgs);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    49
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    50
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    51
        System.exit(EXIT_SUCCESS);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    52
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    53
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    54
    // Each -e or -f or interactive mode is represented
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    55
    // by an instance of Command.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    56
    private static interface Command {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    57
        public void run(String[] arguments);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    58
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    59
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    60
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    61
     * Parses and processes command line options.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    62
     * @param args command line argument array
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    63
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    64
    private static String[] processOptions(String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    65
        // current scripting language selected
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    66
        String currentLanguage = DEFAULT_LANGUAGE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    67
        // current script file encoding selected
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    68
        String currentEncoding = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    69
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    70
        // check for -classpath or -cp first
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    71
        checkClassPath(args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    72
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    73
        // have we seen -e or -f ?
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    74
        boolean seenScript = false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    75
        // have we seen -f - already?
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    76
        boolean seenStdin = false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    77
        for (int i=0; i < args.length; i++) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    78
            String arg = args[i];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    79
            if (arg.equals("-classpath") ||
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    80
                    arg.equals("-cp")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    81
                // handled already, just continue
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    82
                i++;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    83
                continue;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    84
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    85
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    86
            // collect non-option arguments and pass these as script arguments
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    87
            if (!arg.startsWith("-")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    88
                int numScriptArgs;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    89
                int startScriptArg;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    90
                if (seenScript) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    91
                    // if we have seen -e or -f already all non-option arguments
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    92
                    // are passed as script arguments
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    93
                    numScriptArgs = args.length - i;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    94
                    startScriptArg = i;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    95
                } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    96
                    // if we have not seen -e or -f, first non-option argument
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    97
                    // is treated as script file name and rest of the non-option
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    98
                    // arguments are passed to script as script arguments
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
    99
                    numScriptArgs = args.length - i - 1;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   100
                    startScriptArg = i + 1;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   101
                    ScriptEngine se = getScriptEngine(currentLanguage);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   102
                    addFileSource(se, args[i], currentEncoding);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   103
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   104
                // collect script arguments and return to main
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   105
                String[] result = new String[numScriptArgs];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   106
                System.arraycopy(args, startScriptArg, result, 0, numScriptArgs);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   107
                return result;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   108
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   109
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   110
            if (arg.startsWith("-D")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   111
                String value = arg.substring(2);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   112
                int eq = value.indexOf('=');
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   113
                if (eq != -1) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   114
                    System.setProperty(value.substring(0, eq),
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   115
                            value.substring(eq + 1));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   116
                } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   117
                    if (!value.equals("")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   118
                        System.setProperty(value, "");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   119
                    } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   120
                        // do not allow empty property name
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   121
                        usage(EXIT_CMD_NO_PROPNAME);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   122
                    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   123
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   124
                continue;
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   125
            } else if (arg.equals("-?") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   126
                       arg.equals("-h") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   127
                       arg.equals("--help") ||
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   128
                       // -help: legacy.
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
   129
                       arg.equals("-help")) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   130
                usage(EXIT_SUCCESS);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   131
            } else if (arg.equals("-e")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   132
                seenScript = true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   133
                if (++i == args.length)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   134
                    usage(EXIT_CMD_NO_SCRIPT);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   135
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   136
                ScriptEngine se = getScriptEngine(currentLanguage);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   137
                addStringSource(se, args[i]);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   138
                continue;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   139
            } else if (arg.equals("-encoding")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   140
                if (++i == args.length)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   141
                    usage(EXIT_CMD_NO_ENCODING);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   142
                currentEncoding = args[i];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   143
                continue;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   144
            } else if (arg.equals("-f")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   145
                seenScript = true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   146
                if (++i == args.length)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   147
                    usage(EXIT_CMD_NO_FILE);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   148
                ScriptEngine se = getScriptEngine(currentLanguage);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   149
                if (args[i].equals("-")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   150
                    if (seenStdin) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   151
                        usage(EXIT_MULTIPLE_STDIN);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   152
                    } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   153
                        seenStdin = true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   154
                    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   155
                    addInteractiveMode(se);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   156
                } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   157
                    addFileSource(se, args[i], currentEncoding);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   158
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   159
                continue;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   160
            } else if (arg.equals("-l")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   161
                if (++i == args.length)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   162
                    usage(EXIT_CMD_NO_LANG);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   163
                currentLanguage = args[i];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   164
                continue;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   165
            } else if (arg.equals("-q")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   166
                listScriptEngines();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   167
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   168
            // some unknown option...
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   169
            usage(EXIT_UNKNOWN_OPTION);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   170
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   171
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   172
        if (! seenScript) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   173
            ScriptEngine se = getScriptEngine(currentLanguage);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   174
            addInteractiveMode(se);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   175
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   176
        return new String[0];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   177
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   178
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   179
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   180
     * Adds interactive mode Command
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   181
     * @param se ScriptEngine to use in interactive mode.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   182
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   183
    private static void addInteractiveMode(final ScriptEngine se) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   184
        scripts.add(new Command() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   185
            public void run(String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   186
                setScriptArguments(se, args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   187
                processSource(se, "-", null);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   188
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   189
        });
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   190
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   191
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   192
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   193
     * Adds script source file Command
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   194
     * @param se ScriptEngine used to evaluate the script file
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   195
     * @param fileName script file name
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   196
     * @param encoding script file encoding
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   197
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   198
    private static void addFileSource(final ScriptEngine se,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   199
            final String fileName,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   200
            final String encoding) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   201
        scripts.add(new Command() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   202
            public void run(String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   203
                setScriptArguments(se, args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   204
                processSource(se, fileName, encoding);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   205
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   206
        });
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   207
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   208
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   209
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   210
     * Adds script string source Command
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   211
     * @param se ScriptEngine to be used to evaluate the script string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   212
     * @param source Script source string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   213
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   214
    private static void addStringSource(final ScriptEngine se,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   215
            final String source) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   216
        scripts.add(new Command() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   217
            public void run(String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   218
                setScriptArguments(se, args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   219
                String oldFile = setScriptFilename(se, "<string>");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   220
                try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   221
                    evaluateString(se, source);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   222
                } finally {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   223
                    setScriptFilename(se, oldFile);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   224
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   225
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   226
        });
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   227
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   228
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   229
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   230
     * Prints list of script engines available and exits.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   231
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   232
    private static void listScriptEngines() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   233
        List<ScriptEngineFactory> factories = engineManager.getEngineFactories();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   234
        for (ScriptEngineFactory factory: factories) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   235
            getError().println(getMessage("engine.info",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   236
                    new Object[] { factory.getLanguageName(),
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   237
                            factory.getLanguageVersion(),
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   238
                            factory.getEngineName(),
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   239
                            factory.getEngineVersion()
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   240
            }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   241
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   242
        System.exit(EXIT_SUCCESS);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   243
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   244
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   245
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   246
     * Processes a given source file or standard input.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   247
     * @param se ScriptEngine to be used to evaluate
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   248
     * @param filename file name, can be null
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   249
     * @param encoding script file encoding, can be null
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   250
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   251
    private static void processSource(ScriptEngine se, String filename,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   252
            String encoding) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   253
        if (filename.equals("-")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   254
            BufferedReader in = new BufferedReader
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   255
                    (new InputStreamReader(getIn()));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   256
            boolean hitEOF = false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   257
            String prompt = getPrompt(se);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   258
            se.put(ScriptEngine.FILENAME, "<STDIN>");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   259
            while (!hitEOF) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   260
                getError().print(prompt);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   261
                String source = "";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   262
                try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   263
                    source = in.readLine();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   264
                } catch (IOException ioe) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   265
                    getError().println(ioe.toString());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   266
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   267
                if (source == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   268
                    hitEOF = true;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   269
                    break;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   270
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   271
                Object res = evaluateString(se, source, false);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   272
                if (res != null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   273
                    res = res.toString();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   274
                    if (res == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   275
                        res = "null";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   276
                    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   277
                    getError().println(res);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   278
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   279
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   280
        } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   281
            FileInputStream fis = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   282
            try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   283
                fis = new FileInputStream(filename);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   284
            } catch (FileNotFoundException fnfe) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   285
                getError().println(getMessage("file.not.found",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   286
                        new Object[] { filename }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   287
                        System.exit(EXIT_FILE_NOT_FOUND);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   288
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   289
            evaluateStream(se, fis, filename, encoding);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   290
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   291
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   292
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   293
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   294
     * Evaluates given script source
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   295
     * @param se ScriptEngine to evaluate the string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   296
     * @param script Script source string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   297
     * @param exitOnError whether to exit the process on script error
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   298
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   299
    private static Object evaluateString(ScriptEngine se,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   300
            String script, boolean exitOnError) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   301
        try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   302
            return se.eval(script);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   303
        } catch (ScriptException sexp) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   304
            getError().println(getMessage("string.script.error",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   305
                    new Object[] { sexp.getMessage() }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   306
                    if (exitOnError)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   307
                        System.exit(EXIT_SCRIPT_ERROR);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   308
        } catch (Exception exp) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   309
            exp.printStackTrace(getError());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   310
            if (exitOnError)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   311
                System.exit(EXIT_SCRIPT_ERROR);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   312
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   313
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   314
        return null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   315
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   316
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   317
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   318
     * Evaluate script string source and exit on script error
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   319
     * @param se ScriptEngine to evaluate the string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   320
     * @param script Script source string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   321
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   322
    private static void evaluateString(ScriptEngine se, String script) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   323
        evaluateString(se, script, true);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   324
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   325
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   326
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   327
     * Evaluates script from given reader
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   328
     * @param se ScriptEngine to evaluate the string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   329
     * @param reader Reader from which is script is read
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   330
     * @param name file name to report in error.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   331
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   332
    private static Object evaluateReader(ScriptEngine se,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   333
            Reader reader, String name) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   334
        String oldFilename = setScriptFilename(se, name);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   335
        try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   336
            return se.eval(reader);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   337
        } catch (ScriptException sexp) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   338
            getError().println(getMessage("file.script.error",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   339
                    new Object[] { name, sexp.getMessage() }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   340
                    System.exit(EXIT_SCRIPT_ERROR);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   341
        } catch (Exception exp) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   342
            exp.printStackTrace(getError());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   343
            System.exit(EXIT_SCRIPT_ERROR);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   344
        } finally {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   345
            setScriptFilename(se, oldFilename);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   346
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   347
        return null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   348
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   349
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   350
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   351
     * Evaluates given input stream
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   352
     * @param se ScriptEngine to evaluate the string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   353
     * @param is InputStream from which script is read
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   354
     * @param name file name to report in error
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   355
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   356
    private static Object evaluateStream(ScriptEngine se,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   357
            InputStream is, String name,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   358
            String encoding) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   359
        BufferedReader reader = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   360
        if (encoding != null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   361
            try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   362
                reader = new BufferedReader(new InputStreamReader(is,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   363
                        encoding));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   364
            } catch (UnsupportedEncodingException uee) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   365
                getError().println(getMessage("encoding.unsupported",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   366
                        new Object[] { encoding }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   367
                        System.exit(EXIT_NO_ENCODING_FOUND);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   368
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   369
        } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   370
            reader = new BufferedReader(new InputStreamReader(is));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   371
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   372
        return evaluateReader(se, reader, name);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   373
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   374
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   375
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   376
     * Prints usage message and exits
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   377
     * @param exitCode process exit code
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   378
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   379
    private static void usage(int exitCode) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   380
        getError().println(getMessage("main.usage",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   381
                new Object[] { PROGRAM_NAME }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   382
                System.exit(exitCode);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   383
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   384
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   385
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   386
     * Gets prompt for interactive mode
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   387
     * @return prompt string to use
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   388
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   389
    private static String getPrompt(ScriptEngine se) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   390
        List<String> names = se.getFactory().getNames();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   391
        return names.get(0) + "> ";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   392
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   393
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   394
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   395
     * Get formatted, localized error message
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   396
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   397
    private static String getMessage(String key, Object[] params) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   398
        return MessageFormat.format(msgRes.getString(key), params);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   399
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   400
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   401
    // input stream from where we will read
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   402
    private static InputStream getIn() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   403
        return System.in;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   404
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   405
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   406
    // stream to print error messages
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   407
    private static PrintStream getError() {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   408
        return System.err;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   409
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   410
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   411
    // get current script engine
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   412
    private static ScriptEngine getScriptEngine(String lang) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   413
        ScriptEngine se = engines.get(lang);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   414
        if (se == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   415
            se = engineManager.getEngineByName(lang);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   416
            if (se == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   417
                getError().println(getMessage("engine.not.found",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   418
                        new Object[] { lang }));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   419
                        System.exit(EXIT_ENGINE_NOT_FOUND);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   420
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   421
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   422
            // initialize the engine
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   423
            initScriptEngine(se);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   424
            // to avoid re-initialization of engine, store it in a map
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   425
            engines.put(lang, se);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   426
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   427
        return se;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   428
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   429
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   430
    // initialize a given script engine
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   431
    private static void initScriptEngine(ScriptEngine se) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   432
        // put engine global variable
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   433
        se.put("engine", se);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   434
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   435
        // load init.<ext> file from resource
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   436
        List<String> exts = se.getFactory().getExtensions();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   437
        InputStream sysIn = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   438
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   439
        for (String ext : exts) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   440
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   441
                sysIn = Main.class.getModule().getResourceAsStream("com/sun/tools/script/shell/init." + ext);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   442
            } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   443
                throw new RuntimeException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 27565
diff changeset
   444
            }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   445
            if (sysIn != null) break;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   446
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   447
        if (sysIn != null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   448
            evaluateStream(se, sysIn, "<system-init>", null);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   449
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   450
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   451
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   452
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   453
     * Checks for -classpath, -cp in command line args. Creates a ClassLoader
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   454
     * and sets it as Thread context loader for current thread.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   455
     *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   456
     * @param args command line argument array
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   457
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   458
    private static void checkClassPath(String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   459
        String classPath = null;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   460
        for (int i = 0; i < args.length; i++) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   461
            if (args[i].equals("-classpath") ||
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   462
                    args[i].equals("-cp")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   463
                if (++i == args.length) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   464
                    // just -classpath or -cp with no value
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   465
                    usage(EXIT_CMD_NO_CLASSPATH);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   466
                } else {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   467
                    classPath = args[i];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   468
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   469
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   470
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   471
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   472
        if (classPath != null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   473
            /* We create a class loader, configure it with specified
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   474
             * classpath values and set the same as context loader.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   475
             * Note that ScriptEngineManager uses context loader to
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   476
             * load script engines. So, this ensures that user defined
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   477
             * script engines will be loaded. For classes referred
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   478
             * from scripts, Rhino engine uses thread context loader
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   479
             * but this is script engine dependent. We don't have
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   480
             * script engine independent solution anyway. Unless we
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   481
             * know the class loader used by a specific engine, we
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   482
             * can't configure correct loader.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   483
             */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   484
            URL[] urls = pathToURLs(classPath);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   485
            URLClassLoader loader = new URLClassLoader(urls);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   486
            Thread.currentThread().setContextClassLoader(loader);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   487
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   488
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   489
        // now initialize script engine manager. Note that this has to
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   490
        // be done after setting the context loader so that manager
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   491
        // will see script engines from user specified classpath
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   492
        engineManager = new ScriptEngineManager();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   493
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   494
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   495
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   496
     * Utility method for converting a search path string to an array
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   497
     * of directory and JAR file URLs.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   498
     *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   499
     * @param path the search path string
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   500
     * @return the resulting array of directory and JAR file URLs
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   501
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   502
    private static URL[] pathToURLs(String path) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   503
        String[] components = path.split(File.pathSeparator);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   504
        URL[] urls = new URL[components.length];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   505
        int count = 0;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   506
        while(count < components.length) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   507
            URL url = fileToURL(new File(components[count]));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   508
            if (url != null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   509
                urls[count++] = url;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   510
            }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   511
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   512
        if (urls.length != count) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   513
            URL[] tmp = new URL[count];
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   514
            System.arraycopy(urls, 0, tmp, 0, count);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   515
            urls = tmp;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   516
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   517
        return urls;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   518
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   519
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   520
    /**
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   521
     * Returns the directory or JAR file URL corresponding to the specified
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   522
     * local file name.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   523
     *
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   524
     * @param file the File object
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   525
     * @return the resulting directory or JAR file URL, or null if unknown
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   526
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   527
    private static URL fileToURL(File file) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   528
        String name;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   529
        try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   530
            name = file.getCanonicalPath();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   531
        } catch (IOException e) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   532
            name = file.getAbsolutePath();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   533
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   534
        name = name.replace(File.separatorChar, '/');
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   535
        if (!name.startsWith("/")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   536
            name = "/" + name;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   537
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   538
        // If the file does not exist, then assume that it's a directory
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   539
        if (!file.isFile()) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   540
            name = name + "/";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   541
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   542
        try {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   543
            return new URL("file", "", name);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   544
        } catch (MalformedURLException e) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   545
            throw new IllegalArgumentException("file");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   546
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   547
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   548
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   549
    private static void setScriptArguments(ScriptEngine se, String[] args) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   550
        se.put("arguments", args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   551
        se.put(ScriptEngine.ARGV, args);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   552
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   553
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   554
    private static String setScriptFilename(ScriptEngine se, String name) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   555
        String oldName = (String) se.get(ScriptEngine.FILENAME);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   556
        se.put(ScriptEngine.FILENAME, name);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   557
        return oldName;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   558
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   559
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   560
    // exit codes
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   561
    private static final int EXIT_SUCCESS            = 0;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   562
    private static final int EXIT_CMD_NO_CLASSPATH   = 1;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   563
    private static final int EXIT_CMD_NO_FILE        = 2;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   564
    private static final int EXIT_CMD_NO_SCRIPT      = 3;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   565
    private static final int EXIT_CMD_NO_LANG        = 4;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   566
    private static final int EXIT_CMD_NO_ENCODING    = 5;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   567
    private static final int EXIT_CMD_NO_PROPNAME    = 6;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   568
    private static final int EXIT_UNKNOWN_OPTION     = 7;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   569
    private static final int EXIT_ENGINE_NOT_FOUND   = 8;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   570
    private static final int EXIT_NO_ENCODING_FOUND  = 9;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   571
    private static final int EXIT_SCRIPT_ERROR       = 10;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   572
    private static final int EXIT_FILE_NOT_FOUND     = 11;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   573
    private static final int EXIT_MULTIPLE_STDIN     = 12;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   574
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   575
    // default scripting language
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   576
    private static final String DEFAULT_LANGUAGE = "js";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   577
    // list of scripts to process
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   578
    private static List<Command> scripts;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   579
    // the script engine manager
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   580
    private static ScriptEngineManager engineManager;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   581
    // map of engines we loaded
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   582
    private static Map<String, ScriptEngine> engines;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   583
    // error messages resource
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   584
    private static ResourceBundle msgRes;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   585
    private static String BUNDLE_NAME = "com.sun.tools.script.shell.messages";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   586
    private static String PROGRAM_NAME = "jrunscript";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   587
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   588
    static {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   589
        scripts = new ArrayList<Command>();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   590
        engines = new HashMap<String, ScriptEngine>();
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   591
        msgRes = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault());
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   592
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents:
diff changeset
   593
}