vertx.FileResolver文件解析
生活随笔
收集整理的這篇文章主要介紹了
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.cwdString 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, Knopflerfishcase "bundleentry": // Equinoxcase "bundleresource": // Equinoxreturn unpackFromBundleURL(url, isDir);default:throw new IllegalStateException("Invalid url protocol: " + prot);} }note:有時啟動不了Application,很大可能由于用戶權限問題無法建立cache directory 所導致
轉載于:https://www.cnblogs.com/cmfa/p/10550757.html
總結
以上是生活随笔為你收集整理的vertx.FileResolver文件解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解析《中国互联网软件测试行业2018年度
- 下一篇: linux学习笔记-10.解压与压缩