in kernel-api/src/main/scala/org/apache/toree/interpreter/broker/BrokerProcess.scala [69:129]
protected def getTmpDirectory: String = System.getProperty("java.io.tmpdir")
/**
* Returns the subdirectory to use to place any files needed for the process.
*
* @return The directory path as a string
*/
protected lazy val getSubDirectory: String =
s"kernel-$brokerName-" + java.util.UUID.randomUUID().toString
/**
* Copies a resource from an input stream to an output stream.
*
* @param inputStream The input stream to copy from
* @param outputStream The output stream to copy to
*
* @return The result of the copy operation
*/
protected def copy(inputStream: InputStream, outputStream: OutputStream) =
IOUtils.copy(inputStream, outputStream)
/**
* Copies a file from the kernel resources to the temporary directory.
*
* @param resource The resource to copy
*
* @return The string path pointing to the resource's destination
*/
protected def copyResourceToTmp(resource: String): String = {
val brokerRunnerResourceStream = classLoader.getResourceAsStream(resource)
val tmpDirectory = Option(getTmpDirectory)
.getOrElse(throw new BrokerException("java.io.tmpdir is not set!"))
val subDirectory = Option(getSubDirectory).getOrElse("")
val outputName = FilenameUtils.getName(resource)
val outputDir = Seq(tmpDirectory, subDirectory)
.filter(_.trim.nonEmpty).mkString("/")
val outputScript = new File(FilenameUtils.concat(outputDir, outputName))
// If our script destination is a directory, we cannot copy the script
if (outputScript.exists() && outputScript.isDirectory)
throw new BrokerException(s"Failed to create script: $outputScript")
// Ensure that all of the directories leading up to the script exist
val outputDirFile = new File(outputDir)
if (!outputDirFile.exists()) outputDirFile.mkdirs()
// Copy the script to the specified temporary destination
val outputScriptStream = new FileOutputStream(outputScript)
copy(
brokerRunnerResourceStream,
outputScriptStream
)
outputScriptStream.close()
// Return the destination of the script
val destination = outputScript.getPath
logger.debug(s"Successfully copied $resource to $destination")
destination
}