Search code examples
osgiaemapache-felix

How to use multi value(array) property in OSGi?


I have the following service:

@Component(
        immediate = true,
        metatype = true)
@Service
@Property(name = EventConstants.EVENT_TOPIC, value = {ReplicationAction.EVENT_TOPIC})
public class MyService implements EventHandler {

    @Property
    private static final String MULTI_PROPERTY = "config.multiproperty";

    ........
    //another implementation
    ........
}

I want MULTI_PROPERTY to be as array value, to have possibility to use a set of values like on image:

enter image description here

How to implement it?


Solution

  • Use the unbounded attribute to specify multivalued property and use the cardinality attribute to restrict the number of entries.

     @Property(unbounded = PropertyUnbounded.ARRAY, cardinality=10, label = "Some Label")
     private static final String MULTI_PROPERTY = "config.multiproperty";
    

    In order to read the property array you can use #toStringArray() method of the PropertiesUtil

    PropertiesUtil.toStringArray(properties.get(MULTI_PROPERTY));