Skip to content

Commit

Permalink
(intellij-platform-gradle-plugin#1836) JarFilesResourceResolver: add …
Browse files Browse the repository at this point in the history
…a simple path cache

This is far from the optimal yet, but will be a first step towards faster configuration in the IntelliJ Platform Gradle Plugin.
  • Loading branch information
ForNeVeR committed Feb 1, 2025
1 parent 522fc90 commit 730cbe2
Showing 1 changed file with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,66 @@ import com.jetbrains.plugin.structure.intellij.resources.DefaultResourceResolver
import com.jetbrains.plugin.structure.intellij.resources.ResourceResolver
import java.nio.file.FileSystems
import java.nio.file.Path
import java.util.Optional
import java.util.concurrent.ConcurrentHashMap

class JarFilesResourceResolver(private val jarFiles: List<Path>) : ResourceResolver {

// final path ⇒ path to the first JAR file having the particular resource
// None ⇒ not found and never will be
private val cache = ConcurrentHashMap<String, Optional<Path>>()

override fun resolveResource(relativePath: String, basePath: Path): ResourceResolver.Result {
val resourceResult = DefaultResourceResolver.resolveResource(relativePath, basePath)
if (resourceResult !is ResourceResolver.Result.NotFound) {
return resourceResult
}
val finalPath = basePath.resolveSibling(relativePath).toString()
val cachedJarPath = cache[finalPath]
if (cachedJarPath != null) {
println("Cache reused for path finalPath=$finalPath, cachedJarPath=$cachedJarPath")
return if (cachedJarPath.isPresent)
loadFromJar(cachedJarPath.get(), finalPath)!!
else
ResourceResolver.Result.NotFound
}

for (jarFile in jarFiles) {
val jarFs = FileSystems.newFileSystem(jarFile, JarFilesResourceResolver::class.java.classLoader)
val foundResult = try {
val path = jarFs.getPath(finalPath.withZipFsSeparator())
if (path.exists()) {
ResourceResolver.Result.Found(path, path.inputStream(), resourceToClose = jarFs, description = jarFile.description)
} else {
null
}
} catch (e: Throwable) {
jarFs.close()
throw e
val result = loadFromJar(jarFile, finalPath)
if (result != null) {
println("Cache filled for path finalPath=$finalPath, path=$jarFile")
cache[finalPath] = Optional.of(jarFile)
return result
}
if (foundResult != null) {
return foundResult
}
jarFs.close()
}

println("Cache not found for path finalPath=$finalPath, path=NOT FOUND")
cache[finalPath] = Optional.empty<Path>()
return ResourceResolver.Result.NotFound
}
}

private fun loadFromJar(jarFile: Path, finalPath: String): ResourceResolver.Result? {
val jarFs = FileSystems.newFileSystem(jarFile, JarFilesResourceResolver::class.java.classLoader)
val foundResult = try {
val path = jarFs.getPath(finalPath.withZipFsSeparator())
if (path.exists()) {
ResourceResolver.Result.Found(
path,
path.inputStream(),
resourceToClose = jarFs,
description = jarFile.description
)
} else {
null
}
} catch (e: Throwable) {
jarFs.close()
throw e
}
if (foundResult != null) {
return foundResult
}
jarFs.close()
return null
}

0 comments on commit 730cbe2

Please sign in to comment.