2

In my application, using services by themselves is pretty useless. You always need some external configuration information for a service to be usable.

Components coupled with ConfigurationAdmin makes sense, since then for each configuration I create, a component instance will be created. This is just perfect for my use-case.

Now, the question arises, what if I'd like to use a component from an other bundle programmatically? Does this make sense?

I know I could export the component as a service yet again, and consume that from other beans, but let's say I have a servlet, where the user can create the configurations, and for each configured instance there are a list of actions; when he clicks the actions, I'd need to find the appropriate component, and execute the action on it.

What'd be the best way to implement this functionality on top of OSGi?

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Zoltán
  • 121
  • 10

2 Answers2

5

"Using a component from another bundle programatically" sounds exactly like OSGi Services to me.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • is re-exporting automatically constructed components from configuration data a common thing? – Zoltán Mar 05 '12 at 16:50
  • I don't understand what you're asking, could you please clarify? – Neil Bartlett Mar 05 '12 at 18:53
  • if I understand correctly, I could use DS to specify that for each configuration that's available it should create a component; like if I have a Connection service that I'd like to create different configurations for (let's say I want to have a serial connection and a telnet connection available) then could I get DS to construct two components? if so, I could also register the service under a different interface (lets say Connected), which now is a service that has configuration data injected too. sorry for the dumb example :) – Zoltán Mar 05 '12 at 21:35
  • now that I read back, a ManagedServiceFactory seems like an ideal choice - is there anything better? – Zoltán Mar 05 '12 at 21:39
  • Right, DS allows you to create multiple instances of a component by supplying multiple configuration records. However each instance is the same type of component: i.e. the same implementation class, the same published service type(s), the same service references etc. – Neil Bartlett Mar 06 '12 at 00:50
  • `ManagedServiceFactory` is a much lower-level concept. It is simply an object registered under that interface name: you have to decide what to do when the `updated` method is invoked by Config Admin. If you go this way you will have to do a lot more work yourself than if you let DS take care of it. – Neil Bartlett Mar 06 '12 at 00:51
  • Ok, I'll try to do it with DS then. Thanks! – Zoltán Mar 06 '12 at 11:37
1

This method retrieves the osgi service (iso having the osgi container wire the dependencies):

public class ServiceLocator {

  public static <T extends Object> T getService(final Class<T> clazz) {
    final BundleContext bundleContext = FrameworkUtil.getBundle(clazz).getBundleContext();
    // OSGI uses the order of registration if multiple services are found
    final ServiceReference<T> ref =     bundleContext.getServiceReference(clazz);
    return bundleContext.getService(ref);
  }

}

I used this when introducing DS in existing project which does not use DS everywhere. Not all components in the project were instantiated as osgi DS components. Anywhere I need to access a DS Component in classes instantiated by any other means I used this method...

ruben056
  • 112
  • 8