Home » Eclipse Projects » Eclipse Platform » Trying to use eclipse dependency injection of a service from a non-eclipse bundle
Trying to use eclipse dependency injection of a service from a non-eclipse bundle [message #1720764] |
Wed, 20 January 2016 13:27  |
Eclipse User |
|
|
|
I am trying to use eclipse dependency injection to get a service from a
non-eclipse OSGi bundle injected into eclipse components.
What I'm wondering, is: is this supposed to work?
What classes in eclipse can accept such injections?
The documentation I've been able to google up hasn't been clear (to me
at least) on this:
http://eclipsesource.com/blogs/tutorials/eclipse-4-e4-tutorial-part-4-dependency-injection-basics/
https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection
The second figure in the tutorial seems to indicate that services
registered by OSGi bundles should be possible to inject, but I may have
been trying to inject in the wrong classes? Or the service may not be
in the OSGi context of the editor? (is some special magic required to
accomplish this?)
This is what I've done:
1. Created an OSGi bundle with a bundle activator, that registers
no.priv.bang.modeling.modelstore.Modelstore as an OSGi service
https://github.com/steinarb/modelstore
2. Created a tycho project for a GEF editor plugin, with a target
platform that includes the modelstore bundle from a maven
dependency:
https://github.com/steinarb/modeler/tree/using-modelstore
https://github.com/steinarb/modeler/tree/using-modelstore/modeler.target
(Note! This editor was intented for a different backend, that's why
the links above are to the "using-modelstore" branch)
3. Tried adding @Inject of the Modelstore service in both the Model
(currently just a small mock model)
https://github.com/steinarb/modeler/commit/f2e157093d31e51302de5d1bca96840738da6e02
and tried adding @Inject of the Modelstore into the GEF Editor
https://github.com/steinarb/modeler/commit/7def11350f221470fb7d672dac4f8b6891d24239
but as far as can tell, these are not called (breakpoints in these
methods are not hit, and the println() texts don't show up in the
console)
I have started an "eclipse application" run configuration that starts
the application with the "-console" argument, and when I run the 'lb'
command I see this:
126|Active | 4|modeler Core Plugin (1.0.0.qualifier)
127|Active | 4|modelstore OSGi Bundle (1.0.0.SNAPSHOT)
Ie. both the modeler.core plugin (the eclipse plugin containing the GEF
editor) and the OSGi bundle are present and are started.
I can also list the available service in the modelstore bundle:
osgi> inspect service capability 127
modelstore OSGi Bundle (127) provides services:
-----------------------------------------------
objectClass = no.priv.bang.modeling.modelstore.Modelstore
service.id = 67
But there is no requirement for the service in the modeler.core bundle:
osgi> inspect service requirement 126
modeler Core Plugin (126) requires services:
--------------------------------------------
Nothing
I have added a bundle dependency to the modelstore bundle, to be able to
use types from the modelstore bundle in the injections.
Hope somebody has some idea on what I'm doing wrong?
Thanks!
- Steinar
|
|
| |
Re: Trying to use eclipse dependency injection of a service from a non-eclipse bundle [message #1720932 is a reply to message #1720924] |
Thu, 21 January 2016 16:39   |
Eclipse User |
|
|
|
>>>>> Steinar Bang <sb@xxxxxxxx>:
> According to this article https://wiki.eclipse.org/Efxclipse/Runtime/Recipes#Injection_of_OSGi-Services :
> "By default the DI container of Eclipse will inject you a service found in the OSGi-Registry but this default comes with a few restrictions who might cause you troubles:
> - you can only inject one instance (the one with the highest rank in the OSGi-Service-Registry)
> - you won't get a new instance reinjected if a new service instance with a higher ranking is registered on the OSGi-Service-Registry"
> Neither of the two restrictions are a problem for me (at least not for
> the time being). I just need to inject a "hook" that eclipse can work
> with.
> But why don't I see any injections of my service, I wonder...?
My problem could be bundle activation order, perhaps...?
See https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection#Current_Caveats :
Although E4AP's DI will inject OSGi services, it does not currently
track changes to the service. This means that if the service is not
available at time of initial injection, but subsequently becomes
available, existing requestors for the the service are not
notified. Nor are receivers notified should the service
disappear. This work is being tracked across several bugs: bug 331235,
bug 330865, bug 317706.
The eclipse plugin modeler.core is bundle number 126, while the bundle
providing the service is 127.
I also found something here:
https://wiki.eclipse.org/Eclipse4/RCP/FAQ#Why_isn.27t_my_.40Inject-able.2F.40PostConstruct_methods_being_injected.3F
When I did what's suggested in the link above, ie. added
Import-Package: javax.annotation; version="1.1.0"
to the MANIFEST.MF of the eclipse plugin, then:
1. the bundle id of the eclipse plugin changed from 126 to 328
2. bundle 127 (the OSGi bundle) is still listed as ACTIVE
3. none of the inject points where called
So no luck yet.
|
|
|
Re: Trying to use eclipse dependency injection of a service from a non-eclipse bundle [message #1720976 is a reply to message #1720932] |
Fri, 22 January 2016 04:33   |
Eclipse User |
|
|
|
So the let's start from the beginning. Who is creating an instance of
your object? Is it the eclipse framework? If you register your service
through DS the activation order does not matter as DS runs at start level 2.
Tom
On 21.01.16 22:39, Steinar Bang wrote:
>>>>>> Steinar Bang <sb@xxxxxxxx>:
>
>> According to this article https://wiki.eclipse.org/Efxclipse/Runtime/Recipes#Injection_of_OSGi-Services :
>
>> "By default the DI container of Eclipse will inject you a service found in the OSGi-Registry but this default comes with a few restrictions who might cause you troubles:
>> - you can only inject one instance (the one with the highest rank in the OSGi-Service-Registry)
>> - you won't get a new instance reinjected if a new service instance with a higher ranking is registered on the OSGi-Service-Registry"
>
>> Neither of the two restrictions are a problem for me (at least not for
>> the time being). I just need to inject a "hook" that eclipse can work
>> with.
>
>> But why don't I see any injections of my service, I wonder...?
>
> My problem could be bundle activation order, perhaps...?
>
> See https://wiki.eclipse.org/Eclipse4/RCP/Dependency_Injection#Current_Caveats :
>
> Although E4AP's DI will inject OSGi services, it does not currently
> track changes to the service. This means that if the service is not
> available at time of initial injection, but subsequently becomes
> available, existing requestors for the the service are not
> notified. Nor are receivers notified should the service
> disappear. This work is being tracked across several bugs: bug 331235,
> bug 330865, bug 317706.
>
> The eclipse plugin modeler.core is bundle number 126, while the bundle
> providing the service is 127.
>
> I also found something here:
> https://wiki.eclipse.org/Eclipse4/RCP/FAQ#Why_isn.27t_my_.40Inject-able.2F.40PostConstruct_methods_being_injected.3F
>
> When I did what's suggested in the link above, ie. added
> Import-Package: javax.annotation; version="1.1.0"
> to the MANIFEST.MF of the eclipse plugin, then:
> 1. the bundle id of the eclipse plugin changed from 126 to 328
> 2. bundle 127 (the OSGi bundle) is still listed as ACTIVE
> 3. none of the inject points where called
>
> So no luck yet.
>
|
|
| | | | | | | | | | | |
Goto Forum:
Current Time: Fri Mar 28 06:32:16 EDT 2025
Powered by FUDForum. Page generated in 0.05107 seconds
|