0

I'm Migrating Apache Felix SCR Annotations to OSGI Declarative Services[AEM]. While Migration I cant find exact replacement for cardinality in DS .

Existing SCR Implementation :

@Component (ds = true, immediate = true, metatype = false, policy = ConfigurationPolicy.OPTIONAL)
@Service (SampleService.class)
public class SampleServiceImpl implements SampleService
{
   private static final int VECTOR = Integer.MIN_VALUE + 1;

    @Property (value = REPOSTING_PATTERN, cardinality = VECTOR,description = "Event reposting pattern for QueuePosting ")
    private static String EVENT_REPOSTING_PATTERN = "eventRepostingPattern";
}

Now it is migrated as Below in OSGi Declarative services

  @Component (configurationPolicy = ConfigurationPolicy.OPTIONAL, immediate = true, service =SampleService.class,
   property = {SampleServiceImpl .EVENT_REPOSTING_PATTERN +"="+SampleServiceImpl .EVENT_REPOSTING_PATTERN_VALUE })
  public class SampleServiceImpl implements SampleService
   {
     private static String EVENT_REPOSTING_PATTERN = "eventRepostingPattern";
     private static String EVENT_REPOSTING_PATTERN = "eventRepostingPatternValue";
   }

In DS annotation Implementation How I have to map the parameter cardinality which is present in @Property.Kindly suggest me

Kamalraj
  • 17
  • 5

1 Answers1

0

Documentation about the @Property and cardinality is confusing as usual but I am assuming based on this that these are somehow related to configurations. You can setup Type-safe configurations for your service(s) with @Designate and @ObjectClassDefinition annotations. The cardinality option can be found in @AttributeDefinition annotation.

ExampleServiceimpl.java

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;

@Component( immediate = true, service = ExampleService.class )
@Designate( ocd = ExampleServiceConfig.class )
public class ExampleServiceImpl implements ExampleService {
    
    @Activate
    public void activateService(BundleContext context, ExampleServiceConfig config){
        System.out.println(config.some_config());
    }
}

ExampleServiceConfig.java

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(
    name = " Example service Configuration"
)
public @interface ExampleServiceConfig {
    
    @AttributeDefinition( cardinality = 1 )
    String some_config() default "default value";
}

Now as disclaimer I've never managed to used @AttributeDefinition or cardinality annotation myself yet so might be off here. Encountered the cardinality option when I tried to use @AttributeDefinition to see if I could use it to make my service configurations pretty in hawtio but probably missing some steps.

But hopefully this points you to right direction.

Pasi Österman
  • 2,002
  • 1
  • 6
  • 13