I have own OSGI bundle that have folder inside with some resources. How to read that folder from bundle and get all children of it?
I used before following code.:
Bundle bundle = Platform.getBundle(bundleID);
URL fileURL = bundle.getEntry(templatePath);
URL url = FileLocator.resolve(fileURL);
This code workd utils I exported Eclipse application as Eclipse product.
But I have URL is not hierachical excepiton.
You can use the findEntries method on Bundle to locate files in the bundle and any attached fragments:
Bundle bundle = Platform.getBundle(bundleID);
Enumeration<URL> urls = bundle.findEntries("/folder", "*", false);
You should write your code in terms of InputStreams and use the method suggested by #NickWilson to retrieve the input streams and use them in your program.
But if you really need that folder exploded on the disk somewhere, you would use org.eclipse.core.runtime.FileLocator.toFileURL(URL) instead of resolve(URL).
Related
We have a plug-in that is exported to an RCP product. In the plug-in, there is a folder that has some files. How can I access the plug-in files under a certain folder in Eclipse programatically?
Use the org.eclipse.core.runtime.FileLocator class to access files in a plugin.
Bundle bundle = ... bundle containing the files
IPath path = new Path("relative path in plug-in of the file");
URL url = FileLocator.find(bundle, path, null);
URL fileUrl = FileLocator.toFileURL(url);
The url returned by the FileLocator.find method uses an Eclipse specific scheme and can only be used with certain Eclipse APIs.
The FileLocator.toFileURL call converts the URL to a normal file URL, it may be necessary to unpack the plug-in jar to a temporary location in order to do this.
You can get the Bundle using
Bundle bundle = FrameworkUtil.getBundle(getClass());
which gets the bundle containing the current class or
Bundle bundle = Platform.getBundle("plugin id");
to access a bundle by plug-in id.
I am developing an Eclipse plugin, and most of the time I run as "Eclipse Application", but sometimes I also export as a JAR.
I have two source folders, "src/" and "icons/". The problem is that when I run as "Eclipse Application" the path to the Icons are "icons/com/...", while when I generate the JAR the paths become "/com/...".
I would like a common path that will work whether I generate a JAR or run as an Eclipse Application. How can I achieve this?
Don't specify icons as a source folder just leave it as a plain folder. Do include it in the build.properties so that it is included in the built plug-in.
Use the FileLocator class to locate objects in plug-ins.
For images you can use something like:
String path = "icons/xxxxxx.jpg"; // TODO your path to the image
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
Note that the URL from FileLocator.find is only suitable for use by Eclipse APIs. You can convert it to a more normal URL using:
URL fileURL = FileLocator.toFileURL(url);
I want to include a file (text, image, etc) in the root directory of an Eclipse plugin.
When I run the program using a test main method, I can find the file in the working directory.
But when I run the plugin as an Eclipse application, the working directory contains different files depending on the operating system and I can't find the text file.
I tried adding the file to the binary build in the build tab of the xml (build.properties). It doesn't work still.
How will I find the path to the text file? How can I make sure that the text file is exported with the plugin?
Thanks
When you build your Eclipse plugin everything in the plugin is put in to a jar file in the 'plugins' directory. As long as your file is listed in the 'build.properties' (the build tab) it will be included in the jar.
To access a file in the jar use:
Bundle bundle = Platform.getBundle("your plugin id");
URL url = FileLocator.find(bundle, new Path("path in plugin"), null);
The URL returned is suitable for passing to various Eclipse APIs but cannot be used with normal Java APIs such as File. To convert it to a file URL use:
URL fileURL = FileLocator.toFileURL(url);
This will copy the file out of the jar in to a temporary location where it can be accessed as a normal file.
You can also get the Bundle using:
Bundle bundle = FrameworkUtil.getBundle(getClass());
which avoids having to include the plug-in id.
I have developed an eclipse plugin which references an external jar present in a external installation directory.
So I have added an entry to my bundle classpath as below:
Bundle-ClassPath: external:C:\mylib.jar
My class loads properly - and the plugin is able to detect a class MyClass present in this external lib.
However, the method a() - I am calling in the class MyClass is failing.
Method a() is as follows :
public void a()
{
URL url = this.class.getClassLoader().getResource("META-INF/startup-jar ");
...
}
so the URL which is returned is that of the eclipse plugin directory C:\eclipse3.4\test
and not of the physical location of the external jar which is C:\mylib.jar
This is causing method a() to fail. Now, my question is -
As I don't have the external jar copied to my plugin directory (it is only present on the plugin classpath)
how can I ensure the classloader gets the URL path of my external jar and not of my plugin directory?
Note : I cannot change the classloading mechanism in the external jar as it is a third party dependency and I have no control over the code. So please suggest a solution which would help me to load the external jar class correctly so I can get the correct URL.
Thanks a lot for your help - in advance
To explain a bit more on the problem I am facing ::
My external jar is present inside the installation directory of my server installation.
When the class in my external jar calls the URL url = this.class.getClassLoader().getResource("startup-jar")
it returns the URL relative to the eclipse bundle path -
Something like C:\eclipse3.4...
and this URL is used for getting the boot directory (installation directory of the server) .
So it should have returned a path which is relative to the server installation directory, but instead returns a path relative to the eclipse installation directory.
Because of this, I am not able to call any APIs on the server as the server installation directory which it tries to use is incorrect.
So I wanted to know what is the best way I can handle this, so that this method call returns the server installation dir and not eclipse bundle path.
Can't you wrap this 3rd party dependency with the correct OSGI metadata and install it as a plug-in/bundle? We did this for all 3rd-party dependencies, including problematic ones such as Hibernate and made them work.
If it's a popular open source library, you can probably find it with the OSGi metadata added at Spring's repository: www.springsource.com/repository/app
In general, I wouldn't recommend the pattern of referencing external JARs as you describe in your question.
Background:
I have a small problem with Eclipse. I'm working on a workbench plugin which has some classes that validate incoming XML data against a schema. The schema lives inside the plugin project in a "./schemas" folder.
Questions:
When I run the application, how can I read the schema without using a hardcoded path?
When the application is deployed the schema will need to live inside a plugin .jar. Will the solution from (1) be any different in this case?
You can use the FileLocator class from org.eclipse.core.runtime to get a URL to a resource in your bundle.
For example, something like
URL schema = FileLocator.find(myBundle, new Path("/schemas/data.xsd"), null);
You can get your Bundle object from the BundleContext passed into your Activator (if you have one). Or you could use Platform.getBundle.
You should not assume the url is a file on disk, for exactly the case when the plugin is a jar. You can use URL.openStream to get the contents, or FileLocator.toFileURL() to get a file on disk.