From ef2cbc6f6e37d9963b883f3fac64c4eb78809639 Mon Sep 17 00:00:00 2001 From: kiroco12 <48894338+kiroco12@users.noreply.github.com> Date: Thu, 23 Jan 2020 14:35:56 +0100 Subject: [PATCH 1/3] Closing JarFile instances --- .../scalaexercises/evaluator/evaluation.scala | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala b/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala index 6cbf970a..39e1133f 100644 --- a/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala +++ b/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala @@ -27,7 +27,7 @@ import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader import scala.tools.nsc.io.{AbstractFile, VirtualDirectory} import scala.tools.nsc.reporters._ import scala.tools.nsc.{Global, Settings} -import scala.util.Try +import scala.util.{Failure, Success, Try} import scala.util.control.NonFatal class Evaluator[F[_]: Sync](timeout: FiniteDuration = 20.seconds)( @@ -463,14 +463,21 @@ class ${className} extends (() => Any) with java.io.Serializable { val currentClassPath = classPath.head // 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) + currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath.head .endsWith(".jar")) { - val jarFile = currentClassPath(0) + val jarFile = currentClassPath.head val relativeRoot = - new File(jarFile).getParentFile() - val nestedClassPath = - new JarFile(jarFile).getManifest.getMainAttributes - .getValue("Class-Path") + new File(jarFile).getParentFile + val nestedClassPath = Try { + val jar = new JarFile(jarFile) + val CP = jar.getManifest.getMainAttributes.getValue("Class-Path") + jar.close() + CP + } match { + case Success(classPath) => classPath + case Failure(throwable) => + throw new CompilerException(List(List(throwable.getMessage))) + } if (nestedClassPath eq null) { Nil } else { From 051ead2ed759d34c3161d3c6f305a1f245329366 Mon Sep 17 00:00:00 2001 From: Juan Pedro Moreno Date: Thu, 23 Jan 2020 15:39:11 +0100 Subject: [PATCH 2/3] Changes Try by cats.effect. Resource --- .../scalaexercises/evaluator/evaluation.scala | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala b/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala index 39e1133f..3e936494 100644 --- a/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala +++ b/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala @@ -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} @@ -22,12 +22,12 @@ 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} -import scala.util.{Failure, Success, Try} +import scala.util.Try import scala.util.control.NonFatal class Evaluator[F[_]: Sync](timeout: FiniteDuration = 20.seconds)( @@ -462,35 +462,35 @@ 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 { throwable: Throwable => + throw new CompilerException(List(List(throwable.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.head - .endsWith(".jar")) { - val jarFile = currentClassPath.head - val relativeRoot = - new File(jarFile).getParentFile - val nestedClassPath = Try { - val jar = new JarFile(jarFile) - val CP = jar.getManifest.getMainAttributes.getValue("Class-Path") - jar.close() - CP - } match { - case Success(classPath) => classPath - case Failure(throwable) => - throw new CompilerException(List(List(throwable.getMessage))) - } - 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 { From 2baad98308df7e646705a47bd994e88d2b2fed32 Mon Sep 17 00:00:00 2001 From: kiroco12 <48894338+kiroco12@users.noreply.github.com> Date: Thu, 23 Jan 2020 16:11:23 +0100 Subject: [PATCH 3/3] Added code suggestions --- .../main/scala/org/scalaexercises/evaluator/evaluation.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala b/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala index 3e936494..b7fc4e2d 100644 --- a/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala +++ b/server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala @@ -471,8 +471,9 @@ class ${className} extends (() => Any) with java.io.Serializable { val nestedClassPath: IO[String] = jarResource .use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path"))) - .handleError { throwable: Throwable => - throw new CompilerException(List(List(throwable.getMessage))) + .handleError { + case scala.util.control.NonFatal(e) => + throw new CompilerException(List(List(e.getMessage))) } nestedClassPath.map {