`
snoopy7713
  • 浏览: 1123559 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

【绝对路径】OSGi环境中获取Plugin/Bundle中文件资源的绝对路径

    博客分类:
  • OSGi
阅读更多

摘要:在进行Eclipse RCP开发的过程中,需要使用一些其他的资源(如图片、声音、配置文件、数据库等),我们一般将这些资源放到Plugin/Bundle的相应目录下(如 图片资源放到icons目录,声音放到sounds目录,数据库文件放到data目录)。本文先对Eclipse Plugin和Bundle进行分析,之后讲解了如何使用Eclipse API通过这些资源的相对路径(相对于Plugin/Bundle)获取这些资源的绝对路径的方法,最后总结了 org.osgi.framework.Bundle接口和FileLocator工具类的使用方法。

Bundle和Plugin

Eclipse Platform架构于OSGi核心之上,每个Eclipse Plugin就是OSGi的一个Bundle,因此在Eclipse RCP中Plugin和Bundle是等价的概念。

OSGi框架中定义了org.osgi.framework.IBundle接口,表示运行于OSGi环境中的一个Bundle。Eclipse RCP中定义了org.eclipse.core.runtime.Plugin抽象类,代表一个Eclipse插件。但是Plugin抽象类并没有实现 IBundle接口,而是在内部拥有一个IBundle对象实例。Plugin类实现了BundleActivator,控制其内部的IBundle对象 的启动和停止,并负责将BundleContext注入到IBundle对象实例中。

我们可以发现,在IBundle接口中定义了如下几个方法,可以获取位于在Bundle文件目录中的资源:

方法 解释
URL getResource(String name); 通过Bundle的Class Loader加载资源,和Class.getResource(String)类似,注意如果当前的bundle是一个fregment,则返回null
Enumeration getResources(String name) 和上面的方法一样,不过由于Eclipse插件可以包括多个Fregment,每个Fregment中可以分别包含相对路径相同的文件,因此通过一个相对路径可能找到多个匹配的资源
URL getEntry(String path); 在当前Bundle中根据Path找相应的Entry(此处的entry和resource到底有什么区别我还尚不清楚)
Enumeration getEntryPaths(String path); 和上面的方法一样,同getResources(String)
Enumeration findEntries(String path, String filePattern,
boolean recurse);
根据path和pattern找相应的资源文件,可以使用通配符,如需要查找所有语言文件:
bundle.findEntries(“OSGI-INF/l10n”, “*.properties”, true);

如何获取当前的Bundle对象

开发Eclipse Plugin/Bundle应用首先需要创建Activator类,继承自 org.eclipse.ui.plugin.AbstractUIPlugin,该类继承自上文提到的 org.eclipse.core.runtime.Plugin类,OSGi框架在加载Plugin/Bundle的时候首先加载此Activator 类,然后调用此类中定义的start(BundleContext context)和stop(BundleContext context)方法启动和停止这个Plugin/Bundle。在这个类的父类Plugin中声明了Bundle类型实例,可以通过 getBundle()方法获取这个Bundle实例。

以下代码为Activator类中的start(BundleContext)方法,在其中调用了getResource(String)和 getEntry(String)两个方法,同时调用FileLocator.toFileURL(String)将URL转化为文件系统的路径

public void start(BundleContext context) throws Exception {
	super.start(context);
	plugin = this;
	System.out.println(Activator.getDefault().getBundle().getLocation());
	URL url = Activator.getDefault().getBundle().getResource("META-INF/MANIFEST.MF");
	System.out.println(url);
	System.out.println(FileLocator.toFileURL(url));
 
	URL url2 = Activator.getDefault().getBundle().getEntry("META-INF/MANIFEST.MF");
	System.out.println(url2);
	System.out.println(FileLocator.toFileURL(url2));
}
 

上面代码输出结果为:

 

写道
reference:file:/D:/demo/modeling-workspace/com.zhlwish.proppagedemo/
bundleresource://709.fwk15218962:1/META-INF/MANIFEST.MF
file:/D:/demo/modeling-workspace/com.zhlwish.proppagedemo/META-INF/MANIFEST.MF
bundleentry://709.fwk15218962/META-INF/MANIFEST.MF
file:/D:/demo/modeling-workspace/com.zhlwish.proppagedemo/META-INF/MANIFEST.MF
 

 

通过结果可以很明显看到,使用getResource(String)和getEntry(String)获取到的URL是OSGi环境中的路径,需要通过FileLocator.toFileURL(String)方法将OSGi环境中的URL转化为文件系统中的URL。

至于getResource()和getEntry()有什么区别,我到Eclipse Forum里面提了问题,回答是:

  1. getResource uses the bundle’s class loader to load a resource. This resource could come from imported constraints (Import-Package, Require-Bundle) or from the local bundle classpath.
  2. getEntry only searches the bundles jar files itself and does not consider any constraints or the bundle’s class path (Bundle-ClassPath header).”

详见http://www.eclipse.org/forums/index.php/m/710032/#msg_710032

FileLocator

 

static URL find(Bundle bundle, IPath path, Map override);
static URL find(URL url);
static URL[] findEntries(Bundle bundle, IPath path);
static URL[] findEntries(Bundle bundle, IPath path, Map override);
static File getBundleFile(Bundle bundle);
static InputStream openStream(Bundle bundle, IPath file, boolean substituteArgs);
static URL resolve(URL url);
static URL toFileURL(URL url);
 

看起来和Bundle中的方法那么的相似,归根结底,FileLocator中的方法也都是通过调用Bundle中的相对应方法来实现其功能的,除 了resolve(URL)和toFileURL(URL),后者之前演示过其功能,前者的功能也同样是将OSGi环境中的URL解析成为文件系统中的 URL。其实我不认为此FileLocator除去toFileURL()此方法外地其他方法有更好的地方,如果可能,还是直接用Bundle的方法更 好。

总结

很明显,在Bundle中查找文件资源需要通过FileLocator和Bundle两个类的协作才能完成,由于一般情况下Eclipse RCP的属性以及配置值存放于Eclipse Workspace中,从Bundle中读取资源的需求比较少。最主要的应用场景是,一些图片资源可以通过此方式读取,然后在Eclipse RCP应用中处理和显示。

分享到:
评论

相关推荐

    osgi-bundle-status-maven-plugin:OSGi容器的远程捆绑包状态检查器

    OSGi捆绑包状态Maven插件 总览 OSGi容器的远程捆绑软件状态检查器。 请参阅以获取完整文档。 版本控制 遵循准则。

    Struts2开发常用jar包

    struts2-jasperreports-plugin-2.5.10.1.jar,struts2-javatemplates-plugin-2.5.10.1.jar,struts2-osgi-admin-bundle-2.5.10.1.jar,struts2-osgi-demo-bundle-2.5.10.1.jar,struts2-osgi-plugin-2.5.10.1.jar,...

    maven编译pentaho-big-data-plugin遇到的所有问题解决

    maven编译pentaho-big-data-plugin遇到的所有问题解决,pentaho6.0

    ext.bundle.osgi.common-1.0.8.zip

    gradle-android-plugin.zip,用于Gradle构建系统的Android插件。用于Gradle构建系统的Android插件。

    idempiere.maven.tycho.build.extra.bundle:为构建额外的插件上idempiere基地

    HOME] /project.extra.bundle,所以我们有[ROOT-HOME] /project.extra.bundle/plugin-1,[ROOT-HOME] /project.extra.bundle/plugin- 2 加Maven的第谷的支持,您的OSGi插件跟踪维基(添加第谷的支持)[ ] 编辑[ROOT-...

    struts-2.3.30-all所有jar包

    struts2-osgi-plugin-2.3.30.jar, struts2-oval-plugin-2.3.30.jar, struts2-pell-multipart-plugin-2.3.30.jar, struts2-plexus-plugin-2.3.30.jar, struts2-portlet-plugin-2.3.30.jar, struts2-rest-plugin-...

    struts-2.5.2-all所有jar包

    struts2-osgi-plugin-2.5.2.jar, struts2-oval-plugin-2.5.2.jar, struts2-pell-multipart-plugin-2.5.2.jar, struts2-plexus-plugin-2.5.2.jar, struts2-portlet-plugin-2.5.2.jar, struts2-rest-plugin-2.5.2.jar,...

    maven jar包

    maven-bundle-plugin-1.0.0.jar maven-clean-plugin-2.4.1.jar maven-clean-plugin-2.5.jar maven-common-artifact-filters-1.3.jar maven-compat-3.2.1-sources.jar maven-compiler-plugin-2.5.1.jar maven-...

    struts-2.5.10-all所有jar包

    struts2-osgi-admin-bundle-2.5.10.jar,struts2-osgi-demo-bundle-2.5.10.jar,struts2-osgi-plugin-2.5.10.jar,struts2-oval-plugin-2.5.10.jar,struts2-pell-multipart-plugin-2.5.10.jar,struts2-plexus-plugin-...

    renderers-1.4.zip

    gradle-bundle-plugin.zip,生成osgi包的gradle插件。

    basic plugins

    osgi文章中用到的所有依赖jar,资源分不够用了,1积分下载后回复下就可以了

    orbisgis-plugin_maven-archetype:一个Maven原型,可帮助为OrbisGIS创建插件

    OrbisGIS 4.0 OSGI插件包的Maven...OrbisGIS OSGi SQL Bundle Artchetype osgi-sql包含用于编写您自己SQL函数并将其作为新服务公开的原型。 OrbisGIS将注意到该用户定义SQL函数,并将其添加到当前的GDMS SQL引擎中。

    gradle-felix-launcher-plugin:Gradle 构建系统的 Apache Felix 插件

    该插件允许创建 apache felix 框架安装以准备运行 osgi bundle 应用程序。 特征 Felix Launcher 插件的特点: 下载所需的 apache felix 框架包 felix 框架的配置、组装和执行 将自定义包编译并包含到 felix 框架中...

    271个java需要用的jar包

    struts2-osgi-plugin-2.3.15.3.jar struts2-oval-plugin-2.3.15.3.jar struts2-pell-multipart-plugin-2.3.15.3.jar struts2-plexus-plugin-2.3.15.3.jar struts2-portlet-plugin-2.3.15.3.jar struts2-rest-plugin-...

    aem-sample-project:展示简单 maven 设置的示例 AEM 项目

    AEM 示例项目 此示例项目展示了典型 AEM 项目的 maven 设置可能是什么样子。 主要关注的是 maven 插件的使用以及继承可选的构建配置文件installBundle和installPackage 。...| | +- pom.xml # an OSGi bundle that c

    Felix+struts2做的web示例

    参照网上的示例自己做的,主要是webapp这个bundle,可以运行

    karaf-site:Apache karaf网站的镜像

    要安装Jekyll,请参阅 安装Jekyll后,您可以使用以下网站构建网站: bundle exec jekyll serve此命令将构建网站并在上启动本地Jekyll服务器注意:您本地的Jekyll安装可能需要Apache Karaf网站所需的其他模块。...

Global site tag (gtag.js) - Google Analytics