Skip to content

Commit

Permalink
Fix blocking process
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Aug 15, 2024
1 parent b5913a5 commit 32f6ca0
Showing 1 changed file with 66 additions and 51 deletions.
117 changes: 66 additions & 51 deletions weasis-core/src/main/java/org/weasis/core/ui/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.Path;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.swing.Action;
import javax.swing.Icon;
Expand Down Expand Up @@ -404,63 +405,77 @@ public boolean isDicomSelectionAction() {
}

public void launch(ImageViewerEventManager<?> eventManager) {
try {
if (!StringUtil.hasText(binaryPath)) {
return;
}
boolean isMac = compatibility == Compatibility.MAC;
List<String> command = new ArrayList<>(Arrays.asList(binaryPath.trim().split("\\s+")));
if (!isMac && parameters != null && !parameters.isEmpty()) {
for (String param : parameters) {
command.add(resolvePlaceholders(param, eventManager));
}
if (!StringUtil.hasText(binaryPath)) {
return;
}
boolean isMac = compatibility == Compatibility.MAC;
List<String> command = new ArrayList<>(Arrays.asList(binaryPath.trim().split("\\s+")));
if (!isMac && parameters != null && !parameters.isEmpty()) {
for (String param : parameters) {
command.add(resolvePlaceholders(param, eventManager));
}
}

ProcessBuilder processBuilder = new ProcessBuilder(command);
if (StringUtil.hasText(workingDirectory)) {
processBuilder.directory(new File(workingDirectory));
}
if (environmentVariables != null && !environmentVariables.isEmpty()) {
Map<String, String> environment = processBuilder.environment();
for (Entry<String, String> entry : environmentVariables.entrySet()) {
environment.put(entry.getKey(), resolvePlaceholders(entry.getValue(), eventManager));
}
ProcessBuilder processBuilder = new ProcessBuilder(command);
if (StringUtil.hasText(workingDirectory)) {
processBuilder.directory(new File(workingDirectory));
}
if (environmentVariables != null && !environmentVariables.isEmpty()) {
Map<String, String> environment = processBuilder.environment();
for (Entry<String, String> entry : environmentVariables.entrySet()) {
environment.put(entry.getKey(), resolvePlaceholders(entry.getValue(), eventManager));
}
Process p = processBuilder.start();
BufferedReader buffer = new BufferedReader(new InputStreamReader(p.getInputStream()));
}

String data;
while ((data = buffer.readLine()) != null) {
System.out.println(data);
}
int val = 0;
if (p.waitFor() != 0) {
val = p.exitValue();
}
Thread launcherThread =
new Thread(
() -> {
try {
Process p = processBuilder.start();
BufferedReader buffer =
new BufferedReader(new InputStreamReader(p.getInputStream()));

String data;
int lineCount = 0;
while (lineCount < 5 && (data = buffer.readLine()) != null) {
System.out.println(data);
lineCount++;
}

if (isMac && parameters != null && !parameters.isEmpty()) {
for (String param : parameters) {
command.add(resolvePlaceholders(param, eventManager));
}
Thread.sleep(1000);
p = processBuilder.start();
if (p.waitFor() != 0) {
val = p.exitValue();
}
}
int val = getExitValue(p);

if (val != 0) {
JOptionPane.showMessageDialog(
GuiUtils.getUICore().getBaseArea(),
String.format(Messages.getString("error.launching.app"), binaryPath),
Messages.getString("launcher.error"),
JOptionPane.ERROR_MESSAGE);
}
} catch (IOException e1) {
LOGGER.error("Running cmd", e1);
} catch (InterruptedException e2) {
LOGGER.error("Cannot get the exit status of {}", binaryPath, e2);
Thread.currentThread().interrupt();
if (isMac && parameters != null && !parameters.isEmpty()) {
for (String param : parameters) {
command.add(resolvePlaceholders(param, eventManager));
}
Thread.sleep(500);
p = processBuilder.start();
val = getExitValue(p);
}

if (val != 0) {
JOptionPane.showMessageDialog(
GuiUtils.getUICore().getBaseArea(),
String.format(Messages.getString("error.launching.app"), binaryPath),
Messages.getString("launcher.error"),
JOptionPane.ERROR_MESSAGE);
}
} catch (IOException e1) {
LOGGER.error("Running cmd", e1);
} catch (InterruptedException e2) {
LOGGER.error("Cannot get the exit status of {}", binaryPath, e2);
Thread.currentThread().interrupt();
}
});
launcherThread.start();
}

private int getExitValue(Process p) throws InterruptedException {
if (!p.waitFor(15, TimeUnit.SECONDS)) {
LOGGER.warn("Process did not exit within the timeout, detaching the process.");
return 0;
} else {
return p.exitValue();
}
}

Expand Down

0 comments on commit 32f6ca0

Please sign in to comment.