From 9cdad3866cb2d1ee2ef4566972bc62fc30c42547 Mon Sep 17 00:00:00 2001 From: eaceaser Date: Sun, 24 Oct 2010 17:37:51 -0400 Subject: Place compiled files in their own temporary subdir and clear when jvm closes. --- libraries/eval/Eval.scala | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/libraries/eval/Eval.scala b/libraries/eval/Eval.scala index 702c974..3d8be88 100644 --- a/libraries/eval/Eval.scala +++ b/libraries/eval/Eval.scala @@ -68,7 +68,7 @@ import scala.tools.nsc.{Global, Settings} object Eval { private val compilerPath = jarPathOfClass("scala.tools.nsc.Interpreter") private val libPath = jarPathOfClass("scala.ScalaObject") - private val jvmId = Math.abs(new Random().nextInt()) + private val jvmId = java.lang.Math.abs(new Random().nextInt()) private val md = MessageDigest.getInstance("SHA") /** @@ -79,8 +79,9 @@ object Eval { val digest = md.digest(stringToEval.getBytes()) val sha = new BigInteger(digest).toString(16) - val className = "Evaluator" + sha + "_" + jvmId - val targetDir = new File(System.getProperty("java.io.tmpdir")) + val uniqueId = sha + "_" + jvmId + val className = "Evaluator" + uniqueId + val targetDir = new File(System.getProperty("java.io.tmpdir") + "evaluator_" + uniqueId) ifUncompiled(targetDir, className) { targetFile => wrapInClassAndOutput(stringToEval, className, targetFile) compile(targetFile, targetDir) @@ -99,18 +100,22 @@ object Eval { apply(stringToEval) } - private def ifUncompiled(targetDir: File, className: String)(work: File => Unit) { + private def ifUncompiled(targetDir: File, className: String)(f: File => Unit) { + targetDir.mkdirs() + targetDir.deleteOnExit() + val targetFile = new File(targetDir, className + ".scala") if (!targetFile.exists) { val created = targetFile.createNewFile() if (!created) { - // FIXME: this indicates that either another jvm randomly generated the same + // FIXME: this indicates that another jvm randomly generated the same // integer and compiled this file. Or, more likely, this method was called // simultaneously from two threads. } - targetFile.deleteOnExit() - work(targetFile) + f(targetFile) } + + targetDir.listFiles().foreach { _.deleteOnExit() } } /** -- cgit v1.2.3