diff options
author | eaceaser <eac@twitter.com> | 2010-10-24 15:20:04 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-11-07 02:08:37 -0500 |
commit | 854e3f8c865e0b00bca571df07b71d369d8fbfba (patch) | |
tree | f54a92ae0ed5b1a5cc9c235087df8af87e123792 /libraries/eval | |
parent | 844eb291ca490aec45f3198224ecedd4665023e2 (diff) | |
download | cbt-854e3f8c865e0b00bca571df07b71d369d8fbfba.tar.gz cbt-854e3f8c865e0b00bca571df07b71d369d8fbfba.tar.bz2 cbt-854e3f8c865e0b00bca571df07b71d369d8fbfba.zip |
Add a unique-to-JVM id to the file to prevent colissions.
Diffstat (limited to 'libraries/eval')
-rw-r--r-- | libraries/eval/Eval.scala | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/libraries/eval/Eval.scala b/libraries/eval/Eval.scala index a82645a..702c974 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 random = new Random() + private val jvmId = Math.abs(new Random().nextInt()) private val md = MessageDigest.getInstance("SHA") /** @@ -79,11 +79,10 @@ object Eval { val digest = md.digest(stringToEval.getBytes()) val sha = new BigInteger(digest).toString(16) - val className = "Evaluator" + sha + val className = "Evaluator" + sha + "_" + jvmId val targetDir = new File(System.getProperty("java.io.tmpdir")) - val targetFile = new File(targetDir, className + ".scala") - if (!targetFile.exists) { - wrapInClass(stringToEval, className, targetFile) + ifUncompiled(targetDir, className) { targetFile => + wrapInClassAndOutput(stringToEval, className, targetFile) compile(targetFile, targetDir) } val clazz = loadClass(targetDir, className) @@ -100,13 +99,25 @@ object Eval { apply(stringToEval) } + private def ifUncompiled(targetDir: File, className: String)(work: File => Unit) { + 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 + // integer and compiled this file. Or, more likely, this method was called + // simultaneously from two threads. + } + targetFile.deleteOnExit() + work(targetFile) + } + } + /** * Wrap sourceCode in a new class that has an apply method that evaluates that sourceCode. * Write generated (temporary) classes to targetDir */ - private def wrapInClass(sourceCode: String, className: String, targetFile: File) { - targetFile.createNewFile() - targetFile.deleteOnExit() + private def wrapInClassAndOutput(sourceCode: String, className: String, targetFile: File) { val writer = new FileWriter(targetFile) writer.write("class " + className + " extends (() => Any) {\n") writer.write(" def apply() = {\n") |