Init injection and static IEclipseContext [message #1693822] |
Tue, 28 April 2015 14:11 |
Andrea Richiardi Messages: 64 Registered: August 2012 |
Member |
|
|
I am developing part of an open source editor and I developed some of it using Injection and IEclipseContext.
At the moment I have a method that inits the context called at the end of AbstractUIPlugin.start, but it feels a bit hackish.
/** The CCW custom context **/
private static IEclipseContext ccwContext;
....
/**
* Waiting for e4, this is the place where I initialize custom injectable instances.
*/
private IEclipseContext initInjections(BundleContext bundleContext) {
IEclipseContext c = EclipseContextFactory.getServiceContext(bundleContext)
.createChild(StaticStrings.CCW_CONTEXT_NAME);
ContextInjectionFactory.setDefault(c);
// Here is a workaround to use the EventBroker without logger
// Taken from here http://www.eclipse.org/forums/index.php/t/245307/ and here
// http://www.eclipse.org/forums/index.php/t/370090/
c.set(Logger.class, null);
ClojureInvoker.newInvoker(this, "ccw.editors.clojure.hover-support")._("init-injections", c);
return c;
}
First of all, is IEclipseContext thread-safe, meaning, can I share my static instance across multiple threads?
Second, is it the right way to initialize things and that my CCW_CONTEXT?
How can I be sure it is the first one looked up when I simply annotate a field with @Inject ?
Is there a better was to set the Logger instance?
Sorry for the numerous questions here but I am trying to understand how to fully integrate E4 features in this app and it is not always easy to put things together.
Thanks a lot,
-ar
|
|
|
|
|
|
Re: Init injection and static IEclipseContext [message #1693851 is a reply to message #1693846] |
Tue, 28 April 2015 18:21 |
Eclipse User |
|
|
|
It's better to have a child if you are populating something on a leaf. Otherwise place your values wherever it best makes sense. If you have application-level objects, place them on the application context; if you have per-window values, put them on the window context.
I'd configure your logger instance using OSGi DS. And I would definitely provide a logger instance!
Brian.
|
|
|
Re: Init injection and static IEclipseContext [message #1693899 is a reply to message #1693851] |
Wed, 29 April 2015 08:54 |
Andrea Richiardi Messages: 64 Registered: August 2012 |
Member |
|
|
Yes @Brian I think that your suggestion makes sense (of course thanks @Tom to have clarified what I was doing)
My injection is fundamentally a model, read in a PreferencePage (implementing IWorkbenchPreferencePage), with same life cycle as the application, therefore it makes sense to create it in the Application context. First problem solved, I will remove the child...
So this is the code to inject the model (sorry is Clojure, but it basically just sets the instance):
(defn init-injections
"Sets a new instance of HoverModel in the input context."
[^IEclipseContext context]
(.set context StaticStrings/CCW_CONTEXT_VALUE_HOVERMODEL
(reify
HoverModel
(observableHoverDescriptors [this]
(init-atoms)
(update-observables-atom @contributed-hovers))
(persistHoverDescriptors [this list]
(persist-java-hover-descriptors! observable-hovers)))))
My question now is, is there another option in order to init this instance? I am now calling it inside Plugin.start...because we still have legacy code...
/**
* Called in AbstractUIPlugin in order to initialize Application-context instances.
*/
private IEclipseContext initInjections(BundleContext bundleContext) {
IEclipseContext c = EclipseContextFactory.getServiceContext(bundleContext);
ClojureInvoker.newInvoker(this, "ccw.editors.clojure.hover-support")._("init-injections", c);
return c;
}
[Updated on: Wed, 29 April 2015 09:22] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.02100 seconds