Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » @Inject calls context function but ContextInjectionFactory.make does not. Why?
@Inject calls context function but ContextInjectionFactory.make does not. Why? [message #1386255] Mon, 16 June 2014 08:25 Go to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 47
Registered: June 2013
Member
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 #1386258 is a reply to message #1386255] Mon, 16 June 2014 08:39 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5859
Registered: July 2009
Senior Member
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).

One more warning - the
EclipseContextFactory.getServiceContext(bundleContext) - will give you
different context instances for each BundleContext!

Tom

On 16.06.14 10:25, Andreas Sewe wrote:
> 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 #1386279 is a reply to message #1386258] Mon, 16 June 2014 10:46 Go to previous messageGo to next message
Andreas Sewe is currently offline Andreas SeweFriend
Messages: 47
Registered: June 2013
Member
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).
Re: @Inject calls context function but ContextInjectionFactory.make does not. Why? [message #1386282 is a reply to message #1386279] Mon, 16 June 2014 10:53 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5859
Registered: July 2009
Senior Member
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
Previous Topic:using ECommandService and Preferences in LifecycleManager
Next Topic:Using EMenuService between Kepler and Luna
Goto Forum:
  


Current Time: Sun Aug 30 18:11:28 GMT 2015

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

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