Obtain the absolute path of file resources in Plugin/Bundle in Eclipse RCP

Source: Internet
Author: User

Abstract: During Eclipse RCP development, you need to use some other resources (such as slice, sound, configuration file, and database ), we usually put these resources under the corresponding directory of Plugin/Bundle (for example, the image resources are put in the icons directory, the sound is put in the sounds directory, and the database files are put in the data directory ). This article first analyzes Eclipse Plugin and Bundle, and then explains how to use the Eclipse API to obtain the absolute path of these resources through the relative paths of these resources (relative to Plugin/Bundle, finally, org. osgi. framework. how to Use the Bundle interface and FileLocator tool class.

Bundle and Plugin

Eclipse Platform is based on the OSGi core. Each Eclipse Plugin is a Bundle of OSGi. Therefore, plug-in and Bundle are equivalent concepts in Eclipse RCP.

The OSGi framework defines the org. osgi. framework. IBundle interface, which indicates a Bundle running in the OSGi environment. Eclipse RCP defines the org. eclipse. core. runtime. Plugin abstract class, representing an Eclipse plug-in. However, the in abstract class does not implement the IBundle interface, but has an IBundle object instance internally. The Plugin class implements BundleActivator, controls the start and stop of the IBundle object inside it, and injects BundleContext into the IBundle object instance.

We can find that the following methods are defined in the IBundle interface to obtain resources in the Bundle file directory:

Method Explanation
URL getResource (String name ); Load resources through the Class Loader of Bundle, similar to Class. getResource (String). Note that if the current bundle is a fregment, null is returned.
Enumeration getResources (String name) The method above is the same. However, since the Eclipse plug-in can include multiple Fregment, each Fregment can contain files with the same relative path, multiple matching resources may be found through a relative path.
URL getEntry (String path ); Find the corresponding Entry in the current Bundle according to the Path (the difference between the entry and resource here is not clear yet)
Enumeration getEntryPaths (String path ); Same as the above method, it is the same as getResources (String)
Enumeration findEntries (String path, String filePattern,
Boolean recurse );
Find the corresponding resource file based on path and pattern. You can use wildcards. If you need to find all language files:
Bundle. findEntries (OSGI-INF/l10n, "*. properties", true );

How to obtain the current Bundle object

To develop an Eclipse Plugin/Bundle application, you must first create an Activator class that inherits from org. eclipse. ui. plugin. abstractUIPlugin, which inherits from the org. eclipse. core. runtime. plug-in class. The OSGi framework loads This Activator class when loading Plugin/Bundle, and then calls the start (BundleContext context) and stop (BundleContext context) defined in this class) method to start and stop this plug-in/Bundle. The Bundle type instance is declared in the parent class Plugin of this class. You can get this Bundle instance through the getBundle () method.

The following code is the start (BundleContext) method in the Activator class. The getResource (String) and getEntry (String) methods are called and FileLocator is called at the same time. toFileURL (String) converts the URL to the path of the file system

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)); }

The output result of the above Code is:

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

The result shows that the URL obtained by using getResource (String) and getEntry (String) is the path in the OSGi environment and needs to be obtained through FileLocator. the toFileURL (String) method converts a URL in the OSGi environment to a URL in the file system.

As for the difference between getResource () and getEntry (), I asked a question in Eclipse Forum. The answer is:

  1. GetResource uses the bundle's class loader to load a resource. This resource cocould 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 )."

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

FileLocator

As mentioned above, the toFileUrl (String) method of FileLocator is a tool class. The purpose of FileLocator is to find the corresponding resource file in the Bundle. It defines the following methods:

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);

It seems similar to the method in Bundle. In the final analysis, all the methods in FileLocator implement their functions by calling the corresponding method in Bundle, except for resolve (URL) and toFileURL (URL), which previously demonstrated its functionality. The former feature also resolves the URL in the OSGi environment into a URL in the file system. In fact, I do not think that FileLocator except toFileURL () is better for other methods in the field. If possible, it is better to directly use the Bundle method.

Summary

Obviously, finding file resources in a Bundle can only be completed through collaboration between FileLocator and Bundle. Generally, attributes and configuration values of Eclipse RCP are stored in Eclipse Workspace, there is little need to read resources from Bundle. The main application scenario is that some image resources can be read in this way, and then processed and displayed in the Eclipse RCP application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.