Search in sources :

Example 1 with Sleeper

use of com.google.devtools.build.lib.util.Sleeper in project bazel by bazelbuild.

the class HttpDownloader method download.

/**
   * Downloads file to disk and returns path.
   *
   * <p>If the SHA256 checksum and path to the repository cache is specified, attempt to load the
   * file from the {@link RepositoryCache}. If it doesn't exist, proceed to download the file and
   * load it into the cache prior to returning the value.
   *
   * @param urls list of mirror URLs with identical content
   * @param sha256 valid SHA256 hex checksum string which is checked, or empty to disable
   * @param type extension, e.g. "tar.gz" to force on downloaded filename, or empty to not do this
   * @param output destination filename if {@code type} is <i>absent</i>, otherwise output directory
   * @param eventHandler CLI progress reporter
   * @param clientEnv environment variables in shell issuing this command
   * @throws IllegalArgumentException on parameter badness, which should be checked beforehand
   * @throws IOException if download was attempted and ended up failing
   * @throws InterruptedException if this thread is being cast into oblivion
   */
public Path download(List<URL> urls, String sha256, Optional<String> type, Path output, ExtendedEventHandler eventHandler, Map<String, String> clientEnv) throws IOException, InterruptedException {
    if (Thread.interrupted()) {
        throw new InterruptedException();
    }
    Path destination = getDownloadDestination(urls.get(0), type, output);
    // Used to decide whether to cache the download at the end of this method.
    boolean isCaching = false;
    if (!sha256.isEmpty()) {
        try {
            String currentSha256 = RepositoryCache.getChecksum(KeyType.SHA256, destination);
            if (currentSha256.equals(sha256)) {
                // No need to download.
                return destination;
            }
        } catch (IOException e) {
        // Ignore error trying to hash. We'll attempt to retrieve from cache or just download again.
        }
        if (repositoryCache.isEnabled()) {
            isCaching = true;
            Path cachedDestination = repositoryCache.get(sha256, destination, KeyType.SHA256);
            if (cachedDestination != null) {
                // Cache hit!
                return cachedDestination;
            }
        }
    }
    // TODO: Consider using Dagger2 to automate this.
    Clock clock = new JavaClock();
    Sleeper sleeper = new JavaSleeper();
    Locale locale = Locale.getDefault();
    ProxyHelper proxyHelper = new ProxyHelper(clientEnv);
    HttpConnector connector = new HttpConnector(locale, eventHandler, proxyHelper, sleeper);
    ProgressInputStream.Factory progressInputStreamFactory = new ProgressInputStream.Factory(locale, clock, eventHandler);
    HttpStream.Factory httpStreamFactory = new HttpStream.Factory(progressInputStreamFactory);
    HttpConnectorMultiplexer multiplexer = new HttpConnectorMultiplexer(eventHandler, connector, httpStreamFactory, clock, sleeper);
    // Connect to the best mirror and download the file, while reporting progress to the CLI.
    semaphore.acquire();
    try (HttpStream payload = multiplexer.connect(urls, sha256);
        OutputStream out = destination.getOutputStream()) {
        ByteStreams.copy(payload, out);
    } catch (InterruptedIOException e) {
        throw new InterruptedException();
    } catch (IOException e) {
        throw new IOException("Error downloading " + urls + " to " + destination + ": " + e.getMessage());
    } finally {
        semaphore.release();
    }
    if (isCaching) {
        repositoryCache.put(sha256, destination, KeyType.SHA256);
    }
    return destination;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Locale(java.util.Locale) InterruptedIOException(java.io.InterruptedIOException) OutputStream(java.io.OutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) JavaSleeper(com.google.devtools.build.lib.util.JavaSleeper) Sleeper(com.google.devtools.build.lib.util.Sleeper) Clock(com.google.devtools.build.lib.util.Clock) JavaClock(com.google.devtools.build.lib.util.JavaClock) JavaClock(com.google.devtools.build.lib.util.JavaClock) JavaSleeper(com.google.devtools.build.lib.util.JavaSleeper)

Aggregations

Clock (com.google.devtools.build.lib.util.Clock)1 JavaClock (com.google.devtools.build.lib.util.JavaClock)1 JavaSleeper (com.google.devtools.build.lib.util.JavaSleeper)1 Sleeper (com.google.devtools.build.lib.util.Sleeper)1 Path (com.google.devtools.build.lib.vfs.Path)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Locale (java.util.Locale)1