Search in sources :

Example 1 with FMVExecuteException

use of org.fagu.fmv.soft.exec.exception.FMVExecuteException in project fmv by f-agu.

the class IMExceptionKnowAnalyzeTestCase method extractMetadatas.

// ****************************************************************
/**
 * @param resource
 * @param expectedMessage
 * @throws IOException
 */
private void extractMetadatas(String resource, String expectedMessage) throws IOException {
    File folder = new File(System.getProperty("java.io.tmpdir"), "im-extractmetadatas-test");
    try {
        FileUtils.deleteDirectory(folder);
        folder.mkdirs();
        File file = resource != null ? extractResource(folder, resource) : folder;
        try {
            ImageMetadatas extract = ImageMetadatas.with(file).extract();
            fail(extract.toJSON());
        } catch (FMVExecuteException e) {
            if (e.isKnown()) {
                assertEquals(expectedMessage, e.getExceptionKnown().toString());
            } else {
                throw e;
            }
        }
    } finally {
        FileUtils.deleteDirectory(folder);
    }
}
Also used : FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) File(java.io.File)

Example 2 with FMVExecuteException

use of org.fagu.fmv.soft.exec.exception.FMVExecuteException in project fmv by f-agu.

the class GSExceptionKnowAnalyzeTestCase method runPdfToImage.

// *************************************
/**
 * @param srcResource
 * @param expectedMessage
 * @param consumer
 * @throws IOException
 */
private void runPdfToImage(String srcResource, String expectedMessage, Consumer<SoftExecutor> consumer) throws IOException {
    File folder = new File(System.getProperty("java.io.tmpdir"), "gs-pdf2img-test");
    try {
        FileUtils.deleteDirectory(folder);
        folder.mkdirs();
        File srcFile = srcResource != null ? Resource.extract(folder, srcResource) : folder;
        File outFile = new File(srcFile.getPath() + ".jpg");
        try {
            Soft gsSoft = GS.search();
            List<String> list = new ArrayList<>();
            list.add("-sDEVICE=png16m");
            list.add("-sOutputFile=" + outFile.getPath());
            list.add("-r200");
            list.add("-dNOPAUSE");
            list.add("-dBATCH");
            list.add("-dSAFER");
            list.add("-dFirstPage=1");
            list.add("-dLastPage=1");
            list.add("-dUseCropBox");
            list.add(srcFile.getAbsolutePath());
            SoftExecutor customizeExecutor = gsSoft.withParameters(list).workingDirectory(srcFile.getParentFile());
            // .logCommandLine(System.out::println);
            if (consumer != null) {
                consumer.accept(customizeExecutor);
            }
            customizeExecutor.execute();
            fail();
        } catch (FMVExecuteException e) {
            if (e.isKnown()) {
                assertEquals(expectedMessage, e.getExceptionKnown().toString());
            } else {
                new NestedException(e).messageToLines().forEach(System.out::println);
                throw e;
            }
        }
    } finally {
        FileUtils.deleteDirectory(folder);
    }
}
Also used : FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) ArrayList(java.util.ArrayList) SoftExecutor(org.fagu.fmv.soft.SoftExecutor) NestedException(org.fagu.fmv.soft.exec.exception.NestedException) File(java.io.File) Soft(org.fagu.fmv.soft.Soft)

Example 3 with FMVExecuteException

use of org.fagu.fmv.soft.exec.exception.FMVExecuteException in project fmv by f-agu.

the class XpdfExceptionKnowAnalyzeTestCase method extract.

// *************************************
/**
 * @param srcResource
 * @throws IOException
 */
private void extract(String srcResource, String expectedMessage) throws IOException {
    File folder = new File(System.getProperty("java.io.tmpdir"), "xpdf-info-test");
    try {
        FileUtils.deleteDirectory(folder);
        folder.mkdirs();
        File srcFile = srcResource != null ? extractResource(folder, srcResource) : folder;
        try {
            Soft pdfInfoSoft = PdfInfo.search();
            pdfInfoSoft.withParameters(srcFile.getAbsolutePath()).workingDirectory(srcFile.getParentFile()).execute();
            fail();
        } catch (FMVExecuteException e) {
            if (e.isKnown()) {
                assertEquals(expectedMessage, e.getExceptionKnown().toString());
            } else {
                throw e;
            }
        }
    } finally {
        FileUtils.deleteDirectory(folder);
    }
}
Also used : FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) File(java.io.File) Soft(org.fagu.fmv.soft.Soft)

Example 4 with FMVExecuteException

use of org.fagu.fmv.soft.exec.exception.FMVExecuteException in project fmv by f-agu.

the class SoftExecutor method execute.

/**
 * @return Executed time in milliseconds
 * @throws IOException
 */
