src/jdk.internal.ed/share/classes/jdk/internal/editor/external/ExternalEditor.java
author tschatzl
Fri, 29 Nov 2019 10:20:17 +0100
changeset 59321 5775e4825e58
parent 51650 35dee171e59c
permissions -rw-r--r--
8233998: New young regions registered too early in collection set Reviewed-by: sangheki, sjohanss
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     1
/*
51650
35dee171e59c 8209386: [error-prone] StreamResourceLeak in jdk.internal.ed module
iignatyev
parents: 48286
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     4
 *
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    10
 *
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    15
 * accompanied this code).
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    16
 *
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    20
 *
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    23
 * questions.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    24
 */
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    25
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    26
package jdk.internal.editor.external;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    27
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    28
import java.io.IOException;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    29
import java.nio.charset.Charset;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    30
import java.nio.file.ClosedWatchServiceException;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    31
import java.nio.file.FileSystems;
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
    32
import java.nio.file.FileVisitResult;
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    33
import java.nio.file.Files;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    34
import java.nio.file.Path;
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
    35
import java.nio.file.SimpleFileVisitor;
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    36
import java.nio.file.WatchKey;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    37
import java.nio.file.WatchService;
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
    38
import java.nio.file.attribute.BasicFileAttributes;
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    39
import java.util.Arrays;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    40
import java.util.Scanner;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    41
import java.util.function.Consumer;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    42
import java.util.stream.Collectors;
51650
35dee171e59c 8209386: [error-prone] StreamResourceLeak in jdk.internal.ed module
iignatyev
parents: 48286
diff changeset
    43
