Class NestedJarHandler

java.lang.Object
cloud.opencode.base.classloader.resource.NestedJarHandler
All Implemented Interfaces:
AutoCloseable

public class NestedJarHandler extends Object implements AutoCloseable
Nested JAR Handler - Parses and manages inner JARs embedded in fat JARs 嵌套 JAR 处理器 - 解析和管理嵌入在 fat JAR(如 Spring Boot)中的内层 JAR

Supports discovering and extracting nested JARs from common fat JAR layouts including BOOT-INF/lib/, WEB-INF/lib/, and lib/ directories.

支持从常见的 fat JAR 布局中发现和解压嵌套 JAR,包括 BOOT-INF/lib/、WEB-INF/lib/ 和 lib/ 目录。

Features | 主要功能:

  • Discover nested JARs in fat JARs - 在 fat JAR 中发现嵌套 JAR
  • Extract nested JARs to temporary files - 解压嵌套 JAR 到临时文件
  • Reference counting for shared access - 共享访问的引用计数
  • Automatic cleanup on close - 关闭时自动清理

Usage Examples | 使用示例:

try (NestedJarHandler handler = NestedJarHandler.builder().build()) {
    List<String> nested = handler.findNestedJars(fatJarPath);
    Path extracted = handler.extractNestedJar(fatJarPath, nested.get(0));
    // use extracted JAR...
    handler.release(fatJarPath, nested.get(0));
}

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Path traversal protection via normalize + startsWith check - 通过 normalize + startsWith 检查防止路径穿越
Since:
JDK 25, opencode-base-classloader V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static NestedJarHandler.Builder builder()
      Create a new builder 创建新的构建器
      Returns:
      builder instance | 构建器实例
    • findNestedJars

      public List<String> findNestedJars(Path jarPath)
      Discover nested JAR entry paths inside the given outer JAR 发现给定外层 JAR 内的嵌套 JAR 条目路径

      Scans BOOT-INF/lib/, WEB-INF/lib/, and lib/ for .jar entries.

      扫描 BOOT-INF/lib/、WEB-INF/lib/ 和 lib/ 下的 .jar 条目。

      Parameters:
      jarPath - path to the outer JAR | 外层 JAR 路径
      Returns:
      list of nested JAR entry names | 嵌套 JAR 条目名列表
      Throws:
      OpenClassLoaderException - if the JAR cannot be read | JAR 无法读取时抛出
    • extractNestedJar

      public Path extractNestedJar(Path outerJar, String nestedEntry)
      Extract a nested JAR entry to a temporary file 将嵌套 JAR 条目解压到临时文件

      If the same entry has already been extracted, the existing path is returned and the reference count is incremented.

      如果同一条目已被解压,则返回现有路径并递增引用计数。

      Parameters:
      outerJar - path to the outer JAR | 外层 JAR 路径
      nestedEntry - nested JAR entry name (e.g. "BOOT-INF/lib/foo.jar") | 嵌套 JAR 条目名
      Returns:
      path to the extracted temporary JAR file | 解压后的临时 JAR 文件路径
      Throws:
      OpenClassLoaderException - if extraction fails or entry not found | 解压失败或条目不存在时抛出
    • release

      public void release(Path outerJar, String nestedEntry)
      Release a reference to a previously extracted nested JAR 释放对先前解压的嵌套 JAR 的引用

      When the reference count reaches zero the temporary file is deleted.

      当引用计数降为零时,临时文件将被删除。

      Parameters:
      outerJar - path to the outer JAR | 外层 JAR 路径
      nestedEntry - nested JAR entry name | 嵌套 JAR 条目名
    • getTempDirectory

      public Path getTempDirectory()
      Get the temporary directory used for extraction 获取用于解压的临时目录
      Returns:
      temporary directory path | 临时目录路径
    • isClosed

      public boolean isClosed()
      Check if the handler is closed 检查处理器是否已关闭
      Returns:
      true if closed | 已关闭返回 true
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable