OSGI R6 Reference Annotation on Field | Community
Skip to main content
Level 2
September 6, 2021
Solved

OSGI R6 Reference Annotation on Field

  • September 6, 2021
  • 3 replies
  • 2442 views

Hi Folks,
I am using @3214626 annotation from OSGI R6 on a field which is of type Map. 

 

@3214626(service = serviceName.class, cardinality = ReferenceCardinality.MANDATORY_MULTIPLE, policy = ReferencePolicy.DYNAMIC)

Map<String, String> connection = new HashMap<~>(); 

 

I usually get an error the the field type should either collection or list. why so? I want to use map and don't want to modify the connection variable type.

please advise.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shubham_borole

Hi @ravijais04 If I understand the issue/question correctly..
Can you please mention what do the OSGi values in config look like?

With cardinality being multiple, you are probably adding multiple values to it. How would that resolve to a Map?
You would have to get the OSGi values as a list or collection (or String[] may be).

key1|value1 key2|value2 key3|value3

 

If your config has values like above, then you will have to get the OSGi values as list and separate the items based on the delimiter '|' and generate your map in the @activate or @modified method whichever is available and works.

3 replies

Kiran_Vedantam
Community Advisor
Community Advisor
September 6, 2021

Hi @ravijais04,

Which field are you trying to refer here? And when you are creating an object as you did, you are creating a new object for the map but not refrencing it. Try like this

 

@reference(service = serviceName.class, cardinality = ReferenceCardinality.MANDATORY_MULTIPLE, policy = ReferencePolicy.DYNAMIC)

Map<String, String> connection;

 

Add a getter and check if you are getting the service reference object.

 

Thanks,
Kiran Vedantam.

Level 2
September 6, 2021

Thanks Kiran for the response.

 

the field I am referring here is connection which is having HashMap object being assigned at class level. which is totally fine.

@3214626(service = serviceName.class, cardinality = ReferenceCardinality.MANDATORY_MULTIPLE, policy = ReferencePolicy.DYNAMIC)

Map<String, String> connection = new HashMap<~>(); 

 

if I use this way and when the AEM instance starts. it leaves the message in the log that the field connection should be type of collection or list as connection is map here which doesn't fall under collection hierarchy.

 

refer - https://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-reference.field.option

it says for a reference with multiple cardinality, the type of the field must be Collection or List.


Now, my question is that How i can make it work with Map itself as I don't want to change the Map to type of java.util.Collection or its sub-type.

Vijayalakshmi_S
Level 10
September 7, 2021

Hi @ravijais04,

Could you please share the details of the service you are referencing. Is it a OSGi Factory service?

Shubham_borole
Community Advisor
Shubham_boroleCommunity AdvisorAccepted solution
Community Advisor
September 7, 2021

Hi @ravijais04 If I understand the issue/question correctly..
Can you please mention what do the OSGi values in config look like?

With cardinality being multiple, you are probably adding multiple values to it. How would that resolve to a Map?
You would have to get the OSGi values as a list or collection (or String[] may be).

key1|value1 key2|value2 key3|value3

 

If your config has values like above, then you will have to get the OSGi values as list and separate the items based on the delimiter '|' and generate your map in the @activate or @modified method whichever is available and works.