Merge pull request #332 from alexarchambault/topic/fix

Fix plugin handling
This commit is contained in:
Alexandre Archambault 2016-08-24 10:10:17 +02:00 committed by GitHub
commit 162fdb493d
2 changed files with 35 additions and 17 deletions

View File

@ -1,7 +1,7 @@
package coursier package coursier
import java.math.BigInteger import java.math.BigInteger
import java.net.{ HttpURLConnection, URL, URLConnection, URLStreamHandler } import java.net.{ HttpURLConnection, URL, URLConnection, URLStreamHandler, URLStreamHandlerFactory }
import java.nio.channels.{ OverlappingFileLockException, FileLock } import java.nio.channels.{ OverlappingFileLockException, FileLock }
import java.nio.file.{ StandardCopyOption, Files => NioFiles } import java.nio.file.{ StandardCopyOption, Files => NioFiles }
import java.security.MessageDigest import java.security.MessageDigest
@ -243,21 +243,24 @@ object Cache {
Option(handlerClsCache.get(protocol)) match { Option(handlerClsCache.get(protocol)) match {
case None => case None =>
val clsName = s"coursier.cache.protocol.${protocol.capitalize}Handler" val clsName = s"coursier.cache.protocol.${protocol.capitalize}Handler"
val clsOpt = def clsOpt(loader: ClassLoader) =
try Some(Thread.currentThread().getContextClassLoader.loadClass(clsName)) try Some(loader.loadClass(clsName))
catch { catch {
case _: ClassNotFoundException => case _: ClassNotFoundException =>
None None
} }
val clsOpt0 = clsOpt(Thread.currentThread().getContextClassLoader)
.orElse(clsOpt(getClass.getClassLoader))
def printError(e: Exception): Unit = def printError(e: Exception): Unit =
scala.Console.err.println( scala.Console.err.println(
s"Cannot instantiate $clsName: $e${Option(e.getMessage).fold("")(" ("+_+")")}" s"Cannot instantiate $clsName: $e${Option(e.getMessage).fold("")(" ("+_+")")}"
) )
val handlerOpt = clsOpt.flatMap { val handlerFactoryOpt = clsOpt0.flatMap {
cls => cls =>
try Some(cls.newInstance().asInstanceOf[URLStreamHandler]) try Some(cls.newInstance().asInstanceOf[URLStreamHandlerFactory])
catch { catch {
case e: InstantiationException => case e: InstantiationException =>
printError(e) printError(e)
@ -271,6 +274,18 @@ object Cache {
} }
} }
val handlerOpt = handlerFactoryOpt.flatMap {
factory =>
try Some(factory.createURLStreamHandler(protocol))
catch {
case NonFatal(e) =>
scala.Console.err.println(
s"Cannot get handler for $protocol from $clsName: $e${Option(e.getMessage).fold("")(" ("+_+")")}"
)
None
}
}
val prevOpt = Option(handlerClsCache.putIfAbsent(protocol, handlerOpt)) val prevOpt = Option(handlerClsCache.putIfAbsent(protocol, handlerOpt))
prevOpt.getOrElse(handlerOpt) prevOpt.getOrElse(handlerOpt)

View File

@ -1,19 +1,22 @@
package coursier.cache.protocol package coursier.cache.protocol
import java.net.{ URL, URLConnection, URLStreamHandler } import java.net.{ URL, URLConnection, URLStreamHandler, URLStreamHandlerFactory }
class TestprotocolHandler extends URLStreamHandler { class TestprotocolHandler extends URLStreamHandlerFactory {
protected def openConnection(url: URL): URLConnection = {
val resPath = "/test-repo/http/abc.com" + url.getPath
val resURLOpt = Option(getClass.getResource(resPath))
resURLOpt match { def createURLStreamHandler(protocol: String): URLStreamHandler = new URLStreamHandler {
case None => protected def openConnection(url: URL): URLConnection = {
new URLConnection(url) { val resPath = "/test-repo/http/abc.com" + url.getPath
def connect() = throw new NoSuchElementException(s"Resource $resPath") val resURLOpt = Option(getClass.getResource(resPath))
}
case Some(resURL) => resURLOpt match {
resURL.openConnection() case None =>
new URLConnection(url) {
def connect() = throw new NoSuchElementException(s"Resource $resPath")
}
case Some(resURL) =>
resURL.openConnection()
}
} }
} }
} }