Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Inject IEclipseContext in Service-Component
Inject IEclipseContext in Service-Component [message #1709161] Thu, 24 September 2015 14:34 Go to next message
Thibault Pensec is currently offline Thibault PensecFriend
Messages: 18
Registered: September 2015
Location: Nice (France)
Junior Member
Hi,

I need to use DI in the activation method of an OSGI Service-Component. To resume the situation, see bellow my actual configuration.

APPLICATION PLUGIN (com.my.app)
First of all, I inject a custom context bean in the IEclipseContext :

public class LifeCycleManager {
	
	@PostContextCreate
	public void postContextCreate(IEclipseContext context) {
		
		//
		// Initialize the Custom Context and set it into the Eclipse Context
		//
               final MCustomContext customContext = PersistenceUtils.load(MCustomContext.class);
	       context.set(MCustomContext.class, customContext);
	}
}


SERVICE PLUGIN (com.my.service)
Next I declare a Service-Component in the META-INF/MANIFEST.MF of my service plug-in :

Bundle-Name: Service
Bundle-SymbolicName: com.my.servicer;singleton:=true
Require-Bundle: com.my.app,
 javax. inject,
 org.apache.log4j,
 org.eclipse.e4.core.di,
 org.eclipse.e4.core.services,
 org.eclipse.e4.ui. model.workbench,
 org.eclipse.osgi.services
Service-Component: OSGI-INF/mycomponent.xml
Bundle-ActivationPolicy: lazy


Here the content OSGI-INF/mycomponent.xml :

<?xml version="1.0" encoding="UTF-8"?>
<scr:component activate="activate" name="com.my.service">
   <implementation class="com.my.internal.MyServiceComponent"/>
   <service>
      <provide interface="com.my.service.MyService"/>
   </service>
</scr:component>


And finally, the implementation code of my component where I need the retrieve my MCustomContext :

public class MyServiceComponent implements MyService {
	
	@Inject
	MCustomContext customContext;
	
	/**
	 * Activation method
	 */
	protected void activate(ComponentContext context) {
               // this.customContext == null
       }
}


Is it the right solution ? Or is it simply impossible ?

Need your help Cool
Re: Inject IEclipseContext in Service-Component [message #1709166 is a reply to message #1709161] Thu, 24 September 2015 14:52 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2611
Registered: July 2012
Senior Member
You can not use DI in an OSGi service. Dependent on your needs you either specify a referenced service if you want to stay in the OSGi world, or try to use a context function which supports access to the IEclipseContext.

http://www.vogella.com/tutorials/Eclipse4ContextFunctions/article.html
Previous Topic:How to port code using E3 ShowViewDialog to E4?
Next Topic:Initial enable status of Dynamic Menu Contribution not properly determined with sub-elements
Goto Forum:
  


Current Time: Tue Jul 17 13:29:50 GMT 2018

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

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

Back to the top