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();
deleteCacheDir(ar -> latch.countDown());
try {
latch.await(, 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 所导致

最新文章

  1. .Net语言 APP开发平台——Smobiler学习日志:如何实现快速跳转网页
  2. Java程序设计之Constructor
  3. js-Event构造函数,也许你需要
  4. Centos6.5 python升级成2.7版本出现的一些问题解决方法
  5. andorid SQLite数据库的增删改查 和事务操作
  6. Subsets [LeetCode]
  7. ios开发之代理&amp;&amp;协议(补充篇)
  8. PTA 数据结构 银行业务队列简单模拟
  9. php sprintf用法
  10. DefaultNamespaceHandlerResolver中handlerMappings如何初始化
  11. Android-LruCache与DiskLruCache
  12. SqlParameter的两种用法【二】
  13. L305 发邮件15分钟
  14. python 中 __name__ 的使用
  15. Linux内核分析(第六周)
  16. bzoj 4028 : [HEOI2015]公约数数列
  17. TextView上下滑动
  18. Reddit: 只有独生子女才明白的事
  19. 浅入tcp
  20. 《大数据日知录》读书笔记-ch16机器学习:分布式算法

热门文章

  1. 1.promethues监控融入k8s
  2. bugku web 管理员系统
  3. call(),apply(),bind()区别?
  4. 学习STM32F769DK-OTA例程之百度云平台建立MQTT服务器
  5. 51nod97B 异或约束和
  6. Linux uniq 命令
  7. Windows环境下Anaconda安装TensorFlow的避坑指南
  8. 解决springboot jar包冲突
  9. Java虚拟机—Java8内存模型(整理版)
  10. Aras前端的一些知识