除常见的Http、FTP、FILE等协议之外,eclipse平台还支持多种扩展协议,甚至可以支持用户自定义协议。
Eclipse平台支持的协议,包括Plugin、Fragment、Meta、Config等,会在Eclipse runtime启动时候注册,可参见:
(1)org.eclipse.core.internal.runtime.Activator
(2)org.eclipse.core.internal.boot.PlatformURLHandler
(3)PlatformURLConnection
(4)URLConnection
从上述(3)的继承实现上,可以看到,Eclipse对platform协议url的定位的支持情况如下:
platform:base/xxx, 相对于rcp根目录下的资源
platform:config/xxx, 相对于rcp的configuration目录下的资源
platform:fragment/foo.bar/xxx, 相对于foo.bar这个fragment下的资源
platform:meta/foo.bar/xxx, 相对于workspace/.metadata/.plugins/foo.bar目录下的资源
platform:plugin/foo.bar/xxx, 相对于foo.bar这个plugin下的资源
platform:resource/foo/xxx, 相对于workspace里foo工程下的资源, foo工程有可能在workspace目录之外.
也可以通过继承PlatformURLConnection类,定义自己的基于Platform的URL。
从根本上来说,这种便利性是由Java URLConnection 乃至关于URL的标准的可扩展性带来的,Eclipse平台只是有效地利用了这种可扩展性。类似的情况还有OSGI的Bundle协议和Java的Jar协议等。
在实际的插件开发中,常用以下方法给文件定位
1 在插件内定位:
Bundle bundle = MapEditorPlugin.getDefault().getBundle();
URL url = bundle.getEntry("temp/xxx");
这种方式表示 xxx 这个文件在本插件的temp目录下
2 在RCP的配置路径下:
URL url = new URL("platform:config/xxx");
Properties prop = new Properties() ;
prop.load(url.openStream());
这种方式表示xxx这个文件在你的rcp的工作目录下的configuration目录下
另外,应该善加利用Platform里的方法,如调用Platform.getConfigurationLocation()也可获得配置目录的Location,通过location可以getURL()。
转自: http://blog.csdn.net/alpineflame/article/details/1756026