Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » How to retrieve the consumer with declarative services?(Getting rid of the ServiceTracker)
icon5.gif  How to retrieve the consumer with declarative services? [message #631650] Fri, 08 October 2010 11:33 Go to next message
Roland Brand is currently offline Roland BrandFriend
Messages: 42
Registered: July 2009
Member
Hello

I am creating an RCP application which stores its data in files. The format of these files can change in every version of the application and I offer the possibility to migrate the files to a newer version. A complete migration consists of a chain of migration steps. As an example, the migration from version 1.0 to version 2.2 consists of three steps, 1.0 -> 2.0, 2.0 -> 2.1, 2.1 -> 2.2.

Currently the migration steps are declared with DS, each step is an own service. On the consumer side I have a Migrator class, which is a singleton and uses ServiceTracker to get the available MigrationSteps.

After the talk on "Best and Worst OSGi Practices" at the last EclipseCon, I'd like to get rid of the ServiceTracker. So I declared the Migrator as a DS component which uses the MigrationStep service. Now, the Migrator is created automatically and it retrieves the MigrationSteps via the bind/unbind methods.

But then I have the problem that I am not able to get the Migrator instance itself. Because it is created by DS, it is not a singleton anymore. How can I use the Migrator from my application?

Probably I missed something in the DS specification, but I'm really stuck with this and would appreciate any hint.

Roland

Re: How to retrieve the consumer with declarative services? [message #631694 is a reply to message #631650] Fri, 08 October 2010 13:41 Go to previous messageGo to next message
BJ Hargrave is currently offline BJ HargraveFriend
Messages: 60
Registered: July 2009
Member
> I am not able to get the Migrator instance itself

What actor is "I"? You only mention 2 actors, the Migrator and the MIgrationSteps.

If Migrator is a DS component, is it declared as a service? If so, then other actors can get that service.
Re: How to retrieve the consumer with declarative services? [message #631981 is a reply to message #631694] Mon, 11 October 2010 06:54 Go to previous messageGo to next message
Roland Brand is currently offline Roland BrandFriend
Messages: 42
Registered: July 2009
Member
Hi

The third actor is the application that uses the Migrator. On user request, a wizard is shown where the user can choose the version he likes to migrate to. On "Finish" the application has to find the Migrator instance to actually start the migration programmatically.

Up to now, the Migrator is a singleton class and the only instance is retrieved by Migrator.getInstance(). I wonder how I could change this class to use DS instead. When I declare the Migrator as a service consumer (referencing services), without providing a service itself, a Migrator instance is created automatically, but I do not see a possibility to get hold of this instance from another part of my application.
Re: How to retrieve the consumer with declarative services? [message #632076 is a reply to message #631981] Mon, 11 October 2010 13:19 Go to previous messageGo to next message
BJ Hargrave is currently offline BJ HargraveFriend
Messages: 60
Registered: July 2009
Member
So is clearly that case where the Migrator should be a service. Your static factory method, getInstance, must be replaced by using the Migrator as a service.
Re: How to retrieve the consumer with declarative services? [message #632091 is a reply to message #632076] Mon, 11 October 2010 14:21 Go to previous messageGo to next message
Roland Brand is currently offline Roland BrandFriend
Messages: 42
Registered: July 2009
Member
Yes I can declare the Migrator to be a service itself. But this only moves the problem one step further and my Application still cannot get the Migrator automatically with DS.

As far as I know, the application can get a Migrator service instance by using BundleContext.getServiceReference(). Using this method still adds a direct dependency to OSGi which I hoped I could eliminate. I'm not sure, if this is a better solution than using ServiceTracker.
Re: How to retrieve the consumer with declarative services? [message #632129 is a reply to message #632091] Mon, 11 October 2010 16:40 Go to previous message
BJ Hargrave is currently offline BJ HargraveFriend
Messages: 60
Registered: July 2009
Member
There is no magic here.

Writing POJOs is a great idea. You should isolate your code from OSGi API. But you cannot eliminate it. If you use DS, you can move all the OSGi-ness to the component xml. Or you can write a bundle activator which has all your bundle's OSGi code encapsulated. That code should use a ServiceTracker to track the Migrator service. The remaining code can call a method on that class to obtain the Migrator object. Or you can use dependency injection and have the Migrator injected.
Previous Topic:Equinox and CrEme (J2ME CDC)
Next Topic:Source code for "osgi.clean" option
Goto Forum:
  


Current Time: Tue May 14 16:27:42 GMT 2024

Powered by FUDForum. Page generated in 0.04140 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top