Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to create a handler by id?
How to create a handler by id? [message #987082] Fri, 23 November 2012 11:20 Go to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
I know, how to create a ComandHandler by bunde URI.

@Inject
IContributionFactory contributionFactory
..

Object handler = contributionFactory.create("bundleclass://de.plugin.name/de.plugin.path.to.Handler", eclipseContext);


Is there a way to create the handler by id only?
Can I somehow resolve the id to bundleURI?
Re: How to create a handler by id? [message #987133 is a reply to message #987082] Fri, 23 November 2012 17:29 Go to previous messageGo to next message
Eclipse UserFriend
Handlers aren't required to have elementIds, and a command can have many possible handlers. You'd typically request that a handler be executed through the EHandlerService by specifying the command and parameters.
Re: How to create a handler by id? [message #987290 is a reply to message #987133] Sun, 25 November 2012 18:18 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
Sure, Brian, the Handlers are not required to have passwords - but if they do - how would I retrieve Handler's BundleURI by id, if I know the bundle?

Shouldn't there be a possibility, to get the bundle which contributed a custom Handlers with a custom id?

If I could find out, which bundle contributes a Handler with custom id - I could construct the Bundle URI.

I do not whant to use commands, because they are redundant in my case. I only whant to contribute some code through a Bundle, which would return the handler ID.
Re: How to create a handler by id? [message #987414 is a reply to message #987290] Mon, 26 November 2012 14:19 Go to previous messageGo to next message
Eclipse UserFriend
Why don't you just use an extension point?
Re: How to create a handler by id? [message #987638 is a reply to message #987414] Tue, 27 November 2012 12:00 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
Because getting information from an extension point is heavyweight:

IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(
                        MODULE_ID);
for (IConfigurationElement configElement : configElements) {
 //search the Element here...
}





contributing a Handler for a Command and executing it by ID is clearer.

It would be even better to contribute JUST THE Handler (no command), because I know that there will be only one Handler, and so I started this thread.

[Updated on: Tue, 27 November 2012 12:00]

Report message to a moderator

Re: How to create a handler by id? [message #987670 is a reply to message #987638] Tue, 27 November 2012 14:16 Go to previous messageGo to next message
Eclipse UserFriend
Quote:

Is there a way to create the handler by id only?
Can I somehow resolve the id to bundleURI?


Short answer: no. You'll need to do the search and creation yourself. You might want to check if the MHandler has an object already before creating a new one.

Quote:
contributing a Handler for a Command and executing it by ID is clearer.


I'm not sure how it's more clear considering it's (mis-?) using a part of the framework for a totally unanticipated and unintended purpose.

First, heavyweight is usually used in the context of execution time or memory consumption, not in having to code a loop. If you're seeing a performance hit iterating over the extension registry, you should be clear and file a bug report with reproducible data -- it's been pretty heavily tuned.

Second, all the model searches come down to iterating over the model elements. I.e., equally "heavyweight". In fact, it's even more "heavyweight" as handlers can be installed at many different points in the model (see the implementors of MHandlerContainer) -- so you're looking at a complete graph traversal, which is "expensive" in terms of stack (e.g., you'll be implementing BFS or DFS of some kind).

Finally, I'm not understanding your desire for pursuing this appraoch. If you know a handler, why are you using the E4 framework at all? The E4 command/handler design was intended to separate the intent (the command) from the implementation (the handler), where the implementation is identified based on the current context. This allows one part to contribute a completely different "org.eclipse.ui.delete" handler from the Package Explorer, for example. This seems to be an example of "when you have a hammer, everything looks like a nail".

Anyways, best of luck.

Brian.
Re: How to create a handler by id? [message #987794 is a reply to message #987670] Wed, 28 November 2012 08:31 Go to previous messageGo to next message
Alex Kipling is currently offline Alex KiplingFriend
Messages: 260
Registered: July 2012
Senior Member
Thnx for the answer, Brian!
Maybe I should provide some explanation:

You are right - I used "heavyweight" as a large, chunky piece of code, it was not about the erformance.

The Handler I know, because I'm getting a Plugin earlier in my code. This plugin is found dynamically, using the e4 way.
Now this plugin should contribute some own funcitonality. Plugin does this, by providing a handler. How else plugin could contribute some executable piece of code?
For now, to execute the code - plugin has to provide a command either. This is redundant, and so i would like to execute the handler directly.

I am aware about the purposes, which handlers and commands were introduced for.
Re: How to create a handler by id? [message #987807 is a reply to message #987794] Wed, 28 November 2012 09:08 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Hi,

not sure if this is of any help. But everything in e4 is a POJO. So whether you defined your handler in the application model or not, you can execute your handler by simply instantiating a handler pojo and call the execute method. As far as you don't need some injection in your handler this should work without calling any services to retrieve handlers by id. If you need some injection, you could try to inject via ContextInjectionFactory.

...
@Inject IEclipseContext context;

MyHandler myHandler = new MyHandler();
ContextInjectionFactory.inject(myHandler, context);

myHandler.execute(); //if execute is the method you annotated with @Execute
... 


You can get the bundle for the handler with the help of the handler class
Bundle bundle = FrameworkUtil.getBundle(MyHandler.class);


Everything else I know so far about calling handlers programmatically is related to the EHandlerService and the ECommandService which seems to be not the way you want to use it. Nevertheless, here's some code showing how to do it this way.

...

@Inject
ECommandService commandService;
	
@Inject
EHandlerService handlerService;

...
	Map params = new HashMap();
	ParameterizedCommand cmd = commandService.createCommand("my.command.id", params); //$NON-NLS-1$
	handlerService.executeHandler(cmd);
...


Hope this is of any help for you.

Greez,
Dirk
Re: How to create a handler by id? [message #987820 is a reply to message #987807] Wed, 28 November 2012 09:54 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Dirk Fauth wrote on Wed, 28 November 2012 10:08
If you need some injection, you could try to inject via ContextInjectionFactory.

...
@Inject IEclipseContext context;

MyHandler myHandler = new MyHandler();
ContextInjectionFactory.inject(myHandler, context);

myHandler.execute(); //if execute is the method you annotated with @Execute
... 


Thats a good point! You can also use
ContextInjectionFactory.invoke(myHandler, Execute.class, context);
to execute the handler including injections in the annotated method.
Re: How to create a handler by id? [message #987822 is a reply to message #987820] Wed, 28 November 2012 10:00 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Christoph Keimel wrote on Wed, 28 November 2012 10:54

You can also use
ContextInjectionFactory.invoke(myHandler, Execute.class, context);
to execute the handler including injections in the annotated method.


Thanks for pointing to this. This was new to me too. Smile
Re: How to create a handler by id? [message #987829 is a reply to message #987807] Wed, 28 November 2012 10:24 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Am 28.11.12 10:08, schrieb Dirk Fauth:
> Hi,
>
> not sure if this is of any help. But everything in e4 is a POJO. So
> whether you defined your handler in the application model or not, you
> can execute your handler by simply instantiating a handler pojo and call
> the execute method. As far as you don't need some injection in your
> handler this should work without calling any services to retrieve
> handlers by id. If you need some injection, you could try to inject via
> ContextInjectionFactory.
>
>
> ..
> @Inject IEclipseContext context;
>
> MyHandler myHandler = new MyHandler();
> ContextInjectionFactory.inject(myHandler, context);
>

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!

> myHandler.execute(); //if execute is the method you annotated with @Execute
> ..


You should use CIF.invoke()

Tom
Re: How to create a handler by id? [message #987832 is a reply to message #987829] Wed, 28 November 2012 10:34 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
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!

You should use CIF.invoke()

Tom


Thanks for explaining this! I think I had an issue with @Inject in a handler and solved it the way you describe it. But I didn't understand why (and didn't knew about CIF.invoke() ) until now. Learning more and more about e4 everyday. Smile

So to correct my statement from above, if you don't need injection and want to simply call a handler without a context, just create an instance of your handler (which is a POJO) and call the method you want. If you need injection, than use CIF.invoke() as Christoph and Tom explained.

Thanks,
Dirk
Previous Topic:How to tell and convert to pure e4
Next Topic:At what Point to insert a singleton class into the model?
Goto Forum:
  


Current Time: Wed Apr 24 22:39:33 GMT 2024

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

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

Back to the top