Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to inject a service, which has the IEventBroker injected in it, in an Eclipse part
How to inject a service, which has the IEventBroker injected in it, in an Eclipse part [message #1005772] Mon, 28 January 2013 16:45 Go to next message
Sytse Goedemoed is currently offline Sytse Goedemoed
Messages: 2
Registered: January 2013
Junior Member
I want to inject a service into my Eclipse part. In itself this is not a problem, I can use a declarative OSGi service for this. The problem arises when I inject the IEventBroker in my service by means of an Eclipse addon. The addon has a hookListeners method which makes an instance of my service and sets it in the Eclipse context. The service that is injected in my Eclipse part is however another instance with a null IEventBroker. How can I make sure that the service instance which is set in the Eclipse context by the addon, is injected in my Eclipse part? Or is there a better way of achieving the above?
Re: How to inject a service, which has the IEventBroker injected in it, in an Eclipse part [message #1005924 is a reply to message #1005772] Tue, 29 January 2013 10:21 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

What you are trying to achieve above is not that clear. To get a specific thing from the context you put a named service and inject it with @Named. Although if you explain it better you may get a better answer.
Re: How to inject a service, which has the IEventBroker injected in it, in an Eclipse part [message #1005947 is a reply to message #1005924] Tue, 29 January 2013 11:33 Go to previous messageGo to next message
Sytse Goedemoed is currently offline Sytse Goedemoed
Messages: 2
Registered: January 2013
Junior Member
I realise that my explanation is not that clear, but fortunately I solved the problem. The problem was in my hookListeners method in the addon. I put the service in the Eclipse context using the service implementation class as a key.
	
@PostConstruct
void hookListeners(MApplication application)
{
   // Get the eclipse application context
   IEclipseContext context = application.getContext();

   // Instantiate service
   Service service = ContextInjectionFactory.make(ServiceImpl.class, context);

   // And stick it in the context so that e4 parts can inject it
   context.set(ServiceImpl.class, service);
}


The service interface is injected in my Eclipse part (@Inject Service service;) and therefore I guess the interface name is used to lookup the service in the Eclipse context. And if it doesn't find it, a new service instance is created without an IEventBroker injected in it.

So, the solution is to use

context.set(Service.class, service);


in the hookListeners method.
Re: How to inject a service, which has the IEventBroker injected in it, in an Eclipse part [message #1005959 is a reply to message #1005947] Tue, 29 January 2013 12:10 Go to previous message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Yes, and IIRC further more the interface used as a key is transformed internally to a string and put in the context as X.getClass.getName(). The context is string based and you can also put and retrieve keys using custom strings like context.put("CoolInterface",interfaceImpl); and retreive it with context.get or @Inject @Named("CoolInterface"). This idea is implemented in the selection retreival @Inject @Named(IServiceConstants.ACTIVE_SELECTION). the ISC.ACTIVE_SELECTION is just a custom string like CoolInterface.
Previous Topic:How do you inject the IEventBroker in an OSGi service ?
Next Topic:What happened to CSSScratchpad?
Goto Forum:
  


Current Time: Fri Oct 31 20:59:26 GMT 2014

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

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