Init injection and static IEclipseContext [message #1693822] |
Tue, 28 April 2015 10:11  |
Eclipse User |
|
|
|
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 #1693899 is a reply to message #1693851] |
Wed, 29 April 2015 04:54   |
Eclipse User |
|
|
|
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 05:22] by Moderator
|
|
|
Re: Init injection and static IEclipseContext [message #1693924 is a reply to message #1693899] |
Wed, 29 April 2015 06:07  |
Eclipse User |
|
|
|
For the injection part, I ended up exposing a method in my plugin:
public static IEclipseContext getEclipseContext() {
return EclipseContextFactory.getServiceContext(CCWPlugin.getDefault().getBundle().getBundleContext());
}
And retrieving in the PreferencePage with:
ContextInjectionFactory.inject(this, CCWPlugin.getEclipseContext());
Still feels hackish but I guess is my only option at the moment.
|
|
|
Powered by
FUDForum. Page generated in 0.11594 seconds