jboss学习 - vfs---转载
jboss的VFS是为了解决什么问题,他为什么有用呢
在jboss中有很多类似的资源操作的代码都分散在程序的各个地方,大多数情况下代码首先确定操作的资源的类型,比如是文件或者是文件夹,通过URL加载的资源等,在不同的代码中其中包含了相同的代码。
例如
public static URL[] search(ClassLoader cl, String prefix, String suffix)
throws IOException {
Enumeration[] e = new Enumeration[] { cl.getResources(prefix),
cl.getResources(prefix + "MANIFEST.MF") };
Set all = new LinkedHashSet();
URL url;
URLConnection conn;
JarFile jarFile;
for (int i = 0, s = e.length; i < s; ++i) {
while (e[i].hasMoreElements()) {
url = (URL) e[i].nextElement();
conn = url.openConnection();
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
if (conn instanceof JarURLConnection) {
jarFile = ((JarURLConnection) conn).getJarFile();
} else {
jarFile = getAlternativeJarFile(url);
}
if (jarFile != null) {
searchJar(cl, all, jarFile, prefix, suffix);
} else {
boolean searchDone = searchDir(all, new File(URLDecoder
.decode(url.getFile(), "UTF-8")), suffix);
if (searchDone == false) {
searchFromURL(all, prefix, suffix, url);
}
}
}
}
return (URL[]) all.toArray(new URL[all.size()]);
}
private static boolean searchDir(Set result, File file, String suffix)
throws IOException {
if (file.exists() && file.isDirectory()) {
File[] fc = file.listFiles();
String path;
for (int i = 0; i < fc.length; i++) {
path = fc[i].getAbsolutePath();
if (fc[i].isDirectory()) {
searchDir(result, fc[i], suffix);
} else if (path.endsWith(suffix)) {
result.add(fc[i].toURL());
}
}
return true;
}
return false;
}
上面的代码在多个地方都会包含这样的代码。各自进行文件的处理,但是这种方式有一个很大的问题,就是热发布,需要将文件覆盖已经发布锁定的文件。为了解决文件锁的问题,可能需要将操作文件的代码进行集中处理,正式因为这个,VFS工程出现了。
VFS发布的API
VFS的基本使用包括二部分内容
- 简单的资源导航
- 访问者模式的API
综上所属,jdk自身提供的资源导航,需要判定文件的类型,操作比较繁琐。在VFS中我们将所有的类型抽象为一个类型-VirtualFile
public final class VirtualFile implements Serializable{
/**
* Get the simple VF name (X.java)
*
* @return the simple file name
*/
public String getName();
/**
* Get the simple VF name mapped to lowercase (x.java) (used by case-insensitive filesystems like ZIP).
*
* @return the lowercase simple file name
*/
public String getLowerCaseName();
/**
* Get the absolute VFS full path name (/xxx/yyy/foo.ear/baz.jar/org/jboss/X.java)
*
* @return the VFS full path name
*/
public String getPathName();
/**
* Get the path name relative to a parent virtual file. If the given virtual file is not a parent of
* this virtual file, then an {@code IllegalArgumentException} is thrown.
*
* @param parent the parent virtual file
* @return the relative path name as a string
* @throws IllegalArgumentException if the given virtual file is not a parent of this virtual file
*/
public String getPathNameRelativeTo(VirtualFile parent) throws IllegalArgumentException ;
/**
* Get the absolute VFS full path name. If this is a URL then directory entries will have a trailing slash.
*
* @param url whether or not this path is being used for a URL
*
* @return the VFS full path name
*/
String getPathName(boolean url);
/**
* When the file was last modified
*
* @return the last modified time
*/
public long getLastModified();
/**
* Get the size
*
* @return the size
*/
public long getSize();
/**
* Tests whether the underlying implementation file still exists.
*
* @return true if the file exists, false otherwise.
*/
public boolean exists();
/**
* Determines whether this virtual file represents a true root of a file system.
* On UNIX, there is only one root "/". Howevever, on Windows there are an infinite
* number of roots that correspond to drives, or UNC paths.
*
* @return {@code true} if this represents a root.
*/
public boolean isRoot();
/**
* Determine whether the named virtual file is a plain file.
*
* @return {@code true} if it is a plain file, {@code false} otherwise
*/
public boolean isFile();
/**
* Determine whether the named virtual file is a directory.
*
* @return {@code true} if it is a directory, {@code false} otherwise
*/
public boolean isDirectory() ;
/**
* Access the file contents.
*
* @return an InputStream for the file contents.
*
* @throws IOException for any error accessing the file system
*/
public InputStream openStream();
/**
* Delete this virtual file
*
* @return {@code true} if file was deleted
*/
public boolean delete();
/**
* Get a physical file for this virtual file. Depending on the underlying file system type, this may simply return
* an already-existing file; it may create a copy of a file; or it may reuse a preexisting copy of the file.
* Furthermore, the retured file may or may not have any relationship to other files from the same or any other
* virtual directory.
*
* @return the physical file
*
* @throws IOException if an I/O error occurs while producing the physical file
*/
public File getPhysicalFile() ;
/**
* Get a {@code VirtualFile} which represents the parent of this instance.
*
* @return the parent or {@code null} if there is no parent
*/
public VirtualFile getParent();
/**
* Get the children. This is the combined list of real children within this directory, as well as virtual children
* created by submounts.
*
* @return the children
*/
public List<VirtualFile> getChildren();
/**
* Visit the virtual file system
*
* @param visitor the visitor
*
* @throws IOException for any problem accessing the virtual file system
* @throws IllegalArgumentException if the visitor is null
* @throws IllegalStateException if the file is closed
*/
public void visit(VirtualFileVisitor visitor) throws IOException;
......
}
正与以前的对与只读文件的操作,只需要添加一些选项区清楚或者删除资源,有时候清楚或者删除资源需要处理一些临时文件,比如嵌套的jar等。
转换jdk或者RUL资源到VirtualFile,虚拟文件需要一个root,VFS类知道如何根据一个URL取得虚拟文件
public class VFS
{
/**
* Get the virtual file system for a root uri
*
* @param rootURI the root URI
* @return the virtual file system
* @throws IOException if there is a problem accessing the VFS
* @throws IllegalArgumentException if the rootURL is null
*/
static VFS getVFS(URI rootURI) throws IOException
/**
* Create new root
*
* @param rootURI the root url
* @return the virtual file
* @throws IOException if there is a problem accessing the VFS
* @throws IllegalArgumentException if the rootURL
*/
static VirtualFile createNewRoot(URI rootURI) throws IOException
/**
* Get the root virtual file
*
* @param rootURI the root uri
* @return the virtual file
* @throws IOException if there is a problem accessing the VFS
* @throws IllegalArgumentException if the rootURL is null
*/
static VirtualFile getRoot(URI rootURI) throws IOException
/**
* Get the virtual file system for a root url
*
* @param rootURL the root url
* @return the virtual file system
* @throws IOException if there is a problem accessing the VFS
* @throws IllegalArgumentException if the rootURL is null
*/
static VFS getVFS(URL rootURL) throws IOException
/**
* Create new root
*
* @param rootURL the root url
* @return the virtual file
* @throws IOException if there is a problem accessing the VFS
* @throws IllegalArgumentException if the rootURL
*/
static VirtualFile createNewRoot(URL rootURL) throws IOException
/**
* Get the root virtual file
*
* @param rootURL the root url
* @return the virtual file
* @throws IOException if there is a problem accessing the VFS
* @throws IllegalArgumentException if the rootURL
*/
static VirtualFile getRoot(URL rootURL) throws IOException
/**
* Get the root file of this VFS
*
* @return the root
* @throws IOException for any problem accessing the VFS
*/
VirtualFile getRoot() throws IOException
}
注:
这部分代码是老的vfs的代码,在新的vfs代码中,已经不再依靠URL,而是采用mount的方式,此处只是为了表明思路。
你可以采用不同的方式来取得VFS的实例,比如getVFS, createNewRoot and getRoot
由于VFS的新版本已经发生了变化,可以看新代码,此处不再详述。(由于此部分直接翻译,后续可以考虑按照新的版本重新写一份)
VFS架构
VFS的发布的API非常直观,他的实现非常复杂,这里进行一下阐述
每次创建一个VFS实例,配套的实例化一个VFSContext,他是根据VFSContextFactory生成的,他会根据不同的协议映射不同的实现类,比如文件系统映射为FileSystemContextFactory,Zip文件映射为ZipEntryContextFactory。
另外每次VFS创建的时候,匹配的VirtualFileHandler也会被生成,他知道如何处理不同的类型的资源
到这里基本上可以了解vfs的原理,后面不再进行描述,下面会学习vfs3的代码,查看新代码的原理
原文地址:http://netliving.iteye.com/blog/839938
转载于:https://www.cnblogs.com/davidwang456/p/4027077.html
jboss学习 - vfs---转载相关推荐
- 实战 | 深度学习轻松学:如何用可视化界面来部署深度学习模型 转载 2017年12月27日 00:00:00 109 翻译 | AI科技大本营 参与 | 王赫 上个月,我有幸结识了 DeepCogn
实战 | 深度学习轻松学:如何用可视化界面来部署深度学习模型 转载 2017年12月27日 00:00:00 标签: 109 编辑 删除 翻译 | AI科技大本营 参与 | 王赫 上个月,我有幸结识了 ...
- 【日语】日语学习计划(转载)
日语学习计划(转载) 怎样才能学好日语(转载) 如果您打算开始学习日语,但几乎还没什么基础的朋友,那您如果有兴趣可以看看这篇文章. 首先是明确自己学习日语的目的,哪怕只是为了兴趣也没关系,并制定自己的 ...
- YOLOv4学习资源转载
YOLOv4学习资源转载 Redmon写完YOLOv3,退出计算机视觉领域之后,又有其他大佬推动着这一目标检测神器的发展.YOLOv4我没有看原论文,而是找了一些博客资源进行学习,现在把我找到的博客资 ...
- 我的DDK学习经验(转载)
这篇文章在无数技术网站都找的到.我在这里发这篇文章的原因: 1.这篇文章实在很好.别说作者倾情的介绍了自己的经验和感想(尤其是关于"钱途"的感想),里面介绍的一些书,真够有志开发驱 ...
- ecos 学习资料(转载)
ecos 学习资料(转载) 一.英文资料 1.ecos home page:http://ecos.sourceware.org/ 2.ecos user guid:http://ecos.sourc ...
- makefile学习(转载)
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...
- 网络流学习(转载自ssw 的博客)
众所周知,网络流是探究网络上运输的一种图论分支.但是大多数人在第一次接触这个题时都有些畏惧感(比如说我),大佬可以自信跳过.. 本文包括: 1.网络流的概念及基本性质 2.略谈 Edmonds-Kar ...
- Hadoop家族学习路线图--转载
原文地址:http://blog.fens.me/hadoop-family-roadmap/ Sep 6, 2013 Tags: Hadoophadoop familyroadmap Comment ...
- Spring Boot详细学习地址转载
阿里中间件牛人,学习榜样,源码分析: https://fangjian0423.github.io/ 基础.详细.全面的教程: https://gitee.com/roncoocom/spring-b ...
最新文章
- 五、Elasticsearch中的API的简单使用(Python版本)
- float类型转integer_Java基础(一)之数据类型——全面,浅显易懂
- display:block的注意
- eclipse 初步新建jsp页面笔记
- 新浪安装weiphp2.0的方法
- 【十四】无验证码登录配置:通过登录接口获取 token 配置全局变量
- java 读写mysql_Java读写MySQL数据库小实例
- Linux shell__文件操作
- Mac上安装PL/SQL Developer
- centos7 卸载firewalld防火墙安装iptables
- 拿到offer后 还应该继续去面试?
- 使用 Docker Compose 构建复杂的多容器 App
- 三维图形几何变换与投影变换
- 【Demo】银行主数据的修改
- html 键盘按键与按钮功能关联
- python爬取推特图片_twitter图片视频批量下载
- 金融学本科跨考计算机,跨考研究生怎么选专业,计算机金融最喜欢谁?小编今天告诉你...
- 贵金属解套11.19解锁黄金期货原油实时操作建议指导
- 【Unity Shaders】Transparency —— 使用渲染队列进行深度排序
- 解析浏览器渲染的“一帧”——事件循环、帧动画、空闲回调
热门文章
- java中封装日期加时间_java日期处理简单封装
- php mariadb mysql.sock_(LNMP) Nginx_PHP_MariaDB
- 利用python进行数据分析学习笔记 第7章(3)
- 参考地、保护地、大地的概念
- android 默认光标大小设置,如何默认光标位置设置的EditText
- 软件详细设计说明书_校导周绪龙|软件测试第五篇——软件测试的底层思维
- oracle修改表结构精度,常见问题--oracle10g修改表结构
- mysql十大报错_MySQL十大报错函数
- java 方法执行结束局部变量释放_JAVA-方法在执行过程中,JVM的内存分配和变化情况,栈(stack)的情况浅析...
- 训练超参数, 出现 Cannot use GPU in CPU-only Caffe 错误?