日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

vertx.FileResolver文件解析

發布時間:2024/8/26 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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文件解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。