Skip to content

Commit bfa329e

Browse files
committed
Added code from the leak fix patch
1 parent 31d29fa commit bfa329e

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ import java.math.BigInteger
1212
import java.security.MessageDigest
1313
import java.util.jar.JarFile
1414

15+
import cats.effect._
1516
import cats.effect.syntax.concurrent.catsEffectSyntaxConcurrent
16-
import cats.effect.{ConcurrentEffect, Timer}
1717
import cats.implicits._
1818
import coursier._
1919
import coursier.cache.{ArtifactError, FileCache}
2020
import coursier.util.Sync
2121
import org.scalaexercises.evaluator.Eval.CompilerException
2222
import org.scalaexercises.evaluator.{Dependency => EvaluatorDependency}
2323

24+
import scala.jdk.CollectionConverters.ConcurrentMapHasAsScala
2425
import scala.concurrent.duration._
2526
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
2627
import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
@@ -181,7 +182,7 @@ private class StringCompiler(
181182
messageHandler: Option[Reporter]
182183
) extends Closeable {
183184

184-
//val cache = new java.util.concurrent.ConcurrentHashMap[String, Class[_]]
185+
val cache = new java.util.concurrent.ConcurrentHashMap[String, Class[_]].asScala
185186

186187
trait MessageCollector {
187188
val messages: scala.collection.mutable.ListBuffer[List[String]]
@@ -236,27 +237,15 @@ private class StringCompiler(
236237
}
237238
}
238239
global.cleanup
239-
//cache.clear()
240+
cache.clear()
240241
reporter.reset()
241242
}
242243

243244
def findClass(className: String, classLoader: ClassLoader): Option[Class[_]] =
244-
Try( /*cache.getOrElseUpdate(className, */ classLoader.loadClass(className) /*)*/ ) match {
245+
Try( cache.getOrElseUpdate(className, classLoader.loadClass(className) ) ) match {
245246
case Success(cls) => Some(cls)
246247
case Failure(_) => None
247248
}
248-
// synchronized {
249-
// cache.get(className).orElse {
250-
// try {
251-
// val cls = classLoader.loadClass(className)
252-
// cache(className) = cls
253-
// Some(cls)
254-
// } catch {
255-
// case e: ClassNotFoundException => None
256-
// }
257-
// }
258-
// }
259-
//}
260249

261250
/**
262251
* Compile scala code. It can be found using the above class loader.
@@ -466,35 +455,36 @@ class ${className} extends (() => Any) with java.io.Serializable {
466455
val classPath = getClassPath(this.getClass.getClassLoader)
467456
val currentClassPath = classPath.head
468457

458+
def checkCurrentClassPath: List[String] = currentClassPath match {
459+
case List(jarFile) if jarFile.endsWith(".jar") =>
460+
val relativeRoot = new File(jarFile).getParentFile
461+
val jarResource: Resource[IO, JarFile] =
462+
Resource.make(IO(new JarFile(jarFile)))(jar => IO(jar.close()))
463+
464+
val nestedClassPath: IO[String] =
465+
jarResource
466+
.use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path")))
467+
.handleError {
468+
case scala.util.control.NonFatal(e) =>
469+
throw new CompilerException(List(List(e.getMessage)))
470+
}
471+
472+
nestedClassPath.map {
473+
case ncp if ncp eq null => Nil
474+
case ncp =>
475+
ncp
476+
.split(" ")
477+
.map { f =>
478+
new File(relativeRoot, f).getAbsolutePath
479+
}
480+
.toList
481+
482+
}.unsafeRunSync
483+
case _ => Nil
484+
}
485+
469486
// if there's just one thing in the classpath, and it's a jar, assume an executable jar.
470-
currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath(0)
471-
.endsWith(".jar")) {
472-
val jarFile = currentClassPath(0)
473-
val relativeRoot =
474-
new File(jarFile).getParentFile()
475-
val nestedClassPath = Try {
476-
val jar = new JarFile(jarFile)
477-
val CP = jar.getManifest.getMainAttributes.getValue("Class-Path")
478-
jar.close()
479-
CP
480-
} match {
481-
case Success(classPath) => classPath
482-
case Failure(throwable) =>
483-
throw new CompilerException(List(List(throwable.getMessage)))
484-
}
485-
if (nestedClassPath eq null) {
486-
Nil
487-
} else {
488-
nestedClassPath
489-
.split(" ")
490-
.map { f =>
491-
new File(relativeRoot, f).getAbsolutePath
492-
}
493-
.toList
494-
}
495-
} else {
496-
Nil
497-
}) ::: classPath.tail.flatten
487+
currentClassPath ::: checkCurrentClassPath ::: classPath.tail.flatten
498488
}
499489

500490
lazy val compilerOutputDir = target match {

server/src/main/scala/org/scalaexercises/evaluator/services.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ object services {
2727

2828
import EvalResponse.messages._
2929

30-
//def evaluatorInstance[F[_]: ConcurrentEffect: ContextShift: Timer: Sync] =
31-
// new Evaluator[F](20 seconds)
32-
3330
val corsHeaders = Seq(
3431
Header("Vary", "Origin,Access-Control-Request-Methods"),
3532
Header("Access-Control-Allow-Methods", "POST"),

0 commit comments

Comments
 (0)