felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Jencks <david_jen...@yahoo.com>
Subject Re: Dynamically referencing a Service using Felix annotations
Date Wed, 26 Sep 2012 06:33:54 GMT
You don't say which kind of annotations you are using (osgi ds, felix ds, or bnd ds).  I hope
its osgi ds :-)

This will work fine.  However, you don't need the dynamic policy as long as all the CheckReference
implementations are available before this manager component is created.

Also, at least if you use bnd "next" for the annotation processing, you don't need to specify
the spec version, bnd will figure it out for you.

You might not want to instantiate the CheckReferenceImpls unless you actually want to use
one of them.  In this case you can use bind/unbind methods taking a ServiceReference<CheckReference>
 which will give you the properties you need to decide which one you want, and you can use


componentContext.locateService( "CheckReference", ref )

to get the actual service.

david jencks

On Sep 25, 2012, at 10:44 PM, Chetan Mehrotra wrote:

> You can use dynamic references
> @Component(...,specVersion="1.1")
> @References({
>        @Reference(
>                name = "CheckReference",
>                referenceInterface = CheckReference.class,
>                cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
>                policy = ReferencePolicy.DYNAMIC)
> })
> public class LoadReference {
> 
> And then have bind methods like
> 
> private Map<String,CheckReference> references = new
> ConcurrentHashmap<String,CheckReference>()
> 
> private void bindCheckReference(CheckReference ref,Map config) {
>        String domain = (String)config.get("domain");
>        references.put(domain,ref);
> }
> 
> private void unbindCheckReference(CheckReference ref,Map config) {
>        String domain = (String)config.get("domain");
>        references.remove(domain);
> 
> }
> 
> 
> And then in your implementation lookup the appropriate impl from the
> references map
> 
> Chetan Mehrotra


Mime
View raw message