You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by levko <le...@yahoo.com> on 2009/07/11 14:36:59 UTC

How to select imported service in iPOJO?

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: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: How to select imported service in iPOJO?

Posted by Clement Escoffier <cl...@gmail.com>.
Hi,

So, I fix the issue. In fact, it allows me to discover a bug in the  
iPOJO annotation processing, detecting conflicts when there aren't.  
The issue will be fixed shortly.
To avoid this issue, avoid using the 'name' attribute in the  
@Component annotation, and use the class name in the metadata.xml file  
if you want to set the instance name in the XML file:
<instance component="lk.ipojo.samples.service.HelloWorldService1"  
name="helloService1"/>
<instance component="lk.ipojo.samples.service.HelloWorldService2"  
name="helloService2"/>
<instance component="helloTest">
		<property name="requires.from">
			<property name="helloService1" value="helloService1"/>
			<property name="helloService2" value="helloService2"/>
		</property>	
</instance>

Then, I get the following trace:
-> [HelloWorldService1]: started
[HelloWorldService2]: started
[HelloWorldTest]: started
HelloService1: Hello, World!
HelloService2: Hello, World!

-> stop 0
-> -> [HelloWorldTest]: stopped
[HelloWorldService1]: stopped
[HelloWorldService2]: stopped

The issue will be fixed shortly. I already implemented the fix, and  
I'm testing it. It will be contained in the next snapshot  (and  
obviously in the next release).

Regards,

Clement


On 11.07.2009, at 16:18, levko wrote:

>
> Hi Clement,
>
> Thanks for the prompt answer. I use iPOJO 1.2.0 and create the  
> bundle by
> using the ant script copied (and slightly modified) from the 'iPOJO in
> 10mins' tutorial.
>
> 1) After modifying filters in @Request annotations to  
> (instance.name=...),
> the bundle gets activated, but it does not print testing messages,  
> so the
> test component (consumer) is not started.
> 2) After adding attributes name="helloService1" and  
> name="helloService2" to
> appropriate 'instance' elements in the metadata.xml file, and  
> replacing
> filters in @Request annotations with from=..., the bundle gets  
> activated,
> but it does not print anything at all, even the 'start' messages from
> services.
> 3) I also tried adding properties (as you suggested) to the instance
> specification of my consumer component (tester). This had the same  
> effect as
> in p.p.2.
>
> Any ideas of what can be wrong in my configuration?
>
> I have attached zip file containing my project.
>
> http://www.nabble.com/file/p24440689/lk.ipojo.samples.zip
> lk.ipojo.samples.zip
>
> Thanks,
> -- Lev
>
>
> clement escoffier wrote:
>>
>> 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: users-unsubscribe@felix.apache.org
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/How-to-select-imported-service-in-iPOJO--tp24439983p24440689.html
> Sent from the Apache Felix - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: How to select imported service in iPOJO?

Posted by levko <le...@yahoo.com>.
Hi Clement,

Thanks for the prompt answer. I use iPOJO 1.2.0 and create the bundle by
using the ant script copied (and slightly modified) from the 'iPOJO in
10mins' tutorial. 

1) After modifying filters in @Request annotations to (instance.name=...),
the bundle gets activated, but it does not print testing messages, so the
test component (consumer) is not started.
2) After adding attributes name="helloService1" and name="helloService2" to
appropriate 'instance' elements in the metadata.xml file, and replacing
filters in @Request annotations with from=..., the bundle gets activated,
but it does not print anything at all, even the 'start' messages from
services.
3) I also tried adding properties (as you suggested) to the instance
specification of my consumer component (tester). This had the same effect as
in p.p.2.

Any ideas of what can be wrong in my configuration?

I have attached zip file containing my project.

http://www.nabble.com/file/p24440689/lk.ipojo.samples.zip
lk.ipojo.samples.zip 

Thanks,
-- Lev


clement escoffier wrote:
> 
> 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: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
> 
> 
> 

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


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: How to select imported service in iPOJO?

Posted by Clement Escoffier <cl...@gmail.com>.
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: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org