public SoftExecutor.Executed execute() throws IOException {
    return execute((fmvExecutor, commandLine, execListener, readLineList) -> {
        final String cmdLineStr = CommandLineUtils.toLine(commandLine);
        long startTime = System.currentTimeMillis();
        long time = 0;
        int exitValue = 0;
        PIDProcessOperator pidProcessOperator = new PIDProcessOperator();
        fmvExecutor.addProcessOperator(pidProcessOperator);
        try {
            execListener.eventExecuting(cmdLineStr);
            exitValue = geExecuteDelegate().execute(fmvExecutor, commandLine);
            time = System.currentTimeMillis() - startTime;
            execListener.eventExecuted(cmdLineStr, exitValue, time);
        } catch (ExecuteException e) {
            FMVExecuteException fmvExecuteException = new FMVExecuteException(softProvider.getExceptionKnownAnalyzerClass(), e, cmdLineStr, readLineList);
            execListener.eventException(fmvExecuteException);
            ExceptionKnownAnalyzers.doOrThrows(softProvider.getExceptionKnownAnalyzerClass(), fmvExecuteException, exceptionKnowConsumer, exceptionConsumer);
        }
        return new Executed(pidProcessOperator.getPID(), exitValue, time);
    });
}
Also used : PIDProcessOperator(org.fagu.fmv.soft.exec.PIDProcessOperator) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) ExecuteException(org.apache.commons.exec.ExecuteException) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException)

Example 5 with FMVExecuteException

use of org.fagu.fmv.soft.exec.exception.FMVExecuteException in project fmv by f-agu.

the class SoftExecutor method executeInBackground.

/**
 * @param executorService
 * @return
 * @throws IOException
 */
public Future<SoftExecutor.Executed> executeInBackground(ExecutorService executorService) throws IOException {
    return execute((fmvExecutor, commandLine, execListener, readLineList) -> {
        final String cmdLineStr = CommandLineUtils.toLine(commandLine);
        final AtomicLong startTime = new AtomicLong();
        final AtomicLong time = new AtomicLong();
        final PIDProcessOperator pidProcessOperator = new PIDProcessOperator();
        fmvExecutor.addProcessOperator(pidProcessOperator);
        return new WrapFuture<>(fmvExecutor.executeAsynchronous(geExecuteDelegate(), commandLine, executorService, // before
        () -> {
            startTime.set(System.currentTimeMillis());
            execListener.eventExecuting(cmdLineStr);
        }, // after
        exitValue -> {
            time.set(System.currentTimeMillis() - startTime.get());
            execListener.eventExecuted(cmdLineStr, exitValue, time.get());
        }, // exception
        exception -> {
            if (exception instanceof ExecuteException) {
                ExecuteException e = (ExecuteException) exception;
                FMVExecuteException fmvExecuteException = new FMVExecuteException(softProvider.getExceptionKnownAnalyzerClass(), e, cmdLineStr, readLineList);
                execListener.eventException(fmvExecuteException);
                ExceptionKnownAnalyzers.doOrThrows(softProvider.getExceptionKnownAnalyzerClass(), fmvExecuteException, exceptionKnowConsumer, exceptionConsumer);
            }
        }), exitValue -> new Executed(pidProcessOperator.getPID(), exitValue, time.get()));
    });
}
Also used : PIDProcessOperator(org.fagu.fmv.soft.exec.PIDProcessOperator) FMVCommandLine(org.fagu.fmv.soft.exec.FMVCommandLine) HashMap(java.util.HashMap) CommandLine(org.apache.commons.exec.CommandLine) PIDProcessOperator(org.fagu.fmv.soft.exec.PIDProcessOperator) ArrayList(java.util.ArrayList) SoftProvider(org.fagu.fmv.soft.find.SoftProvider) OptionalLong(java.util.OptionalLong) Future(java.util.concurrent.Future) ExceptionKnownConsumer(org.fagu.fmv.soft.exec.exception.ExceptionKnownConsumer) WrapFuture(org.fagu.fmv.soft.exec.WrapFuture) ExceptionConsumer(org.fagu.fmv.soft.exec.exception.ExceptionConsumer) Duration(java.time.Duration) Map(java.util.Map) LookReader(org.fagu.fmv.soft.exec.LookReader) ExecuteException(org.apache.commons.exec.ExecuteException) ExecutorService(java.util.concurrent.ExecutorService) CommandLineUtils(org.fagu.fmv.soft.exec.CommandLineUtils) FMVExecutor(org.fagu.fmv.soft.exec.FMVExecutor) ReadLine(org.fagu.fmv.soft.exec.ReadLine) ExceptionKnownAnalyzers(org.fagu.fmv.soft.exec.exception.ExceptionKnownAnalyzers) IOException(java.io.IOException) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) ExecHelper(org.fagu.fmv.soft.exec.ExecHelper) File(java.io.File) Objects(java.util.Objects) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) BufferedReadLine(org.fagu.fmv.soft.exec.BufferedReadLine) Proxifier(org.fagu.fmv.soft.utils.Proxifier) Collections(java.util.Collections) AtomicLong(java.util.concurrent.atomic.AtomicLong) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) WrapFuture(org.fagu.fmv.soft.exec.WrapFuture) ExecuteException(org.apache.commons.exec.ExecuteException) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException)

Aggregations

FMVExecuteException (org.fagu.fmv.soft.exec.exception.FMVExecuteException)7 File (java.io.File)6 ArrayList (java.util.ArrayList)3 Soft (org.fagu.fmv.soft.Soft)3 ExecuteException (org.apache.commons.exec.ExecuteException)2 PIDProcessOperator (org.fagu.fmv.soft.exec.PIDProcessOperator)2 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 OptionalLong (java.util.OptionalLong)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 CommandLine (org.apache.commons.exec.CommandLine)1 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)1