@Inject calls context function but ContextInjectionFactory.make does not. Why? [message #1386255] |
Mon, 16 June 2014 04:25  |
Eclipse User |
|
|
|
Hi,
I have a context function registered as an OSGi service:
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.recommenders.privacy.rcp">
<implementation class="org.eclipse.recommenders.privacy.rcp.PrivacySettingsServiceFactory"/>
<service>
<provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
</service>
<property name="service.context.key" type="String" value="org.eclipse.recommenders.privacy.rcp.IPrivacySettingsService"/>
</scr:component>
Upon an "@Inject IPrivacySettingsService" said context function gets called. This works fine.
If, however, I obtain the same(!) IEclipseContext through
BundleContext bundleContext = FrameworkUtil.getBundle(Startup.class).getBundleContext();
IEclipseContext eclipseContext = EclipseContextFactory.getServiceContext(bundleContext);
and then do a
service = ContextInjectionFactory.make(IPrivacySettingsService.class, eclipseContext); my context function is not called. Instead, the ContextInjectionFactory tries to directly instantiate the interface IPrivacySettingsService (which obviously fails).
Why the different behavior? What am I missing?
Any pointers are greatly appreciated.
Andreas
|
|
|
|
|
Re: @Inject calls context function but ContextInjectionFactory.make does not. Why? [message #1386282 is a reply to message #1386279] |
Mon, 16 June 2014 06:53  |
Eclipse User |
|
|
|
On 16.06.14 12:46, Andreas Sewe wrote:
> Quote:
>> You get what you asked for -
>> ContextInjectionFactory.make(IPrivacySettingsService.class,eclipseContext)
>>
>> - requests to create an instance of IPrivacySettingsService. I suppose
>> what you wanted to make is an instance of an Object that retrieves the
>> IPrivacySettingsService.
>>
>> If you really want to obtain an instance of IPrivacySettingsService
>> you'd simply do eclipseContext.get(IPrivacySettingsService.class).
>
> Of course! Thank you.
>
> Quote:
>> One more warning - the
>> EclipseContextFactory.getServiceContext(bundleContext) - will give you
>> different context instances for each BundleContext!
>
> Yes, but the OSGi service should be available in all of them, so I won't
> run into problems, I think.
>
> Anyways, what's the preferred way to obtain something from the Eclipse
> context from old E3 code? That's where I currently use
> EclipseContextFactory.getServiceContext(bundleContext).
The preferred way is to grab the
org.eclipse.e4.ui.workbench.IWorkbench-Instance from the OSGi-Service
registry and/or to get the local IEclipseContext you can use
IServiceLocator s = ...;
IEclipseContext ctx = s.getService(IEclipseContext.class);
which IMHO is the best way because then you get the correct context for
your usage e.g. the one on the part, perspective, window, ... .
Tom
|
|
|
Powered by
FUDForum. Page generated in 0.04002 seconds