Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Init injection and static IEclipseContext
Init injection and static IEclipseContext [message #1693822] Tue, 28 April 2015 14:11 Go to next message
Andrea Richiardi is currently offline Andrea RichiardiFriend
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 #1693825 is a reply to message #1693822] Tue, 28 April 2015 14:34 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
I don't have a clue what you are trying to achieve. What is your goal by keeping a static IEclipseContext? There is a hierarchy of IEclipseContexts, so making some static access (which is private in your example) doesn't look correct.

Why can't you simply use the IEclipseContext of the application to put your instances into? Why can't you use @Creatable and @Singleton? Why do you use AbstractUIPlugin.start? IMHO a ModelProcessor or ModelAddon is a better way to go.
Re: Init injection and static IEclipseContext [message #1693846 is a reply to message #1693825] Tue, 28 April 2015 17:28 Go to previous messageGo to next message
Andrea Richiardi is currently offline Andrea RichiardiFriend
Messages: 64
Registered: August 2012
Member
Sorry, yes, first of all it is a legacy approach as we have not ported everything to E4 yet.
Second, sorry again, the static field is the exposed with a getter.

About the application context, it is true I could put everything there, but I have read here that is kind of better to have your own child. In my case I push custom instances in the context somewhere and then I try to read them somewhere else.
Re: Init injection and static IEclipseContext [message #1693850 is a reply to message #1693846] Tue, 28 April 2015 18:15 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Yes but your are overwriting the default context of your IDE which is
going the make many things fail!

You can easily get access to the IEclipseContext yourself from the
ServiceLocator simply request IEclipseContext from there and you get the
correct one and from there please create the child context you want to
use or publish your stuff in the upstream hierarchy

Tom

On 28.04.15 19:28, Andrea Richiardi wrote:
> Sorry, yes, first of all it is a legacy approach as we have not ported
> everything to E4 yet.
> Second, sorry again, the static field is the exposed with a getter.
>
> About the application context, it is true I could put everything there,
> but I have read here that is kind of better to have your own child. In
> my case I push custom instances in the context somewhere and then I try
> to read them somewhere else.
Re: Init injection and static IEclipseContext [message #1693851 is a reply to message #1693846] Tue, 28 April 2015 18:21 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Andrea Richiardi is currently offline Andrea RichiardiFriend
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

Re: Init injection and static IEclipseContext [message #1693924 is a reply to message #1693899] Wed, 29 April 2015 10:07 Go to previous message
Andrea Richiardi is currently offline Andrea RichiardiFriend
Messages: 64
Registered: August 2012
Member
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.
Previous Topic:How to get objects be injected into an preference page?
Next Topic:Size of a Part's client area during PostConstruct
Goto Forum:
  


Current Time: Tue Mar 19 02:05:47 GMT 2024

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

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

Back to the top