Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Eclipse contexts & Gemini blueprint
Eclipse contexts & Gemini blueprint [message #989974] Mon, 10 December 2012 10:28 Go to next message
Gaetan Pitteloud is currently offline Gaetan Pitteloud
Messages: 17
Registered: September 2012
Junior Member
I'm developing a rcp application with gemini blueprint, and I am getting confused with eclipse contexts :

1. The eclipse context I get with DI in my UI code is a WorkbenchContext (or a child of it), which parent is an EclipseOsgiContext for bundle org.eclipse.e4.ui.workbench.

2.The eclipse context that I get with this snippet
EclipseContextFactory.getServiceContext(BundleContext)

invoked from a spring bean using the injected BundleContext is an EclipseOsgiContext for my own bundle, which is different from the context I get in point 1 above.

OSGi services are declared in the blueprint xml configuration (with <reference> tag), and are available for DI in my UI code, while other spring beans (declared with standard <bean>) are not.

I would like to be able to inject standard spring beans into my UI code. Is there a way to either get the UI eclipse context from a spring bean (so that I can register the bean in the UI context), or is there another way to achieve it ?

Thanks in advance for any solution.
--
Gaƫtan
Re: Eclipse contexts & Gemini blueprint [message #991088 is a reply to message #989974] Mon, 17 December 2012 10:36 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Eclipse DI will give you services in the OSGi level. I'm not sure how these spring beans are registered in the OSGi layer but the fact that you can not inject them from eclipse means that they are not registered as OSGi services.

If I remember correctly you can not get IECs from the OSGi level. If you want your beans to communicate with the UI you can either supply a "central" declarative service which both "branches" (Eclipse and blueprint) can access or use the EventAdmin and have your beans put their stuff in the event bus and then subscribe for them through the IEventBroker in the Eclipse part. This should work because the EventAdmin is global for the whole OSGi runtime.
Re: Eclipse contexts & Gemini blueprint [message #991963 is a reply to message #991088] Thu, 20 December 2012 14:58 Go to previous messageGo to next message
Gaetan Pitteloud is currently offline Gaetan Pitteloud
Messages: 17
Registered: September 2012
Junior Member
I'm not sure I get you right, and I think the spring related question only adds a non-necessary layer of complexity, so let me explain my question with this example:

The Activator of my plugin:
public class MyActivator implements BundleActivator {
  public void start(BundleContext ctx) {
    Foo f = new Foo();
    expose(Foo.class, f);
  }
  public void stop(BundleContext ctx) {...}
}

Then, in the same plugin, I would like use this:
public class MyHandler {
  @Injected private Foo f;
  ...
}

How can I implement the expose method of MyActivator so that the 'f' instance that was created in MyActivator.start() gets injected in MyHandler ?
Re: Eclipse contexts & Gemini blueprint [message #992271 is a reply to message #991963] Fri, 21 December 2012 08:54 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

You can register Foo as an OSGi service, either through Declarative Services or programmatically. There are tutorials on that online. If you don't find any, ping back.
Re: Eclipse contexts & Gemini blueprint [message #992394 is a reply to message #992271] Fri, 21 December 2012 14:36 Go to previous message
Gaetan Pitteloud is currently offline Gaetan Pitteloud
Messages: 17
Registered: September 2012
Junior Member
I would prefer to not register Foo as an OSGi service. The idea is to register most of "local" spring beans (like Foo) in the Eclipse context of my plugin.

I tried a few things and came to this solution :

1. In OSGiApplicationContext for my plugin:
1.1. Define a bean of type IEclipseContext with EclipseContextFactory.create() named 'eclipseContext'
1.2. Define a bean post-processor that register beans in the eclipseContext

2. Register a lifeCycleURI class with @PostContextCreate in my plugin:
@PostContextCreate
public void exposeSpringBeans(IEcliseContext context)  {
  // 2.1. Get (and maybe wait until loaded) ApplicationContext from my BundleContext
  BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
  ApplicationContext ac = getApplicationContext(bundleContext);

  // 2.2. Get the eclipseContext that was created and populated during AC creation
  IEclipseContext eclipseSpringContext = ac.getBean("eclipseContext", IEclipseContext.class);

  // context.getParent() is the context for 'org.eclipse.e4.ui.workbench' bundle
  // this context has no parent
  // 2.3. Set the spring eclipse context as the parent of this one
  IEclipseContext eclipseUiPluginContext = context.getParent();
  eclipseUiPluginContext.setParent(eclipseSpringContext);
}


It finally works: I can inject a Spring bean in a UI component using @Inject.

But is it legal ? I mean, can I set a parent to the EclipseContextOSGi ? API won't prevent it, but I'm afraid to tweak something linked with OSGi.
Previous Topic:Re: dynamically add part to partStack
Next Topic:Background image on main shell
Goto Forum:
  


Current Time: Thu Oct 23 15:50:48 GMT 2014

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

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