From a3d4558dd796e489ed3bc73d1e3b5ffc92168b9c Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Wed, 28 Oct 2015 05:59:21 -0700 Subject: [PATCH] Convert to Kurl. --- .../com/beust/kobalt/maven/ArtifactFetcher.kt | 19 ++---- .../kobalt/maven/IClasspathDependency.kt | 2 +- .../kotlin/com/beust/kobalt/maven/Kurl.kt | 65 +++++++++++++++++++ .../com/beust/kobalt/maven/MavenDependency.kt | 23 ++++--- .../kotlin/com/beust/kobalt/maven/MavenId.kt | 13 ++++ .../com/beust/kobalt/maven/RepoFinder.kt | 25 +------ 6 files changed, 99 insertions(+), 48 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/maven/Kurl.kt diff --git a/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt b/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt index 0d736b4e..4294b840 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/ArtifactFetcher.kt @@ -53,23 +53,14 @@ class ArtifactFetcher @Inject constructor(@Assisted("url") val url: String, private fun getBytes(url: String) : ByteArray { log(2, "$url: downloading to $fileName") - val body = http.get(url) - if (body.code == 200) { - val buffer = ByteArrayOutputStream(estimatedSize) - body.getAsStream().copyTo(buffer, estimatedSize) - return buffer.toByteArray() - } else { - throw KobaltException("$url: failed to download, code: ${body.code}") - } + return Kurl(url, http).bytes } override fun call() : File { - val md5Body = http.get(url + ".md5") - val remoteMd5 = if (md5Body.code == 200) { - md5Body.getAsString().trim(' ', '\t', '\n').substring(0, 32) - } else { - null - } + val k = Kurl(url + ".md5", http) + val remoteMd5 = + if (k.exists) k.string.trim(' ', '\t', '\n').substring(0, 32) + else null val file = File(fileName) file.parentFile.mkdirs() diff --git a/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt b/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt index b13a670f..8d012e9a 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/IClasspathDependency.kt @@ -8,7 +8,7 @@ import java.util.concurrent.Future interface IClasspathDependency { companion object { - val PREFIX_FILE: String = "file:/" + val PREFIX_FILE: String = "file://" } /** Identifier for this dependency */ diff --git a/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt b/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt new file mode 100644 index 00000000..05f617d5 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/maven/Kurl.kt @@ -0,0 +1,65 @@ +package com.beust.kobalt.maven + +import com.google.common.io.ByteStreams +import com.google.inject.assistedinject.Assisted +import java.io.* +import java.net.HttpURLConnection +import java.net.URL +import java.net.URLConnection +import javax.inject.Inject +import kotlin.properties.Delegates + +/** + * Abstracts a URL so that it works transparently on either http:// or file:// + */ +public class Kurl @Inject constructor(@Assisted val url: String, val http: Http) { + val connection : URLConnection by lazy { + URL(url).openConnection() + } + val exists : Boolean + get() { + if (url.contains("android")) { + println("DONOTCOMMIT") + } + + val result = + if (connection is HttpURLConnection) { + (connection as HttpURLConnection).responseCode == 200 + } else if (url.startsWith(IClasspathDependency.PREFIX_FILE)) { + val fileName = url.substring(IClasspathDependency.PREFIX_FILE.length) + File(fileName).exists() + } else { + false + } + return result + } + + val estimatedSize = 18000000 + + val bytes : ByteArray + get() { + val buffer = ByteArrayOutputStream(estimatedSize) + ByteStreams.copy(connection.inputStream, buffer) + return buffer.toByteArray() + } + + val string: String + get() { + val sb = StringBuilder() + connection.inputStream.let { inputStream -> + val reader = BufferedReader(InputStreamReader(inputStream)) + + var line: String? = reader.readLine() + try { + while (line != null) { + sb.append(line).append('\n') + line = reader.readLine() + } + } finally { + inputStream.close() + } + } + + return sb.toString() + } +} diff --git a/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt b/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt index 5f0a0730..ea62e304 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/MavenDependency.kt @@ -31,7 +31,7 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v jarFile = CompletedFuture(jar) pomFile = CompletedFuture(pom) } else { - val repoResult = repoFinder.findCorrectRepo(toId(groupId, artifactId, packaging, version)) + val repoResult = repoFinder.findCorrectRepo(MavenId.toId(groupId, artifactId, packaging, version)) if (repoResult.found) { jarFile = if (repoResult.hasJar) { @@ -42,7 +42,8 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v pomFile = downloadManager.download(repoResult.repoUrl + toPomFile(repoResult), pom.absolutePath, executor) } else { - throw KobaltException("Couldn't resolve ${toId(groupId, artifactId, packaging, version)}") + throw KobaltException("Couldn't resolve " + + "${MavenId.toId(groupId, artifactId, packaging, version)}") } } } @@ -62,15 +63,15 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v return depFactory.create(id, ex) } - fun toId(g: String, a: String, packaging: String?, v: String) = + fun _toId(g: String, a: String, packaging: String?, v: String) = if (packaging.isNullOrBlank()) "$g:$a:$v" else "$g:$a:$packaging:$v" } - public override fun toString() = toId(groupId, artifactId, packaging, version) + public override fun toString() = MavenId.toId(groupId, artifactId, packaging, version) - override val id = toId(groupId, artifactId, packaging, version) + override val id = MavenId.toId(groupId, artifactId, packaging, version) override fun toMavenDependencies(): org.apache.maven.model.Dependency { with(org.apache.maven.model.Dependency()) { @@ -89,10 +90,14 @@ public class MavenDependency @Inject constructor(override @Assisted("groupId") v override fun directDependencies() : List { val result = arrayListOf() - pomFactory.create(id, pomFile.get()).dependencies.filter { - it.mustDownload && it.isValid - }.forEach { - result.add(create(toId(it.groupId, it.artifactId, it.packaging, it.version))) + try { + pomFactory.create(id, pomFile.get()).dependencies.filter { + it.mustDownload && it.isValid + }.forEach { + result.add(create(MavenId.toId(it.groupId, it.artifactId, it.packaging, it.version))) + } + } catch(ex: Exception) { + warn("Exception when trying to resolve dependencies for $id: " + ex.message) } return result } diff --git a/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt b/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt index 836940cd..196fba2d 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/MavenId.kt @@ -6,6 +6,16 @@ public class MavenId(val id: String) { var packaging: String? = null var version: String? = null + companion object { + fun create(groupId: String, artifactId: String, packaging: String?, version: String?) = + MavenId(toId(groupId, artifactId, packaging, version)) + + fun toId(groupId: String, artifactId: String, packaging: String? = null, version: String?) = + "$groupId:$artifactId" + + (if (packaging != null) ":$packaging" else "") + + ":$version" + } + init { val c = id.split(":") if (c.size != 3 && c.size != 4) { @@ -18,4 +28,7 @@ public class MavenId(val id: String) { } val hasVersion = version != null + + val toId = MavenId.toId(groupId, artifactId, packaging, version) + } diff --git a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt index 37dafce2..cf317d2a 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt @@ -9,9 +9,6 @@ import com.google.common.cache.CacheBuilder import com.google.common.cache.CacheLoader import com.google.common.cache.LoadingCache import java.io.File -import java.net.HttpURLConnection -import java.net.URI -import java.net.URL import java.util.concurrent.Callable import java.util.concurrent.ExecutorCompletionService import java.util.concurrent.TimeUnit @@ -66,20 +63,6 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal } } - private fun urlExists(url: String) : Boolean { - val connection = URL(url).openConnection() - val result = - if (connection is HttpURLConnection) { - connection.responseCode == 200 - } else if (url.startsWith(IClasspathDependency.PREFIX_FILE)) { - val fileName = url.substring(IClasspathDependency.PREFIX_FILE.length) - File(fileName).exists() - } else { - false - } - return result - } - /** * Execute a single HTTP request to one repo. */ @@ -114,13 +97,7 @@ public class RepoFinder @Inject constructor(val http: Http, val executors: Kobal val dep = SimpleDep(groupId, artifactId, packaging, version) // Try to find the jar file val urlJar = repoUrl + dep.toJarFile(dep.version) - - if (repoUrl.contains("beust")) { - println("DONOTCOMMIT") - } - - val hasJar = urlExists(urlJar) - + val hasJar = Kurl(urlJar, http).exists val found = if (! hasJar) { // No jar, try to find the directory