Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Hook in, after the MApplication context is filled by Services?
Hook in, after the MApplication context is filled by Services? [message #989004] Tue, 04 December 2012 09:43 Go to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
Is there a way to hook in, in order to do something with the MApplicationContext,
directly after Services like EHandlerService, ECommandService are inserted into it?

I need a pointer to the MApplication-Context,
directly after Services are inserted in order to reuse the EHandlerService, ECommandService etc. in my objects,
which can not inject the Services because they are not part of the e4 Model.
Re: Hook in, after the MApplication context is filled by Services? [message #989007 is a reply to message #989004] Tue, 04 December 2012 09:59 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1434
Registered: July 2012
Senior Member
I'll assume it would be using a lifecycle handler and create a method annotated with @ProcessAdditions. As this method is called before the renderer is called, it can be used to add elements to / remove elements from the model before the application finally starts.
Re: Hook in, after the MApplication context is filled by Services? [message #989010 is a reply to message #989007] Tue, 04 December 2012 10:06 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
Thnx Dirk, but when i do the following in the LyfeCycleManager - the EHandlerService is not found
    @ProcessAdditions
    void onProcessAddition(IEclipseContext eclipseContext) {
        Object o = eclipseContext.get(EHandlerService.class); // no EHandler
    }
Re: Hook in, after the MApplication context is filled by Services? [message #989015 is a reply to message #989010] Tue, 04 December 2012 10:30 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1434
Registered: July 2012
Senior Member
Ok I see, the issue seems to be that the EHandlerService is added to the context by the CommandServiceAddon. And this seems to be added after your @ProcessAdditions

Another way of achieving what you are trying to do is to use the injection mechanisms. You need to know that methods annotated with @Inject will be
a) called on instantiation of the class
b) called whenever the parameter value changes in context

Therefore the following snippet should be a solution (note that it don't need to be a lifecycle hook then I guess, but would make sense to put it there):
public class LifecycleHook {

	@Inject
	public void afterServiceInjection(@Optional EHandlerService service) {
		if (service != null) {
			System.out.println("do something when the service is set to the context");
		}
	}
}
Re: Hook in, after the MApplication context is filled by Services? [message #989017 is a reply to message #989010] Tue, 04 December 2012 10:33 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
IIRC handler service and command service are pushed by addons. So the
first possible position to get them is when those got processed. If your
addon is the last one in the row of processed ones you can get them
injected, I don't think there's a lifecylce event for this.

I have a proposal to add lifecycle to various elements model elements in
bugzilla but didn't proceed with it yet
(https://bugs.eclipse.org/bugs/show_bug.cgi?id=392903)

Tom

Am 04.12.12 11:06, schrieb Alex Kipling:
> Thnx Dirk, but when i do the following in the LyfeCycleManager - the
> EHandlerService is not found
>
> @ProcessAdditions
> void onProcessAddition(IEclipseContext eclipseContext) {
> Object o = eclipseContext.get(EHandlerService.class); // no EHandler
> }
>
Re: Hook in, after the MApplication context is filled by Services? [message #989020 is a reply to message #989015] Tue, 04 December 2012 10:44 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
Thnx Dirk.
I am fighting an additional problem, which I initiall was not aware of:

The Context, which I get inside the LifeCycleManager is different, from the context, which I to get inside my Model-Parts.

Is there some kind of LifecycleManager, where it would make sence to put your snippet into, in order to retrieve the Model-Part-Context?
Re: Hook in, after the MApplication context is filled by Services? [message #989025 is a reply to message #989020] Tue, 04 December 2012 10:52 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1434
Registered: July 2012
Senior Member
As Tom posted, no currently there is no such lifecycle manager. I'm with Tom, saying that there need to be something like a lifecycle handler for application model elements too.

So the only way at the moment would be something like adding my snippet directly to the part whose context you are needing. Adding the context as an additional parameter for the injection.

This dirty workaround is also the only way at the moment to register a different IWindowCloseHandler to your application.

Sorry, of course it is not the only way, you could also execute handlers and stuff, but that doesn't make it better. Smile

[Updated on: Tue, 04 December 2012 10:53]

Report message to a moderator

Re: Hook in, after the MApplication context is filled by Services? [message #989033 is a reply to message #989020] Tue, 04 December 2012 11:12 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
Am 04.12.12 11:44, schrieb Alex Kipling:
> Thnx Dirk. I am fighting an additional problem, which I initiall was not
> aware of:
>
> The Context, which I get inside the LifeCycleManager is different, from
> the context, which I to get inside my Model-Parts.
>

Yes that is expected. Contexts are hiearchical, we create a context for
each element in the model which inherits from MContext (e.g.
MApplication,MWindow,MPerspective,MPart)

Tom
Re: Hook in, after the MApplication context is filled by Services? [message #989043 is a reply to message #989033] Tue, 04 December 2012 12:19 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 386
Registered: December 2010
Location: Germany
Senior Member
Hi Aley,

You can create your objects with the ContextInjectionFactory passing the corret context.
If you do that you can inject the services normaly with @Inject.

Greetings
Christoph
Re: Hook in, after the MApplication context is filled by Services? [message #989050 is a reply to message #989043] Tue, 04 December 2012 12:56 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
@Tom - inside the Lifecycle it seems to be a complete different Context hierarchy with a differend root node. It is not just the Context objects, which is different.
@Christoph - my problem is - getting the right Context, immediately after EHandlerService and things ae created.

Additional Problems:

An additional constraint:
Is it possible, to do something with the Application-Context


  1. After the EHandler Services was injected into the Context
  2. But before the PostConstruct methods of the Handlers are triggered?


Re: Hook in, after the MApplication context is filled by Services? [message #989060 is a reply to message #989050] Tue, 04 December 2012 13:35 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1434
Registered: July 2012
Senior Member
Yes you can work on the Application context. Either inject MApplication and call getContext() on that, or go the hierarchy upwards with your current context calling getParent().

As I posted already, use my snippet to do something when the EHandlerService is injected.

I never used @PostConstruct on handlers. Not sure if your requirement fits into the lifecycle at all.
Re: Hook in, after the MApplication context is filled by Services? [message #989062 is a reply to message #989050] Tue, 04 December 2012 13:44 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
Am 04.12.12 13:56, schrieb Alex Kipling:
> @Tom - inside the Lifecycle it seems to be a complete different Context
> hierarchy with a differend root node. It is not just the Context
> objects, which is different.

Yes it the is the root-context of your application but IIRC it is the
same one as the the of MApplication. Like I said it is hiearchical

> @Christoph - my problem is - getting the right Context, immediately
> after EHandlerService and things ae created.
>
> Additional Problems:
>
> An additional constraint:
> Is it possible, to do something with the Application-Context
>
> After the EHandler Services was injected into the Context
> But before the PostConstruct methods of the Handlers are triggered?
>

The EHandlerService is not injected what we inject is a IContextFunction
and if you request the EHandlerService this function makes sure you get
an instance of EHandlerService.

The reason is that the EHandlerService is working in the local request
context. So you'll notice that in different areas of your application
you'll get different EHandlerService instances.

Tom
Re: Hook in, after the MApplication context is filled by Services? [message #989086 is a reply to message #989062] Tue, 04 December 2012 15:12 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
@Dirk
The Handlers seem to be constructed quite early.

For sure the @Inject annotated functions are triggered AFTER Handler's @PostConstruct methods.

Do you know, other possibilities,
to Hook in BETWEEN MApplication-Context creation and Handler's @PostConstruct methods?
Re: Hook in, after the MApplication context is filled by Services? [message #989094 is a reply to message #989086] Tue, 04 December 2012 15:24 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1434
Registered: July 2012
Senior Member
No AFAIK there is no such possibility. I'm still not sure if using @PostConstruct in a handler is a good idea. @PostConstruct is a specializiation of @Inject. And as Tom wrote in another thread:

Thomas Schindl wrote on Wed, 28 November 2012 11:24

No never use DI-Fields inside handlers! Never use @Inject inside
handlers this is doomed to fail miserably because the handler is created
in a completely different context then it is executed!


I might be wrong, but it seems what you are trying to do is not working at the moment, because there is no possibility to react in between the lifecycle like you want to.
Re: Hook in, after the MApplication context is filled by Services? [message #989105 is a reply to message #989094] Tue, 04 December 2012 15:44 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
Maybe Alex should describe his problem so maybe he's running into a
completely wrong direction. So what do you want to do in abstract terms
and why do you want to do it.

Tom

Am 04.12.12 16:24, schrieb Dirk Fauth:
> No AFAIK there is no such possibility. I'm still not sure if using
> @PostConstruct in a handler is a good idea. @PostConstruct is a
> specializiation of @Inject. And as Tom wrote in another thread:
>
> Thomas Schindl wrote on Wed, 28 November 2012 11:24
>> No never use DI-Fields inside handlers! Never use @Inject inside
>> handlers this is doomed to fail miserably because the handler is created
>> in a completely different context then it is executed!
>
>
> I might be wrong, but it seems what you are trying to do is not working
> at the moment, because there is no possibility to react in between the
> lifecycle like you want to.
Re: Hook in, after the MApplication context is filled by Services? [message #989260 is a reply to message #989105] Wed, 05 December 2012 11:47 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
What I did was:
Instantiating a creatable Class X Inside a Handler. The instantiateion was triggered inside a @PostConstruct method.
Handler{
..
@PostConstruct
OnPostConstruct(X myClassXObject){

}

@Creatable
X{
    @Inject
    EHandlerService handlerService;

    @Inject
    ECommandService commandService;

    @Inject
    MApplication app;
}


As seen the Class X required a pointer to the services, and the MApplication.
First I had to move the instantiation of X to a later time, by using IEventBroker.

Then I found out, that annotation the Handlers by @Optional makes the construct work.
The Handlers are not available at creation time of X,
but when X's methods are used - the Services are allready there.

Question:
Can seomone explain me, why the Injected Objects are empty on constuction, but non empty on method execution?
Does the System monitor @Optional Objects, where Injection failed and injects them later, as soon as a fitting Object becomes available?

Handler{
..
@PostConstruct
OnPostConstruct(X myClassXObject){

}

@Creatable
X{
    @Optional
    @Inject
    EHandlerService handlerService;

    @Optional
    @Inject
    ECommandService commandService;

    @Optional
    @Inject
    MApplication app;
}
Re: Hook in, after the MApplication context is filled by Services? [message #989270 is a reply to message #989260] Wed, 05 December 2012 12:43 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
The question remains why are you creating this instance of X. Why can't
you do that when the handler is really executed? What does this X do
that it has to be created when the handler is created.

Even if this would work as I outlined before the context that creates
the handler is completely a different one than the context in which the
handler is executed (=context of the currently active MPart).

There is no guarantee that the EHandlerService and ECommandService are
not bound to the invoking context and only work there appropriately
(e.g. there are handlers available on different levels and it depends on
the current active area which one take precendence).

Tom

Am 05.12.12 12:47, schrieb Alex Kipling:
> What I did was:
> Instantiating a creatable Class X Inside a Handler. The instantiateion
> was triggered inside a @PostConstruct method.
>
> Handler{
> .
> @PostConstruct
> OnPostConstruct(X myClassXObject){
>
> }
>
> @Creatable
> X{
> @Inject
> EHandlerService handlerService;
>
> @Inject
> ECommandService commandService;
>
> @Inject
> MApplication app;
> }
>
>
> As seen the Class X required a pointer to the services, and the
> MApplication. First I had to move the instantiation of X to a later
> time, by using IEventBroker.
>
> Then I found out, that annotation the Handlers by @Optional makes the
> construct work.
> The Handlers are not available at creation time of X, but when X's
> methods are used - the Services are allready there.
>
> Question:
> Can seomone explain me, why the Injected Objects are empty on
> constuction, but non empty on method execution?
> Does the System monitor @Optional Objects, where Injection failed and
> injects them later, as soon as a fitting Object becomes available?
>
>
> Handler{
> .
> @PostConstruct
> OnPostConstruct(X myClassXObject){
>
> }
>
> @Creatable
> X{
> @Optional
> @Inject
> EHandlerService handlerService;
>
> @Optional
> @Inject
> ECommandService commandService;
>
> @Optional
> @Inject
> MApplication app;
> }
>
Re: Hook in, after the MApplication context is filled by Services? [message #989273 is a reply to message #989270] Wed, 05 December 2012 13:12 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 258
Registered: July 2012
Senior Member
Hello Tom,
I felt, like it is no important - what the X does in @PostConstruct,
but if you think that it is important:

The Handler registers some IAdapterFactory objects to the Platform.
It does not use the Context, here. Later the registered Factories are executed in the right Context, so everything works fine.
Re: Hook in, after the MApplication context is filled by Services? [message #989386 is a reply to message #989273] Thu, 06 December 2012 00:59 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 5487
Registered: July 2009
Senior Member
But why is this done in a Handler? Why are you not simply contributing
an addon which points to X?

Tom

Am 05.12.12 14:12, schrieb Alex Kipling:
> Hello Tom,
> I felt, like it is no important - what the X does in @PostConstruct,
> but if you think that it is important:
>
> The Handler registers some IAdapterFactory objects to the Platform.
> It does not use the Context, here. Later the registered Factories are
> executed in the right Context, so everything works fine.
Previous Topic:Does a Lifecycle / Context creation diagram exist for e4?
Next Topic:Tooltip on viewer item when using AdapterFactoryLabelProvider
Goto Forum:
  


Current Time: Mon Dec 22 06:19:15 GMT 2014

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

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