0

I coded a bundle that uses Apache Felix Dependency management. It's Activator extends DependencyActivatorBase. But Plugin my plugin activator extends AbstractUIPlugin. How can I get services from the felix dependency manager from within the eclipse plugin?

DependencyManager has a getDepenencyManagers method but it is a list, not sure how I would know the right manager in the list.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
harschware
  • 13,006
  • 17
  • 55
  • 87

2 Answers2

2

Yes you can use Dependency Manager in any OSGi framework, including Equinox (on which Eclipse is based).

Why does your bundle activator need to extend AbstractUIPlugin?? Are you actually using AbstractUIPlugin, or was this just generated for you because you used Eclipse PDE to generate the initial code? The project templates in PDE are basically junk, most bundles do not need activators at all, and very very few really need to extend AbstractUIPlugin.

So, just change your activator to extend DependencyActivatorBase instead of AbstractUIPlugin.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Exactly right, I extended from PDE samples. I guess I just assumed AbstractUIPlugin was a necessary component to adding any UI elements in my plugin. Can you briefly say what the purpose of AbstractUIPlugin is? – harschware Jun 08 '13 at 17:10
  • 1
    `AbstractUIPlugin` gives slightly easier access to things like preferences and the image registry. The JavaDoc for the class has full details. However none of these things are difficult to access... if you need them then just read the code of `AbstractUIPlugin` and follow the same pattern. – Neil Bartlett Jun 08 '13 at 21:34
2

The DependencyActivatorBase class is just a base class that is there for your convenience. If, for some reason, you cannot use it (like, arguably, in your case), you can always instantiate an instance of DependencyManager yourself from your own class. All it needs is a reference to the BundleContext (which you can get from the start() method of BundleActivator, assuming you do implement that yourself). Then just do something like this:

DependencyManager dm = new DependencyManager(bundleContext);
dm.add(dm.createComponent()
  .setImplementation(YourComponent.class)
  .add(dm.createServiceDependency()
    .setService(LogService.class)
  )
);
Marcel Offermans
  • 3,313
  • 1
  • 15
  • 23
  • I was thinking of DependencyManager like Spring's ApplicationContext. So, unlike Spring's ApplicationContext, it is OK to have more than one DependencyManager? I'm assuming there is no need for one DependencyManager since it is merely interacting the services maintained by the OSGI container rather than maintaining them in its own context? – harschware Jun 10 '13 at 20:36
  • 1
    That is perfectly fine. You should have at least one DependencyManager for each bundle, but that's because it uses the BundleContext you pass in the constructor and that should not be shared amongst bundles. You can use more than one DependencyManager per bundle if you want. It is just an instance that holds the declarative data about components and their dependencies. – Marcel Offermans Jun 10 '13 at 20:50