Skip to content

Closing JarFile instances #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.math.BigInteger
import java.security.MessageDigest
import java.util.jar.JarFile

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

import scala.concurrent.duration._
import scala.language.reflectiveCalls
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
import scala.tools.nsc.reporters._
import scala.tools.nsc.{Global, Settings}
Expand Down Expand Up @@ -462,28 +462,36 @@ class ${className} extends (() => Any) with java.io.Serializable {
val classPath = getClassPath(this.getClass.getClassLoader)
val currentClassPath = classPath.head

def checkCurrentClassPath: List[String] = currentClassPath match {
case List(jarFile) if jarFile.endsWith(".jar") =>
val relativeRoot = new File(jarFile).getParentFile
val jarResource: Resource[IO, JarFile] =
Resource.make(IO(new JarFile(jarFile)))(jar => IO(jar.close()))

val nestedClassPath: IO[String] =
jarResource
.use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path")))
.handleError {
case scala.util.control.NonFatal(e) =>
throw new CompilerException(List(List(e.getMessage)))
}

nestedClassPath.map {
case ncp if ncp eq null => Nil
case ncp =>
ncp
.split(" ")
.map { f =>
new File(relativeRoot, f).getAbsolutePath
}
.toList

}.unsafeRunSync
case _ => Nil
}

// if there's just one thing in the classpath, and it's a jar, assume an executable jar.
currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath(0)
.endsWith(".jar")) {
val jarFile = currentClassPath(0)
val relativeRoot =
new File(jarFile).getParentFile()
val nestedClassPath =
new JarFile(jarFile).getManifest.getMainAttributes
.getValue("Class-Path")
if (nestedClassPath eq null) {
Nil
} else {
nestedClassPath
.split(" ")
.map { f =>
new File(relativeRoot, f).getAbsolutePath
}
.toList
}
} else {
Nil
}) ::: classPath.tail.flatten
currentClassPath ::: checkCurrentClassPath ::: classPath.tail.flatten
}

lazy val compilerOutputDir = target match {
Expand Down