Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » How do I consume a DS? (Take 2)(A rephrasing of an earlier question on consuming Declared Services. Help needed!)
icon5.gif  How do I consume a DS? (Take 2) [message #666957] Mon, 25 April 2011 06:02 Go to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
I posted a question last week but got no response so I will try rephrasing the question. How does one consume a service declared using Declarative Services? The current practice seems to be to inject it rather than using a ServiceTracker. I have to assume this means declaring the service consumer itself as a component under Declarative Services, not specifying a service but instead referencing the required service. For example:

Component registering service to be consumed:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" immediate="false" name="ca.footeware.hello.service">
   <implementation class="ca.footeware.hello.service.internal.HelloService"/>
   <service>
      <provide interface="ca.footeware.hello.service.IHelloService"/>
   </service>
</scr:component>


Component consuming above service:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="ca.footeware.hello.service.consumer">
   <implementation class="ca.footeware.hello.service.consumer.HelloConsumer"/>
   <reference bind="setHelloService" cardinality="1..1" interface="ca.footeware.hello.service.IHelloService" name="helloService" policy="static" unbind="unsetHelloService"/>
</scr:component>


This works fine but in practice I often have need for the service in a class whose lifecycle I cannot place under the control of Declarative Services, e.g. a ViewPart. Where once I would call out from the ViewPart to its containing bundle's Activator where I had a ServiceTracker to provide me the service, I'm trying to refactor to remove the ServiceTracker and somehow inject the service instead. I presume it should be injected where I need it, i.e. the ViewPart.

Using the above component structure, with my ViewPart as the consuming component, things don't work quite right. DS creates an instance of my ViewPart on application start, injects the service and calls its activate(). Then my RCP application creates another ViewPart instance when its Window > Show View menu is invoked. This new instance never gets injected or activated! How would you consume a DS from a ViewPart - I'm obviously doing it wrong Sad

What am I missing? Is there some combination of settings in the component.xml's that will make this work? Or should I componentize the Activator and again reach out to it from the ViewPart? Come on people, someone must have some ideas. I've been stuck here for weeks and I'm worried the mark on my forehead will become permanent!

Craig
Re: How do I consume a DS? (Take 2) [message #667001 is a reply to message #666957] Mon, 25 April 2011 12:27 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You're right about the lifecycle being off. The ViewPart is created
under control of the user (through Show View, for example). How dynamic
is the service that the view is consuming?

The view is going to have to request the service (either through
ServiceTracker or by being a service listener itself).

Or have your view reach out to a service component of its own plugin and
have that injected with the other OSGi services (the usefulness of this
depends on how your view can function if the service appears/disappears).

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


icon9.gif  Re: How do I consume a DS? (Take 2) [message #667133 is a reply to message #667001] Wed, 27 April 2011 00:18 Go to previous messageGo to next message
Craig Foote is currently offline Craig FooteFriend
Messages: 217
Registered: July 2009
Senior Member
Thanks Paul. That clears things up for me.

My service is not now dynamic but I was hoping to code its consumption to work if it later became so. I'll try injecting it into a utility class as a static variable and consume it in my ViewPart, that should work. I'm just glad I'm not chasing that wild goose anymore!

I'm a little disappointed that DS has to immediately load a pojo to inject it with a referenced service. I was expecting immediate=true to specify that. Without it, I expected the pojo to be injected with its reference and activated only when it was instantiated, much as when a declared service is created only when needed.

Am I correct that in Eclipse 4.x, the @Inject annotation will inject a service into a ViewPart (or any platform managed class) only when it's instantiated by the platform?

Thanks again,
Craig
Re: How do I consume a DS? (Take 2) [message #667199 is a reply to message #667133] Wed, 27 April 2011 11:18 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 04/26/2011 08:18 PM, Craig Foote wrote:
> I'm a little disappointed that DS has to immediately load a pojo to
> inject it with a referenced service. I was expecting immediate=true to
> specify that. Without it, I expected the pojo to be injected with its
> reference and activated only when it was instantiated, much as when a
> declared service is created only when needed.
> Am I correct that in Eclipse 4.x, the @Inject annotation will inject a
> service into a ViewPart (or any platform managed class) only when it's
> instantiated by the platform?

Yes, but that's because the showView execution path in 4.x specifically
injects the view instance after it is created from the IExecutableExtension.


In the DS case you are seeing, the DS service implementations are under
control of the DS framework, which has to instantiate them to inject
them. It's responsible for setting up the service trackers (or
equivalent) correctly to feed them into the Service.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Previous Topic:Equinox Aspects not functioning as documented
Next Topic:Forcing a plug-in to start
Goto Forum:
  


Current Time: Thu Sep 19 19:08:29 GMT 2024

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

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

Back to the top