TheAptKid
TheAptKid

Reputation: 1571

Referencing a local folder in an osgi plugin

I want to reference a local resource folder (not the files in it), with the name 'test', of my osgi plugin. I found these two examples (1, 2) on the internet:

Bundle bundle = Platform.getBundle("de.vogella.example.readfile");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
    file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

This one doesn't work for me, because the FileLocator object isn't resolved (nothing is recommended by eclipse to resolve this).

The second snippet:

URL iconUrl = FileLocator.find(Platform.getBundle("myBundle"), new Path("icons/someIcon.png"))

This one doesn't work for me, because the Platfrom, FileLocator and Path objects arent resolved.

Is there a different way I could go about referencing a local folder than the two ways described above? The folder is located one level below root (same as the libs folder for storing jar files).

Upvotes: 0

Views: 225

Answers (1)

Peter Kriens
Peter Kriens

Reputation: 15372

A resource in bundle does not have to be located on the file system. The URL has a special URL scheme to reflect this. It is just bad practice to assume you can convert a URL to a file in any way. A URL can be turned into a InputStream ... that is the only guarantee you get.

So you should take the input stream and copy it to a temporary file. However, generally you can use the Input Stream directly in most cases.

In OSGi, you can, however, traverse the folders inside the bundle. Look at Bundle.findEntries.

Upvotes: 4

Related Questions