override def execute()

in kernel/src/main/scala/org/apache/toree/magic/builtin/AddJar.scala [107:183]


  override def execute(code: String): Unit = {
    val nonOptionArgs = parseArgs(code.trim)

    // Check valid arguments
    if (nonOptionArgs.length != 1) {
      printHelp(printStream, """%AddJar <jar_url>""")
      return
    }

    // Check if the jar we want to download is valid
    val jarRemoteLocation = nonOptionArgs(0)
    if (jarRemoteLocation.isEmpty) {
      printHelp(printStream, """%AddJar <jar_url>""")
      return
    }

    // Get the destination of the jar
    val jarName = getFileFromLocation(jarRemoteLocation)

    // Ensure the URL actually contains a jar or zip file
    if (!jarName.endsWith(".jar") && !jarName.endsWith(".zip")) {
        throw new IllegalArgumentException(
          s"The jar file $jarName must end in .jar or .zip."
        )
    }

    val downloadLocation = getJarDir(config) + "/" + jarName

    logger.debug("Downloading jar to %s".format(downloadLocation))

    val fileDownloadLocation = new File(downloadLocation)

    // Check if exists in cache or force applied
    if (_force || !fileDownloadLocation.exists()) {
      // Report beginning of download
      printStream.println(s"Starting download from $jarRemoteLocation")

      val jar = URI.create(jarRemoteLocation)
      if (HADOOP_FS_SCHEMES.contains(jar.getScheme)) {
        val conf = kernel.sparkContext.hadoopConfiguration
        val jarPath = new Path(jarRemoteLocation)
        val fs = jarPath.getFileSystem(conf)
        val destPath = if (downloadLocation.startsWith("file:")) {
          new Path(downloadLocation)
        } else {
          new Path("file:" + downloadLocation)
        }

        fs.copyToLocalFile(
          false /* keep original file */,
          jarPath, destPath,
          true /* don't create checksum files */)
      } else {
        downloadFile(
          new URL(jarRemoteLocation),
          new File(downloadLocation).toURI.toURL
        )
      }

      // Report download finished
      printStream.println(s"Finished download of $jarName")
    } else {
      printStream.println(s"Using cached version of $jarName")
    }

    // validate jar file
    if(! isValidJar(fileDownloadLocation)) {
      throw new IllegalArgumentException(s"Jar '$jarName' is not valid.")
    }

    if (_magic) {
      val plugins = pluginManager.loadPlugins(fileDownloadLocation)
      pluginManager.initializePlugins(plugins)
    } else {
      kernel.addJars(fileDownloadLocation.toURI)
    }
  }