ComponentFactory unsatisfied [message #878378] |
Tue, 29 May 2012 07:24 |
Bert Robben Messages: 1 Registered: May 2012 |
Junior Member |
|
|
Hi all,
I'm using declarative services and want to do the following:
- I have two components A and B
- An instance of A always has a 1..1 reference to an instance of B
- I want to have multiple instances of A and B
So I tried the following (complete source in attached file):
- define component A with a factory and a 1..1 reference to B
<scr:component xmlns:scr="..." activate="activate" factory="playground_osgi.A" name="A">
<implementation class="playground_osgi.A"/>
<property name="name" type="String" value="unknown"/>
<reference bind="onBUp" cardinality="1..1" interface="playground_osgi.B" name="B" policy="static" target="(name=to_be_set_dynamically)"/>
</scr:component>
- define component B with a factory
<scr:component xmlns:scr="..." activate="activate" factory="playground_osgi.B" name="B">
<implementation class="playground_osgi.B"/>
<property name="name" type="String" value="unknown"/>
<service>
<provide interface="playground_osgi.B"/>
</service>
</scr:component>
- inject the componentFactory of A and the componentFactory of B somewhere and dynamically create new instances. To make sure that each A is connected to his own instance of B dynamically set the target of the reference to an appropriate value. This is shown in the following code.
for (int i = 1; i < 5; i++) {
Dictionary<String, String> properties = new Hashtable<String, String>();
String nameB = "B_" + i;
properties.put("name", nameB);
componentFactoryB.newInstance(properties);
properties.put("name", "A_" + i);
properties.put("B.target", "(name=" + nameB + ")");
componentFactoryA.newInstance(properties);
}
However, when I try to do this, this fails because A is unsatisfied and as a result I can't inject the componentFactory of A. A is unsatisfied because it can't find an appropriate instance of B to refer to.
One work-around (hack?) to solve this issue is to change the cardinality of the reference from A->B to 0..1. If I do that, the componentFactory of A can be resolved and the piece of code above nicely creates 5 couples (A->B).
However, this really seems to be a dirty hack to me. In addition, I don't fully understand what I am doing wrong here. Is this not supported by the spec? Or is this a bug/feature of equinox?
Bert
|
|
|
|
Powered by
FUDForum. Page generated in 0.03375 seconds