设为首页 收藏本站
查看: 654|回复: 0

[经验分享] apache commons-vfs源码赏析(二)

[复制链接]

尚未签到

发表于 2017-1-7 10:30:44 | 显示全部楼层 |阅读模式
在上篇中说到了第一个比较重要的类是VFS类.在那个类中说到StandardFileSystemManager这个类.因此在这里我们引申出vfs中第二个比较重要的类:StandardFileSystemManager.这个类集成一个父类DefaultFileSystemManager
在上篇中说道VFS会初始化一个StandardFileSystemManager对象,并调用其init方法.因此我们便去看看这个类的init方法.
因为这个类的比较大,所以在说的时候我只列出我认为比较重要的代码.

public void init() throws FileSystemException
{
// Set the replicator and temporary file store (use the same component)
final DefaultFileReplicator replicator = createDefaultFileReplicator();
setReplicator(new PrivilegedFileReplicator(replicator));
setTemporaryFileStore(replicator);
/* replaced by findClassLoader
if (classLoader == null)
{
// Use default classloader
classLoader = getClass().getClassLoader();
}
*/
if (configUri == null)
{
// Use default config
final URL url = getClass().getResource(CONFIG_RESOURCE);
if (url == null)
{
throw new FileSystemException("vfs.impl/find-config-file.error", CONFIG_RESOURCE);
}
configUri = url;
}
// Configure
configure(configUri);
// Configure Plugins
configurePlugins();
// Initialise super-class
super.init();
}

我们可以看到这个init方法做了很多事,首先是设置临时文件并复制和存储,然后做了一些配置,配置插件并调用其父类的init方法.
在这个方法中用到一个常量CONFIG_RESOURCE和一个变量configUri,
在类的开头已经定义.

private static final String CONFIG_RESOURCE = "providers.xml";
private URL configUri;

说的意思就是我们可以通过configUri去自定义配置文件.如果没有的话就使用默认的providers.xml.
我们重点要看的就是init方法要调用configure(Url url)和configurePlugins()方法
首先看看configure(Url url)方法,这个方法主要做的是根据url去Load一个配置文件.然后里面在去调用configure(Element config)方法.
这个方法的部分代码

InputStream configStream = null;
try
{
// Load up the config
// TODO - validate
final DocumentBuilder builder = createDocumentBuilder();
configStream = configUri.openStream();
final Element config = builder.parse(configStream).getDocumentElement();
configure(config);
}
catch (final Exception e)
{
throw new FileSystemException("vfs.impl/load-config.error", configUri.toString(), e);
}


因此重点要看的就是看看configure(Element config)方法里面做了什么事情.这个比较关键.

private void configure(final Element config) throws FileSystemException
{
// Add the providers
final NodeList providers = config.getElementsByTagName("provider");
final int count = providers.getLength();
for (int i = 0; i < count; i++)
{
final Element provider = (Element) providers.item(i);
addProvider(provider, false);
}
// Add the operation providers
final NodeList operationProviders = config.getElementsByTagName("operationProvider");
for (int i = 0; i < operationProviders.getLength(); i++)
{
final Element operationProvider = (Element) operationProviders.item(i);
addOperationProvider(operationProvider);
}
// Add the default provider
final NodeList defProviders = config.getElementsByTagName("default-provider");
if (defProviders.getLength() > 0)
{
final Element provider = (Element) defProviders.item(0);
addProvider(provider, true);
}
// Add the mime-type maps
final NodeList mimeTypes = config.getElementsByTagName("mime-type-map");
for (int i = 0; i < mimeTypes.getLength(); i++)
{
final Element map = (Element) mimeTypes.item(i);
addMimeTypeMap(map);
}
// Add the extension maps
final NodeList extensions = config.getElementsByTagName("extension-map");
for (int i = 0; i < extensions.getLength(); i++)
{
final Element map = (Element) extensions.item(i);
addExtensionMap(map);
}
}

通过这个方法我们可以看到就是把从配置文件读到的一些内容加入一些容器里面.实际上这些容器在这个类的父类中已经定义过.

private final Map providers = new HashMap();
private final Map operationProviders = new HashMap();
private final FileTypeMap map = new FileTypeMap();

说白了就是从配置文件中读取一些信息加入到这些map中.具体是怎么加入的我就不列举出来,感兴趣的可以自己去看看源码.
init中另外调用的一个方法就是configurePlugins()方法.这个方法就是如果有一些文件系统在vfs中没有实现,我们就可以通过插件的方式自己去实现.
例如smb(共享)这类文件系统的操作在vfs中并没有提供实现,但是apache中沙箱中已经提供实现.这个后面会提供介绍.
这个方法其实就是调用了另外一个配置文件.跟providers.xml类似.然后也加入哪些map中.

因此从中我们可以看出来VFS.getManager()其实做的事情就是读取一些配置文件并讲其内容加入到定义好的map中.
因此我们在使用VFS.getManager()获取FileSystemManager时最好只使用一次,因为这个
方法要读取大量的配置文件并初始话一些数据,会大量占用资源.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-325008-1-1.html 上篇帖子: Subversion+apache搭建svn服务器 下篇帖子: org.apache.commons.beanutils.BeanUtils简介[转]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表