Accessing plugins from fragments and vise versa

212 Views Asked by At

I'm trying to add a fragment to a plugin in order to have information added to the plugin without touching the latter.

However, the problem I experienced is that I can access resources that are in the plugin from the fragment but not the other way around.

This is how I'm fetching the resource and the manifests of both projects. The resource file "patch.xml" is in /src folder.

test.plugin2:

main class:

public class Main{

    public static void main(String[] args) {
        RetrieveResource ma = new RetrieveResource();
        ma.retrieve();
    }
}

RetrieveResource:

public class RetrieveResource {

    public RetrieveResource(){

    }

    public void retrieve(){
        URL url = this.getClass().getClassLoader().getResource("patch.xml");
        System.out.println(url);
    }
}

url is always null.

manifest.mf:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Plugin2
Bundle-SymbolicName: test.plugin2;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.7.0"

build.properties

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\

test.fragment2:

manifest.mf

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Fragment2
Bundle-SymbolicName: test.fragment2
Bundle-Version: 1.0.0.qualifier
Fragment-Host: test.plugin2;bundle-version="1.0.0.qualifier"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: test.fragment2.classes

build.properties

source.. = src/
output.. = bin/
bin.includes = META-INF/,\
               .,\

What is missing or where did I go wrong?
Is it even possible to access fragments from plugins?

PROBLEM SOLVED: In my tests, I was starting the test.plugin2 project as in "java application" mode. Since the fragment and plugin have to be merged at runtime, the application have to be started in "eclipse application" mode and there will be no problem fetching the resource.

1

There are 1 best solutions below

0
On BEST ANSWER

Use FileLocator.find (in org.eclipse.core.runtime) to search for resources in a plugin and fragments.

Bundle bundle = ... your plugin Bundle

IPath path = new Path("patch.xml");

URL url = FileLocator.find(bundle, path, null);