vertx.FileResolver文件解析


FileResolver Class

//文件復制解析,復制文件到cache directory 中 ,VM options : -Dvertx.disableFileCPResolving
public static final String DISABLE_CP_RESOLVING_PROP_NAME = "vertx.disableFileCPResolving";
private static final boolean ENABLE_CP_RESOLVING = !Boolean.getBoolean(DISABLE_CP_RESOLVING_PROP_NAME);

/**
  * enableCaching 文件解析器是否用緩存,默認ture, 
  * 設置兩種方式:一、VertxOptions類的setFileResolverCachingEnabled方法
  * 二、設置system property,VM options "-Dvertx.disableFileCaching",對原代碼無侵入性
  */
public FileResolver(Vertx vertx, boolean enableCaching) {
    this.vertx = vertx;
    this.enableCaching = enableCaching;
    //獲取工作目錄 -Dvertx.cwd
    String cwdOverride = System.getProperty("vertx.cwd");
    if (cwdOverride != null) {
      cwd = new File(cwdOverride).getAbsoluteFile();
    } else {
      cwd = null;
    }
    if (ENABLE_CP_RESOLVING) {
      setupCacheDir();
    }
}


/**
  *  建立cache目錄
  */
private void setupCacheDir() {
  //CACHE_DIR_BASE 通過-Dvertx.cacheDirBase設置,默認當前工作目錄 .vertx隱藏目錄下
    String cacheDirName = CACHE_DIR_BASE + "/file-cache-" + UUID.randomUUID().toString();
    cacheDir = new File(cacheDirName);
    if (!cacheDir.mkdirs()) {
      throw new IllegalStateException("Failed to create cache dir");
    }
    // 添加 shutdown hook,kill -15 pid 觸發緩存清理
    shutdownHook = new Thread(() -> {
      CountDownLatch latch = new CountDownLatch(1);
      deleteCacheDir(ar -> latch.countDown());
      try {
        latch.await(10, TimeUnit.SECONDS);
      } catch (Exception ignore) {
      }
    });
    Runtime.getRuntime().addShutdownHook(shutdownHook);
}

/**
  * 刪除cache目錄
  */
private void deleteCacheDir(Handler<AsyncResult<Void>> handler) {
    if (cacheDir != null && cacheDir.exists()) {
      vertx.fileSystem().deleteRecursive(cacheDir.getAbsolutePath(), true, handler);
    } else {
      handler.handle(Future.succeededFuture());
    }
}

 

解析文件

/**
 * 解析文件
 */
public File resolveFile(String fileName) {
    // 現在disk查找文件
    File file = new File(fileName);
    if (cwd != null && !file.isAbsolute()) {//是否是絕對路徑
      file = new File(cwd, fileName);
    }
    // -Dvertx.disableFileCPResolving 設置
    if (!ENABLE_CP_RESOLVING) {
      return file;
    }
    
    /**
     * synchronized同步塊,防止多線程對cache directory操作導致線程安全問題
     */
    synchronized (this) {
      if (!file.exists()) {
        // 首先本地文件cache 查找
        File cacheFile = new File(cacheDir, fileName);
        if (enableCaching && cacheFile.exists()) {
          return cacheFile;
        }
        // 在classpath 查找
        ClassLoader cl = getClassLoader();
        //檢查是否是UNIX separator,不是將文件路徑 \ 替換為 /,不同操作系統 separator 存在差異
        if (NON_UNIX_FILE_SEP) {
          fileName = fileName.replace(FILE_SEP, "/");
        }

        
        String parentFileName = file.getParent();
        if (parentFileName != null) {
        //緩存父目錄中存在的所有資源
          URL directoryContents = cl.getResource(parentFileName);
          if (directoryContents != null) {
            unpackUrlResource(directoryContents, parentFileName, cl, true);
          }
        }

        URL url = cl.getResource(fileName);
        if (url != null) {
          return unpackUrlResource(url, fileName, cl, false);
        }
      }
    }
    return file;
}

/**
  * 復制目錄下所有文件到cacheDir目錄下
  */
private File unpackUrlResource(URL url, String fileName, ClassLoader cl, boolean isDir) {
  //獲取協議
    String prot = url.getProtocol();
    switch (prot) {
      case "file":
        return unpackFromFileURL(url, fileName, cl);
      case "jar":
        return unpackFromJarURL(url, fileName, cl);
      case "bundle": // Apache Felix, Knopflerfish
      case "bundleentry": // Equinox
      case "bundleresource": // Equinox
        return unpackFromBundleURL(url, isDir);
      default:
        throw new IllegalStateException("Invalid url protocol: " + prot);
    }
}

note:有時啟動不了Application,很大可能由於用戶權限問題無法建立cache directory 所導致


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM