Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Constructor of Part is being called twice
Constructor of Part is being called twice [message #1358295] Thu, 15 May 2014 22:55 Go to next message
Fernando Paz is currently offline Fernando PazFriend
Messages: 22
Registered: May 2014
Junior Member
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 Smile
Re: Constructor of Part is being called twice [message #1368206 is a reply to message #1358295] Tue, 20 May 2014 03:37 Go to previous messageGo to next message
Fernando Paz is currently offline Fernando PazFriend
Messages: 22
Registered: May 2014
Junior Member
I noticed that this happens only when I use the Declarative Service feature from OSGi. If I disable it (removing the Service-Component from the MANIFEST.MF) the Part constructor is called just once.
Anyone knows why?
Re: Constructor of Part is being called twice [message #1368599 is a reply to message #1368206] Tue, 20 May 2014 07:32 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
This does not make any sense to me - you are doing something wrong - try
to provide stripping it down to the minimum and share your code.

Tom

On 20.05.14 05:37, Fernando Paz wrote:
> I noticed that this happens only when I use the Declarative Service
> feature from OSGi. If I disable it (removing the Service-Component from
> the MANIFEST.MF) the Part constructor is called just once.
> Anyone knows why?
>
Re: Constructor of Part is being called twice [message #1370211 is a reply to message #1368599] Tue, 20 May 2014 23:05 Go to previous messageGo to next message
Fernando Paz is currently offline Fernando PazFriend
Messages: 22
Registered: May 2014
Junior Member
Hi Tom

I'm attaching a very simple e4 application similar to my previous example (a Widget interface, bundles implementing Widget and a Part that looks up for all widgets).

If you read the outputs you'll notice that the Part constructor is being called twice. Remove the Service-Component from the manifest and it will be called once.

Regards
  • Attachment: e4-test.zip
    (Size: 30.67KB, Downloaded 103 times)

[Updated on: Tue, 20 May 2014 23:23]

Report message to a moderator

Re: Constructor of Part is being called twice [message #1371017 is a reply to message #1370211] Wed, 21 May 2014 07:17 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

Well of course there are 2 instances:

1st instance is created by OSGi-DS because you registered the guy as a
DS-Service

2nd instance is created by e4 because you have an MPart pointing to MainPart

You need to rethink your contribution strategy. I think your you want to
replace your MainPart-Instance in the OSGi-Registry with something like
WidgetComponent and work with it something like this in MainPart

// OSGi-Collector of widgets
public class WidgetComponent {
// best would be some kind of ObservableList!
private List<Widget> widgets;

public void addWidget(Widget w) {
widgets.add(w);
// if widgets is not an observable list fire a modification event
}

public List getWidgets() {
return widgets;
}
}

public class MainPart {
@PostConstruct
public void init( Composite c, WidgetComponent component ) {
// ...
}
}

Tom

On 21.05.14 01:05, Fernando Paz wrote:
> Hi Tom
>
> I'm attaching a very simple e4 application similar to my previous example (a Widget interface, bundles implementing Widget and a Part that looks up for all widgets). If you read the outputs you'll notice that the Part constructor is being called twice.
>
> Regards
>
Re: Constructor of Part is being called twice [message #1372516 is a reply to message #1371017] Wed, 21 May 2014 21:36 Go to previous messageGo to next message
Fernando Paz is currently offline Fernando PazFriend
Messages: 22
Registered: May 2014
Junior Member
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.
Re: Constructor of Part is being called twice [message #1373399 is a reply to message #1372516] Thu, 22 May 2014 06:38 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
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.
>
Previous Topic:Hide MWindow
Next Topic:Decorating in e4
Goto Forum:
  


Current Time: Tue Mar 19 10:15:06 GMT 2024

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

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

Back to the top