Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

OSGI R6 Reference Annotation on Field

Avatar

Level 3

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

 

@reference(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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

4 Replies

Avatar

Community Advisor

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.

Avatar

Level 3

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.

@reference(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-referen...

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.

Avatar

Community Advisor

Hi @ravijais04,

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

Avatar

Correct answer by
Community Advisor

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.