|
13 | 13 |
|
14 | 14 | import java.io.File; |
15 | 15 | import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.nio.charset.StandardCharsets; |
16 | 18 | import java.nio.file.Files; |
17 | 19 | import java.nio.file.Paths; |
18 | 20 | import java.util.Map; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.ExecutionException; |
19 | 23 |
|
| 24 | +import org.apache.commons.io.IOUtils; |
20 | 25 | import org.eclipse.jdi.internal.VirtualMachineImpl; |
21 | 26 | import org.eclipse.jdi.internal.VirtualMachineManagerImpl; |
22 | 27 | import org.eclipse.jdi.internal.connect.SocketLaunchingConnectorImpl; |
23 | 28 | import org.eclipse.jdi.internal.connect.SocketListeningConnectorImpl; |
24 | 29 |
|
25 | 30 | import com.microsoft.java.debug.core.DebugUtility; |
| 31 | +import com.microsoft.java.debug.core.LaunchException; |
26 | 32 | import com.sun.jdi.VirtualMachine; |
27 | 33 | import com.sun.jdi.connect.Connector; |
28 | 34 | import com.sun.jdi.connect.IllegalConnectorArgumentsException; |
@@ -83,18 +89,100 @@ public VirtualMachine launch(Map<String, ? extends Argument> connectionArgs) |
83 | 89 | String address = listenConnector.startListening(args); |
84 | 90 |
|
85 | 91 | String[] cmds = constructLaunchCommand(connectionArgs, address); |
86 | | - Process process = Runtime.getRuntime().exec(cmds, envVars, workingDir); |
87 | 92 |
|
88 | | - VirtualMachineImpl vm; |
| 93 | + /* Launch the Java process */ |
| 94 | + final Process process = Runtime.getRuntime().exec(cmds, envVars, workingDir); |
| 95 | + |
| 96 | + /* A Future that will be completed if we successfully connect to the launched process, or |
| 97 | + will fail with an Exception if we do not. |
| 98 | + */ |
| 99 | + final CompletableFuture<VirtualMachineImpl> result = new CompletableFuture<>(); |
| 100 | + |
| 101 | + /* Listen for the debug connection from the Java process */ |
| 102 | + CompletableFuture.runAsync(() -> { |
| 103 | + try { |
| 104 | + VirtualMachineImpl vm = (VirtualMachineImpl) listenConnector.accept(args); |
| 105 | + vm.setLaunchedProcess(process); |
| 106 | + result.complete(vm); |
| 107 | + } catch (IllegalConnectorArgumentsException e) { |
| 108 | + result.completeExceptionally(e); |
| 109 | + } catch (IOException e) { |
| 110 | + if (result.isDone()) { |
| 111 | + /* The result Future has already been completed by the Process onExit hook */ |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + final String stdout = streamToString(process.getInputStream()); |
| 116 | + final String stderr = streamToString(process.getErrorStream()); |
| 117 | + |
| 118 | + process.destroy(); |
| 119 | + |
| 120 | + result.completeExceptionally(new LaunchException( |
| 121 | + String.format("VM did not connect within given time: %d ms", ACCEPT_TIMEOUT), |
| 122 | + process, |
| 123 | + false, |
| 124 | + -1, |
| 125 | + stdout, |
| 126 | + stderr |
| 127 | + )); |
| 128 | + } catch (RuntimeException e) { |
| 129 | + result.completeExceptionally(e); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + /* Wait for the Java process to exit; if it exits before the debug connection is made, report it as an error. */ |
| 134 | + process.onExit().thenAcceptAsync(theProcess -> { |
| 135 | + if (result.isDone()) { |
| 136 | + /* The result Future has already been completed by successfully connecting to the debug connection */ |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + final int exitStatus = theProcess.exitValue(); |
| 141 | + final String stdout = streamToString(process.getInputStream()); |
| 142 | + final String stderr = streamToString(process.getErrorStream()); |
| 143 | + |
| 144 | + result.completeExceptionally(new LaunchException( |
| 145 | + String.format("VM exited with status %d", exitStatus), |
| 146 | + theProcess, |
| 147 | + true, |
| 148 | + exitStatus, |
| 149 | + stdout, |
| 150 | + stderr |
| 151 | + )); |
| 152 | + |
| 153 | + /* Stop the debug connection attempt */ |
| 154 | + try { |
| 155 | + listenConnector.stopListening(args); |
| 156 | + } catch (IOException e) { |
| 157 | + /* Ignore */ |
| 158 | + } |
| 159 | + }); |
| 160 | + |
89 | 161 | try { |
90 | | - vm = (VirtualMachineImpl) listenConnector.accept(args); |
91 | | - } catch (IOException | IllegalConnectorArgumentsException e) { |
92 | | - process.destroy(); |
93 | | - throw new VMStartException(String.format("VM did not connect within given time: %d ms", ACCEPT_TIMEOUT), process); |
| 162 | + return result.get(); |
| 163 | + } catch (ExecutionException e) { |
| 164 | + if (e.getCause() instanceof IOException) { |
| 165 | + throw (IOException) e.getCause(); |
| 166 | + } else if (e.getCause() instanceof IllegalConnectorArgumentsException) { |
| 167 | + throw (IllegalConnectorArgumentsException) e.getCause(); |
| 168 | + } else if (e.getCause() instanceof VMStartException) { |
| 169 | + throw (VMStartException) e.getCause(); |
| 170 | + } else if (e.getCause() instanceof RuntimeException) { |
| 171 | + throw (RuntimeException) e.getCause(); |
| 172 | + } else { |
| 173 | + throw new IllegalStateException("Unexpected exception thrown when launching VM", e.getCause()); |
| 174 | + } |
| 175 | + } catch (InterruptedException e) { |
| 176 | + throw new VMStartException("VM start interrupted", process); |
94 | 177 | } |
| 178 | + } |
95 | 179 |
|
96 | | - vm.setLaunchedProcess(process); |
97 | | - return vm; |
| 180 | + private String streamToString(final InputStream inputStream) { |
| 181 | + try { |
| 182 | + return IOUtils.toString(inputStream, StandardCharsets.UTF_8); |
| 183 | + } catch (IOException ioe) { |
| 184 | + return null; |
| 185 | + } |
98 | 186 | } |
99 | 187 |
|
100 | 188 | private static String[] constructLaunchCommand(Map<String, ? extends Argument> launchingOptions, String address) { |
|
0 commit comments