Reamer commented on code in PR #4746:
URL: https://github.com/apache/zeppelin/pull/4746#discussion_r1560782307


##########
zeppelin-test/src/main/java/org/apache/zeppelin/test/DownloadUtils.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.tongfei.progressbar.DelegatingProgressBarConsumer;
+import me.tongfei.progressbar.ProgressBar;
+import me.tongfei.progressbar.ProgressBarBuilder;
+import me.tongfei.progressbar.ProgressBarStyle;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Utility class for downloading spark/flink/livy. This is used for 
spark/flink integration test.
+ */
+public class DownloadUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DownloadUtils.class);
+
+  private static final String MIRROR_URL = 
"https://www.apache.org/dyn/closer.lua?preferred=true";;
+  private static final String ARCHIVE_URL = "https://archive.apache.org/dist/";;
+
+  private static String downloadFolder = System.getProperty("user.home") + 
"/.cache";
+  public static final String DEFAULT_SPARK_VERSION = "3.4.2";
+  public static final String DEFAULT_SPARK_HADOOP_VERSION = "3";
+
+  private DownloadUtils() {
+    throw new IllegalStateException("Utility class");
+  }
+
+  static {
+    try {
+      FileUtils.forceMkdir(new File(downloadFolder));
+    } catch (IOException e) {
+      throw new RuntimeException("Fail to create download folder: " + 
downloadFolder, e);
+    }
+  }
+
+  /**
+   * Download Spark with default versions
+   *
+   * @return home of Spark installation
+   */
+  public static String downloadSpark() {
+    return downloadSpark(DEFAULT_SPARK_VERSION, DEFAULT_SPARK_HADOOP_VERSION);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String 
hadoopVersion) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    File targetSparkHomeFolder =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion);
+    return downloadSpark(sparkVersion, hadoopVersion, targetSparkHomeFolder);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @param targetSparkHomeFolder - where should the spark archive be extracted
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String hadoopVersion,
+      File targetSparkHomeFolder) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    sparkFolder.mkdir();
+    if (targetSparkHomeFolder.exists()) {
+      LOGGER.info("Skip to download Spark {}-{} as it is already downloaded.", 
sparkVersion,
+          hadoopVersion);
+      return targetSparkHomeFolder.getAbsolutePath();
+    }
+    File sparkTarGZ =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion + ".tgz");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      URL archiveURL = new URL(ARCHIVE_URL + generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      LOGGER.info("Download Spark {}-{}", sparkVersion, hadoopVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), sparkTarGZ);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)

Review Comment:
   I do not see the use case, but have implemented this via the environment 
variable `PROGRESS_BAR_UPDATE_INTERVAL`.



##########
zeppelin-test/src/main/java/org/apache/zeppelin/test/DownloadUtils.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.tongfei.progressbar.DelegatingProgressBarConsumer;
+import me.tongfei.progressbar.ProgressBar;
+import me.tongfei.progressbar.ProgressBarBuilder;
+import me.tongfei.progressbar.ProgressBarStyle;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Utility class for downloading spark/flink/livy. This is used for 
spark/flink integration test.
+ */
+public class DownloadUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DownloadUtils.class);
+
+  private static final String MIRROR_URL = 
"https://www.apache.org/dyn/closer.lua?preferred=true";;

Review Comment:
   Good point. I have adapted it.



