Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection/IOC when extending the Eclipse IDE

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

like image 898
Eric Anderson Avatar asked Nov 25 '25 02:11

Eric Anderson


2 Answers

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.

like image 58
Dave Ray Avatar answered Nov 27 '25 17:11

Dave Ray


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.

like image 38
zvikico Avatar answered Nov 27 '25 16:11

zvikico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!