views:

100

answers:

2

Suppose I am building a very simple eclipse plugin for creating new java projects.

I obviously will create a new Wizard for the extension point org.eclipse.ui.newWizards. However, what I really want is to allow other plugins to implement a service that drives this new wizard.

So in theory we have three plugins:

  • My "Primary Plugin" (with MyNewWizard)
  • My "interface plugin" (with IMyService)
  • My implementation plugin (with MyServiceImpl)

Using standard OSGI stuff, I would just use the services from a ServiceTracker.

Unfortunately, Im in Eclipse OSGI land, where I don't get to create my wizard class, passing in my ServiceTracker, but rather Eclipse makes my plugin.

WITHOUT using a singleton in my Activator, does Eclipse provide some mechanism for IoC/Dependency Injection or at the very least a way to query for services from these UI classes?

Thanks

+2  A: 

Here's one approach you could take which I think is a pretty Eclipse-y way to do it.

  • Define your IMyService interface
  • In your plugin, define a new extension point, say "myplugin.myservice"
  • In the schema for that extension point (PDE will create one for you), add an element called "myService" with an attribute called "class" (or whatever) and set it's type to "java". Set the required interface (Implements field) to IMyService.

Now, in your wizard, add some code to locate and instantiate an implementation of IMyService. Something like this:

IExtensionRegistry registry = Platform.getExtensionRegistry();
for(IConfigurationElement element : registry.getConfigurationElementsFor("myplugin.myservice"))
{
    if("myService".equals(element.getName()))
    {
        return (IMyService) element.createExecutableExtension("class"));
    }
}
return new DefaultMyService();

This will give you the first registered implementation, or a default if there are none. Alternatively, you could add more metadata to your extension point schema and use it to generate a list of options for the user to choose from or something.

This is, I believe, the preferred Eclipse way to do it, because it preserves lazy loading. That is, until you actually call createExecutableExtension, the plugin with the custom service implementation can remain unloaded.

Dave Ray
+1  A: 

Extension points seems right.

If you still want to go with OSGi, to create a ServiceTracker, you first need a bundle context. Naturally, if you have a singleton Activator, it is plain and simple to get (.getDefault().getBundle()).

However, you can just find your bundle if you know its' symbolic name using Platform.getBundle(). This also works.

zvikico