class QsubCmdExecutor implements CmdExecutor {

    @Override
    public String executeCommand(final String originalCommand,
                                 final Map<String, String> environmentVariables,
                                 final File workDir,
                                 final String username) {
        log.info("Executing command '{}' using qsub", originalCommand);
        Path outputLog = null;
        Path errorLog = null;
        Path originalCommandScript = null;
        try {
            outputLog = createFile("qsub-output", ".log");
            errorLog = createFile("qsub-error", ".log");
            originalCommandScript = createFile("qsub-script", ".sh");
            Files.write(originalCommandScript, Collections.singleton(originalCommand));

            final String qsubCommand = String.format(
                qsubTemplate,
                outputLog.toAbsolutePath().toString(),
                errorLog.toAbsolutePath().toString(),
                originalCommandScript.toAbsolutePath().toString()
            );

            cmdExecutor.executeCommand(qsubCommand, environmentVariables, workDir, username);

            return Files.lines(outputLog).collect(Collectors.joining("\n"));
        } catch (CmdExecutionException | IOException e) {
            throw new CmdExecutionException(String.format(
                    "Qsub execution of original command '%s' went bad", originalCommand), e);
        } finally {
            Stream.of(outputLog, errorLog, originalCommandScript)
                .filter(Objects::nonNull)
                .map(Path::toFile)
                .forEach(File::delete);
        }
    }

}