Hi,

Which version of iPOJO are you using? Because "component.name" disappeared in the 1.2.0 to become "instance.name", moreover a "from" attribute allows you to select the provider.

So, imagine that you have a component type providing the service HelloWorld, and an instance created as follows:
<instance component="MyComponentType" name="MyProvider"/>

Then, you can write a service dependencies targeting only this provider:
@Requires(from="MyProvider")
private HelloWorld hello;

You can also configure the "from" attribute in the instance configuration such as:
<instance name="MYConsumer" component="MyConsumerType">
        <property name="requires.from">
                <property name="helloService1" value="MyProvider"/>
        </property>
</instance>
With the following dependency:
@Requires(id="helloService1")
private HelloWorld hello;

Regards,

Clement



On 11.07.2009, at 14:36, levko wrote:


I have 2 components/services providing the same interface. When both services are imported by using field injection (@Request annotations), I need to
select appropriate service. For this purpose I use LDAP filters
(component.name=...) in the @Request annotations. When I start my bundle in the Apache Felix OSGi, the bundle is activated, but the service calls fail.
If I remove the LDAP filters from the @Request annotations, only one
imported service is bound to both fields, and only one service is actually
called.

Can somebody help me with the problem of selecting appropriate imported
service from several services providing the same interface?

Here is my iPOJO test code:

a) service interface:
   public interface HelloWorld {
       void sayHello();
   }

b) 2 different components/services providing this interface:
   // component helloService1
   @Component(name="helloService1")
   @Provides(specifications=HelloWorld.class)
   public class HelloWorldService1 implements HelloWorld {
       @Override
public void sayHello() { System.out.println("HelloService1: Hello,
World!"); }
       @Validate public void startService() { System.out.println(
"[HelloWorldService1]: started" ); }
       @Invalidate public void stopService() { System.out.println(
"[HelloWorldService1]: stopped" );}
   }
   // component helloService2
   @Component(name="helloService2")
   @Provides(specifications=HelloWorld.class)
   public class HelloWorldService2 implements HelloWorld {
       @Override public void sayHello() {
System.out.println("HelloService2: Hello, World!"); }
       @Validate public void startService() { System.out.println(
"[HelloWorldService2]: started" ); }
       @Invalidate public void stopService() { System.out.println(
"[HelloWorldService2]: stopped" ); }
   }

c) the test needs to import and use both services:
   @Component(name="helloTest")
   public class HelloWorldTest {
       @Requires(id="helloService1",
filter="(component.name=helloService1)")
       private HelloWorld _helloService1;
       @Requires(id="helloService2",
filter="(component.name=helloService2)")
       private HelloWorld _helloService2;
       @Validate public void start() {
System.out.println("[HelloWorldTest]: started");
           _helloService1.sayHello(); _helloService2.sayHello();
       }
       @Invalidate public void stop() {
System.out.println("[HelloWorldTest]: stopped"); }
   }

Any ideas?

Thanks,
-- Lev
--
View this message in context: 
http://www.nabble.com/How-to-select-imported-service-in-iPOJO--tp24439983p24439983.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to