Is there an equivalent to BundleActivator for Eclipse fragment projects?

2.5k Views Asked by At

I am building an Eclipse plug-in that provides a set of core features in the regular plug-in project. The optional features I am providing via fragment projects. But I need the fragments to register themselves with the main plug-in on start-up.

I cannot have a Bundle-Activator in the fragment project. So I am wondering is there some alternate mechanism to declare an entry point or some call-back that I can hook?

And if there is no alternative other than converting the fragment project to a regular plug-in project is there a downside that I need to be aware of?

This is the solution I used based on the accepted answer:

final IExtensionRegistry registry = Platform.getExtensionRegistry();
final IExtensionPoint extensionPoint = registry.getExtensionPoint("myextensionid");
final IExtension[] extensions = extensionPoint.getExtensions();
for (int j = 0; j < extensions.length; ++j)
{
    final IConfigurationElement[] points = extensions[j].getConfigurationElements();
    for (int i = 0; i < points.length; ++i)
    {
        if ("myelementname".equals(points[i].getName()))
        {
            try
            {
                final Object objImpl= points[i].createExecutableExtension("class");
                objImplList.add(provider);
            }
            catch (CoreException e)
            {
            }
        }
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

You could define an extension point and lookup/call your fragment classes via extensions.

    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint extensionPoint = registry
            .getExtensionPoint("myplugin.myextension");
    IConfigurationElement points[] = extensionPoint
            .getConfigurationElements();
    for (IConfigurationElement point : points) {
        if ("myextensionFactory".equals(point.getName())) {
            Object impl = point.createExecutableExtension("class");
            if (impl instanceof IMyExtension) {
                ((IMyExtension) impl).foo();
            }
        }
    }
}

EDIT:

To use this approach I have to convert my fragments projects to plug-in projects. – bmatthews68

You shouldn't have to. For example, in my test code, I have the following files in the host plugin:

META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Myplugin Plug-in
Bundle-SymbolicName: myplugin;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: myplugin.Activator
Require-Bundle: org.eclipse.core.runtime
Eclipse-LazyStart: true
Export-Package: myplugin

plugin.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
    <extension-point id="myextension" name="myextension"
        schema="schema/myextension.exsd" />
</plugin>

The fragment contains these files:

META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Myfragment Fragment
Bundle-SymbolicName: myfragment;singleton:=true
Bundle-Version: 1.0.0
Fragment-Host: myplugin;bundle-version="1.0.0"

fragment.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<fragment>
   <extension
         point="myplugin.myextension">
      <myextensionFactory
            class="myfragment.MyExtension1">
      </myextensionFactory>
   </extension>
</fragment>

These projects were generated using Eclipse 3.3.1.1.