src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/ExternalEditor.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 32692 nashorn/src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/ExternalEditor.java@8f60bd284bf4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     1
/*
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     4
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    10
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    15
 * accompanied this code).
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    16
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    20
 *
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    23
 * questions.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    24
 */
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    25
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    27
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    28
import java.io.IOException;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    29
import java.nio.charset.Charset;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    30
import java.nio.file.ClosedWatchServiceException;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    31
import java.nio.file.FileSystems;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    32
import java.nio.file.Files;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    33
import java.nio.file.Path;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    34
import java.nio.file.WatchKey;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    35
import java.nio.file.WatchService;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    36
import java.util.List;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    37
import java.util.function.Consumer;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    38
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    39
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    40
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    41
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    42
final class ExternalEditor {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    43
    private final Consumer<String> errorHandler;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    44
    private final Consumer<String> saveHandler;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    45
    private final Console input;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    46
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    47
    private WatchService watcher;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    48
    private Thread watchedThread;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    49
    private Path dir;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    50
    private Path tmpfile;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    51
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
    52
    ExternalEditor(final Consumer<String> errorHandler, final Consumer<String> saveHandler, final Console input) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    53
        this.errorHandler = errorHandler;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    54
        this.saveHandler = saveHandler;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    55
        this.input = input;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    56
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    57
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
    58
    private void edit(final String cmd, final String initialText) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    59
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    60
            setupWatch(initialText);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    61
            launch(cmd);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    62
        } catch (IOException ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    63
            errorHandler.accept(ex.getMessage());
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    64
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    65
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    66
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    67
    /**
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    68
     * Creates a WatchService and registers the given directory
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    69
     */
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
    70
    private void setupWatch(final String initialText) throws IOException {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    71
        this.watcher = FileSystems.getDefault().newWatchService();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    72
        this.dir = Files.createTempDirectory("REPL");
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    73
        this.tmpfile = Files.createTempFile(dir, null, ".js");
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    74
        Files.write(tmpfile, initialText.getBytes(Charset.forName("UTF-8")));
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    75
        dir.register(watcher,
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    76
                ENTRY_CREATE,
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    77
                ENTRY_DELETE,
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    78
                ENTRY_MODIFY);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    79
        watchedThread = new Thread(() -> {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    80
            for (;;) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    81
                WatchKey key;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    82
                try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    83
                    key = watcher.take();
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
    84
                } catch (final ClosedWatchServiceException ex) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    85
                    break;
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
    86
                } catch (final InterruptedException ex) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    87
                    continue; // tolerate an intrupt
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    88
                }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    89
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    90
                if (!key.pollEvents().isEmpty()) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    91
                    if (!input.terminalEditorRunning()) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    92
                        saveFile();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    93
                    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    94
                }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    95
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    96
                boolean valid = key.reset();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    97
                if (!valid) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    98
                    errorHandler.accept("Invalid key");
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
    99
                    break;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   100
                }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   101
            }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   102
        });
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   103
        watchedThread.start();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   104
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   105
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
   106
    private void launch(final String cmd) throws IOException {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   107
        ProcessBuilder pb = new ProcessBuilder(cmd, tmpfile.toString());
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   108
        pb = pb.inheritIO();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   109
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   110
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   111
            input.suspend();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   112
            Process process = pb.start();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   113
            process.waitFor();
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
   114
        } catch (final IOException ex) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   115
            errorHandler.accept("process IO failure: " + ex.getMessage());
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
   116
        } catch (final InterruptedException ex) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   117
            errorHandler.accept("process interrupt: " + ex.getMessage());
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   118
        } finally {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   119
            try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   120
                watcher.close();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   121
                watchedThread.join(); //so that saveFile() is finished.
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   122
                saveFile();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   123
            } catch (InterruptedException ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   124
                errorHandler.accept("process interrupt: " + ex.getMessage());
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   125
            } finally {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   126
                input.resume();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   127
            }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   128
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   129
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   130
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   131
    private void saveFile() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   132
        List<String> lines;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   133
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   134
            lines = Files.readAllLines(tmpfile);
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
   135
        } catch (final IOException ex) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   136
            errorHandler.accept("Failure read edit file: " + ex.getMessage());
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   137
            return ;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   138
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   139
        StringBuilder sb = new StringBuilder();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   140
        for (String ln : lines) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   141
            sb.append(ln);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   142
            sb.append('\n');
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   143
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   144
        saveHandler.accept(sb.toString());
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   145
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   146
32692
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
   147
    static void edit(final String cmd, final Consumer<String> errorHandler, final String initialText,
8f60bd284bf4 8055917: jdk.nashorn.internal.codegen.CompilationPhase$N should be renamed to proper classes
sundar
parents: 32313
diff changeset
   148
            final Consumer<String> saveHandler, final Console input) {
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   149
        ExternalEditor ed = new ExternalEditor(errorHandler,  saveHandler, input);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   150
        ed.edit(cmd, initialText);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   151
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents:
diff changeset
   152
}