All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.documents4j.conversion.ProcessFutureWrapper Maven / Gradle / Ivy

There is a newer version: 1.1.12
Show newest version
package com.documents4j.conversion;

import com.documents4j.throwables.ConverterException;
import org.zeroturnaround.exec.StartedProcess;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ProcessFutureWrapper implements Future {

    private final StartedProcess startedProcess;

    public ProcessFutureWrapper(StartedProcess processFuture) {
        this.startedProcess = processFuture;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return startedProcess.getFuture().cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return startedProcess.getFuture().isCancelled();
    }

    @Override
    public boolean isDone() {
        return startedProcess.getFuture().isDone();
    }

    @Override
    public Boolean get() throws InterruptedException, ExecutionException {
        return evaluateExitValue(startedProcess.getFuture().get().getExitValue());
    }

    @Override
    public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return evaluateExitValue(startedProcess.getFuture().get(timeout, unit).getExitValue());
    }

    private boolean evaluateExitValue(int exitValue) throws ExecutionException {
        try {
            return ExternalConverterScriptResult
                    .from(exitValue)
                    .resolve();
        } catch (ConverterException e) {
            throw new ExecutionException("The conversion finished unsuccessful", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy