Example usage for javafx.concurrent Task getTotalWork

List of usage examples for javafx.concurrent Task getTotalWork

Introduction

In this page you can find the example usage for javafx.concurrent Task getTotalWork.

Prototype

@Override
    public final double getTotalWork() 

Source Link

Usage

From source file:sh.isaac.api.util.DownloadUnzipTask.java

/**
 * Call.//from w  w w . j av a  2 s . c  o m
 *
 * @return the file
 * @throws Exception the exception
 * @see javafx.concurrent.Task#call()
 */
@Override
protected File call() throws Exception {
    final File dataFile = download(this.url);
    String calculatedSha1Value = null;
    String expectedSha1Value = null;
    ;

    try {
        LOG.debug("Attempting to get .sha1 file");

        final File sha1File = download(new URL(this.url.toString() + ".sha1"));

        expectedSha1Value = Files.readAllLines(sha1File.toPath()).get(0);

        final Task<String> calculateTask = ChecksumGenerator.calculateChecksum("SHA1", dataFile);

        calculateTask.messageProperty().addListener(
                (ChangeListener<String>) (observable, oldValue, newValue) -> updateMessage(newValue));
        calculateTask.progressProperty().addListener((ChangeListener<Number>) (observable, oldValue,
                newValue) -> updateProgress(calculateTask.getProgress(), calculateTask.getTotalWork()));
        WorkExecutors.get().getExecutor().execute(calculateTask);
        calculatedSha1Value = calculateTask.get();
        sha1File.delete();
    } catch (final Exception e1) {
        LOG.debug("Failed to get .sha1 file", e1);
    }

    if ((calculatedSha1Value != null) && !calculatedSha1Value.equals(expectedSha1Value)) {
        if (this.failOnBadCheksum) {
            throw new RuntimeException("Checksum of downloaded file '" + this.url.toString()
                    + "' does not match the expected value!");
        } else {
            LOG.warn("Checksum of downloaded file '" + this.url.toString()
                    + "' does not match the expected value!");
        }
    }

    if (this.cancel) {
        LOG.debug("Download cancelled");
        throw new Exception("Cancelled!");
    }

    if (this.unzip) {
        updateTitle("Unzipping");

        try {
            final ZipFile zipFile = new ZipFile(dataFile);

            zipFile.setRunInThread(true);
            zipFile.extractAll(this.targetFolder.getAbsolutePath());

            while (zipFile.getProgressMonitor().getState() == ProgressMonitor.STATE_BUSY) {
                if (this.cancel) {
                    zipFile.getProgressMonitor().cancelAllTasks();
                    LOG.debug("Download cancelled");
                    throw new Exception("Cancelled!");
                }

                updateProgress(zipFile.getProgressMonitor().getPercentDone(), 100);
                updateMessage("Unzipping " + dataFile.getName() + " at "
                        + zipFile.getProgressMonitor().getPercentDone() + "%");

                try {
                    // TODO see if there is an API where I don't have to poll for completion
                    Thread.sleep(25);
                } catch (final InterruptedException e) {
                    // noop
                }
            }

            LOG.debug("Unzip complete");
        } catch (final Exception e) {
            LOG.error("error unzipping", e);
            throw new Exception("The downloaded file doesn't appear to be a zip file");
        } finally {
            dataFile.delete();
        }

        return this.targetFolder;
    } else {
        return dataFile;
    }
}