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:
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
Here's one approach you could take which I think is a pretty Eclipse-y way to do it.
IMyService interface 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With