vertx.FileResolver文件解析
生活随笔
收集整理的這篇文章主要介紹了
vertx.FileResolver文件解析
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
FileResolver Class
//文件復(fù)制解析,復(fù)制文件到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 文件解析器是否用緩存,默認(rèn)ture, * 設(shè)置兩種方式:一、VertxOptions類的setFileResolverCachingEnabled方法* 二、設(shè)置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設(shè)置,默認(rèn)當(dāng)前工作目錄 .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 觸發(fā)緩存清理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) {// 現(xiàn)在disk查找文件File file = new File(fileName);if (cwd != null && !file.isAbsolute()) {//是否是絕對路徑file = new File(cwd, fileName);}// -Dvertx.disableFileCPResolving 設(shè)置if (!ENABLE_CP_RESOLVING) {return file;}/*** synchronized同步塊,防止多線程對cache directory操作導(dǎo)致線程安全問題*/synchronized (this) {if (!file.exists()) {// 首先本地文件cache 查找File cacheFile = new File(cacheDir, fileName);if (enableCaching && cacheFile.exists()) {return cacheFile;}// 在classpath 查找ClassLoader cl = getClassLoader();//檢查是否是UNIX separator,不是將文件路徑 \ 替換為 /,不同操作系統(tǒng) 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; }/*** 復(fù)制目錄下所有文件到cacheDir目錄下*/ private File unpackUrlResource(URL url, String fileName, ClassLoader cl, boolean isDir) {//獲取協(xié)議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:有時(shí)啟動(dòng)不了Application,很大可能由于用戶權(quán)限問題無法建立cache directory 所導(dǎo)致
轉(zhuǎn)載于:https://www.cnblogs.com/cmfa/p/10550757.html
總結(jié)
以上是生活随笔為你收集整理的vertx.FileResolver文件解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解析《中国互联网软件测试行业2018年度
- 下一篇: linux学习笔记-10.解压与压缩