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