0

Let's imagine a bundle in which exists:

  • A component is responsible of listen all "Device" service instances in the service registry.

  • The same component needs an "adaptor factory" in order to create "Adaptors" by using the discovered devices.

  • The factory is owned by another bundle.

I can solve part of the problem by using a ServiceTracker (Activator + Service Tracker): the activator instantiates the ServiceTraker and it can register all changes in "Device" services.

But i can't inject to this service tracker the DS factory created in other bundle, because it will result in two instances (one created by activator AND without the member /// another created by osgi AND with member variable ok but can't listen the "Device" service changes).

So... how can i solve this scenario? How can i have a Service Tracker (perfect for me) with a DS as a class member?

Community
  • 1
  • 1

1 Answers1

1

Use no Activator, instead use a component or service (we will call it A) with a declarative services 'activate(ComponentContext)' method. Within the activate method, you can instantiate your ServiceTracker like normal.

When you instantiate the ServiceTracker within A's activate method, you can also pass in the AdapterFactory into the ServiceTracker. You can get the AdapterFactory by pulling it out of the BundleContext taken from ComponentContext or (even better) use DS and make it a service reference to your A component.

That said: why do you need ServiceTracker for this? Unless I misunderstand, you can use DS bind and unbind to receive events on the availability of a Service.

EDIT: An (OLD) example of Bind/Unbind behavior using multiple cardinality: http://blog.tfd.co.uk/2009/11/12/declarative-optional-multiple-references-flaky-in-osgi/

EDIT: A comparision of the two approaches but doesn't go into bind/unbind so much: http://njbartlett.name/2010/08/05/when-servicetrackers-trump-ds.html

EDIT2: That said: my general policy is to not use an Activator except in super rare cases. Use DS, ipojo, etc and use the components you define with those techs in order get access to the BundleContext to build more low level objects like ServiceTrackers.

Sheena Artrip
  • 1,990
  • 12
  • 16
  • Thanks Sheena, i've done the opposite. I mean, recover the factory in service tracker (i have got the context). Anyway, i understand that DS with multiple cardinality is a replacement for service trackers... am i right? I haven't seen any example explaining how to replace a service tracker by using DS... can you point me to a useful one? – user2451444 Sep 12 '13 at 15:35
  • Yes you can certainly do that and it should work just fine. Hard to say without code examples of exactly what is happening. Yes it is a replacement but it's not 100%. ServiceTracker can still be useful and it can be used together with DS. I've updated my answer with some blog post examples. – Sheena Artrip Sep 12 '13 at 15:47
  • Well I hope it is going well. =) If you have any additional thoughts I'd be interested to hear. (p.s.: I'm not involved in OSGI groups so there isn't much I can do about those thoughts, but I am looking to eventually publish some software and docs on the technology and like to hear what people have to say). – Sheena Artrip Sep 13 '13 at 21:28
  • mmm... i'm trying to port my code to full DS mode. So, i have a bundle that "launches" components with a factory, and i'm trying to track these components in another bundle. So far so good, i can "listen" the new components as soon as they are created, but the unbind method is never called when i kill the component in the original factory. Any ideas? Is that issue related to cardinality? – user2451444 Sep 18 '13 at 16:18
  • forget it, it was my stupid fault :) Hope to hear about your project soon. Regards – user2451444 Sep 18 '13 at 16:30
  • Actually googled to remind myself how to do this. This answer came up -_-. – Sheena Artrip Sep 23 '13 at 00:35
  • hilarious¡ btw, maybe you can help me with other question: http://stackoverflow.com/questions/18982190/osgi-equinox-enable-component-from-external-bundle – user2451444 Sep 24 '13 at 13:22