import java.util.stream.Stream;
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    44
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    45
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    46
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    47
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    48
/**
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    49
 * Wrapper for controlling an external editor.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    50
 */
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    51
public class ExternalEditor {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    52
    private final Consumer<String> errorHandler;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    53
    private final Consumer<String> saveHandler;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    54
    private final boolean wait;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    55
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    56
    private final Runnable suspendInteractiveInput;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    57
    private final Runnable resumeInteractiveInput;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    58
    private final Runnable promptForNewLineToEndWait;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    59
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    60
    private WatchService watcher;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    61
    private Thread watchedThread;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    62
    private Path dir;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    63
    private Path tmpfile;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    64
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    65
    /**
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    66
     * Launch an external editor.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    67
     *
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    68
     * @param cmd the command to launch (with parameters)
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    69
     * @param initialText initial text in the editor buffer
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    70
     * @param errorHandler handler for error messages
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    71
     * @param saveHandler handler sent the buffer contents on save
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    72
     * @param suspendInteractiveInput a callback to suspend caller (shell) input
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    73
     * @param resumeInteractiveInput a callback to resume caller input
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    74
     * @param wait true, if editor process termination cannot be used to
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    75
     * determine when done
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    76
     * @param promptForNewLineToEndWait a callback to prompt for newline if
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    77
     * wait==true
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    78
     */
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    79
    public static void edit(String[] cmd, String initialText,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    80
            Consumer<String> errorHandler,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    81
            Consumer<String> saveHandler,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    82
            Runnable suspendInteractiveInput,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    83
            Runnable resumeInteractiveInput,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    84
            boolean wait,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    85
            Runnable promptForNewLineToEndWait) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    86
        ExternalEditor ed = new ExternalEditor(errorHandler, saveHandler, suspendInteractiveInput,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    87
             resumeInteractiveInput, wait, promptForNewLineToEndWait);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    88
        ed.edit(cmd, initialText);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    89
    }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    90
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    91
    ExternalEditor(Consumer<String> errorHandler,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    92
            Consumer<String> saveHandler,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    93
            Runnable suspendInteractiveInput,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    94
            Runnable resumeInteractiveInput,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    95
            boolean wait,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    96
            Runnable promptForNewLineToEndWait) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    97
        this.errorHandler = errorHandler;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    98
        this.saveHandler = saveHandler;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
    99
        this.wait = wait;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   100
        this.suspendInteractiveInput = suspendInteractiveInput;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   101
        this.resumeInteractiveInput = resumeInteractiveInput;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   102
        this.promptForNewLineToEndWait = promptForNewLineToEndWait;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   103
    }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   104
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   105
    private void edit(String[] cmd, String initialText) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   106
        try {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   107
            setupWatch(initialText);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   108
            launch(cmd);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   109
        } catch (IOException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   110
            errorHandler.accept(ex.getMessage());
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   111
        } finally {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   112
            deleteDirectory();
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   113
        }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   114
    }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   115
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   116
    /**
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   117
     * Creates a WatchService and registers the given directory
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   118
     */
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   119
    private void setupWatch(String initialText) throws IOException {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   120
        this.watcher = FileSystems.getDefault().newWatchService();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   121
        this.dir = Files.createTempDirectory("extedit");
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   122
        this.tmpfile = Files.createTempFile(dir, null, ".java");
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   123
        Files.write(tmpfile, initialText.getBytes(Charset.forName("UTF-8")));
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   124
        dir.register(watcher,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   125
                ENTRY_CREATE,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   126
                ENTRY_DELETE,
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   127
                ENTRY_MODIFY);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   128
        watchedThread = new Thread(() -> {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   129
            for (;;) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   130
                WatchKey key;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   131
                try {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   132
                    key = watcher.take();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   133
                } catch (ClosedWatchServiceException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   134
                    // The watch service has been closed, we are done
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   135
                    break;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   136
                } catch (InterruptedException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   137
                    // tolerate an interrupt
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   138
                    continue;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   139
                }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   140
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   141
                if (!key.pollEvents().isEmpty()) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   142
                    saveFile();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   143
                }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   144
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   145
                boolean valid = key.reset();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   146
                if (!valid) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   147
                    // The watch service has been closed, we are done
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   148
                    break;
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   149
                }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   150
            }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   151
        });
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   152
        watchedThread.start();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   153
    }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   154
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   155
    private void launch(String[] cmd) throws IOException {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   156
        String[] params = Arrays.copyOf(cmd, cmd.length + 1);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   157
        params[cmd.length] = tmpfile.toString();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   158
        ProcessBuilder pb = new ProcessBuilder(params);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   159
        pb = pb.inheritIO();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   160
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   161
        try {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   162
            suspendInteractiveInput.run();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   163
            Process process = pb.start();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   164
            // wait to exit edit mode in one of these ways...
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   165
            if (wait) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   166
                // -wait option -- ignore process exit, wait for carriage-return
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   167
                Scanner scanner = new Scanner(System.in);
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   168
                promptForNewLineToEndWait.run();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   169
                scanner.nextLine();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   170
            } else {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   171
                // wait for process to exit
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   172
                process.waitFor();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   173
            }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   174
        } catch (IOException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   175
            errorHandler.accept("process IO failure: " + ex.getMessage());
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   176
        } catch (InterruptedException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   177
            errorHandler.accept("process interrupt: " + ex.getMessage());
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   178
        } finally {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   179
            try {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   180
                watcher.close();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   181
                watchedThread.join(); //so that saveFile() is finished.
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   182
                saveFile();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   183
            } catch (InterruptedException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   184
                errorHandler.accept("process interrupt: " + ex.getMessage());
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   185
            } finally {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   186
                resumeInteractiveInput.run();
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   187
            }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   188
        }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   189
    }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   190
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   191
    private void saveFile() {
51650
35dee171e59c 8209386: [error-prone] StreamResourceLeak in jdk.internal.ed module
iignatyev
parents: 48286
diff changeset
   192
        try (Stream<String> lines = Files.lines(tmpfile)) {
35dee171e59c 8209386: [error-prone] StreamResourceLeak in jdk.internal.ed module
iignatyev
parents: 48286
diff changeset
   193
            saveHandler.accept(lines.collect(Collectors.joining("\n", "", "\n")));
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   194
        } catch (IOException ex) {
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   195
            errorHandler.accept("Failure in read edit file: " + ex.getMessage());
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   196
        }
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   197
    }
48286
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   198
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   199
    private void deleteDirectory() {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   200
        try {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   201
            Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   202
                @Override
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   203
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   204
                        throws IOException {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   205
                    Files.delete(file);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   206
                    return FileVisitResult.CONTINUE;
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   207
                }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   208
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   209
                @Override
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   210
                public FileVisitResult postVisitDirectory(Path directory, IOException fail)
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   211
                        throws IOException {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   212
                    if (fail == null) {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   213
                        Files.delete(directory);
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   214
                        return FileVisitResult.CONTINUE;
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   215
                    }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   216
                    throw fail;
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   217
                }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   218
            });
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   219
        } catch (IOException exc) {
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   220
            // ignore: The end-user will not want to see this, it is in a temp
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   221
            // directory so it will go away eventually, and tests verify that
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   222
            // the deletion is occurring.
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   223
        }
745ea7d5039a 8162989: jshell tool: /edit with external editor leaks files in /tmp
rfield
parents: 47216
diff changeset
   224
    }
41884
b7c2cc7a10fa 8168972: Editor support: move built-in and external editor support to the jdk repo
rfield
parents:
diff changeset
   225
}