2023-03-20 22:55:11 +01:00
|
|
|
package stirling.software.SPDF.utils;
|
|
|
|
|
2023-03-25 23:16:26 +01:00
|
|
|
import java.io.BufferedReader;
|
2023-07-22 17:57:40 +02:00
|
|
|
import java.io.File;
|
2023-03-20 22:55:11 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.ArrayList;
|
2023-03-25 23:16:26 +01:00
|
|
|
import java.util.List;
|
2023-04-01 22:02:54 +02:00
|
|
|
import java.util.Map;
|
2023-04-16 23:03:30 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2023-04-01 22:02:54 +02:00
|
|
|
import java.util.concurrent.Semaphore;
|
2023-04-22 13:51:01 +02:00
|
|
|
|
2023-03-20 22:55:11 +01:00
|
|
|
public class ProcessExecutor {
|
2023-04-22 13:51:01 +02:00
|
|
|
|
|
|
|
public enum Processes {
|
2023-07-22 17:57:40 +02:00
|
|
|
LIBRE_OFFICE,
|
|
|
|
OCR_MY_PDF,
|
|
|
|
PYTHON_OPENCV,
|
|
|
|
GHOSTSCRIPT,
|
|
|
|
WEASYPRINT
|
2023-04-01 22:02:54 +02:00
|
|
|
}
|
2023-03-20 22:55:11 +01:00
|
|
|
|
2023-04-22 13:51:01 +02:00
|
|
|
private static final Map<Processes, ProcessExecutor> instances = new ConcurrentHashMap<>();
|
2023-03-20 22:55:11 +01:00
|
|
|
|
2023-04-22 13:51:01 +02:00
|
|
|
public static ProcessExecutor getInstance(Processes processType) {
|
|
|
|
return instances.computeIfAbsent(
|
|
|
|
processType,
|
|
|
|
key -> {
|
|
|
|
int semaphoreLimit =
|
|
|
|
switch (key) {
|
|
|
|
case LIBRE_OFFICE -> 1;
|
|
|
|
case OCR_MY_PDF -> 2;
|
2023-05-01 22:57:48 +02:00
|
|
|
case PYTHON_OPENCV -> 8;
|
|
|
|
case GHOSTSCRIPT -> 16;
|
2023-07-22 17:57:40 +02:00
|
|
|
case WEASYPRINT -> 16;
|
2023-04-22 13:51:01 +02:00
|
|
|
};
|
|
|
|
return new ProcessExecutor(semaphoreLimit);
|
|
|
|
});
|
|
|
|
}
|
2023-03-20 22:55:11 +01:00
|
|
|
|
2023-04-01 22:02:54 +02:00
|
|
|
private final Semaphore semaphore;
|
2023-03-20 22:55:11 +01:00
|
|
|
|
2023-04-01 22:02:54 +02:00
|
|
|
private ProcessExecutor(int semaphoreLimit) {
|
|
|
|
this.semaphore = new Semaphore(semaphoreLimit);
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public ProcessExecutorResult runCommandWithOutputHandling(List<String> command)
|
|
|
|
throws IOException, InterruptedException {
|
2023-07-22 17:57:40 +02:00
|
|
|
return runCommandWithOutputHandling(command, null);
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public ProcessExecutorResult runCommandWithOutputHandling(
|
|
|
|
List<String> command, File workingDirectory) throws IOException, InterruptedException {
|
2023-04-22 13:51:01 +02:00
|
|
|
int exitCode = 1;
|
2023-07-29 14:53:30 +02:00
|
|
|
String messages = "";
|
2023-04-22 13:51:01 +02:00
|
|
|
semaphore.acquire();
|
|
|
|
try {
|
|
|
|
|
|
|
|
System.out.print("Running command: " + String.join(" ", command));
|
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder(command);
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-22 17:57:40 +02:00
|
|
|
// Use the working directory if it's set
|
|
|
|
if (workingDirectory != null) {
|
|
|
|
processBuilder.directory(workingDirectory);
|
|
|
|
}
|
2023-04-22 13:51:01 +02:00
|
|
|
Process process = processBuilder.start();
|
|
|
|
|
|
|
|
// Read the error stream and standard output stream concurrently
|
|
|
|
List<String> errorLines = new ArrayList<>();
|
|
|
|
List<String> outputLines = new ArrayList<>();
|
|
|
|
|
|
|
|
Thread errorReaderThread =
|
|
|
|
new Thread(
|
|
|
|
() -> {
|
|
|
|
try (BufferedReader errorReader =
|
|
|
|
new BufferedReader(
|
|
|
|
new InputStreamReader(
|
|
|
|
process.getErrorStream(),
|
|
|
|
StandardCharsets.UTF_8))) {
|
|
|
|
String line;
|
|
|
|
while ((line = errorReader.readLine()) != null) {
|
|
|
|
errorLines.add(line);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-04-22 13:51:01 +02:00
|
|
|
Thread outputReaderThread =
|
|
|
|
new Thread(
|
|
|
|
() -> {
|
|
|
|
try (BufferedReader outputReader =
|
|
|
|
new BufferedReader(
|
|
|
|
new InputStreamReader(
|
|
|
|
process.getInputStream(),
|
|
|
|
StandardCharsets.UTF_8))) {
|
|
|
|
String line;
|
|
|
|
while ((line = outputReader.readLine()) != null) {
|
|
|
|
outputLines.add(line);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
errorReaderThread.start();
|
|
|
|
outputReaderThread.start();
|
|
|
|
|
|
|
|
// Wait for the conversion process to complete
|
|
|
|
exitCode = process.waitFor();
|
|
|
|
|
|
|
|
// Wait for the reader threads to finish
|
|
|
|
errorReaderThread.join();
|
|
|
|
outputReaderThread.join();
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-04-22 13:51:01 +02:00
|
|
|
if (outputLines.size() > 0) {
|
|
|
|
String outputMessage = String.join("\n", outputLines);
|
2023-07-29 14:53:30 +02:00
|
|
|
messages += outputMessage;
|
2023-04-22 13:51:01 +02:00
|
|
|
System.out.println("Command output:\n" + outputMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorLines.size() > 0) {
|
|
|
|
String errorMessage = String.join("\n", errorLines);
|
2023-07-29 14:53:30 +02:00
|
|
|
messages += errorMessage;
|
2023-04-22 13:51:01 +02:00
|
|
|
System.out.println("Command error output:\n" + errorMessage);
|
|
|
|
if (exitCode != 0) {
|
|
|
|
throw new IOException(
|
|
|
|
"Command process failed with exit code "
|
|
|
|
+ exitCode
|
|
|
|
+ ". Error message: "
|
|
|
|
+ errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
semaphore.release();
|
|
|
|
}
|
2023-07-29 14:53:30 +02:00
|
|
|
return new ProcessExecutorResult(exitCode, messages);
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public class ProcessExecutorResult {
|
|
|
|
int rc;
|
|
|
|
String messages;
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public ProcessExecutorResult(int rc, String messages) {
|
|
|
|
this.rc = rc;
|
|
|
|
this.messages = messages;
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public int getRc() {
|
|
|
|
return rc;
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public void setRc(int rc) {
|
|
|
|
this.rc = rc;
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public String getMessages() {
|
|
|
|
return messages;
|
|
|
|
}
|
2023-12-30 20:11:27 +01:00
|
|
|
|
2023-07-29 14:53:30 +02:00
|
|
|
public void setMessages(String messages) {
|
|
|
|
this.messages = messages;
|
|
|
|
}
|
2023-04-01 22:02:54 +02:00
|
|
|
}
|
2023-03-20 22:55:11 +01:00
|
|
|
}
|