Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » declarative service dependencies(how to control startup order?)
declarative service dependencies [message #1731207] Tue, 03 May 2016 14:24 Go to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
I have a declarative service ServiceA in bundle A and ServiceB in bundle B.
I inject ServiceB in the implementation of ServiceA. How can I be sure serviceB already exists when it gets injected in serviceA ?
Re: declarative service dependencies [message #1731209 is a reply to message #1731207] Tue, 03 May 2016 14:40 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
You don't have to. The declarative services will ensure that the service gets injected correctly at the correct time.
Re: declarative service dependencies [message #1731217 is a reply to message #1731209] Tue, 03 May 2016 15:14 Go to previous messageGo to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
The problem currently is that we get a NullPointerException when trying to use ServiceB. Viewing that we don't do everything 'according to the book' it might be related to that. For instance we don't consistently use interfaces for our services, and we don't include all three advertised plugins (org.eclipse.equinox.ds, org.eclipse.equinox.util and org.eclipse.osgi.services). when starting up the product. We don't have the util, do have ds, and use org.eclipse.osgi instead of org.eclipse.osgi.services .. I will first improve that, and hope it will work from that moment on ...
Re: declarative service dependencies [message #1731233 is a reply to message #1731217] Tue, 03 May 2016 16:02 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
Not sure what you mean ... without org.eclipse.equinox.ds there will be no declarative services. And in my environment I need both bundles, org.eclipse.osgi AND org.eclipse.osgi.services together with the org.eclipse.equinox.util so all dependencies are resolved.

Have you checked that your bundles are all resolved and active? In which environment (plain OSGi or Eclipse RCP)?
Re: declarative service dependencies [message #1731277 is a reply to message #1731233] Wed, 04 May 2016 06:49 Go to previous messageGo to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
The environment is RCP E4, and we are considering if we should use declarative services or not (replace them by classes annotated with @Creatable, and inject instances of these classes where needed). The application is split in three layers: UI, services and connector. The current question is if we should use Osgi/declarative services in the service layer or not at all ...
Re: declarative service dependencies [message #1731298 is a reply to message #1731277] Wed, 04 May 2016 07:58 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
If you want to implement a service layer, declarative services are a perfect match. Doing something custom with @Creatable will bring up more issues than you are trying to solve.
Re: declarative service dependencies [message #1731299 is a reply to message #1731298] Wed, 04 May 2016 07:59 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
BTW, declarative services can also be injected with e4 di
Re: declarative service dependencies [message #1731729 is a reply to message #1731299] Mon, 09 May 2016 15:12 Go to previous messageGo to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
It's only a pity that we cannot just inject declarative services into other declarative services as far as I know. The only way I got it working is by having a bind method and service reference in the component.xml. Is it correct to say that at the moment you have to include the references in the component.xml, and the @Inject does not work for such references, or am I missing something ?? Another question: how can you use the e4 preferences in a declarative service? We keep the user language in the e4 preferences, and would like to know that user language in the service ...

[Updated on: Mon, 09 May 2016 15:14]

Report message to a moderator

Re: declarative service dependencies [message #1731730 is a reply to message #1731729] Mon, 09 May 2016 15:39 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
Quote:
It's only a pity that we cannot just inject declarative services into other declarative services as far as I know


You need to learn about the differences what is OSGi and what is Eclipse on top of OSGi. Declarative Services are plain OSGi. You can inject via service references, but you can not inject using the @Inject annotation. The @Inject annotation is added on top via Eclipse 4, therefore the lower OSGi level is not taking part here. But in principal the service reference is the same.

Working with declarative services becomes easier when using the Declarative Services Annotations that become part of Eclipse Neon. But you can already use it via Markteplace in Mars or earlier: https://marketplace.eclipse.org/content/declarative-services-annotations-support

Quote:
how can you use the e4 preferences in a declarative service?


Similar to the above. E4 preferences are on the Eclipse level, and the declarative services are not aware of them. IIRC the e4 preferences make use of the OSGi preferences service, so you could try to make use of it this way. But that means you need to learn about OSGi basics.

Dependent on what you need you could also think about using a ContextFunction.
Re: declarative service dependencies [message #1731805 is a reply to message #1731730] Tue, 10 May 2016 09:41 Go to previous messageGo to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
In fact the @Inject annotation is coming from javax, and many frameworks (like e4 and spring) have adopted it, so in theory OSGi might one day adopt the same annotation too. Thanks for the pointer to the declarative services annotations, I'll have a closer look to them later today. The only thing I still don't understand about all this is that E4 seems to be able to use @Inject in its own declarative services: for instance TranslationObjectSupplier in org.eclipse.e4.core.services uses '@Inject private IMessageFactoryService factoryService;'. Both of them are declarative services that are respectively defined in translationsupplier.xml and messagefactory.xml, and in translationsupplier.xml there is no reference to the IMessageFactoryService.
Re: declarative service dependencies [message #1731815 is a reply to message #1731805] Tue, 10 May 2016 10:43 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
The little but important difference is that the TranslationObjectSupplier is an ExtendedObjectSupplier, which is something special in the e4 layer. Technically there is some framework code (ProviderHelper) that searches for all registered DS components of type ExtendedObjectSupplier and performs the injection on them. But with the restriction that you can only inject objects from the app context (top-level IEclipseContext) IIRC, but definitely not the calling context. This limits you in what you can inject.

http://www.vogella.com/tutorials/Eclipse4OwnAnnotations/article.html
Re: declarative service dependencies [message #1731816 is a reply to message #1731815] Tue, 10 May 2016 10:58 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6361
Registered: July 2009
Senior Member
Dirk is right but I think we should remove the @Inject and replace them
with OSGi-Dependencies.

The current code eg does not take into account that an higher-ranked
service could be provided later on.

Tom

On 10.05.16 12:44, Dirk Fauth wrote:
> The little but important difference is that the
> TranslationObjectSupplier is an ExtendedObjectSupplier, which is
> something special in the e4 layer. Technically there is some framework
> code (ProviderHelper) that searches for all registered DS components of
> type ExtendedObjectSupplier and performs the injection on them. But with
> the restriction that you can only inject objects from the app context
> (top-level IEclipseContext) IIRC, but definitely not the calling
> context. This limits you in what you can inject.
>
> http://www.vogella.com/tutorials/Eclipse4OwnAnnotations/article.html
Re: declarative service dependencies [message #1731817 is a reply to message #1731816] Tue, 10 May 2016 11:10 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
@Tom
you mean in general or in our translation related services?

I still think the e(fx)clipse @Service annotation would be very useful in E4 in general! Wink
Unfortunately too late for Neon correct?
Re: declarative service dependencies [message #1731824 is a reply to message #1731817] Tue, 10 May 2016 11:31 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6361
Registered: July 2009
Senior Member
Hi,

Yes - it is a very very weird thing that is done with this @Inject when
we lookup our IContext & ExtendedObjectSupplier stuff.

They are operation on the OSGi-Layer so they should not use @Inject.

Tom

On 10.05.16 13:10, Dirk Fauth wrote:
> @Tom
> you mean in general or in our translation related services?
>
> I still think the e(fx)clipse @Service annotation would be very useful
> in E4 in general! ;)
> Unfortunately too late for Neon correct?
Re: declarative service dependencies [message #1731830 is a reply to message #1731824] Tue, 10 May 2016 11:48 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
@Tom
I just looked into TranslationObjectSupplier and the two field injections can be changed to OSGi service bindings to operate on the OSGi layer only. But what would we do about the Locale that is set via method injection?
Re: declarative service dependencies [message #1731858 is a reply to message #1731830] Tue, 10 May 2016 14:08 Go to previous messageGo to next message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
According to one of my colleagues the interface/implementation split is a bit out of fashion today, and he suggests that we only have an interface for (declarative) services that need more then one implementation. In the E4 trainings I got from Lars Vogel and Jonas Helmig we were advised to always use interfaces for DS. What is the 'current truth' in this debate ?
Re: declarative service dependencies [message #1731861 is a reply to message #1731858] Tue, 10 May 2016 14:24 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2495
Registered: July 2012
Senior Member
I can only laugh about the "out of fashion" statement. We are developing software not designing a website. But with the arise of script languages every good principle is "out of fashion" when you simply can do anything easily.

Nevermind, the answer to your question is "It depends!". Most people only see the "I only need one implementation" in terms of production. What about testing? Don't you want to test your code maybe with a mockup service? Sure you can mock methods of the service itself, but I personally find it easier to use a mockup implementation of the interface. But there are for sure situations where creating an interface is unnecessary and this works fine even with DS if you specify the class itself as provided service. Mostly these are "endpoints" e.g. a Felix Shell command to trigger a service.

So basically you need to check if you really only have one implementation ever (and please consider testing which is often missed). If you find that you only need one implementation you don't have to specify an interface.
Re: declarative service dependencies [message #1731866 is a reply to message #1731861] Tue, 10 May 2016 14:42 Go to previous message
Erik Vande Velde is currently offline Erik Vande VeldeFriend
Messages: 82
Registered: September 2012
Member
Thanks for your patience Dirk! I'm very impressed by the speed and quality of the answers we got out of you. You really helped us a lot, we learned many useful things in the course of this discussion, and I feel backed up in my opinion that we should use DS for the service layer ...
Previous Topic:P2 update from specific repo to latest
Next Topic:Toolbar and Statusbar disappears
Goto Forum:
  


Current Time: Sun Sep 24 05:13:16 GMT 2017

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

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