Skip to content

Commit 259525e

Browse files
Merge pull request #84 from scala-exercises/enrique-0-5-0-memory-fix
Closing JarFile instances
2 parents a7c25e5 + 2baad98 commit 259525e

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

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

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

15-
import cats.effect.{Concurrent, ConcurrentEffect, Timer}
15+
import cats.effect._
1616
import cats.implicits._
1717
import coursier._
1818
import coursier.cache.{ArtifactError, FileCache}
@@ -22,8 +22,8 @@ import org.scalaexercises.evaluator.{Dependency => EvaluatorDependency}
2222

2323
import scala.concurrent.duration._
2424
import scala.language.reflectiveCalls
25-
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
2625
import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
26+
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
2727
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
2828
import scala.tools.nsc.reporters._
2929
import scala.tools.nsc.{Global, Settings}
@@ -462,28 +462,36 @@ class ${className} extends (() => Any) with java.io.Serializable {
462462
val classPath = getClassPath(this.getClass.getClassLoader)
463463
val currentClassPath = classPath.head
464464

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

489497
lazy val compilerOutputDir = target match {

0 commit comments

Comments
 (0)