Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Kitodo-Command/src/main/java/org/kitodo/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -47,17 +49,17 @@ public CommandResult runCommand(String command) {
InputStream errorInputStream = process.getErrorStream()) {
List<String> outputMessage = inputStreamArrayToList(inputStream);
List<String> errorMessage = inputStreamArrayToList(errorInputStream);
List<String> combinedMessages = Stream.concat(outputMessage.stream(), errorMessage.stream())
.collect(Collectors.toList());
int errCode = process.waitFor();

outputMessage.addAll(errorMessage);

commandResult = new CommandResult(command, errCode == 0, outputMessage);
if (commandResult.isSuccessful()) {
if (Integer.valueOf(errCode).equals(0)) {
commandResult = new CommandResult(command, true, outputMessage);
logger.info("Execution of Command {} was successful!: {}",
commandResult.getCommand(), commandResult.getMessages());
commandResult.getCommand(), combinedMessages);
} else {
commandResult = new CommandResult(command, false, errorMessage);
logger.error("Execution of Command {} failed!: {}",
commandResult.getCommand(), commandResult.getMessages());
commandResult.getCommand(), combinedMessages);
}
}
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.kitodo.api.command.CommandInterface;
import org.kitodo.api.command.CommandResult;
import org.kitodo.production.helper.Helper;
import org.kitodo.serviceloader.KitodoServiceLoader;

public class CommandService {
Expand Down Expand Up @@ -53,8 +54,12 @@ public CommandResult runCommand(String script) throws IOException {
}
CommandResult commandResult = commandModule.runCommand(script);
List<String> commandResultMessages = commandResult.getMessages();
if (!commandResultMessages.isEmpty() && commandResultMessages.get(0).contains("IOException")) {
throw new IOException(commandResultMessages.get(1));
if (!commandResult.isSuccessful() && !commandResultMessages.isEmpty()) {
for (String message : commandResultMessages) {
Helper.setErrorMessage(message);
}
String fullErrorMessage = String.join(" | ", commandResultMessages);
throw new IOException(fullErrorMessage);
}
return commandResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,14 @@ public boolean executeScript(Task task, String script, boolean automatic) throws
executedSuccessful = true;
} else {
logger.info("Calling the shell: {}", script);

CommandService commandService = ServiceManager.getCommandService();
CommandResult commandResult = commandService.runCommand(script);
executedSuccessful = commandResult.isSuccessful();
if (executedSuccessful && !commandResult.getMessages().isEmpty()) {
for (String message : commandResult.getMessages()) {
Helper.setMessage(message);
}
}
}
finishOrReturnAutomaticTask(task, automatic, executedSuccessful);
} catch (IOException | DAOException | InvalidImagesException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,13 @@ private boolean runScriptCondition(String script, Process process) throws IOExce
process, null);

script = replacer.replace(script);

CommandResult commandResult = ServiceManager.getCommandService().runCommand(script);
return commandResult.isSuccessful();
try {
CommandResult commandResult = ServiceManager.getCommandService().runCommand(script);
return commandResult.isSuccessful();
} catch (IOException e) {
Helper.setErrorMessage("runCommandError", logger, e);
return false;
}
}

private boolean runXPathCondition(Process process, String xpath) throws IOException {
Expand Down
Loading