Constructor of Part is being called twice [message #1358295] |
Thu, 15 May 2014 18:55  |
Eclipse User |
|
|
|
I'm creating a very simple e4 RCP application for study purposes. Therefore, I'm facing a weird behaviour in the platform.
What I want to achieve:
A view that is capable of loading widgets (clock, weather forecast, feed, etc.) in runtime.
What I think I should do:
Each widget has to be a bundle that offers the same base service (i.e. IWidget). A view would lookup for all widgets and add/remove them whenever they are installed/uninstalled in the OSGi container. I would be able to achieve this through Declarative Services (DS).
What I currently have:
I have 4 bundles in different projects:
- Widget bundle, which has just an interface IWidget with a method getName() only;
- Clock Widget, an implementation of IWidget;
- Main View Part, which has a constructor (that currently does nothing), a @PostConstruct method and bind methods (addWidget(IWidget) and removeWidget(IWidget)) used by the DS.
- Application, that contains a e4xmi with a Part referencing the MainViewPart class.
A class diagram can be found here: i.imgur.com/TD8JRSv.jpg
What my issue is:
When I ran my application I noticed that the MainViewPart constructor is being called twice. Here are the outputs:
1. MainViewPart - Constructor
2. ClockWidget - Constructor
3. MainViewPart - Adding widget ClockWidget
4. MainViewPart - Constructor
5. MainViewPart - @PostConstruct
6. MainViewPart - Removing widget ClockWidget
As you can see, the MainViewPart constructor is called first time. Then the ClockWidget is instantiated and after that the MainViewPart constructor is called again (line 4.). This behaviour brings a terrible limitation to what I'm trying to do. I haven't found people complaining about this on the web, so I suppose it is something expected, or maybe I should try another approach. I'm not following any specific tutorial, just trying to do something with the experience earned so far.
Any help is appreciated
|
|
|
|
|
|
|
|
Re: Constructor of Part is being called twice [message #1373399 is a reply to message #1372516] |
Thu, 22 May 2014 02:38  |
Eclipse User |
|
|
|
using DI has the 2 advantages:
a) no compile time dependency on OSGi
b) you get the dynamics from OSGi so you can install/uninstall
components at runtime
Tom
On 21.05.14 23:36, Fernando Paz wrote:
> I thought that e4 wouldn't create a second instance if there is already
> a service registered. I was suspecting that was happening though.
>
> To avoid that I was using a similar approach: my MainPart was looking up
> for widgets using the bundleContext.getServiceReferences(). I didn't
> know that you could inject any DS object in the @PostConstruct. This way
> seems better.
>
> Thanks, mate.
>
|
|
|
Powered by
FUDForum. Page generated in 0.45665 seconds