##########
zeppelin-test/src/test/java/org/apache/zeppelin/test/DownloadUtilsTest.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+
+//@Disabled("Takes a long time and depends on external factors.")
+class DownloadUtilsTest {
+
+  @Test
+  void downloadHadoop() {
+    String hadoopHome = DownloadUtils.downloadHadoop("3.4.0");

Review Comment:
   These are only test methods. It is up to the caller to decide which version 
is finally downloaded.



##########
zeppelin-test/src/main/java/org/apache/zeppelin/test/DownloadUtils.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.tongfei.progressbar.DelegatingProgressBarConsumer;
+import me.tongfei.progressbar.ProgressBar;
+import me.tongfei.progressbar.ProgressBarBuilder;
+import me.tongfei.progressbar.ProgressBarStyle;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Utility class for downloading spark/flink/livy. This is used for 
spark/flink integration test.
+ */
+public class DownloadUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DownloadUtils.class);
+
+  private static final String MIRROR_URL = 
"https://www.apache.org/dyn/closer.lua?preferred=true";;
+  private static final String ARCHIVE_URL = "https://archive.apache.org/dist/";;
+
+  private static String downloadFolder = System.getProperty("user.home") + 
"/.cache";
+  public static final String DEFAULT_SPARK_VERSION = "3.4.2";
+  public static final String DEFAULT_SPARK_HADOOP_VERSION = "3";
+
+  private DownloadUtils() {
+    throw new IllegalStateException("Utility class");
+  }
+
+  static {
+    try {
+      FileUtils.forceMkdir(new File(downloadFolder));
+    } catch (IOException e) {
+      throw new RuntimeException("Fail to create download folder: " + 
downloadFolder, e);
+    }
+  }
+
+  /**
+   * Download Spark with default versions
+   *
+   * @return home of Spark installation
+   */
+  public static String downloadSpark() {
+    return downloadSpark(DEFAULT_SPARK_VERSION, DEFAULT_SPARK_HADOOP_VERSION);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String 
hadoopVersion) {

Review Comment:
   At the moment I cannot say what the download directory of Spark 4.x looks 
like. But I have included the Scala version (default `null`).



##########
zeppelin-test/src/main/java/org/apache/zeppelin/test/DownloadUtils.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.tongfei.progressbar.DelegatingProgressBarConsumer;
+import me.tongfei.progressbar.ProgressBar;
+import me.tongfei.progressbar.ProgressBarBuilder;
+import me.tongfei.progressbar.ProgressBarStyle;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Utility class for downloading spark/flink/livy. This is used for 
spark/flink integration test.
+ */
+public class DownloadUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DownloadUtils.class);
+
+  private static final String MIRROR_URL = 
"https://www.apache.org/dyn/closer.lua?preferred=true";;
+  private static final String ARCHIVE_URL = "https://archive.apache.org/dist/";;
+
+  private static String downloadFolder = System.getProperty("user.home") + 
"/.cache";
+  public static final String DEFAULT_SPARK_VERSION = "3.4.2";
+  public static final String DEFAULT_SPARK_HADOOP_VERSION = "3";
+
+  private DownloadUtils() {
+    throw new IllegalStateException("Utility class");
+  }
+
+  static {
+    try {
+      FileUtils.forceMkdir(new File(downloadFolder));
+    } catch (IOException e) {
+      throw new RuntimeException("Fail to create download folder: " + 
downloadFolder, e);
+    }
+  }
+
+  /**
+   * Download Spark with default versions
+   *
+   * @return home of Spark installation
+   */
+  public static String downloadSpark() {
+    return downloadSpark(DEFAULT_SPARK_VERSION, DEFAULT_SPARK_HADOOP_VERSION);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String 
hadoopVersion) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    File targetSparkHomeFolder =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion);
+    return downloadSpark(sparkVersion, hadoopVersion, targetSparkHomeFolder);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @param targetSparkHomeFolder - where should the spark archive be extracted
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String hadoopVersion,
+      File targetSparkHomeFolder) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    sparkFolder.mkdir();
+    if (targetSparkHomeFolder.exists()) {
+      LOGGER.info("Skip to download Spark {}-{} as it is already downloaded.", 
sparkVersion,
+          hadoopVersion);
+      return targetSparkHomeFolder.getAbsolutePath();
+    }
+    File sparkTarGZ =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion + ".tgz");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      URL archiveURL = new URL(ARCHIVE_URL + generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      LOGGER.info("Download Spark {}-{}", sparkVersion, hadoopVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), sparkTarGZ);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (
+          InputStream fis = Files.newInputStream(sparkTarGZ.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          InputStream gzis = new GzipCompressorInputStream(bis);
+          ArchiveInputStream<TarArchiveEntry> o = new 
TarArchiveInputStream(gzis)) {
+        LOGGER.info("Unarchive Spark {}-{} to {}", sparkVersion, hadoopVersion,
+            targetSparkHomeFolder);
+        unarchive(o, targetSparkHomeFolder, 1);
+        LOGGER.info("Unarchive Spark {}-{} done", sparkVersion, hadoopVersion);
+      }
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download spark", e);
+    }
+    return targetSparkHomeFolder.getAbsolutePath();
+  }
+
+
+  public static void download(String url, int retries, File dst) throws 
IOException {
+    download(new URL(url), retries, dst);
+  }
+
+  public static void download(DownloadRequest downloadRequest, File dst) 
throws IOException {
+    if (dst.exists()) {
+      LOGGER.info("Skip Download of {}, because it exists", dst);
+    } else {
+      boolean urlDownload = download(downloadRequest.getUrl(), 
downloadRequest.getRetries(), dst);
+      if (urlDownload) {
+        LOGGER.info("Download successfully");
+        return;
+      }
+      Optional<URL> alternativeURL = downloadRequest.getAlternativeUrl();
+      if (alternativeURL.isPresent()) {
+        urlDownload = download(alternativeURL.get(), 
downloadRequest.getRetries(), dst);
+        if (urlDownload) {
+          LOGGER.info("Download from alternative successfully");
+          return;
+        }
+      }
+      throw new IOException("Unable to download from " + 
downloadRequest.getUrl());
+    }
+  }
+
+  private static boolean download(URL url, int retries, File dst) {
+    int retry = 0;
+    while (retry < retries) {
+      try {
+        HttpURLConnection httpConnection = (HttpURLConnection) 
(url.openConnection());
+        long completeFileSize = httpConnection.getContentLength();
+        ProgressBarBuilder pbb = new ProgressBarBuilder()
+            .setTaskName("Download " + dst.getName())
+            .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+            .setStyle(ProgressBarStyle.ASCII)
+            .setUpdateIntervalMillis(1000)
+            .setInitialMax(completeFileSize)
+            .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+        try (
+            OutputStream fileOS = Files.newOutputStream(dst.toPath());
+            InputStream is = url.openStream();
+            InputStream pbis = ProgressBar.wrap(is, pbb);
+            InputStream bis = new BufferedInputStream(pbis)) {
+          IOUtils.copyLarge(bis, fileOS);
+          return true;
+        }
+      } catch (IOException e) {
+        LOGGER.info("Unable to download from {}", url, e);
+        ++retry;
+      }
+    }
+    return false;
+  }
+
+  /**
+   * @param livyVersion
+   * @param targetLivyHomeFolder
+   * @return livyHome
+   */
+  public static String downloadLivy(String livyVersion, File 
targetLivyHomeFolder) {
+    File livyDownloadFolder = new File(downloadFolder, "livy");
+    livyDownloadFolder.mkdir();
+    if (targetLivyHomeFolder.exists()) {
+      LOGGER.info("Skip to download Livy {} as it is already downloaded.", 
livyVersion);
+      return targetLivyHomeFolder.getAbsolutePath();
+    }
+    File livyZip = new File(livyDownloadFolder, "livy-" + livyVersion + 
".zip");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
"incubator/livy/"
+              + livyVersion
+              + "/apache-livy-" + livyVersion + "-bin.zip");
+      URL archiveURL = new 
URL("https://archive.apache.org/dist/incubator/livy/"; + livyVersion
+          + "/apache-livy-" + livyVersion + "-bin.zip");
+      LOGGER.info("Download Livy {}", livyVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), livyZip);
+      LOGGER.info("Unzip Livy {} to {}", livyVersion, targetLivyHomeFolder);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv Livy")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (InputStream fis = Files.newInputStream(livyZip.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          ZipInputStream zis = new ZipInputStream(bis)) {
+        unzip(zis, targetLivyHomeFolder, 1);
+      }
+      LOGGER.info("Unzip Livy {} done", livyVersion);
+      // Create logs directory
+      File logs = new File(targetLivyHomeFolder, "logs");
+      logs.mkdir();
+    } catch (MalformedURLException e) {
+      LOGGER.error("invalid URL", e);
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download livy", e);
+    }
+    return targetLivyHomeFolder.getAbsolutePath();
+  }
+
+  /**
+   * @param livyVersion
+   * @return return livyHome
+   * @throws IOException
+   */
+  public static String downloadLivy(String livyVersion) {

Review Comment:
   I have added methods and related tests to download livy 0.8.0 in the future.



##########
zeppelin-test/pom.xml:
##########
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements.  See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You under the Apache License, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License.  You may obtain a copy of the License at
+  ~
+  ~    http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.zeppelin</groupId>
+    <artifactId>zeppelin</artifactId>
+    <version>0.12.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>zeppelin-test</artifactId>
+  <name>Zeppelin: Test</name>
+  <description>Zeppelin test code used in other modules</description>
+  <properties>
+    <progressbar.version>0.9.5</progressbar.version>
+  </properties>
+  <packaging>jar</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-compress</artifactId>
+      <version>${commons.compress.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>me.tongfei</groupId>
+      <artifactId>progressbar</artifactId>
+      <version>${progressbar.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-engine</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-slf4j-impl</artifactId>
+      <version>${log4j2.version}</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Review Comment:
   Thanks for the hint. I often point out this uncleanliness to my colleagues 
myself. Adjusted.



##########
zeppelin-test/src/main/java/org/apache/zeppelin/test/DownloadUtils.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.tongfei.progressbar.DelegatingProgressBarConsumer;
+import me.tongfei.progressbar.ProgressBar;
+import me.tongfei.progressbar.ProgressBarBuilder;
+import me.tongfei.progressbar.ProgressBarStyle;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Utility class for downloading spark/flink/livy. This is used for 
spark/flink integration test.
+ */
+public class DownloadUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DownloadUtils.class);
+
+  private static final String MIRROR_URL = 
"https://www.apache.org/dyn/closer.lua?preferred=true";;
+  private static final String ARCHIVE_URL = "https://archive.apache.org/dist/";;
+
+  private static String downloadFolder = System.getProperty("user.home") + 
"/.cache";
+  public static final String DEFAULT_SPARK_VERSION = "3.4.2";
+  public static final String DEFAULT_SPARK_HADOOP_VERSION = "3";
+
+  private DownloadUtils() {
+    throw new IllegalStateException("Utility class");
+  }
+
+  static {
+    try {
+      FileUtils.forceMkdir(new File(downloadFolder));
+    } catch (IOException e) {
+      throw new RuntimeException("Fail to create download folder: " + 
downloadFolder, e);
+    }
+  }
+
+  /**
+   * Download Spark with default versions
+   *
+   * @return home of Spark installation
+   */
+  public static String downloadSpark() {
+    return downloadSpark(DEFAULT_SPARK_VERSION, DEFAULT_SPARK_HADOOP_VERSION);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String 
hadoopVersion) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    File targetSparkHomeFolder =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion);
+    return downloadSpark(sparkVersion, hadoopVersion, targetSparkHomeFolder);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @param targetSparkHomeFolder - where should the spark archive be extracted
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String hadoopVersion,
+      File targetSparkHomeFolder) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    sparkFolder.mkdir();
+    if (targetSparkHomeFolder.exists()) {
+      LOGGER.info("Skip to download Spark {}-{} as it is already downloaded.", 
sparkVersion,
+          hadoopVersion);
+      return targetSparkHomeFolder.getAbsolutePath();
+    }
+    File sparkTarGZ =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion + ".tgz");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      URL archiveURL = new URL(ARCHIVE_URL + generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      LOGGER.info("Download Spark {}-{}", sparkVersion, hadoopVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), sparkTarGZ);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (
+          InputStream fis = Files.newInputStream(sparkTarGZ.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          InputStream gzis = new GzipCompressorInputStream(bis);
+          ArchiveInputStream<TarArchiveEntry> o = new 
TarArchiveInputStream(gzis)) {
+        LOGGER.info("Unarchive Spark {}-{} to {}", sparkVersion, hadoopVersion,
+            targetSparkHomeFolder);
+        unarchive(o, targetSparkHomeFolder, 1);
+        LOGGER.info("Unarchive Spark {}-{} done", sparkVersion, hadoopVersion);
+      }
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download spark", e);
+    }
+    return targetSparkHomeFolder.getAbsolutePath();
+  }
+
+
+  public static void download(String url, int retries, File dst) throws 
IOException {
+    download(new URL(url), retries, dst);
+  }
+
+  public static void download(DownloadRequest downloadRequest, File dst) 
throws IOException {
+    if (dst.exists()) {
+      LOGGER.info("Skip Download of {}, because it exists", dst);
+    } else {
+      boolean urlDownload = download(downloadRequest.getUrl(), 
downloadRequest.getRetries(), dst);
+      if (urlDownload) {
+        LOGGER.info("Download successfully");
+        return;
+      }
+      Optional<URL> alternativeURL = downloadRequest.getAlternativeUrl();
+      if (alternativeURL.isPresent()) {
+        urlDownload = download(alternativeURL.get(), 
downloadRequest.getRetries(), dst);
+        if (urlDownload) {
+          LOGGER.info("Download from alternative successfully");
+          return;
+        }
+      }
+      throw new IOException("Unable to download from " + 
downloadRequest.getUrl());
+    }
+  }
+
+  private static boolean download(URL url, int retries, File dst) {
+    int retry = 0;
+    while (retry < retries) {
+      try {
+        HttpURLConnection httpConnection = (HttpURLConnection) 
(url.openConnection());
+        long completeFileSize = httpConnection.getContentLength();
+        ProgressBarBuilder pbb = new ProgressBarBuilder()
+            .setTaskName("Download " + dst.getName())
+            .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+            .setStyle(ProgressBarStyle.ASCII)
+            .setUpdateIntervalMillis(1000)
+            .setInitialMax(completeFileSize)
+            .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+        try (
+            OutputStream fileOS = Files.newOutputStream(dst.toPath());
+            InputStream is = url.openStream();
+            InputStream pbis = ProgressBar.wrap(is, pbb);
+            InputStream bis = new BufferedInputStream(pbis)) {
+          IOUtils.copyLarge(bis, fileOS);
+          return true;
+        }
+      } catch (IOException e) {
+        LOGGER.info("Unable to download from {}", url, e);
+        ++retry;
+      }
+    }
+    return false;
+  }
+
+  /**
+   * @param livyVersion
+   * @param targetLivyHomeFolder
+   * @return livyHome
+   */
+  public static String downloadLivy(String livyVersion, File 
targetLivyHomeFolder) {
+    File livyDownloadFolder = new File(downloadFolder, "livy");
+    livyDownloadFolder.mkdir();
+    if (targetLivyHomeFolder.exists()) {
+      LOGGER.info("Skip to download Livy {} as it is already downloaded.", 
livyVersion);
+      return targetLivyHomeFolder.getAbsolutePath();
+    }
+    File livyZip = new File(livyDownloadFolder, "livy-" + livyVersion + 
".zip");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
"incubator/livy/"
+              + livyVersion
+              + "/apache-livy-" + livyVersion + "-bin.zip");
+      URL archiveURL = new 
URL("https://archive.apache.org/dist/incubator/livy/"; + livyVersion
+          + "/apache-livy-" + livyVersion + "-bin.zip");
+      LOGGER.info("Download Livy {}", livyVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), livyZip);
+      LOGGER.info("Unzip Livy {} to {}", livyVersion, targetLivyHomeFolder);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv Livy")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (InputStream fis = Files.newInputStream(livyZip.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          ZipInputStream zis = new ZipInputStream(bis)) {
+        unzip(zis, targetLivyHomeFolder, 1);
+      }
+      LOGGER.info("Unzip Livy {} done", livyVersion);
+      // Create logs directory
+      File logs = new File(targetLivyHomeFolder, "logs");
+      logs.mkdir();
+    } catch (MalformedURLException e) {
+      LOGGER.error("invalid URL", e);
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download livy", e);
+    }
+    return targetLivyHomeFolder.getAbsolutePath();
+  }
+
+  /**
+   * @param livyVersion
+   * @return return livyHome
+   * @throws IOException
+   */
+  public static String downloadLivy(String livyVersion) {
+    File livyDownloadFolder = new File(downloadFolder, "livy");
+    File targetLivyHomeFolder = new File(livyDownloadFolder, "livy-" + 
livyVersion);
+    return downloadLivy(livyVersion, targetLivyHomeFolder);
+  }
+
+  private static File newFile(File destinationDir, ZipEntry zipEntry, int 
strip)
+      throws IOException {
+    String filename = zipEntry.getName();
+    for (int i = 0; i < strip; ++i) {
+      if (filename.contains(File.separator)) {
+        filename = filename.substring(filename.indexOf(File.separator) + 1);
+      }
+    }
+    File destFile = new File(destinationDir, filename);
+    String destDirPath = destinationDir.getCanonicalPath();
+    String destFilePath = destFile.getCanonicalPath();
+
+    if (!destFilePath.startsWith(destDirPath + File.separator)) {
+      throw new IOException("Entry is outside of the target dir: " + 
zipEntry.getName());
+    }
+
+    return destFile;
+  }
+
+  private static File newFile(File destDir, ArchiveEntry archiveEntry, int 
strip)
+      throws IOException {
+    String filename = archiveEntry.getName();
+    for (int i = 0; i < strip; ++i) {
+      if (filename.contains(File.separator)) {
+        filename = filename.substring(filename.indexOf(File.separator) + 1);
+      }
+    }
+    File destFile = new File(destDir, filename);
+    String destDirPath = destDir.getCanonicalPath();
+    String destFilePath = destFile.getCanonicalPath();
+
+    if (!destFilePath.startsWith(destDirPath + File.separator)) {
+      throw new IOException("Entry is outside of the target dir: " + 
archiveEntry.getName());
+    }
+
+    return destFile;
+  }
+
+  private static void unarchive(ArchiveInputStream<? extends ArchiveEntry> 
ais, File destDir,
+      int strip) throws IOException {
+    byte[] buffer = new byte[1024];
+    ArchiveEntry archiveEntry = ais.getNextEntry();
+    while (archiveEntry != null) {
+      File newFile;
+      try {
+        newFile = newFile(destDir, archiveEntry, strip);
+      } catch (IOException e) {
+        LOGGER.info("Skip {}", archiveEntry.getName());
+        archiveEntry = ais.getNextEntry();
+        continue;
+      }
+      if (archiveEntry.isDirectory()) {
+        if (!newFile.isDirectory() && !newFile.mkdirs()) {
+          throw new IOException("Failed to create directory " + newFile);
+        }
+      } else {
+        // fix for Windows-created archives

Review Comment:
   I have the code from here. I don't think it's out of the question to support 
other operating systems in the future.
   However, there is no comment in their Git. I will therefore also remove it.
   
https://github.com/eugenp/tutorials/blob/c0559cbb6d6c66c3a87898805e28310a02a52458/core-java-modules/core-java-io/src/main/java/com/baeldung/unzip/UnzipFile.java#L23-L27



##########
zeppelin-test/src/main/java/org/apache/zeppelin/test/DownloadUtils.java:
##########
@@ -0,0 +1,537 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zeppelin.test;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import me.tongfei.progressbar.DelegatingProgressBarConsumer;
+import me.tongfei.progressbar.ProgressBar;
+import me.tongfei.progressbar.ProgressBarBuilder;
+import me.tongfei.progressbar.ProgressBarStyle;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * Utility class for downloading spark/flink/livy. This is used for 
spark/flink integration test.
+ */
+public class DownloadUtils {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DownloadUtils.class);
+
+  private static final String MIRROR_URL = 
"https://www.apache.org/dyn/closer.lua?preferred=true";;
+  private static final String ARCHIVE_URL = "https://archive.apache.org/dist/";;
+
+  private static String downloadFolder = System.getProperty("user.home") + 
"/.cache";
+  public static final String DEFAULT_SPARK_VERSION = "3.4.2";
+  public static final String DEFAULT_SPARK_HADOOP_VERSION = "3";
+
+  private DownloadUtils() {
+    throw new IllegalStateException("Utility class");
+  }
+
+  static {
+    try {
+      FileUtils.forceMkdir(new File(downloadFolder));
+    } catch (IOException e) {
+      throw new RuntimeException("Fail to create download folder: " + 
downloadFolder, e);
+    }
+  }
+
+  /**
+   * Download Spark with default versions
+   *
+   * @return home of Spark installation
+   */
+  public static String downloadSpark() {
+    return downloadSpark(DEFAULT_SPARK_VERSION, DEFAULT_SPARK_HADOOP_VERSION);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String 
hadoopVersion) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    File targetSparkHomeFolder =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion);
+    return downloadSpark(sparkVersion, hadoopVersion, targetSparkHomeFolder);
+  }
+
+  /**
+   * Download of a Spark distribution
+   *
+   * @param sparkVersion
+   * @param hadoopVersion
+   * @param targetSparkHomeFolder - where should the spark archive be extracted
+   * @return home of Spark installation
+   */
+  public static String downloadSpark(String sparkVersion, String hadoopVersion,
+      File targetSparkHomeFolder) {
+    File sparkFolder = new File(downloadFolder, "spark");
+    sparkFolder.mkdir();
+    if (targetSparkHomeFolder.exists()) {
+      LOGGER.info("Skip to download Spark {}-{} as it is already downloaded.", 
sparkVersion,
+          hadoopVersion);
+      return targetSparkHomeFolder.getAbsolutePath();
+    }
+    File sparkTarGZ =
+        new File(sparkFolder, "spark-" + sparkVersion + "-bin-hadoop" + 
hadoopVersion + ".tgz");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      URL archiveURL = new URL(ARCHIVE_URL + generateDownloadURL(
+              "spark", sparkVersion, "-bin-hadoop" + hadoopVersion + ".tgz", 
"spark"));
+      LOGGER.info("Download Spark {}-{}", sparkVersion, hadoopVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), sparkTarGZ);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (
+          InputStream fis = Files.newInputStream(sparkTarGZ.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          InputStream gzis = new GzipCompressorInputStream(bis);
+          ArchiveInputStream<TarArchiveEntry> o = new 
TarArchiveInputStream(gzis)) {
+        LOGGER.info("Unarchive Spark {}-{} to {}", sparkVersion, hadoopVersion,
+            targetSparkHomeFolder);
+        unarchive(o, targetSparkHomeFolder, 1);
+        LOGGER.info("Unarchive Spark {}-{} done", sparkVersion, hadoopVersion);
+      }
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download spark", e);
+    }
+    return targetSparkHomeFolder.getAbsolutePath();
+  }
+
+
+  public static void download(String url, int retries, File dst) throws 
IOException {
+    download(new URL(url), retries, dst);
+  }
+
+  public static void download(DownloadRequest downloadRequest, File dst) 
throws IOException {
+    if (dst.exists()) {
+      LOGGER.info("Skip Download of {}, because it exists", dst);
+    } else {
+      boolean urlDownload = download(downloadRequest.getUrl(), 
downloadRequest.getRetries(), dst);
+      if (urlDownload) {
+        LOGGER.info("Download successfully");
+        return;
+      }
+      Optional<URL> alternativeURL = downloadRequest.getAlternativeUrl();
+      if (alternativeURL.isPresent()) {
+        urlDownload = download(alternativeURL.get(), 
downloadRequest.getRetries(), dst);
+        if (urlDownload) {
+          LOGGER.info("Download from alternative successfully");
+          return;
+        }
+      }
+      throw new IOException("Unable to download from " + 
downloadRequest.getUrl());
+    }
+  }
+
+  private static boolean download(URL url, int retries, File dst) {
+    int retry = 0;
+    while (retry < retries) {
+      try {
+        HttpURLConnection httpConnection = (HttpURLConnection) 
(url.openConnection());
+        long completeFileSize = httpConnection.getContentLength();
+        ProgressBarBuilder pbb = new ProgressBarBuilder()
+            .setTaskName("Download " + dst.getName())
+            .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+            .setStyle(ProgressBarStyle.ASCII)
+            .setUpdateIntervalMillis(1000)
+            .setInitialMax(completeFileSize)
+            .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+        try (
+            OutputStream fileOS = Files.newOutputStream(dst.toPath());
+            InputStream is = url.openStream();
+            InputStream pbis = ProgressBar.wrap(is, pbb);
+            InputStream bis = new BufferedInputStream(pbis)) {
+          IOUtils.copyLarge(bis, fileOS);
+          return true;
+        }
+      } catch (IOException e) {
+        LOGGER.info("Unable to download from {}", url, e);
+        ++retry;
+      }
+    }
+    return false;
+  }
+
+  /**
+   * @param livyVersion
+   * @param targetLivyHomeFolder
+   * @return livyHome
+   */
+  public static String downloadLivy(String livyVersion, File 
targetLivyHomeFolder) {
+    File livyDownloadFolder = new File(downloadFolder, "livy");
+    livyDownloadFolder.mkdir();
+    if (targetLivyHomeFolder.exists()) {
+      LOGGER.info("Skip to download Livy {} as it is already downloaded.", 
livyVersion);
+      return targetLivyHomeFolder.getAbsolutePath();
+    }
+    File livyZip = new File(livyDownloadFolder, "livy-" + livyVersion + 
".zip");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
"incubator/livy/"
+              + livyVersion
+              + "/apache-livy-" + livyVersion + "-bin.zip");
+      URL archiveURL = new 
URL("https://archive.apache.org/dist/incubator/livy/"; + livyVersion
+          + "/apache-livy-" + livyVersion + "-bin.zip");
+      LOGGER.info("Download Livy {}", livyVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), livyZip);
+      LOGGER.info("Unzip Livy {} to {}", livyVersion, targetLivyHomeFolder);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv Livy")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (InputStream fis = Files.newInputStream(livyZip.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          ZipInputStream zis = new ZipInputStream(bis)) {
+        unzip(zis, targetLivyHomeFolder, 1);
+      }
+      LOGGER.info("Unzip Livy {} done", livyVersion);
+      // Create logs directory
+      File logs = new File(targetLivyHomeFolder, "logs");
+      logs.mkdir();
+    } catch (MalformedURLException e) {
+      LOGGER.error("invalid URL", e);
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download livy", e);
+    }
+    return targetLivyHomeFolder.getAbsolutePath();
+  }
+
+  /**
+   * @param livyVersion
+   * @return return livyHome
+   * @throws IOException
+   */
+  public static String downloadLivy(String livyVersion) {
+    File livyDownloadFolder = new File(downloadFolder, "livy");
+    File targetLivyHomeFolder = new File(livyDownloadFolder, "livy-" + 
livyVersion);
+    return downloadLivy(livyVersion, targetLivyHomeFolder);
+  }
+
+  private static File newFile(File destinationDir, ZipEntry zipEntry, int 
strip)
+      throws IOException {
+    String filename = zipEntry.getName();
+    for (int i = 0; i < strip; ++i) {
+      if (filename.contains(File.separator)) {
+        filename = filename.substring(filename.indexOf(File.separator) + 1);
+      }
+    }
+    File destFile = new File(destinationDir, filename);
+    String destDirPath = destinationDir.getCanonicalPath();
+    String destFilePath = destFile.getCanonicalPath();
+
+    if (!destFilePath.startsWith(destDirPath + File.separator)) {
+      throw new IOException("Entry is outside of the target dir: " + 
zipEntry.getName());
+    }
+
+    return destFile;
+  }
+
+  private static File newFile(File destDir, ArchiveEntry archiveEntry, int 
strip)
+      throws IOException {
+    String filename = archiveEntry.getName();
+    for (int i = 0; i < strip; ++i) {
+      if (filename.contains(File.separator)) {
+        filename = filename.substring(filename.indexOf(File.separator) + 1);
+      }
+    }
+    File destFile = new File(destDir, filename);
+    String destDirPath = destDir.getCanonicalPath();
+    String destFilePath = destFile.getCanonicalPath();
+
+    if (!destFilePath.startsWith(destDirPath + File.separator)) {
+      throw new IOException("Entry is outside of the target dir: " + 
archiveEntry.getName());
+    }
+
+    return destFile;
+  }
+
+  private static void unarchive(ArchiveInputStream<? extends ArchiveEntry> 
ais, File destDir,
+      int strip) throws IOException {
+    byte[] buffer = new byte[1024];
+    ArchiveEntry archiveEntry = ais.getNextEntry();
+    while (archiveEntry != null) {
+      File newFile;
+      try {
+        newFile = newFile(destDir, archiveEntry, strip);
+      } catch (IOException e) {
+        LOGGER.info("Skip {}", archiveEntry.getName());
+        archiveEntry = ais.getNextEntry();
+        continue;
+      }
+      if (archiveEntry.isDirectory()) {
+        if (!newFile.isDirectory() && !newFile.mkdirs()) {
+          throw new IOException("Failed to create directory " + newFile);
+        }
+      } else {
+        // fix for Windows-created archives
+        File parent = newFile.getParentFile();
+        if (!parent.isDirectory() && !parent.mkdirs()) {
+          throw new IOException("Failed to create directory " + parent);
+        }
+
+        // write file content
+        try (FileOutputStream fos = new FileOutputStream(newFile)) {
+          int len;
+          while ((len = ais.read(buffer)) > 0) {
+            fos.write(buffer, 0, len);
+          }
+        }
+        // Change permissions and metadata
+        if (newFile.getParentFile().getName().contains("bin")
+            && !newFile.setExecutable(true, false)) {
+          LOGGER.info("Setting file {} to executable failed", newFile);
+        }
+        if 
(!newFile.setLastModified(archiveEntry.getLastModifiedDate().getTime())) {
+          LOGGER.info("Setting last modified date to file {} failed", newFile);
+        }
+      }
+      archiveEntry = ais.getNextEntry();
+    }
+  }
+
+  private static void unzip(ZipInputStream zis, File destDir, int strip) 
throws IOException {
+    byte[] buffer = new byte[1024];
+    ZipEntry zipEntry = zis.getNextEntry();
+    while (zipEntry != null) {
+      File newFile;
+      try {
+        newFile = newFile(destDir, zipEntry, strip);
+      } catch (IOException e) {
+        LOGGER.info("Skip {}", zipEntry.getName());
+        zipEntry = zis.getNextEntry();
+        continue;
+      }
+      if (zipEntry.isDirectory()) {
+        if (!newFile.isDirectory() && !newFile.mkdirs()) {
+          throw new IOException("Failed to create directory " + newFile);
+        }
+      } else {
+        // fix for Windows-created archives
+        File parent = newFile.getParentFile();
+        if (!parent.isDirectory() && !parent.mkdirs()) {
+          throw new IOException("Failed to create directory " + parent);
+        }
+
+        // write file content
+        try (FileOutputStream fos = new FileOutputStream(newFile)) {
+          int len;
+          while ((len = zis.read(buffer)) > 0) {
+            fos.write(buffer, 0, len);
+          }
+        }
+        // Change permissions and metadata
+        if (newFile.getParentFile().getName().contains("bin")
+            && !newFile.setExecutable(true, false)) {
+          LOGGER.info("Setting file {} to executable failed", newFile);
+        }
+        if 
(!newFile.setLastModified(zipEntry.getLastModifiedTime().toMillis())) {
+          LOGGER.info("Setting last modified date to file {} failed", newFile);
+        }
+      }
+      zipEntry = zis.getNextEntry();
+    }
+    zis.closeEntry();
+  }
+
+  public static String downloadFlink(String flinkVersion, String scalaVersion) 
{
+    File flinkDownloadFolder = new File(downloadFolder, "flink");
+    flinkDownloadFolder.mkdir();
+    File targetFlinkHomeFolder = new File(flinkDownloadFolder, "flink-" + 
flinkVersion);
+    if (targetFlinkHomeFolder.exists()) {
+      LOGGER.info("Skip to download Flink {}_{} as it is already downloaded.", 
flinkVersion,
+          scalaVersion);
+      return targetFlinkHomeFolder.getAbsolutePath();
+    }
+    File flinkTGZ = new File(flinkDownloadFolder,
+        "flink-" + flinkVersion + "-bin-scala_" + scalaVersion + ".tgz");
+    try {
+      URL mirrorURL = new URL(
+          IOUtils.toString(new URL(MIRROR_URL), StandardCharsets.UTF_8) + 
generateDownloadURL(
+              "flink", flinkVersion, "-bin-scala_" + scalaVersion + ".tgz", 
"flink"));
+      URL archiveURL = new URL(ARCHIVE_URL + generateDownloadURL(
+          "flink", flinkVersion, "-bin-scala_" + scalaVersion + ".tgz", 
"flink"));
+      LOGGER.info("Download Flink {}_{}", flinkVersion, scalaVersion);
+      download(new DownloadRequest(mirrorURL, archiveURL), flinkTGZ);
+      ProgressBarBuilder pbb = new ProgressBarBuilder()
+          .setTaskName("Unarchiv Flink")
+          .setUnit("MiB", 1048576) // setting the progress bar to use MiB as 
the unit
+          .setStyle(ProgressBarStyle.ASCII)
+          .setUpdateIntervalMillis(1000)
+          .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info));
+      try (
+          InputStream fis = Files.newInputStream(flinkTGZ.toPath());
+          InputStream pbis = ProgressBar.wrap(fis, pbb);
+          InputStream bis = new BufferedInputStream(pbis);
+          InputStream gzis = new GzipCompressorInputStream(bis);
+          ArchiveInputStream<TarArchiveEntry> o = new 
TarArchiveInputStream(gzis)) {
+        LOGGER.info("Unarchive Flink {}_{} to {}", flinkVersion, scalaVersion,
+            targetFlinkHomeFolder);
+        unarchive(o, targetFlinkHomeFolder, 1);
+        LOGGER.info("Unarchive Flink done");
+      }
+    } catch (IOException e) {
+      throw new RuntimeException("Unable to download flink", e);
+    }
+
+
+    // download other dependencies for running flink with yarn and hive

Review Comment:
   Many thanks for the hint. I have adjusted the download paths.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to