Is there an equivalent to BundleActivator for Eclipse fragment projects?

放肆的年华 提交于 2019-12-10 09:23:39

问题


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:


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.



来源:https://stackoverflow.com/questions/365946/is-there-an-equivalent-to-bundleactivator-for-eclipse-fragment-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!